[jboss-user] [Javassist] - Javassist seems to be stripping off existing annotations - why?

Paul Cugini do-not-reply at jboss.com
Mon Jun 4 08:32:19 EDT 2012


Paul Cugini [https://community.jboss.org/people/cuginip] created the discussion

"Javassist seems to be stripping off existing annotations - why?"

To view the discussion, visit: https://community.jboss.org/message/739662#739662

--------------------------------------------------------------
Hello.   I'm trying to use Javassist in a jUnit test.   My objective is to create a dynamic class based on an existing class, for the purposes of dynamically adding an annotation.   In my test I'm also replacing the method body of one of the methods of the original class.   This basically seems to work.  However I've noticed something which is very annoying and which has hindered my progress.  It seems as though when javassist is creating the new class based on the specified original, all of the existing annotations on the existing class are stripped off in the new dynamic class created by Javassist.  This seems to be occurring even when my code doesn't include any modifications to the annotations on the original.  The only way I've seen to get around this is to re-add all of the annotations that need to be there, but this is very cumbersome.  The version of javassist I'm using is 3.12.   I'm wondering if this is a known issue, or if perhaps I'm doing something wrong that is causing this to happen.  

Here is a snippet of my code:

@Test
public testCase {
...
Class newClass = buildClassWithMethod(ExistingClass.class, "getRecordClass", "{return + dynamicBeanClass.getName() +  ".class";}");
...
Method controlInitMethod = getInitMethod(ExistingClass.class);
ExistingAnnotation controlExistingAnnotation = controlInitMethod.getAnnotation(ExistingAnnotation.class);
Assert.assertNotNull(controlExistingAnnotation); // PASSES as expected.
Method initMethod = getInitMethod(newClass);
ExistingAnnotation existingAnnot = initMethod.getAnnotation(ExistingAnnotation.class);
Assert.assertNotNull(existingAnnot); // FAILS!
...
}


private Class buildClassWithMethod(Class originalClass,
                                                     String newMethodName,
                                                     Stirng newMethodText) throws Exception {
ClassPool pool = ClassPool.getDefault();
Loader loader = new Loader(pool);
pool.insertClassPath(new ClassClassPath(originalClass));
CtClass cc = pool.getCtClass(originalClass.getName());
CtMethod methodToReplace = cc.getDeclaredMethod(newMethodName);
methodToReplace.setBody(newMethodText);
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();
Class dynamicBeanClass = cc.toClass(loader, null);
return dynamicBeanClass;
}
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/739662#739662]

Start a new discussion in Javassist at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20120604/fb1b78ec/attachment.html 


More information about the jboss-user mailing list