博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA 注解反射 过滤修改对象值
阅读量:2385 次
发布时间:2019-05-10

本文共 2779 字,大约阅读时间需要 9 分钟。

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 PriceLimit {    /**     * 字段类型(0:不限制;1:进价类权限限制;2:价格类权限限制)     */    Type type() default Type.ALL;    public enum Type {        ALL(0), JLIMIT(1), LIMIT(2);        private final int value;        Type(int value) {            this.value = value;        }        public int value() {            return this.value;        }    }}

2、实体类

import com.hieasy.comm.annotation.PriceLimit;import lombok.Data;@Datapublic class Xssj {    @PriceLimit(type = PriceLimit.Type.LIMIT)    private String xsje;    @PriceLimit(type = PriceLimit.Type.JLIMIT)    private String bzjje;    @PriceLimit(type = PriceLimit.Type.JLIMIT)    private String mlr;    @PriceLimit(type = PriceLimit.Type.JLIMIT)    private String mll;    private double pjzk;    private double tbl;    private double hbl;    private String dxdm;    private String dxmc;    private String type;    private int sl;    @PriceLimit(type = PriceLimit.Type.LIMIT)    private String je;    @PriceLimit(type = PriceLimit.Type.JLIMIT)    private String mlrqn; //去年毛利润    @PriceLimit(type = PriceLimit.Type.JLIMIT)    private String mlrsy; //上月毛利润}

 

3、工具类

 

public 
void fltPriceLimit(Object object,SysUserLimit limit) throws Exception { Field[] fields = object.getClass().getDeclaredFields(); // 获取实体类的所有属性,返回Field数组 for(Field field:fields){ PriceLimit attr = field.getAnnotation(PriceLimit.class); Integer type=0; if(null!=attr) type=attr.type().value(); if(type==1) { if(limit.getLimitJPriceShow()==1 || limit.getLimitPriceShow()==1) { if(field!=null){ field.setAccessible(true);//修改作用域 field.set(object,"***");//设置值 } } }else if(type==2){ if(limit.getLimitPriceShow()==1) { if(field!=null){ field.setAccessible(true);//修改作用域 field.set(object,"***");//设置值 } } } } }

4、测试

public static void main(String[] args) throws Exception{        SysUserLimit limit=new SysUserLimit();        limit.setLimitJPriceShow(1);        limit.setLimitPriceShow(0);        Xssj xssj=new Xssj();        System.out.println("原对象:"+xssj);        CommonService commonService=new CommonServiceImpl();        commonService.fltPriceLimit(xssj,limit);        System.out.println("过滤对象:"+xssj);    }

5、输出结果

6、扩展

此类方法还可以应用到非整形的数据格式化..

 

转载地址:http://pjnab.baihongyu.com/

你可能感兴趣的文章
Linux系统安全工具之:Sxid和Skey
查看>>
arp工具简介_arptables_arpwatch
查看>>
linux core dump 知识整理
查看>>
RPM 校验软件包完整性
查看>>
OSSEC错误解决
查看>>
RSA SecurID Authentication linux sshd PAM deploy
查看>>
转: pam 禁止某些用户使用ssh 远程登录
查看>>
小包优先+web优先+游戏爆发+单IP限速+连接数限制 脚本V2.0
查看>>
How to Ninja – Ubuntu 10.04
查看>>
自动实时监控Windows2003服务器终端登录
查看>>
iptables限制每个IP的链接数
查看>>
Rhel5 配置NTP服务
查看>>
IDS日志分析
查看>>
网游运营中的外挂与bug处理模式
查看>>
oracle加固经验
查看>>
Linux 操作系统安装盘的定制
查看>>
定制rhel的stage2.img/minstg2.img文件
查看>>
ZZ Quick-Tip: Linux NAT in Four Steps using iptables
查看>>
北京的住房公积金是否可用于还外地的房贷
查看>>
mysqlhotcopy 热备工具体验与总结
查看>>