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