Ich habe eine groovy-reflection-troubleshooting
repo erstellt, um dies vollständig und einfach zu reproduzieren.Groovy mag nicht Reflections Constructor unter Null Args
Grundsätzlich ist dieser Groovy-Code läuft gut:
Class<?> clazz = Class.forName('troubleshooting.DangWidget')
Constructor<?> constructor = clazz.getConstructor(Configuration)
Object testClassInstance = constructor.newInstance(new Configuration())
// Object testClassInstance = constructor.newInstance(null)
Aber das wirft eine IllegalArgumentException
:
Class<?> clazz = Class.forName('troubleshooting.DangWidget')
Constructor<?> constructor = clazz.getConstructor(Configuration)
// Object testClassInstance = constructor.newInstance(new Configuration())
Object testClassInstance = constructor.newInstance(null)
Stapelüberwachung:
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)
Ich denke, ich frage mich, Warum kann ichnicht passieren?in constructor.newInstance(null)
und was kann ich übergeben constructor.newInstance
, wenn ich wirklich die neue Instanz den Wert übergeben möchten null
?!
Dank @Mureinik (+1) aber Ihr Vorschlag funktioniert noch nicht (gleiche Ausnahme). Überzeugen Sie sich selbst, indem Sie das Repo klonen ... irgendwelche Ideen? Danke noch einmal! – smeeb
@smeeb Ja, mein Schlechter. Alte Java-Gewohnheiten schätze ich - obwohl meine Argumentation solide war, war meine Lösung definitiv nicht. Bitte sehen Sie meine bearbeitete Antwort für eine Lösung, die funktioniert. – Mureinik