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


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());
                }
            }
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM