I have a few classes that each implement an interface. From these classes I search out a method using an annotation. This method returns a boolean and always has an object as parameter, which always inherits from another fixed object. Now I want to create a functional interface from this method. Optimally of course a Predicate, which takes over the mentioned parameter. This I have now tried for some time to implement with LambdaMetafactory:
private Predicate<Parent> toPredicate(final ExampleInterface instance, final Method method) {
try {
final MethodHandle handle = LOOKUP.unreflect(method);
final MethodType signature = MethodType.methodType(boolean.class, method.getParameters()[0].getType());
final CallSite callSite = LambdaMetafactory.metafactory(
LOOKUP,
"test",
MethodType.methodType(Predicate.class, instance.getClass()),
signature,
handle,
signature
);
return (Predicate<Parent>) callSite.getTarget().invoke(instance);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
My problem now is that when I call the Predicate's test method, an AbstractMethodError is thrown. When I use signature with (boolean.class, Parent.class) I get a LambdaConversionException. Is it even possible to implement it that dynamically? If yes, how?