[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:59:00 EST 2015


    [ https://issues.jboss.org/browse/JASSIST-256?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13142831#comment-13142831 ] 

Rui C. commented on JASSIST-256:
--------------------------------

Hi,

I have resolve this problem. I fogot to specify the annotation type 
{code:java}
CtClass entityclass = pool.getCtClass(Entity.class.getName());
Annotation entityAnno = new Annotation(constpool, entityclass);
{code}


But I found there's some wrong with ArrayMemberValue 
if I don't initialization the value for  Annotation array paramter. It will produce an null exception with ArrayMemberValue in line 133.
{code:java}
    public void write(AnnotationsWriter writer) throws IOException {
        int num = values.length;
        writer.arrayValue(num);
        for (int i = 0; i < num; ++i)
            values[i].write(writer);
    }
{code}


> 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