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>
?
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>
?
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