如何使用 cglib 双重增强一个类?

发布于 2024-08-02 02:28:06 字数 1051 浏览 10 评论 0原文

这是代码:

    Patient patient = factory.createPatient();           

    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(patient.getClass());
    enhancer.setCallback(new DefaultMethodInterceptor(patient));
    patient = (Patient) enhancer.create();

    assertThat(patient.getFirstName()).isNotNull();


    Enhancer enhancer2 = new Enhancer();
    enhancer2.setSuperclass(patient.getClass());
    enhancer2.setCallback(new DefaultMethodInterceptor(patient));
    patient = (Patient) enhancer2.create();

    assertThat(patient.getFirstName()).isNotNull();

它在最后一个断言上失败,

net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
...
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file my/package/entity/Patient$$EnhancerByCGLIB$$ca1e6685$$EnhancerByCGLIB$$f52743be

因为我想增强 Hibernate 的实体,但有时它会自行返回已经增强的实体,而我的第二次增强会失败。 我怎样才能避免这种情况?

Here's the code:

    Patient patient = factory.createPatient();           

    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(patient.getClass());
    enhancer.setCallback(new DefaultMethodInterceptor(patient));
    patient = (Patient) enhancer.create();

    assertThat(patient.getFirstName()).isNotNull();


    Enhancer enhancer2 = new Enhancer();
    enhancer2.setSuperclass(patient.getClass());
    enhancer2.setCallback(new DefaultMethodInterceptor(patient));
    patient = (Patient) enhancer2.create();

    assertThat(patient.getFirstName()).isNotNull();

It fails on the last assert with

net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
...
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file my/package/entity/Patient$EnhancerByCGLIB$ca1e6685$EnhancerByCGLIB$f52743be

I ask this because I want to enhance Hibernate's entities, but sometimes it returns already enhanced ones by itself and my second enhancement fails. How can I avoid this?

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

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

发布评论

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

评论(2

我是男神闪亮亮 2024-08-09 02:28:07

您需要通过 Enhancer.isEnhanced() 方法检查您的类是否已被增强。

如果是,您的第二个增强应该应用于原始类,而不是像上面代码中那样应用于已经增强的版本。 您仍然可以在 MethodInterceptor.intercept() 实现中复合增强功能,但必须小心行事。

You need to check whether your class is already enhanced via Enhancer.isEnhanced() method.

If it is, your 2nd enhancement should be applied to original class, not the already enhanced version like you do in the above code. You can still compound your enhancements within MethodInterceptor.intercept() implementation but you have to do that with care.

巾帼英雄 2024-08-09 02:28:07

这对我来说也很有帮助。 只是想指出,在链上调用 getSuperclass() 并检查每个的Enhancer.isEnhanced() 应该找到正确的超类。

This was quite helpful to me, as well. Just wanted to point out that calling getSuperclass() up the chain and checking Enhancer.isEnhanced() for each should locate the proper superclass.

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