I want to create method in a Utils
class that accepts two parameters, the first is the domain class, and the second is a method from the domain class (passed as reference). This Utils
' class has a method that will create an instance of the class and execute the method in the scope of the domain class instance.
For example, the domain class:
public class DomainClass {
public void dummyMethod() {}
}
The Utils class:
public class Utils {
public static void execute(Class<?> clazz, Runnable referenceMethod) {
Object objInstance = clazz.getConstructor().newInstance();
// execute the 'referenceMethod' on the scope of 'objInstance'
}
}
The call I want is something like: Utils.execute(DomainClass.class, DomainClass::dummyMethod)
. However, this scenario has some problems:
- How can I pass this parameters for the
Utils
class (now I'm having some compilation problems)? - How can I call the 'referenceMethod' with the scope of 'objInstance'?