javassist给方法添加注解
2018-07-20 来源:open-open
package cn.outofmemory.hello.javassist; import javassist.*; import javassist.bytecode.AnnotationsAttribute; import javassist.bytecode.ClassFile; import javassist.bytecode.ConstPool; 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(); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:iOS 字符串常用判定库
下一篇:java8的接口默认方法实例
最新资讯
热门推荐