Array class in constructor with reflection

I need to use reflection in my code, so my program does not interrupt with every other version. I want to create a new instance of the class and the constructor that I want to use contains an array of the class. But this class must also be found using reflection. This is an example of what I have.

Constructor<?> constructor = getClass("className").getConstructor(getClass("anotherClass"));

private Class<?> getClass(String name) {
    return Class.forName("my.package." + version + "." + name);
}

However, the constructor does not use this class, but an array of this class, so how would I turn this class into its array?

+4
source share
1 answer

Java . Class#forName. , , String.

0 getClass().

Class<?> componentType = Class.forName("java.lang.String");
Class<?> arrayType = java.lang.reflect.Array.newInstance(componentType, 0).getClass();
System.out.println(arrayType);

class [Ljava.lang.String;

arrayType Class . .

+6

All Articles