栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java注解之运行时修改字段的注解值操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java注解之运行时修改字段的注解值操作

今天遇到需求:导入Excel时候列头会发生变化,客户是大爷要求你改代码,

导入Excel是用easypoi做的,识别表头是用注解@Excel(name = "xxx")通过这个name来匹配

那你表头要动,我这个注解是硬编码

所以就有动态设置这个表头

public class JavaVo{
@Excel(name = "xxx")
private String userName;
//省略getset方法
}

ExcelimportUtil.importExcel(file.getInputStream(), configClass(JavaVo.class), params);

代码如下

 private Class configClass(Class c , String val) {
   
   Field[] fields = c.getDeclaredFields();
   try {
  for(int i = 0;i < fields.length;i++){
  Field f = fields[i];
  Excel excelAn = f.getAnnotation(Excel.class);//Excel是注解类型
  if(excelAn == null){
   continue;
  }
  InvocationHandler h = Proxy.getInvocationHandler(excelAn);
  Field hField = h.getClass().getDeclaredField("memberValues");
    // 因为这个字段事 private final 修饰,所以要打开权限
    hField.setAccessible(true);
    // 获取 memberValues
    Map memberValues = (Map) hField.get(h);
    // 修改 value 属性值 这里修改的是@Excel(name = "姓名")
 //name是key
    memberValues.put("name", val);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 return c;
 }

补充知识:java动态修改 注解的值,控制对象转化为json字符串的字段是否序列化

定义一个对象使用@JSONField控制该对象属性是否需要序列化

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

@Data
public class A {
  @JSonField(serialize = false)
  private String extendParams;

  @JSonField(serialize = true)
  private String sad;
}

编写工具类

import com.alibaba.fastjson.annotation.JSONField;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
import lombok.val;


public class AnnotationUtils {
  
  public Object getJSonFieldProp(T t, String name) throws NoSuchFieldException {
    Field field = t.getClass().getDeclaredField(name);
    JSonField annotation = field.getAnnotation(JSONField.class);
    val serialize = annotation.serialize();
    return serialize;
  }

  
  public Object setJSonFieldProp(T t,String name, Object value) throws NoSuchFieldException, IllegalAccessException {
    Field field = t.getClass().getDeclaredField(name);
    JSonField annotation = field.getAnnotation(JSONField.class);
    InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
    Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
    memberValues.setAccessible(true);
    Map map = (Map) memberValues.get(invocationHandler);
    map.put("serialize",value);
    val serialize = annotation.serialize();
    return serialize;
  }
}

测试

import com.alibaba.fastjson.JSON;

public class TT {

  public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
    AnnotationUtils aAnnotationUtils = new AnnotationUtils<>();
    A a = new A();
    a.setExtendParams("exex");
    a.setSad("sadsad");

    Object extendParams = aAnnotationUtils.getJSonFieldProp(a, "extendParams");//查询注解的值
    System.out.println(extendParams.toString());
//    System.out.println(JSON.toJSonString(a));

    Object extendParams1 = aAnnotationUtils.setJSonFieldProp(a, "extendParams", true);//修改注解的值
    System.out.println(extendParams1.toString());
    System.out.println(JSON.toJSonString(a));
  }
}

去掉main里面的注解看看效果,这个好像是发生了jvm优化导致的问题。。。

注释第一个print 打印结果如下:

false
true
{"extendParams":"exex","sad":"sadsad"}

不注释第一个print 打印结果如下:

false
{"sad":"sadsad"}
true
{"sad":"sadsad"}

接下来我们在做一个测试

  public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
    List aList = new ArrayList<>();
    for(int i=0; i<10; i++){
      AnnotationUtils aAnnotationUtils = new AnnotationUtils<>();
      A a = new A();
      a.setExtendParams("exex");
      a.setSad("sadsad");
      if(i%2 == 0) {
 aAnnotationUtils.setJSonFieldProp(a, "extendParams", true);//修改注解的值
      }
      aList.add(a);
    }
    System.out.println(JSON.toJSonString(aList));
  }

打印结果

[{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"},{"extendParams":"exex","sad":"sadsad"}]

我本想用修改注解的方式来修改某个字段的序列化与不序列化,但是我发现注解是在class层面的并不是在对象层面。所以我的设想失败了。。

以上这篇java注解之运行时修改字段的注解值操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132374.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号