Class.getArrayType in Java?
- by ???
I use the following trick to get the array type of a specific class:
@SuppressWarnings("unchecked")
public static <T> Class<T[]> getArrayType(Class<T> componentType) {
String arrayClassName = "[L" + componentType.getName() + ";";
try {
return (Class<T[]>) Class.forName(arrayClassName);
} catch (ClassNotFoundException e) {
throw new UnexpectedException("Can't get the array type for " + componentType, e);
}
}
But, is there any more elegant way to get this?