[jboss-jira] [JBoss JIRA] (JASSIST-256) Javassist - add annotation to class doesn't work on reflections

Rui C. (JIRA) issues at jboss.org
Fri Dec 18 02:10:00 EST 2015


Rui C. created JASSIST-256:
------------------------------

             Summary: Javassist - add annotation to class doesn't work on reflections
                 Key: JASSIST-256
                 URL: https://issues.jboss.org/browse/JASSIST-256
             Project: Javassist
          Issue Type: Bug
    Affects Versions: 3.20.0-GA
         Environment: JDK1.7, IDEA
            Reporter: Rui C.
            Assignee: Shigeru Chiba
            Priority: Minor



{code:java}
           ClassPool pool = ClassPool.getDefault();
            CtClass superclass = pool.getCtClass(AbstractEntity.class.getName());
            CtClass cc = pool.makeClass("com.rapid.develop.entity.Ponit", superclass);

            ClassFile ccFile = cc.getClassFile();
            ConstPool constpool = ccFile.getConstPool();

            AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
            Annotation entityAnno = new Annotation("Entity", constpool);

            Annotation tableAnno = new Annotation("Table", constpool);
            tableAnno.addMemberValue("name", new StringMemberValue("test", ccFile.getConstPool()));

            attr.addAnnotation(entityAnno);
            attr.addAnnotation(tableAnno);
            ccFile.addAttribute(attr);

           Class clazz = cc.toClass();
            Object o = clazz.newInstance();
            assertTrue(o.getClass().getName().equals("com.rapid.develop.entity.Ponit"));
            
            // annotations length = zero
            java.lang.annotation.Annotation[] annotations = clazz.getDeclaredAnnotations();
            assertEquals(2, annotations.length); 
{code}

I used javassist to add annotation for class, I saw these annotations were added to class file.
But I can't get them using java API clazz.getDeclaredAnnotations() and clazz.getAnnotations() 
 



--
This message was sent by Atlassian JIRA
(v6.4.11#64026)



More information about the jboss-jira mailing list