dimanche 22 juillet 2018

Class.getConstructors returns an incorrectly identified type for one of the constructor parameters

A class that has a single 2-parameter constructor is first loaded via

val clazz = Class.forName(myClassName)

Then the single constructor is found:

val constructor = clazz.getConstructors().find(_.getParameterTypes.length == args.length).head

Running to this point in the Intellij debugger we see that the input parameters are appropriate for the signature:

enter image description here

However the parameter types of the constructor are showing a problem:

  • the first one is correctly identified as java.lang.String
  • the second parameter is .. not identified at all ??

enter image description here

And so in fact when an attempt to instantiate the class via:

constructor.newInstance(args: _*).asInstanceOf[T]

This fails with argument type mismatch:

 Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

Why would the constructor be mischaracterized as shown above?





Aucun commentaire:

Enregistrer un commentaire