1

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 it possible to fille the TODO here? How / Why?

Makan
  • 2,508
  • 4
  • 24
  • 39
  • 1
    If you are already doing this by reflection, then maybe you can just create own "dynamic object" with method to fetch all possible actions akak methods? Would be great to know what actually you want to do. It is possible to create own type that have list of possible functions to execute. It is possible to add new methods before class is loaded and it is possible to generate new class with some methods. But you can't add method at runtime to already loaded class. – GotoFinal Jul 18 '19 at 08:17
  • @GotoFinal Thanks, and you are correct. I prefered to load all functions from package `A` instead of having a responsible class in package `B`, just because I wanted the complexity to remain in the `A`. I gave up and went another way alltogether, So in the future when I have some time, I will implement the 'before classLoader' approach, and put it here. – Makan Jul 18 '19 at 08:23
  • But your comments pointed to the right direction anyway. :bd – Makan Jul 18 '19 at 08:24

0 Answers0