Access the concrete class from a spring proxy

Working with Spring-AOP there are moments when you want to access the class type of the incoming method call. In my example I’ve implemented a MethodInterceptor to capture method invocations on a class of type net.solidsyntax.MyProxiedClass.

1
2
3
4
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<? extends Object> clazz = invocation.getThis().getClass()
System.out.println(clazz);
}

Surprisingly the output does not show something like:
class net.solidsyntax.MyProxiedClass

Instead we get a:
com.sun.proxy.$Proxy110

As it turns out the object on which we are working is already wrapped in a proxy by another aspect. In this case one to handle transactions.
So how do we access the concrete class from a spring proxy ?

Our first option is to use the Spring AopUtils.

1
2
3
4
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<? extends Object> clazz = AopUtils.getTargetClass(invocation.getThis());
System.out.println(clazz);
}

However this solution still gives us:
com.sun.proxy.$Proxy110

The reason for this is rather simple. Not only is our object already wrapped by a transaction proxy, there is also a proxy for exception handling. We could iterate until we get a concrete class but we don’t have to. Spring already contains AopProxyUtils which does the work for us.

1
2
3
4
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<?> clazz = AopProxyUtils.ultimateTargetClass(invocation.getThis());
System.out.println(clazz);
}

This changes our output to:
class net.solidsyntax.MyProxiedClass

AopProxyUtils can handle class-based proxies generated with CGLIB as well as interface proxies.