private void getAllFieldNames(Class<?> clazz, String prefix, List<String> fieldNames, Set<Class<?>> visited) {
if (clazz == null || visited.contains(clazz)) {
return;
}
visited.add(clazz);
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
String fieldName = prefix.isEmpty() ? field.getName() : prefix + "." + field.getName();
if (List.class.isAssignableFrom(field.getType())) {
Type genericType = field.getGenericType();
if (genericType instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) genericType;
Type[] actualTypeArguments = pType.getActualTypeArguments();
if (actualTypeArguments.length > 0 && actualTypeArguments[0] instanceof Class) {
Class<?> listClass = (Class<?>) actualTypeArguments[0];
if (!field.getType().isPrimitive() && isNotWrapperType(listClass)) {
getAllFieldNames(listClass, fieldName, fieldNames, visited);
} else {
fieldNames.add(fieldName);
}
}
}
} else if (!field.getType().isPrimitive() && isNotWrapperType(field.getType())) {
fieldNames.add(fieldName);
getAllFieldNames(field.getType(), fieldName, fieldNames, visited);
} else {
fieldNames.add(fieldName);
}
}
}
public static boolean isNotWrapperType(Class fieldType) {
return
fieldType != Integer.class &&
fieldType != Long.class &&
fieldType != Byte.class &&
fieldType != Short.class &&
fieldType != Float.class &&
fieldType != Double.class &&
fieldType != Character.class &&
fieldType != Date.class &&
fieldType != String.class &&
fieldType != Boolean.class;
}
01-28
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-02
5659
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-31
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交