使用Javassist向类添加注释

使用Javassist向类添加注释,java,reflection,annotations,javassist,Java,Reflection,Annotations,Javassist,我正在尝试使用javassist动态地向类添加注释 我的代码如下 private Class addAnnotation(String className,String annotationName, int frequency) throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.makeClass(className); ClassFile classFi

我正在尝试使用javassist动态地向类添加注释

我的代码如下

private Class addAnnotation(String className,String annotationName, int frequency) throws Exception{
    ClassPool pool = ClassPool.getDefault();
    CtClass ctClass = pool.makeClass(className);

    ClassFile classFile = ctClass.getClassFile();
    ConstPool constpool = classFile.getConstPool();

    AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    Annotation annotation = new Annotation(annotationName, constpool);
    annotation.addMemberValue("frequency", new IntegerMemberValue(classFile.getConstPool(), frequency));
    annotationsAttribute.setAnnotation(annotation);

    ctClass.getClassFile().addAttribute(annotationsAttribute);

    return ctClass.toClass();
  }
但是返回的类没有添加注释

Class annotatedClass = addFrequencyAnnotation(MyClass.class.getSimpleName(),
          MyAnnotation.class.getSimpleName(), 10);

annotatedClass.isAnnotationPresent(MyAnnotation.class); // Returns false

我不确定我的代码中缺少了什么。是否有人可以帮助确定问题?

您应该使用
MyAnnotation.class.getName
而不是
MyAnnotation.class.getSimpleName
。因为有
MyAnnotation
但没有
yourpackage.MyAnnotation

  public static void main(String[] args) throws Exception {
    Class<?> annotatedClass = addAnnotation(MyClass.class.getName(), MyAnnotation.class.getName(), 10);

    System.out.println(annotatedClass.getAnnotation(MyAnnotation.class));
  }

  private static Class<?> addAnnotation(String className, String annotationName, int frequency) throws Exception {
    ClassPool pool = ClassPool.getDefault();
    CtClass ctClass = pool.makeClass(className + "1");//because MyClass has been defined

    ClassFile classFile = ctClass.getClassFile();
    ConstPool constpool = classFile.getConstPool();

    AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    Annotation annotation = new Annotation(annotationName, constpool);
    annotation.addMemberValue("frequency", new IntegerMemberValue(classFile.getConstPool(), frequency));
    annotationsAttribute.setAnnotation(annotation);

    ctClass.getClassFile().addAttribute(annotationsAttribute);
    return ctClass.toClass();
  }
publicstaticvoidmain(字符串[]args)引发异常{
Class annotatedClass=addAnnotation(MyClass.Class.getName(),MyAnnotation.Class.getName(),10);
System.out.println(annotatedClass.getAnnotation(MyAnnotation.class));
}
私有静态类addAnnotation(String className、String annotationName、int frequency)引发异常{
ClassPool=ClassPool.getDefault();
CtClass CtClass=pool.makeClass(className+“1”);//因为已经定义了MyClass
ClassFile ClassFile=ctClass.getClassFile();
ConstPool ConstPool=classFile.getConstPool();
AnnotationsAttribute AnnotationsAttribute=新的AnnotationsAttribute(constpool,AnnotationsAttribute.visibleTag);
注释=新注释(注释名称,constpool);
annotation.addMemberValue(“frequency”,新的IntegerMemberValue(classFile.getConstPool(),frequency));
annotationsAttribute.setAnnotation(注释);
ctClass.getClassFile().addAttribute(annotationsAttribute);
返回ctClass.toClass();
}