lundi 13 mai 2019

How to get 'proxied' object from an existing dynamic proxy

Is there an some API like Proxy.getProxiedObject(), that will return the original object of a dynamic proxy? I would like, for example, to invoke equals on the proxied objects, rather than on the dynamic proxies themselves, like in the following example:

public class ProxyTest implements InvocationHandler {

    public static Object createProxy(Object target) {
        Class<? extends Object> clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ProxyTest());      
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
        // PROXIED OBJECTS COMPARISON - DESIRED
//      return Proxy.getProxiedObject(proxy).equals(Proxy.getProxiedObject(args[0]));

        // DYNAMIC PROXIES COMPARISON - UNDESIRED
//      return proxy.equals(args[0]);

        return null;
    }

    public static void main(String[] args) {
        Object proxied = createProxy(new Object());
        System.out.println(proxied.equals(proxied));
    }
}





Aucun commentaire:

Enregistrer un commentaire