mercredi 17 juillet 2019

`.addMethod(method)` in Java? Use Java reflection to add methods to an anonymous Class

I have an anonymous class:

Object myArbitraryObject = new Object(){
};

Now I load a list of functions at runtime:

for(String methodName: methodNames){
    Method arbitraryMethod = this.getClass().getMethod(methodName);
    // TODO: add arbitraryMethod to arbitraryObject.
}

... and at a later stage:

Method [] allArbitMethods = myArbitraryObject.getClass.getMethods();
// invoking allMethods....

Is this possible? How / Why?





Aucun commentaire:

Enregistrer un commentaire