mardi 26 avril 2016

Creating class with parameter constructor in Scala reflection

I have a class that may take from 1 to 4 parameters. They are always Strings. I would like to create an object of this class based on the number of arguments passed to the function. Is there any way to go around having to create constructor and passing an array of Objects directly to newInstance?

        NewInstanceWithReflection clazz = (NewInstanceWithReflection)Class.forName("NewInstanceWithReflection").newInstance();
        Constructor constructor = NewInstanceWithReflection.class.getDeclaredConstructor( new Class[] {String.class});
        NewInstanceWithReflection object1 = (NewInstanceWithReflection)clazz.newInstance(new Object[]{"StackOverFlow"});

This code pasted into sbt interpreter does not seem to work. Any help appreciated.





Aucun commentaire:

Enregistrer un commentaire