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);
}
}