In Java, how do you get the original class object and/or class name of a Java EE (CDI) proxy?
When using getName()
on a proxy instance, the name returned is something like
com.company.employeemgmt.EmployeeManager$Proxy$_$$_WeldSubclass
Is there some functionaliy in Java SE (7) or EE (6) that will return either the original, unproxied class instance or its name?
I need:
com.company.employeemgmt.EmployeeManager
Of course, I could simply use string manipulation, but I would like to know if such functionality is already Java-(EE)-inbuilt.
I already found java.reflect.Proxy
, which I could use to detect proxies:
public static void doSomething( Class<? implements Serializable> managerClass )
{
if ( Proxy.isProxyClass( managerClass ) )
{
// unproxy how?
managerClass = managerClass.getUnproxiedClass();
}
// delegate
doSomething( managerClass.getName() );
}
public static void doSomething( String prefix )
{
// do real work
...
}
..., but how would you dereference the original class?
Update:
The trick would be to access MyUtil.doSomething( EmployeeManager.class )
(or MyUtil.doSomething( EmployeeManager.class.getName() )
), but I would like to use/pass MyUtil.doSomething( this.getClass() )
(or MyUtil.doSomething( this.getClass().getName() )
) from all clients as this code can be copied around without manual changes.
A proxy class is a class created at runtime that implements a specified list of interfaces, known as proxy interfaces. A proxy instance is an instance of a proxy class. Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler .
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.
This method returns the java. lang. Class object for a proxy class given a class loader and an array of interfaces. This method returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.
The Proxy method is Structural design pattern that allows you to provide the replacement for an another object. Here, we use different classes to represent the functionalities of another class. The most important part is that here we create an object having original object functionality to provide to the outer world.
Manual Proxy Configuration A manual configuration is generally characterized by a set of proxy addresses, one for each transfer protocol, and a list of domain names for which the proxies should not be used. The set of proxyable transfer protocols includes HTTP, FTP, Gopher, SSL, and WAIS.
Depending on your reasons for using proxies, you may not need or want to configure all protocols. If your users are behind a firewall, it is probably necessary to proxy all requests. If not, you might want to proxy only HTTP and FTP. A proxy cannot cache SSL responses because the content is encrypted.
Finally, a manual configuration is not flexible enough to accommodate systems with multiple proxy machines. For example, you might want to forward one class of requests to one proxy and the remaining traffic to another. To configure Microsoft Internet Explorer Version 5, select View → Internet Options from the menu.
The no_proxy variable holds URL domain names that should not be forwarded to the proxy. In most cases, this includes your local domain (or domains). To specify multiple domains, simply separate them with commas.
Since the proxy class inherits from the original class, I think that you can obtain the original class by getting the proxy superclass.
It depends. You can get the InvocationHandler for a proxy using Proxy.getInvocationHandler(manager). Alas, InvocationHandler is an interface with only one invoke
method and with no feature that lets you get a target class; it all depends on the implementation.
As an example the CXF web servcie framework has a Client and uses a ClientProxy as an associated invocation handler, you can get the Client as such:
ClientProxy handler = (ClientProxy)Proxy.getInvocationHandler(proxiedObject);
Client client = handler.getClient();
To add insult to injury, it seems that the WeldInvocationHandler that you are probably using simply delegates the call to a org.jboss.wsf.spi.invocation.InvocationHandler that that it stores its delegate in a private field. So you need to do quite some magic with reflection to find out the actual class of the target object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With