package com.bonus.material.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

public class ReflectUtils {

    /**
     * 修改注解的属性值
     */
    public static void setAnnotationValue(Annotation annotation, String attrName, Object value) throws Exception {
        // 1. 获取注解的代理对象
        InvocationHandler handler = Proxy.getInvocationHandler(annotation);
        // 2. 获取代理对象的memberValues字段（存储注解属性）
        Field memberValuesField = handler.getClass().getDeclaredField("memberValues");
        memberValuesField.setAccessible(true);
        // 3. 修改属性值
        Map<String, Object> memberValues = (Map<String, Object>) memberValuesField.get(handler);
        memberValues.put(attrName, value);
    }
}
