Proxy
public class Proxy implements java.io.Serializable {
protected InvocationHandler h;
protected Proxy(InvocationHandler h) {
doNewInstanceCheck();
this.h = h;
}
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHandler h)
{
/*
* Look up or generate the designated proxy class.
*/
Class<?> cl = getProxyClass0(loader, interfaces); // stack walk magic: do not refactor
/*
* Invoke its constructor with the designated invocation handler.
*/
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
return newInstance(cons, ih);
}
private static Object newInstance(Constructor<?> cons, InvocationHandler h) {
return cons.newInstance(new Object[] {h} );
}
Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
创建代理类class,代理类实现了指定的接口并继承了Proxy类。代理类中持有InvocationHandler的引用。通过代理类的构造函数反射创建代理类的实例,并InvocationHandler注入到代理类中。
代理类如下,
- public final class $Proxy11 extends Proxy implements UserService
- {
- // 构造方法,参数就是刚才传过来的MyInvocationHandler类的实例
- public $Proxy11(InvocationHandler invocationhandler)
- {
- super(invocationhandler);
- }
- public final boolean equals(Object obj)
- {
- try
- {
- return ((Boolean)super.h.invoke(this, m1, new Object[] {
- obj
- })).booleanValue();
- }
- catch(Error _ex) { }
- catch(Throwable throwable)
- {
- throw new UndeclaredThrowableException(throwable);
- }
- }
- /**
- * 这个方法是关键部分
- */
- public final void add()
- {
- try
- {
- // 实际上就是调用MyInvocationHandler的public Object invoke(Object proxy, Method method, Object[] args)方法,第二个问题就解决了
- super.h.invoke(this, m3, null);
- return;
- }
- catch(Error _ex) { }
- catch(Throwable throwable)
- {
- throw new UndeclaredThrowableException(throwable);
- }
- }
- public final int hashCode()
- {
- try
- {
- return ((Integer)super.h.invoke(this, m0, null)).intValue();
- }
- catch(Error _ex) { }
- catch(Throwable throwable)
- {
- throw new UndeclaredThrowableException(throwable);
- }
- }
- public final String toString()
- {
- try
- {
- return (String)super.h.invoke(this, m2, null);
- }
- catch(Error _ex) { }
- catch(Throwable throwable)
- {
- throw new UndeclaredThrowableException(throwable);
- }
- }
- private static Method m1;
- private static Method m3;
- private static Method m0;
- private static Method m2;
- // 在静态代码块中获取了4个方法:Object中的equals方法、UserService中的add方法、Object中的hashCode方法、Object中toString方法
- static
- {
- try
- {
- m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {
- Class.forName("java.lang.Object")
- });
- m3 = Class.forName("dynamic.proxy.UserService").getMethod("add", new Class[0]);
- m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
- m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
- }
- catch(NoSuchMethodException nosuchmethodexception)
- {
- throw new NoSuchMethodError(nosuchmethodexception.getMessage());
- }
- catch(ClassNotFoundException classnotfoundexception)
- {
- throw new NoClassDefFoundError(classnotfoundexception.getMessage());
- }
- }
- }
InvocationHandler 可以通过Method 对象和方法参数反射调用被代理对象上的方法,可以在方法调用前后插入想切入的逻辑。