Like "getConstructor" where the constructor signature contains a java array

Is it possible to use getConstructor to get the constructor of class X below?

public class A { } public class Y { } public class X extends Y { public X(A a, Y[] yy) { } public void someMethod() throws SecurityException, NoSuchMethodException { Class<? extends Y> clazz = X.class; Constructor<? extends Y> c = clazz.getConstructor(new Class[]{ A.class, /* what do I put in here for the array of Ys? */ }); } } 

thanks

+7
java reflection
source share
2 answers

You can create class literals using array notation in the same way as with "dysfunctional" classes, namely ClassName[].class . This literal gives "a class that describes arrays of instances of ClassName ". In your case:

 clazz.getConstructor(new Class[] { A.class, Y[].class }); 
+6
source share

Or shorter.

  Constructor<X> c = X.class.getConstructor(A.class, Y[].class); 
+6
source share

All Articles