我正在尝试向类和方法添加注释。我正在使用javaassist创建类。我尝试了下面的代码,但它没有为类和方法添加注释。
ClassFile ccFile = cc.getClassFile();
ConstPool cp = ccFile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
Annotation as = new Annotation("WebService", cp);
attr.setAnnotation(as);
ccFile.addAttribute(attr);
attM = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
Annotation am = new Annotation("WebMethod", cp);
attM.setAnnotation(am);
String methodString ="...";
CtMethod method = CtNewMethod.make(methodString, cc);
method.getMethodInfo().addAttribute(attM);
cc.addMethod(method);
System.out.println("cc annotation: "+cc.getAvailableAnnotations().length);
cc注释:0
非常感谢这里的任何帮助/指示。谢谢!