zl程序教程

您现在的位置是:首页 >  后端

当前栏目

javassist给方法添加注解详解编程语言

方法编程语言 详解 添加 注解
2023-06-13 09:20:29 时间
import javassist.bytecode.annotation.Annotation; import javassist.bytecode.annotation.IntegerMemberValue; import java.io.IOException; * add annotation with javassist * Created by outofmemory.cn on 2015/12/14. public class App { public static void main(String[] args) throws CannotCompileException, IOException { ClassPool pool = ClassPool.getDefault(); // create the class CtClass cc = pool.makeClass("foo"); 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); // create the method CtMethod mthd = CtNewMethod.make("public Integer getInteger() { return null; }", cc); cc.addMethod(mthd); mthd.getMethodInfo().addAttribute(attr); cc.writeFile("./"); // generate the class Class ? clazz = cc.toClass(); // length is zero java.lang.annotation.Annotation[] annots = clazz.getAnnotations(); }

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10474.html

cjava