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

网友投稿 714 2022-11-25


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;

/**

* 动态操作注解属性

* @since 2020年8月13日20:49:26

*/

public class AnnotationUtils {

/**

* 查看注解属性

* @param t

* @param name

* @return

* @throws NoSuchFieldException

*/

public Object getJSONFieldProp(T t, String name) throws NoSuchFieldException {

Field field = t.getClass().getDeclaredField(name);

JSONField annotation = field.getAnnotathttp://ion(JSONField.class);

val serialize = annotation.serialize();

return serialize;

}

/**

* 修改注解属性

* @param t

* @param value

* @return

* @throws NoSuchFieldException

* @throws IllegalAccessException

*/

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层面的并不是在对象层面。所以我的设想失败了。。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PropertiesLoaderUtils 出现中文乱码的解决方式
下一篇:java8 stream 由一个list转化成另一个list案例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~