实例化目标类->实例化MyHandler(实现IvocationHandler接口:依赖目标对象:重写invoke方法:调用目标对象的方法)->获取代理对象Proxy.newProxyInstance(target.getClass.getClassLoader(),target.getClass.getInterfaces(), h)->代理对象调用目标方法
target代理对象构造参数MyHandler->target代理对象本身继承Proxy并实现目标类得所有接口->target代理对象调用目标方法->目标方法指定调用Proxy.h.invoke()
1.创建代理类的源码;
2.对源码进行编译成字节码;
3.将字节码加载到内存;
4.实例化代理类对象并返回给调用者;
这是一个配置项,它获取的值为JVM中的参数,可以通过在代码中动态设置来让动态代理生成的Proxy类保存成文件。saveGeneratedFiles的值随JDK版本的不同而不同,目前共两种取值,设置方法如下:
System.setProperty("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
System.setProperty("Dsun.misc.ProxyGenerator.saveGeneratedFiles","true")
System.getProperties().setProperty("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
手动获取代理对象的字节码信息:
//获得字节码的测试方法
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy1", Car.class.getInterfaces());
FileOutputStream out = null;
try {
out = new FileOutputStream(System.getProperty("user.dir") + "\\$Proxy1.class");
out.write(classFile);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//生成的字节码:
importcn.com.goser.proxy.imooc.staticproxy.Moveable;
importjava.lang.reflect.InvocationHandler;
importjava.lang.reflect.Method;
importjava.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class $Proxy1 extends Proxy
implements Moveable
{
private static Method m1;
private static Method m3;
private static Method m0;
private static Method m2;
public $Proxy1(InvocationHandler paramInvocationHandler)
throws
{
super(paramInvocationHandler);
}
public final boolean equals(Object paramObject)
throws
{
try
{
return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final void move()
throws Exception
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (Exception localException)
{
throw localException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final int hashCode()
throws
{
try
{
return ((Integer)this.h.invoke(this, m0, null)).intValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final String toString()
throws
{
try
{
return (String)this.h.invoke(this, m2, null);
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
static
{
try
{
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m3 = Class.forName("cn.com.goser.proxy.imooc.staticproxy.Moveable").getMethod("move", 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]);
return;
}
catch (NoSuchMethodExceptionlocalNoSuchMethodException)
{
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
}
catch (ClassNotFoundExceptionlocalClassNotFoundException)
{
}
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}