All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wing4j.common.utils.ReflectionUtils Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy