Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Original class name of a proxy (without manual string manipulation)

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.

like image 330
Kawu Avatar asked Jan 24 '13 21:01

Kawu


People also ask

What is a proxy class in Java?

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 .

What is the meaning of 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.

How do you use Java Lang reflect proxy class write a program?

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.

What is a proxy 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.

What is a manual proxy configuration?

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.

Do I need to configure all protocols with my proxies?

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.

Is it possible to configure multiple proxy machines?

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.

What is the no_proxy variable used for?

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.


2 Answers

Since the proxy class inherits from the original class, I think that you can obtain the original class by getting the proxy superclass.

like image 112
tahagh Avatar answered Sep 22 '22 15:09

tahagh


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.

like image 44
nd. Avatar answered Sep 19 '22 15:09

nd.