Reputation: 5753
I have a ProxyGenerator which looks like the one at bottom. My problem now is that I don't know which type this is:
Consumer<...?????> myConsumer = (proxy, method, args) -> method.invoke(realSubject, args);
Consumer is wrong, is there a simple way of determine of which type the Lambda expression is (e.g. with Eclipse)?
public class ProxyGenerator {
public static <P> P makeProxy(Class<P> subject, P realSubject) {
Consumer<Subject_A> myConsumer = (proxy, method, args) -> method.invoke(realSubject, args);
final Object proxyInstance = Proxy.newProxyInstance(subject.getClassLoader(), new Class<?>[] { subject },
(proxy, method, args) -> method.invoke(realSubject, args));
return subject.cast(proxyInstance);
}
}
Upvotes: 1
Views: 205
Reputation: 502
In Eclipse, you can simply move the mouse pointer on to the "->" symbol: the tooltip that will be shown has full method signature for the implemented lambda.
In your case, the implemented method is simply InvocationHandler.invoke method.
So, code declaring and using myConsumer
should be declared instead as:
final InvocationHandler myHandler = (proxy, method, args) -> method.invoke(realSubject, args);
final Object proxyInstance = Proxy.newProxyInstance(subject.getClassLoader(), new Class<? >[] { subject }, myHandler);
Note that Consumer
, while being a very useful interface for "capturing" at once all lambdas consuming an argument without any result, is just that. If your lambda doesn't fit that model, there's no way to declare it as a Consumer
; in particular, your lambda cannot fit Consumer
's accept
method since it has:
Upvotes: 3