Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the underlying type of a proxy object in java?

Tags:

java

proxy

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

Is this possible?

like image 309
blank Avatar asked Jul 27 '10 13:07

blank


People also ask

What is proxying in Java?

Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.

What is the proxy class?

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 type of dynamic proxy in Spring framework?

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. It can be only proxy by interface so target class needs to implement interface.

What is proxy class with example?

The Proxy class is basically the Proxy design pattern. In this pattern an object provides a modified interface for another class. Let us see one example. In this example, we want to make an array class, that can store only binary values [0, 1].


4 Answers

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.

like image 192
Bozho Avatar answered Oct 18 '22 06:10

Bozho


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());
}
like image 26
Enginer Avatar answered Oct 18 '22 06:10

Enginer


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 ...)

like image 44
Stephen C Avatar answered Oct 18 '22 06:10

Stephen C


Simple and robust:

AopUtils.getTargetClass(object).getName(); 

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

like image 8
winne2 Avatar answered Oct 18 '22 04:10

winne2