Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating java proxy class in Eclipse

Tags:

java

eclipse

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()
    } 
}
like image 513
dfb Avatar asked Jul 07 '11 22:07

dfb


2 Answers

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

like image 67
James Scriven Avatar answered Sep 30 '22 18:09

James Scriven


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)
like image 32
home Avatar answered Sep 30 '22 19:09

home