mercredi 6 septembre 2017

How to get Class object from parametrized List for input to new object declaration?

I rewrited this line of code:

JsonResponse<LoginResult> response = new JsonResponse<>(LoginResult.class);

to this:

JsonResponse<LoginResult[]> response = new JsonResponse<>(LoginResult[].class);

Because I wanted to pass the array of LoginResults.

But now I would like to rewrite it using ArrayList and of course I stuck in this:

JsonResponse<List<LoginResult>> response = new JsonResponse<List<LoginResult>>(List<LoginResult>.class);

I understand that I cannot get Class object from parametrized type. I read a lot of topics how to do that but I am not sure how to rewrite code in my situation, because I need the Class object as the input for JSonResponse constructor?

public class JsonResponse<T> extends JsonRequest {

    private Type type;

    public JsonResponse(Class<T> type) {
        this.type = type;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @SuppressWarnings("unchecked")
    public T getResult() {
        return (T) getAttribute(RESULT);
    }
}

Can you help me or give me some clue how solve this problem? Thank you!





Aucun commentaire:

Enregistrer un commentaire