Java 动态修改注解值

1、先看注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
 * @Author Minco
 * @Date 8:44 2020-07-24
 * @Description 品牌权限数据注解
 */

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface LimitPinpai {


    /**
     * 用以判断品牌权限的品牌代码字段
     */
    public String target() default "ppdm";

    public boolean limit()  default true;


}

2、实体类

@Data
public class Kchz {

    private String type;

    private String dxdm;
    @LimitCangku(target = "dxdm")
    @LimitPinpai(target = "dxdm")
    private String dxmc;

    private Integer sl;
    @LimitPrice(type = LimitPrice.Type.JL,priceType =LimitPrice.PriceType.BZJJ)
    private String bzjje;
    @LimitPrice(type = LimitPrice.Type.L,priceType =LimitPrice.PriceType.BZSJ)
    private String bzsje;
    private Integer spks;
}

3、动态修改注解

    @GetMapping("getKchz")
    @ApiOperation(value = "库存汇总", notes = "group(分组,[cangku,pinpai,dalei,ndjj])")
    @ApiOperationSupport(includeParameters = {"page","rows","ckdms","group"})
    public JsonResult<IPage<Kchz>> getKchz(Term term) throws Exception {

        fillTermAuthRep(term);

        IPage<Kchz> iPage=zhService.getKchz(term);

        String group =term.getGroup();
        SysUserLimit limit= getUserLimit();
        List<Kchz> list=iPage.getRecords();
        for (Kchz et:list) {
            boolean cangkuL_bool=group.equals("cangku"),pinpaiL_boo=group.equals("pinpai");

            Field detailField = et.getClass().getDeclaredField("dxmc");
            if (detailField != null) {
                detailField.setAccessible(true);
                LimitCangku annotation = detailField.getAnnotation(LimitCangku.class);
                if (annotation != null) {
                    InvocationHandler ih = Proxy.getInvocationHandler(annotation);
                    Field memberValuesField = ih.getClass().getDeclaredField("memberValues");
                    memberValuesField.setAccessible(true);
                    Map memberValues = (Map)memberValuesField.get(ih);
                    memberValues.put("limit", cangkuL_bool);  // set value to false
                }
                LimitPinpai annotation_pp = detailField.getAnnotation(LimitPinpai.class);
                if (annotation_pp != null) {
                    InvocationHandler ih = Proxy.getInvocationHandler(annotation_pp);
                    Field memberValuesField = ih.getClass().getDeclaredField("memberValues");
                    memberValuesField.setAccessible(true);
                    Map memberValues = (Map)memberValuesField.get(ih);
                    memberValues.put("limit", pinpaiL_boo);
                }

            }
            hiddenLimit(et,limit);
        }
        return  toJRAjaxData(iPage);
    }

上一篇:Spring 自定义注解(导出的时候需要增加导出日志而写的)


下一篇:springboot无法加载静态内容问题