Given a class Something with a constructor public Something(List<String> l), I would like to use klass.getConstructor(parameterTypes) where parameterTypes[0] is the class java.util.ArrayList (because I need to "match" a given specific instance) and not the interface java.util.List.
This doesn't work (NoSuchMethodException), because Java Reflection appears to need an EXACT type class match. What is the best way around this?
Might be overkill, and admittedly could pick up too many constructors:
Constructor[] constructors = klass.getConstructors();
for(Constructor constructor:constructors) {
Class<?>[] params = constructor.getParameterTypes();
if(params.length == 1 && params[0].isAssignableFrom(ArrayList.class) {
//Yep could be the one you want.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With