jeudi 29 juillet 2021

Java Reflection: create a constructor for an abstract class

If I have this class that I cannot modify...

public abstract class AClass {
    Object value;
    public AClass(Object value) { this.value = value; }
}

Then how would I create an instance of it with reflection?
This...

Class<?> clazz = // some class
Constructor</*someClass*/> constructor = AClass.class.getConstructor(clazz);
Object key = clazz.getConstructor(Object.class).newInstance(/*something*/);
/*someClassResult */ result = constructor.newInstance(key);

throws an InstantiationException at the last line.





Aucun commentaire:

Enregistrer un commentaire