How can I double-enhance a class with cglib?

Posted by artemb on Stack Overflow See other posts from Stack Overflow or by artemb
Published on 2009-08-14T13:11:41Z Indexed on 2010/04/05 16:43 UTC
Read the original article Hit count: 169

Filed under:
|

Hi there!

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?

© Stack Overflow or respective owner

Related posts about cglib

Related posts about hibernate