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相关文章
- python中os模块的常用方法详解编程语言
- java按文件大小、名称、日期排序方法详解编程语言
- js方法将系统当前时间转换为字符串详解编程语言
- [javaSE] 集合框架(共性方法)详解编程语言
- Eclipse中出现JS文件前有红叉的解决方法详解编程语言
- 多线程并发测试代码及解决方法详解编程语言
- JS遍历属性和方法详解编程语言
- kafka运行错误:提示找不到或者无法加载主类错误解决方法详解编程语言
- Java:判断字符串是否为数字的五种方法详解编程语言
- Python3.x:os.mkdir与 os.makedirs(创建目录方法)区别详解编程语言
- ConcurrentLinkedQueue使用和方法介绍详解编程语言
- 官方提供的屏蔽百度转码Baidu Transcoder的方法no-transform详解编程语言
- javascript 生成blob url的方法详解编程语言
- CSS3手机端字体不能小于12号的方法详解编程语言
- php cli传递参数的方法详解编程语言
- PHP判断是否为手机端的方法详解编程语言
- php显示emoji表情的方法详解编程语言
- Edge浏览器自动给数字加下划线的解决方法详解编程语言
- 一种内表转XML的方法step by ste详解编程语言
- ABAP 动态生成内表的几种方法(Several Methods of Dynamic Internal Table Creation)详解编程语言