1

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);
    }
}
1
  • What type does it need to be? myConsumer isn't used anywhere in your example. Commented Aug 24, 2016 at 9:20

1 Answer 1

3

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:

  • three arguments instead of one
  • a result, instead of being void
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.