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

Posted z-test

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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());
                }
            }
        }

 

以上是关于java 反射获取设置私有成员变量的值的主要内容,如果未能解决你的问题,请参考以下文章