码迷,mamicode.com
首页 > 编程语言 > 详细

java 反射获取设置私有成员变量的值

时间:2019-05-13 12:19:33      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:info   util   私有   inf   app   tac   not   userinfo   nbsp   

for (Object arg:args) {
            //处理applicationCode
            Class<?> argClass = arg.getClass();
            Field applicationCode =null;
            try {
                applicationCode =argClass.getDeclaredField("applicationCode");
            }catch (NoSuchFieldException e){

            }
            JWTData jwtData = CurrentUser.getUserInfo();
            if (applicationCode!=null&&jwtData != null){
                applicationCode.setAccessible(true);
                if ( StringUtils.isNotEmpty(jwtData.getApplicationCode())) {
                    applicationCode.set(arg,jwtData.getApplicationCode());
                }
            }
            //处理 createdBy
            Field createdBy = null;
            try {
                createdBy =argClass.getDeclaredField("createdBy");
            }catch (NoSuchFieldException e){

            }
            createdBy.setAccessible(true);
            if (createdBy !=null&&createdBy.get(arg)==null &&jwtData != null){
                if ( StringUtils.isNotEmpty(jwtData.getUsername())) {
                    createdBy.set(arg,jwtData.getUsername());
                }
            }
        }

 

java 反射获取设置私有成员变量的值

标签:info   util   私有   inf   app   tac   not   userinfo   nbsp   

原文地址:https://www.cnblogs.com/z-test/p/10855745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!