java 反射读取基类成员的值

var parameterValue = com.alibaba.fastjson.JSON.parseObject(body, parameter.getParameterType());
                    var currentClass = parameter.getParameterType();
                    Field taskIdField = null;
                    while (currentClass != null) {
                        var fields = Arrays.stream(currentClass.getDeclaredFields()).collect(Collectors.toList());
                        var taskIdFieldWrapper = fields.stream()
                                .filter(x -> x.getName().equals(annotation.fieldName())).findFirst();
                        if (taskIdFieldWrapper.isPresent()) {
                            taskIdField = taskIdFieldWrapper.get();
                            break;
                        }
                        currentClass = currentClass.getSuperclass();
                    }

                    if (taskIdField == null) {
                        return false;
                    }

                    taskIdField.setAccessible(true);
                    var taskIdValue = ReflectionUtils.getField(taskIdField, parameterValue);

 

posted on 2022-04-12 15:30  空明流光  阅读(79)  评论(0编辑  收藏  举报

导航