How to unwrap the original object from a dynamic proxy

后端 未结 3 590
借酒劲吻你
借酒劲吻你 2021-01-11 15:53

what\'s the best approach to unwrap a dynamic proxy to retrieve the original object beneath? The dynamic proxy has been created using java.lang.reflect.Proxy.newProxyI

3条回答
  •  臣服心动
    2021-01-11 16:54

    There's no good method: Proxy.getInvocationHandler(proxy) returns handler, but the problem is to extract the original object from the handler. If your handler is an anonymous class, the only way to extract original object is to use reflection and extract original from field named val$something - very ugly method. Better way is to create non-anonymous handler class with a getter, then you do:

    ((YourHandler)Proxy.getInvocationHandler(proxy)).getOriginalObject()
    

提交回复
热议问题