Java proxy
5858 ワード
Java proxy
proxy, a design pattern base on reflect, it handle method invocation on object with a proxy instance,
------
class & interface
include:
* java.lang.reflect.Proxy
help to create proxy instance,
* java.lang.reflect.InvocationHandler
implement it to specify the proxy logic,
------
create & use a proxy
steps:
* pre-step:
* create the proxyed interface
* create the proxyed class implements the proxyed interface
*
* create a class implement InvocateHandler,
do:
* provide a field which is the proxyed object,
* provide a bind method(e.g. bind()), pass the proxyed object as param, and call Proxy.newProxyInstance() to create & return a proxy instance,
* implement the invoke() method, it call the method and return the return value, also do some other logic as you wish,
* get proxy instance
create an instance of the class that implement InvocateHandler,
and call the bind method with a proxyed object as param, to get a proxy instance,
cast the proxy instance to type that is one of the proxyed object's interface,
* call method with proxy instance
just the same as call on the proxyed object,
*
------
proxy & interface
proxy instance must be cast to an interface type, but not the class itself or it's superclass,
and all method to be invode via proxy, must be declared in the interface,
this is because Proxy.newProxyInstance's second param accept only interface, typically we use Class.getInterfaces() to pass the param,
------
code
IHello.java
Hello.java
TimerProxy.java
ProxyTest.java
------
proxy, a design pattern base on reflect, it handle method invocation on object with a proxy instance,
------
class & interface
include:
* java.lang.reflect.Proxy
help to create proxy instance,
* java.lang.reflect.InvocationHandler
implement it to specify the proxy logic,
------
create & use a proxy
steps:
* pre-step:
* create the proxyed interface
* create the proxyed class implements the proxyed interface
*
* create a class implement InvocateHandler,
do:
* provide a field which is the proxyed object,
* provide a bind method(e.g. bind()), pass the proxyed object as param, and call Proxy.newProxyInstance() to create & return a proxy instance,
* implement the invoke() method, it call the method and return the return value, also do some other logic as you wish,
* get proxy instance
create an instance of the class that implement InvocateHandler,
and call the bind method with a proxyed object as param, to get a proxy instance,
cast the proxy instance to type that is one of the proxyed object's interface,
* call method with proxy instance
just the same as call on the proxyed object,
*
------
proxy & interface
proxy instance must be cast to an interface type, but not the class itself or it's superclass,
and all method to be invode via proxy, must be declared in the interface,
this is because Proxy.newProxyInstance's second param accept only interface, typically we use Class.getInterfaces() to pass the param,
------
code
IHello.java
package eric.j2se.proxy;
/**
* interface for proxyed class
*
* @author eric
* @date Dec 7, 2012 6:07:36 PM
*/
public interface IHello {
public void hello(String name);
}
Hello.java
package eric.j2se.proxy;
/**
* proxyed class
*
* @author eric
* @date Dec 7, 2012 6:07:49 PM
*/
public class Hello implements IHello {
@Override
public void hello(String name) {
System.out.printf("hello, %s!
", name);
}
}
TimerProxy.java
package eric.j2se.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.log4j.Logger;
/**
* a proxy that help to logging method execution time
* <p>
* <b>how to use</b>:
* <p>
* <b>way one</b>: create an instance of this class, and call bind() with the object to be proxyed, and get a proxy instance, cast the return value of bind() to
* a type that is one of the interfaces of the proxyed object, then call on the proxy instance,
*
* <pre>
* e.g:
* IHello hello = (IHello) new TimerProxy().bind(new Hello());
* hello.hello("eric");
* </pre>
* <p>
* <b>way two</b>: static getProxy() is a simpler way to use, underling it use way one,
*
* <pre>
* e.g:
* IHello hello = TimerProxy.getProxy(new Hello(), IHello.class);
* hello.hello("eric");
* </pre>
*
* </p>
*
* @author eric
* @date Dec 7, 2012 4:14:27 PM
*/
public class TimerProxy implements InvocationHandler {
private Object obj; // the object to be proxyed
private static Logger logger = Logger.getLogger(TimerProxy.class);
/**
* create a proxy instance
*
* @param obj
* the object to be proxyed
* @return the proxy instance
*/
public Object bind(Object obj) {
this.obj = obj;
return Proxy.newProxyInstance(this.obj.getClass().getClassLoader(), this.obj.getClass().getInterfaces(), this);
}
/**
* get a proxy instance
*
* @param obj
* @return
*/
public static Object getProxy(Object obj) {
return new TimerProxy().bind(obj);
}
/**
* get a proxy instance, and cast to specified type,
*
* @param obj
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getProxy(Object obj, Class<T> clazz) {
return (T) (getProxy(obj));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
long start = System.currentTimeMillis();
result = method.invoke(this.obj, args);
logger.info(method.getDeclaringClass().getName() + "." + method.getName() + "() called, during " + (System.currentTimeMillis() - start)
+ " milliseconds.");
return result;
}
}
ProxyTest.java
package eric.j2se.proxy;
/**
* proxy test
*
* @author eric
* @date Dec 7, 2012 4:11:03 PM
*/
public class ProxyTest {
public static void main(String[] args) {
// test();
test2();
}
public static void test() {
IHello hello = (IHello) new TimerProxy().bind(new Hello());
hello.hello("eric");
}
public static void test2() {
IHello hello = TimerProxy.getProxy(new Hello(), IHello.class);
hello.hello("eric");
}
}
------