Getting the class of an n dimensional array of an runtime supplied class name
- by MeBigFatGuy
Given a fully qualified class name, and a number of dimensions, i would like to get the Class name for this class. I believe i can do this like such
public Class<?> getArrayClass(String className, int dimensions) throws ClassNotFoundException {
Class<?> elementType = Class.forName(className);
return Array.newInstance(elementType, new int[dimensions]).getClass();
}
However this requires me to create an unneeded instance of the class. Is there a way to do this without creating the instance?
It does not appear that Class.forName("[[[[Ljava/lang/String;") (or a algorithmically generated version) works correctly in all instances from various blog posts i've seen.