9

Is there a way in Eclipse to generate a proxy class (as in proxy pattern, not a remote call)? Something along the lines of this

public class FooBar{
    public int getBiz(){
        return 1234;
    } 
}

generates

public class FooBarProxy{
    protected FooBar foobar;
    public FooBarProxy(FooBar f) {...}
    public int getBiz(){
        return foobar.getBiz()
    } 
}

2 Answers 2

14

Create the proxy class yourself, and add the FooBar instance variable. Select the variable, right click-->source-->generate delegate methods

Sign up to request clarification or add additional context in comments.

1 Comment

This is extremely useful when creating a decorator to change the behaviour of a specific method.
2

Why not use java's built in dynamic proxy. It generates a proxy at runtime:

  1. implement your proxy logic by implementing java.lang.reflect.InvocationHandler
  2. create a dynamic proxy; see http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Proxy.html (example included)

1 Comment

It's interesting, but seems different usecase - mainly for interception/reflection magic.. For the plan old proxy pattern in Java, it's way messier than typing things out (also for debugging), IMHO. BTW, similar constructs look more acceptable in other languages (eg Forwardable in Ruby ruby-doc.org/stdlib-2.0/libdoc/forwardable/rdoc/…)

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.