1

Can I get Class<T> from Class<T[]>?

public static <T> void doSometing(final Class<T[]> arrayType) {

    final Class<T> elementType; // = @@?
}

Or, can I get Class<T[]> from Class<T>?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

1 Answers1

5

You can use Class.getComponentType(), although there's no way for that method to give you generic type safety, so you'll need to do an unchecked cast to get a Class<T>:

public static <T> void doSomething(final Class<T[]> arrayType) {

    @SuppressWarnings("unchecked")
    final Class<T> componentType = (Class<T>)arrayType.getComponentType();

    //etc.
}

For the reverse, see this question: Obtaining the array Class of a component type

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • Can you please take a look at this following question? http://stackoverflow.com/questions/17204788/two-methods-for-creating-a-generic-array – Jin Kwon Jun 20 '13 at 03:29
  • @JinKwon Sorry I didn't see that sooner. Looks like it's been answered pretty well. – Paul Bellora Jun 20 '13 at 13:22