Dynamic proxy and checked exceptions

泄露秘密 提交于 2019-12-04 18:25:02

问题


How can I make my dynamic proxy throw checked exceptions?

I need a transparent wrapper for an interface which sometimes throws checked exceptions such as IOException. Is it possible without 3rd party AOP or writing my own proxy? Modifying all 20 methods of the interface by hand is not an option either.


回答1:


You can use a dynamic proxy. As long as the checked exceptions are part of the interface you can throw the checked exceptions from the invocation handler. Otherwise this is illegal and will create an UndeclaredThrowableException that wraps the thrown checked exception.

interface A{
    void x() throws IOException;
}

A proxy = (A) newProxyInstance(classLoader, new Class<?>[]{A.class}, 
  new InvocationHandler() {      
        @Override
        public Object invoke(Object arg0, Method arg1, Object[] arg2) 
            throws Throwable {
            throw new IOException();
        }
   }
);
proxy.x();

Output:

Exception in thread "main" java.io.IOException
at X$1.invoke(X.java:19)
at $Proxy0.x(Unknown Source)
at X.main(X.java:22)

With an undeclared checked exception for interface A:

interface A{
    void x();
}

Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
  at $Proxy0.x(Unknown Source)
  at X.main(X.java:22)
Caused by: java.io.IOException
  at X$1.invoke(X.java:19)
  ... 2 more



回答2:


What you probably are looking for is this, as Konrad mentions above:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
        Object value = method.invoke(delegate, args);
        return value;
    }
    catch (InvocationTargetException ex) {
        throw ex.getCause();
    }
}

Source: https://web.archive.org/web/20120130204437/http://benpryor.com/blog/2006/08/15/java-dynamic-proxies-and-invocationtargetexception/




回答3:


A dynamic proxy can throw checked exception if the exception is declared in the signature of the method of the interface it is proxying. From the Sun's Dynamic Proxy reference:

If an exception is thrown by the invoke method, it will be also thrown by the method invocation on the proxy instance.

The exception's type must be assignable to either any of the exception types declared in the signature of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error.

If a checked exception is thrown by invoke that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException will be thrown by the method invocation on the proxy instance. The UndeclaredThrowableException will be constructed with the exception that was thrown by the invoke method.



来源:https://stackoverflow.com/questions/3715298/dynamic-proxy-and-checked-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!