kullanarak bir çalışma zamanı tarafından oluşturulan yöntem/sınıf ek açıklama ekleme foo
, bar
yöntemiylekullanmak için kullanıyorum, ancak ek açıklama eklemenin bir yolunu bulmak gibi görünmüyor (ek açıklama kendisi değil) t çalışma zamanı oluşturuldu).Javassist
ClassPool pool = ClassPool.getDefault();
// create the class
CtClass cc = pool.makeClass("foo");
// create the method
CtMethod mthd = CtNewMethod.make("public Integer getInteger() { return null; }", cc);
cc.addMethod(mthd);
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();
// create the annotation
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
Annotation annot = new Annotation("MyAnnotation", constpool);
annot.addMemberValue("value", new IntegerMemberValue(ccFile.getConstPool(), 0));
attr.addAnnotation(annot);
ccFile.addAttribute(attr);
// generate the class
clazz = cc.toClass();
// length is zero
java.lang.annotation.Annotation[] annots = clazz.getAnnotations();
Ve açıkçası ben annots
beri yanlış bir şey yapıyorum boş dizidir: Denedim kod buna benzemez.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
int value();
}