Java动态修改注解

更新时间:2023-08-03 12:35:38 阅读: 评论:0

Java动态修改注解
import cn.l.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
public class Animal {
@ApiModelProperty(value = "年龄", position = 3)
福建土楼介绍//    @Export(name = "年龄", order = 1, width = 20)
@Excel(name = "年龄", orderNum = "1")
public String age;
}
import javassist.ClassPool;
import javassist.CtClass;
招财图片头像import javassist.CtField;
import javassist.NotFoundException;
import javassist.bytecode.*;
import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.StringMemberValue;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws NotFoundException {
ClassPool pool = Default();
CtClass ct = ("Animal");
带新字的成语CtField cf = ct.getField("age");
加强责任心>奇淫技巧
FieldInfo fieldInfo = cf.getFieldInfo();
System.out.println("属性名称: " + cf.getName());
System.out.println("***********************************");
AnnotationsAttribute attribute = (AnnotationsAttribute) Attribute(AnnotationsAttribute.visibleTag);        ConstPool cp = ConstPool();
Annotation annotation = new Annotation("Export(name=\"年龄\", order=1, width=20)", cp);
attribute.addAnnotation(annotation);
System.out.println(attribute);
Annotation[] annotations = Annotations();
System.out.String(annotations));
System.out.println(annotation);
孙悟空有什么特点System.out.println("***********************************");
ConstPool cp = ConstPool();
AnnotationsAttribute attribute2 = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
筋膜炎的原因System.out.println(attribute2);
//        Annotation annotation = new Annotation("Export", cp);
System.out.println(annotation);
annotation.addMemberValue("unitName", new StringMemberValue("basic-entity", cp));
attribute2.tAnnotation(annotation);
Annotation annotation2 = Annotation("Export");
String text = ((StringMemberValue) MemberValue("unitName")).getValue();
System.out.println("修改后的注解名称==:" + text);
AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(cp, AnnotationsAttribute.visible
Tag);        System.out.Annotation("io.swagger.annotations.ApiModelProperty"));
System.out.println(annotationsAttribute);
}
}
/**
* 功能,动态的给类属性添加注解
驼背鲸*
* @param className 类名
* @param attributeName 类属性
* @param typeName 注解类型
*/
public static void addAnnotation(String className, String attributeName, String typeName) {
try {
ClassPool pool = Default();
CtClass ct = (className);
CtField cf = ct.getField(attributeName);
FieldInfo fieldInfo = cf.getFieldInfo();
AnnotationsAttribute attribute = (AnnotationsAttribute) Attribute(AnnotationsAttribute.visibleTag);            ConstPool cp = ConstPool();
Annotation annotation = new Annotation(typeName, cp);
System.out.println(annotation);
attribute.addAnnotation(annotation);
System.out.Annotations()));
} catch (NotFoundException e) {
<("此类不存在", e);
}
}

本文发布于:2023-08-03 12:35:38,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1127645.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:注解   责任心   动态   给类   属性   福建
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图