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

你可能感兴趣的:(Java基础篇)