Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic proxy and checked exceptions

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.

like image 551
Konrad Garus Avatar asked Sep 15 '10 06:09

Konrad Garus


People also ask

What is a dynamic proxy?

A dynamic proxy class is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface.

What is the difference between static proxy and dynamic proxy?

Dynamic Proxy. Dynamic proxies differ from static proxies in a way that they do not exist at compile time. Instead, they are generated at runtime by the JDK and then made available to the users at runtime. We need to understand the following two components to write a dynamic proxy.

How does JDK dynamic proxy work?

JDK Dynamic Proxies allow one to create implementations of Java interfaces at runtime by the means of Reflection. A proxy may be seen as a subject that will forward method calls to target instances and eventually return any result produced by the target instance to the caller.

How many types of dynamic proxy is available in spring?

Spring used two types of proxy strategy one is JDK dynamic proxy and other one is CGLIB proxy. JDK dynamic proxy is available with the JDK.


2 Answers

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/

like image 167
rvange Avatar answered Oct 11 '22 14:10

rvange


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.

like image 37
Abhinav Sarkar Avatar answered Oct 11 '22 14:10

Abhinav Sarkar