Java动态修改注解

import cn.afterturn.easypoi.excel.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 = ClassPool.getDefault();
        CtClass ct = pool.get("Animal");
        CtField cf = ct.getField("age");
        FieldInfo fieldInfo = cf.getFieldInfo();
        System.out.println("属性名称: " + cf.getName());

        System.out.println("***********************************");
        AnnotationsAttribute attribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
        ConstPool cp = fieldInfo.getConstPool();
        Annotation annotation = new Annotation("Export(name=\"年龄\", order=1, width=20)", cp);

        attribute.addAnnotation(annotation);
        System.out.println(attribute);
        Annotation[] annotations = attribute.getAnnotations();
        System.out.println(Arrays.toString(annotations));
        System.out.println(annotation);
        System.out.println("***********************************");


        ConstPool cp = fieldInfo.getConstPool();
        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.setAnnotation(annotation);

        Annotation annotation2 = attribute2.getAnnotation("Export");
        String text = ((StringMemberValue) annotation2.getMemberValue("unitName")).getValue();
        System.out.println("修改后的注解名称==:" + text);

        AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
        System.out.println(annotationsAttribute.getAnnotation("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 = ClassPool.getDefault();
            CtClass ct = pool.get(className);
            CtField cf = ct.getField(attributeName);
            FieldInfo fieldInfo = cf.getFieldInfo();
            AnnotationsAttribute attribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
            ConstPool cp = fieldInfo.getConstPool();
            Annotation annotation = new Annotation(typeName, cp);
            System.out.println(annotation);
            attribute.addAnnotation(annotation);
            System.out.println(Arrays.toString(attribute.getAnnotations()));
        } catch (NotFoundException e) {
            log.error("此类不存在", e);
        }
    }

 

https://blog.csdn.net/jly4758/article/details/44774217

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中,动态添加字段注解通常是通过反射(Reflection)API实现的。反射允许程序在运行时检查和修改类、接口、字段、方法等的信息。以下是一个简单的例子,展示如何动态添加`@Deprecated`注解: ```java import java.lang.reflect.Field; import java.lang.annotation.Annotation; public class DynamicAnnotationExample { public static void addDeprecatedAnnotation(String className, String fieldName) throws Exception { // 获取目标类的Class对象 Class<?> clazz = Class.forName(className); // 获取字段 Field field = clazz.getDeclaredField(fieldName); // 检查是否已存在注解,如果已存在,则不需要再次添加 if (!field.isAnnotationPresent(Deprecated.class)) { // 获取注解类型 Class<? extends Annotation> annotationType = Deprecated.class; // 创建注解实例 Annotation annotation = annotationType.newInstance(); // 添加注解到字段 field.setAccessible(true); // 如果是private,需要设置为可访问 field.addAnnotation(annotation); } } public static void main(String[] args) throws Exception { addDeprecatedAnnotation("com.example.MyClass", "myField"); // 替换为你的类名和字段名 } } ``` 在这个例子中,我们首先获取指定类和字段的`Class`对象,然后检查该字段是否已经应用了`@Deprecated`注解。如果没有,我们创建一个新的`Deprecated`注解实例,并使用`addAnnotation`方法将其添加到字段上。 请注意,这只是一个基本示例,实际使用时可能需要处理异常,并且动态添加注解可能违反某些设计原则或引发安全问题,因此通常只在特定场景下(如测试工具或特定框架内部)使用。此外,对于私有字段,`setAccessible(true)`会暴露潜在的安全漏洞,仅在必要时使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值