5

I have a class with a generic type. import java.lang.reflect.Array;

public abstract class MyClass<T> {

    private Class<T> clazz;
    private Class<?> arrayClazz;

    public MyClass() {
        clazz = ClassUtils.getParameterizedClass(getClass());
        arrayClazz = Array.newInstance(clazz, 0).getClass();
    }
}

The Utils method reads the generic type of the given class so I don't have to pass the same class as a constructor parameter. What I'm trying to achieve is getting the array class of clazz.

For example if T is String then

clazz = String.class;
arrayClazz = String[].class;

I currently solved it by creating a new instance of T[] and reading its class. I wanted to know if there's a better way or if there are any downsides with this method.

Update

What I'm trying to do: I have a generic DataProvider which requests JSON from a server. I use GSON to parse the response.

public abstract class DataProvider<T> {

    private final Class<T> resourceClass;
    private final Class arrayClass;


    protected DataProvider() {
        this.resourceRootPath = resourceRootPath;
        this.resourceClass = ClassUtils.getParameterizedClass(getClass());
        arrayClass = Array.newInstance(resourceClass, 0).getClass();
    }

    public void get(String id) {
        ...
        T obj = gson.fromJson(response.body().charStream(), resourceClass)
        ...
    }

    public void list(String id) {
        ...
        T[] objs = gson.fromJson(response.body().charStream(), arrayClass)
        ...
    }
}
5
  • What are you trying to do? Why are you trying to get a parameter type for an array type? If you want to keep a collection of elements of type T somewhere why not make a field List<T> list; and instantiate it? No magic required. :-) Commented Jun 25, 2015 at 13:46
  • @SotiriosDelimanolis I'm pretty certain it works. I didn't write it so I'm not allowed to share it. Commented Jun 25, 2015 at 14:08
  • @Buurman I update the question to explain what I'm doing. Hope it clarifies it a bit. Commented Jun 25, 2015 at 14:17
  • @SotiriosDelimanolis correct. That's how it works. You're right I should have made my first example abstract. I forgot about that because I tried to keep it as simple as possible. Turns out it was too simple ;) Commented Jun 25, 2015 at 14:40
  • 2
    stackoverflow.com/questions/13392160/… Commented Jun 25, 2015 at 14:46

1 Answer 1

3

You should look up Type Erasure, which may prevent a clean way of doing what you're asking for. (Generics can't help you here.)

The problem is that in Java, generic type data is used at compile time to ensure everything looks good... and then Java forgets about it entirely. The types aren't compiled in in the way you'd hope, they're just gone.

Because of that, your approach is (I think!) the cleanest, even if it feels hacky. You need to instantiate one and use reflection to get it's type.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I now understand Generics a little better. I didn't know that they get compiled to Object. That's the reason why some of the solutions in stackoverflow.com/questions/13392160/… dont work. So I will stick to my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.