lundi 3 août 2020

Pass reflection method as argument (functional interface)

I have the below functional interface

@FunctionalInterface
public interface Processor { void handle(String text); }

I have a method

doSomething(Processor processor)

I can call doSomething like this

public class Demo {
    public void rockTheWorldTest(String text) {
    // Processing 
    }
}

I can call it like below

doSomething(new Demo::rockTheWorldTest);

But I wont be able to know the name of the method in a particular class and I want to call this using the reflection from another class

Method[] testClasMethods = DemoAbstractOrchestratorTest.getClass().getDeclaredMethods();
for (Method method : testClasMethods) {
   doSomething(method) // Not able to do this.
}




Aucun commentaire:

Enregistrer un commentaire