覆盖等于cglib代理 [英] Override equals on a cglib proxy

查看:80
本文介绍了覆盖等于cglib代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CGLIB代理将我自己的可重用equals()方法添加到现有对象中.

对象不一定实现任何接口,我需要能够将代理对象强制转换为原始类(而无需获取代理的目标).

不幸的是,似乎CGLIB实现了自己的equals()方法并确保仅调用该方法:有一个私有静态类(EqualsInterceptor),其方法intercept()实现了用于比较代理对象的合理逻辑.问题在于该方法最后将比较委托给目标对象:我需要重新使用一些目标类未实现的逻辑.

使用标准代理,我可以截获对equals()方法的调用并执行我的逻辑.问题是这类代理不能转换为原始类.

似乎唯一的方法是重写CGLIB库中的某些类.这似乎不是一个好主意.

解决方案

否,使用cglib是不可能的.

您可以使用另一个库(例如Byte Buddy),使您可以像其他任何方法一样拦截 equals / hashCode .

公开信息:我是Byte Buddy的作者,也是cglib的维护者,这两家公司均已获得Apache 2.0许可.

I would like to use a CGLIB proxy to add my own reusable equals() method to existing objects.

The objects do not necessarily implement any interfaces and I need to be able to cast the proxied object to the original class (without getting the target of the proxy).

Unfortunately, it seems that CGLIB implements its own equals() method and makes sure that only that method is called: there is a private static class (EqualsInterceptor) whose method intercept() implements a reasonable logic to compare proxied objects. The problem is that this method, at the end, delegates the comparison to the target objects: I need instead to reuse some logic that is not implemented by the target classes.

Using a standard proxy, I was able to intercept the call to the equals() method and execute my logic. The problem is that these kind of proxies cannot be cast to the original class.

It seems that the only way is to rewrite some classes in the CGLIB library. It does not seem a good idea.

解决方案

No, this is not possible using cglib.

You can use another library such as Byte Buddy which allows you to intercept equals/hashCode just like any other method.

For disclosure: I am the author of Byte Buddy and a maintainer for cglib which are both Apache 2.0 licensed.

这篇关于覆盖等于cglib代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆