}
public class JdkProxyTest {
public static void main(String[] args) {
// 创建目标类对象
IA target = new IAImpl();
// 创建代理了目标类IAImpl的动态代理,根据不同的目标对象,生成不同的动态代理字节码
IA dynamicProxy = (IA)Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandlerImpl(target));
// 调用动态代理的方法
try {
dynamicProxy.run();
} catch (IOException e) { // 能捕获到动态代理抛出的IOException异常
e.printStackTrace();
}
}
}
具体Proxy在运行期生成的动态代理类的字节码反编译后如下:
package aop;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class $Proxy0 extends Proxy
implements IA
{
private static Method m1;
private static Method m3;
private static Method m0;
private static Method m4;
private static Method m2;
public $Proxy0(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 run()
throws IOException
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (RuntimeException localRuntimeException) // 如果是运行是异常,则直接抛出
{
throw localRuntimeException;
}
catch (IOException iOexception) // 如果是方法声明的异常,则直接抛出
{
throw iOexception;
}
catch (Throwable localThrowable) // 如果异常没被声明,则最终被代理对象包装成运行期异常UndeclaredThrowableException抛出
{
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 show(String paramString)
throws
{
try
{
return (String)this.h.invoke(this, m4, new Object[] { paramString });
}
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("aop.IA").getMethod("run", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
m4 = Class.forName("aop.IA").getMethod("show", new Class[] { Class.forName("java.lang.String") });
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
return;
}
catch (NoSuchMethodException localNoSuchMethodException)
{
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
}
catch (ClassNotFoundException localClassNotFoundException)
{
}
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}