mardi 19 décembre 2017

Groovy does not like Reflections Constructor taking null args

I have created a groovy-reflection-troubleshooting repo to reproduce this entirely and easily.


Basically this Groovy code runs fine:

Class<?> clazz = Class.forName('troubleshooting.DangWidget')
Constructor<?> constructor = clazz.getConstructor(Configuration)
Object testClassInstance = constructor.newInstance(new Configuration())
// Object testClassInstance = constructor.newInstance(null)

But this throws an IllegalArgumentException:

Class<?> clazz = Class.forName('troubleshooting.DangWidget')
Constructor<?> constructor = clazz.getConstructor(Configuration)
// Object testClassInstance = constructor.newInstance(new Configuration())
Object testClassInstance = constructor.newInstance(null)

Stack trace:

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
        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)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

I guess I'm wondering why I can't pass null into constructor.newInstance(null) and what I can pass constructor.newInstance if I truly wish to pass the new instance the value null?!





Aucun commentaire:

Enregistrer un commentaire