如何从动态代理中解开原始对象

发布于 2024-10-06 21:14:29 字数 112 浏览 0 评论 0原文

解开动态代理以检索下面的原始对象的最佳方法是什么? 动态代理已使用 java.lang.reflect.Proxy.newProxyInstance() 创建,

谢谢。

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思故 2024-10-13 21:14:29

每个代理都有一个 InitationHandler< /code>与之关联。只有 IncationHandler 知道哪个对象(如果有)位于代理之下。如果您控制代理的创建,那么您可以提供自己的 InitationHandler ,它将具有您想要的额外功能(即能够公开底层对象)。如果您不这样做,那我恐怕你就不走运了。

Each proxy has an InvocationHandler associated with it. Only the InvocationHandler knows which object (if any) underlies the proxy. If you control the creation of the proxy, then you can supply your own InvocationHandler that will have the extra functionality that you desire (i.e. will be able to disclose the underlying object.) If you don't, then I am afraid you're out of luck.

相权↑美人 2024-10-13 21:14:29

没有好的方法:Proxy.getInspirationHandler(proxy) 返回处理程序,但问题是从处理程序中提取原始对象。如果您的处理程序是匿名类,则提取原始对象的唯一方法是使用反射并从名为 val$something 的字段中提取原始对象 - 非常丑陋的方法。
更好的方法是使用 getter 创建非匿名处理程序类,然后执行以下操作:

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

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()
昵称有卵用 2024-10-13 21:14:29

您可以使用Proxy.getInitationHandler(proxy)方法来获取原始的InitationHandler。

You can use the Proxy.getInvocationHandler(proxy) method to obtain the original InvocationHandler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文