public interface IA{
public void run()throws IOException;
}
// 实现类
public class IAImpl implements IA{
@Override
public void run() throws IOException{
System.out.println("IAImpl的run方法");
throw new IOException();·
}
}
// InvocationHandle的实现
class InvocationHandlerImpl implements InvocationHandler {
//目标对象
private Object target;
public InvocationHandlerImpl(Object target){
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {·
Object result = null;
System.out.println("before...");
// 调用目标方法,目标方法所有的异常会被包装成InvocationTargetException异常往上抛
try {
result = method.invoke(this.target, args);
} catch (InvocationTargetException e) { // 如果这里返货捕获InvocationTargetException 异常并且返回原始异常,则动态代理接收到的就是InvocationTargetException异常
throw e.getTargetException();
// 如果是InvocationTargetException 异常,则返回被包装的目标方法原始异常(Spring AOP调用目标方法后抛出异常也是这样实现)
// 因为此异常没被声明,最终会被动态代理包成为UndeclaredThrowableException异常
}
System.out.println("after...");
return result;
}
}
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());
}
}