I am trying to add annotation to class dynamically using javassist
My code as follows
private Class addFrequencyAnnotation(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();
}
But the class returned does not have annotation added. I am not sure what is missing in my code. Could someone help to identify the issue?
Aucun commentaire:
Enregistrer un commentaire