java 反射获取对应字段的值

 Field[] fields = obj.getRows().getClass().getDeclaredFields();
                Boolean hasEndSpot = false;
                Boolean hasStartingPoint = false;
                for (Field field : fields) {
                    if (field.getName() == "startingPoint") {
                        hasStartingPoint = true;
                    }
                    if (field.getName() == "endSpot") {
                        hasEndSpot = true;
                    }
                }
                garbageCan.setAssName(gridObjectService.getName(garbageCan.getAssNumber()));
                if (hasStartingPoint) {
                    Field declaredField = getField(obj, "startingPoint");
                    Object startingPoint = declaredField.get(obj.getRows());
                    if (!Treat.isEmpty(startingPoint)) {
                        garbageCan.setStartingPoint(startingPoint.toString());
                    }
                }
 private Field getField(Result<? extends InitGridName> obj, String field) throws NoSuchFieldException {
        Field declaredField = obj.getRows().getClass().getDeclaredField(field);
        declaredField.setAccessible(Boolean.TRUE);
        return declaredField;
    }

 https://blog.csdn.net/nbzhaomao/article/details/125472669

posted @ 2023-06-25 15:17  八英里  阅读(547)  评论(0编辑  收藏  举报