4

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)
        ...
    }
}
Dominic
  • 3,353
  • 36
  • 47
  • 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 list;` and instantiate it? No magic required. :-) – Buurman Jun 25 '15 at 13:46
  • @SotiriosDelimanolis I'm pretty certain it works. I didn't write it so I'm not allowed to share it. – Dominic Jun 25 '15 at 14:08
  • @Buurman I update the question to explain what I'm doing. Hope it clarifies it a bit. – Dominic Jun 25 '15 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 ;) – Dominic Jun 25 '15 at 14:40
  • 2
    http://stackoverflow.com/questions/13392160/about-java-get-string-class-from-string-class-what-if-string-class-is – Sotirios Delimanolis Jun 25 '15 at 14:46

1 Answers1

2

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.

Dean J
  • 39,360
  • 16
  • 67
  • 93
  • 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 http://stackoverflow.com/questions/13392160/about-java-get-string-class-from-string-class-what-if-string-class-is dont work. So I will stick to my solution. – Dominic Jun 29 '15 at 10:13