org.wing4j.common.utils.ReflectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wing4j-common-utils Show documentation
Show all versions of wing4j-common-utils Show documentation
This is the wing4j-family member wing4j-common-utils
package org.wing4j.common.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* 反射工具类
*/
public abstract class ReflectionUtils {
/**
* 获取一个类对象所以的字段
* @param clazz 提取类对象
* @return 字段列表
*/
public static List getFields(Class> clazz) {
//获取当前类的字段
List result = new ArrayList(20);
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
result.add(field);
}
//递归调用获取超类的字段
Class> superClass = clazz.getSuperclass();
if (superClass.equals(Object.class)) {
return result;
}
List superFields = getFields(superClass);
result.addAll(superFields);
return result;
}
/**
* 获取字段,排除有transient关键字的字段
* @param clazz 提取类对象
* @return 字段列表
*/
public static List getFieldsExcludeTransient(Class> clazz) {
List result = new ArrayList();
List fields = getFields(clazz);
for (Field field : fields) {
if (Modifier.isTransient(field.getModifiers())) {
continue;
}
result.add(field);
}
return result;
}
}