I have an interface Action:
package action;
public interface Action {
public String act();
}
Class SimpleAction:
package action;
public class SimpleAction implements Action {
String action;
public SimpleAction() {}
public SimpleAction(String x) {
this.action = x;
}
@Override
public String act() {
return "Act method from -" + this.action;
}
}
Class ComplexAction:
package action;
public class ComplexAction implements Action{
String action;
public ComplexAction() {}
public ComplexAction(String x) {
this.action = x;
}
@Override
public String act() {
return "Act method from -" + this.action;
}
}
I want to create a function which takes the name of class and returns an object of that class. This is what I have so far in my function -
public static Action resultOfActMethod(String objclass){
Class clazz = Class.forName(objclass);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findConstructor(clazz,MethodType.methodType(void.class, String.class));
}
Figured it out.
public static Action resultOfActMethod(String objclass){
Class clazz = Class.forName(objclass);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findConstructor(clazz, MethodType.methodType(void.class, String.class));
Function<String, Action> constructor = (Function<String, Action>)LambdaMetafactory.metafactory(lookup, "apply",MethodType.methodType(Function.class),
mh.type().generic(), mh, mh.type()).getTarget().invokeExact();
Action action = constructor.apply(objclass);
return action;
}