java - 如何从父类对象中获取所有子类对象并放入map java spring
public static Map<String, Object> ConvertObjectToMap(Object obj, Integer number) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> pomclass = obj.getClass();
pomclass = obj.getClass();
Method[] methods = obj.getClass().getMethods();
Map<String, Object> map = new HashMap<String, Object>();
for (Method m : methods) {
if (m.getName().startsWith("get") && !m.getName().startsWith("getClass")) {
Object value = null;
value = (Object) m.invoke(obj);
if (number <= 1) {
if (value.getClass().isAnnotation()) {
map.putAll(ConvertObjectToMap(value, number++));
} else {
map.put(m.getName().substring(3), (Object) value);
}
}
}
}
return map;
}
这个对吗 ?