JDK内置 Proxy
类和 InvocationHandler
接口来提供动态代理的实现。在实现连接池的时候动态代理就可以派上用场了。通过代理接管close方法, connectoin关闭的时候就不需要真正关闭,而只是放回连接池,具体实现原理可以参考红薯关于连接池的文章。
我要写的呢是关于在测试使用动态代理时碰到的一个问题,先看我的代码:
首先是接口 IFunction
public interface IFunction {
void doSomething () throws IllegalStateException;
}
接口实现 FunctionImpl
public class FunctionImpl implements IFunction {
@Override
public void doSomething() throws IllegalStateException {
// 方法什么也不做, 只抛异常
throw new IllegalStateException();
}
}
拦截 IFunctioin 的动态代理类 FunctionHandler
public class FunctionHandler implements InvocationHandler{
private IFunction fun;
public FunctionHandler(IFunction function) {
this.fun = function;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(fun, args);
}
}
最后是简单的调用
public class Client {
public static void main(String[] args) {
IFunction fun = (IFunction) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class[]{IFunction.class}, new FunctionHandler(new FunctionImpl()));
try {
fun.doSomething();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
如此,一个简单的动态代理完成了, 一眼瞥上去没什么问题, 可惜一运行就抛异常了
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy0.doSomething(Unknown Source)
at designpattern.proxy.Client.main(Client.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at designpattern.proxy.FunctionHandler.invoke(FunctionHandler.java:21)
... 7 more
Caused by: java.lang.IllegalStateException
at designpattern.proxy.FunctionImpl.doSomething(FunctionImpl.java:9)
... 12 more
很奇怪啊,接口只声明了IllegalStateException
, 结果却抛出了 InvocationTargetException
及 UndeclaredThrowableException
.
接下来我们看看这两个不速之客是如何产生的。
首先这两个异常肯定是接口实现类抛出 IllegalStateException
引起的, 于是可以定位到 java.lang.reflect.Method
的 invoke(Object obj, Object... args)
, 该方法文档已经说明: 当代理的方法抛出异常时 invoke(Object obj, Object... args)
方法会抛出 InvocationTargetException
异常, 也就是说我的 IllegalStateException
此时会被包装成 InvocationTargetException
。
好,现在已经知道 InvocationTargetException
是因为Method反射机制包装产生的。
接下来再看 UndeclaredThrowableException
如何产生。
在 InvocationHandler
声明的方法 invoke(Object proxy, Method method, Object[] args)
的文档中有这么一句话
Throwable the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the
throws
clause of the interface method or to the unchecked exception typesjava.lang.RuntimeException
orcode java.lang.Error
. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in thethrows
clause of the interface method, then anUndeclaredThrowableException
containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.
这里也就是说被代理接口的方法在执行的时候抛出的受检异常必须是接口定义中声明的异常, 如果抛出的受检异常未被接口声明, 那么此时这个异常就会被包装成UndeclaredThrowableException
。
那么也就清楚了,之前已经看到Method.invoke()
时抛出了异常InvocationTargetException
恰好不在 接口声明的异常范围内, 因此动态代理执行的时候会抛出异常 UndeclaredThrowableException
。
对于这个问题可以改良下 FunctionHandler
的代码就可解决
public class FunctionHandler implements InvocationHandler{
private IFunction fun;
public FunctionHandler(IFunction function) {
this.fun = function;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(fun, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}
这样再次运行测试得到的结果如下
java.lang.IllegalStateException
at designpattern.proxy.FunctionImpl.doSomething(FunctionImpl.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at designpattern.proxy.FunctionHandler.invoke(FunctionHandler.java:23)
at com.sun.proxy.$Proxy0.doSomething(Unknown Source)
at designpattern.proxy.Client.main(Client.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
两个不速之客已经消失了, "老板再也不用担心我的异常", 哈哈。
个人博客地址: http://hellofalcon.com/post/p-jdkdynamicproxy/