38

I'd like to access the classname of the underlying class which is an instance of java.lang.reflect.Proxy.

Is this possible?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
blank
  • 17,852
  • 20
  • 105
  • 159

9 Answers9

32

You can get the InvocationHandler with which the proxy was created, by calling Proxy.getInvocationHandler(proxy)

Note that in the case of java.lang.reflect.Proxy there is no underlying class per se. The proxy is defined by:

  • interface(s)
  • invocation handler

And the wrapped class is usually passed to the concrete invocation handler.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    Thanks. What does the invocation handler get me? I've got a proxy but i want the classname of the object that is implementing the interface the proxy is built from. I'm thinking it's not possible ... – blank Jul 27 '10 at 14:05
  • There is no object other than the proxy object. The invocation handler is responsible for dispatching the call – Bozho Jul 27 '10 at 14:10
28

I found a good solution on this site (now archived):

@SuppressWarnings({"unchecked"})
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception {
  if (AopUtils.isJdkDynamicProxy(proxy)) {
    return (T) ((Advised)proxy).getTargetSource().getTarget();
  } else {
    return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
  }
}

Usage

@Override
protected void onSetUp() throws Exception {
  getTargetObject(fooBean, FooBeanImpl.class).setBarRepository(new MyStubBarRepository());
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
Enginer
  • 3,048
  • 1
  • 26
  • 22
  • The argument "targetClass" is not used :) – Jad B. Jan 27 '16 at 13:10
  • Yes, it is only for type casting – Enginer Jan 27 '16 at 13:53
  • 1
    In that case, wouldn't it be better to actually use it for casting, rather than suppressing warnings, e.g. `targetClass.cast(proxy)`? This way you're actually guaranteed to get an object of that type, whereas in the other case due to type erasure the casting might not occur depending on the usage pattern. – M. Justin Jun 27 '16 at 18:28
  • This worked perfectly in Java 11 and Spring Boot 2.1.1 as well – Don Rhummy Dec 18 '18 at 20:31
16

Well a Proxy instance won't be an instance of java.lang.reflect.Proxy per se. Rather, it will be an instance of a subclass of java.lang.reflect.Proxy.

Anyway, the way to get the actual proxy classes name is:

Proxy proxy = ...
System.err.println("Proxy class name is " + proxy.getClass().getCanonicalName());

However, you cannot get the name of the class that the Proxy is a proxy for, because:

  1. you proxy interfaces not classes, and
  2. a Proxy can be a proxy for multiple interfaces

However, from looking at the source code of the ProxyGenerator class, it seems that the interfaces are recorded in the generated proxy class as the interfaces of the class. So you should be able to get them at runtime via the proxy classes Class object; e.g.

Class<?>[] classes = proxy.getClass().getInterfaces();

(Note: I've not tried this ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Perfect for searching original interface with only dynamic implementation (by framework). Like port of WS: `Stream.of(port.getClass().getInterfaces()).filter(__ -> __.getAnnotation(WebService.class) != null).findAny()` – gavenkoa Feb 10 '19 at 08:27
8

Simple and robust:

AopUtils.getTargetClass(object).getName(); 

Will also work for CGLIB proxies and non-proxy objects.

winne2
  • 2,188
  • 2
  • 17
  • 13
5

Here was the solution we used with my team (we need the name of the class behind the proxy) :

if (getTargetName(yourBean) ... ) {

}

With this little helper :

private String getTargetName(final Object target) {

    if (target == null) {
        return "";
    }

    if (targetClassIsProxied(target)) {

        Advised advised = (Advised) target;

        try {

            return advised.getTargetSource().getTarget().getClass().getCanonicalName();
        } catch (Exception e) {

            return "";
        }
    }

    return target.getClass().getCanonicalName();
}

private boolean targetClassIsProxied(final Object target) {

    return target.getClass().getCanonicalName().contains("$Proxy");
}

Hope it helps!

lboix
  • 969
  • 12
  • 14
4

You can use the following code for retrieve the info (ArrayUtils is from Apache commons lang) about invocation handler and the interfaces of the current proxy:

String.format("[ProxyInvocationHandler: %s, Interfaces: %s]", 
     Proxy.getInvocationHandler(proxy).getClass().getSimpleName(), 
     ArrayUtils.toString(proxy.getClass().getInterfaces()));

Example result:

[ProxyInvocationHandler: ExecuteProxyChain, Interfaces: {interface com.example.api.CustomerApi}]}
Przemek Nowak
  • 7,173
  • 3
  • 53
  • 57
0

First of all, java.lang.reflect.Proxy works only on interfaces. The framework creates a descendant class that implements the interface(s) but extends java.lang.reflect.Proxy rather than an application class that may be of interest to you. There is no inheritance of multiple classes in modern (2016) Java.

In my case, the debugger shows that the object of interest is in the obj field in the invocation handler of the proxy object.

    Object handler = Proxy.getInvocationHandler(somethingProxied);
    Class handlerClass = handler.getClass();
    Field objField = handlerClass.getDeclaredField("obj");
    objField.setAccessible(true);
    Object behindProxy = objField.get(handler);

You will have to catch() two exceptions: NoSuchFieldException and IllegalAccessException.

I found it useful to print the list of fields of declared fields from the catch() clause:

...
} catch (NoSuchFieldException nsfe) {
    nsfe.printStackTrace();

    Object handler = Proxy.getInvocationHandler(somethingProxied);
    Class handlerClass = handler.getClass();
    for (Field f : handlerClass.getDeclaredFields()) {
        f.setAccessible(true);
        String classAndValue = null;
        try {
            Object v = f.get(handler);
            classAndValue= "" + (v == null ? "" : v.getClass()) + " : " + v;
        } catch (IllegalAccessException iae) {
            iae.printStackTrace();
        }
        System.out.println(" field: " + f.getName() + " = " + classAndValue+ ";");
    }
...
}

Note that different frameworks use different proxies and even different techniques of proxying. The solution that worked for me may be not applicable in your case. (It definitely will not work for Javassist or Hibernate proxies.)

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

Static Hibernate.getClass() might be useful in this scenario.

Get the true, underlying class of a proxied persistent class

    public static Class getClass(Object proxy) {
        if ( proxy instanceof HibernateProxy ) {
            return ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer()
                    .getImplementation()
                    .getClass();
        }
        else {
            return proxy.getClass();
        }
    }
Rafael
  • 2,521
  • 2
  • 33
  • 59
0

I found the perfect solution for me in org.springframework.aop.framework.AopProxyUtils:

AopProxyUtils.ultimateTargetClass(object).getCanonicalName()

Nedko
  • 567
  • 4
  • 12