Class.getArrayType in Java?
Posted
by
???
on Stack Overflow
See other posts from Stack Overflow
or by ???
Published on 2011-01-18T02:24:11Z
Indexed on
2011/01/18
2:54 UTC
Read the original article
Hit count: 156
java
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?
© Stack Overflow or respective owner