dimanche 31 mai 2015

Dynamically build java/scala method body at runtime and execute it

Suppose I have the following Interface in java:

public interface DynamicMethod {
    String doit();
}

I would like to build an Object during runtime which conforms to the above interface such that I inject doit method body in it and then execute it? Is this possible with Java Reflection API, or any other way? Or probably in some way in Scala?


Note that doit body for my objects would be dynamic and are not known a priori. You can assume that in run-time an array CodeArray[1..10] of Strings is provided and each entry of this array holds the code for each doit method. I would appreciate if you could answer with a sample code.





Aucun commentaire:

Enregistrer un commentaire