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

io.github.icodegarden.nutrient.lang.util.ClassUtils Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.github.icodegarden.nutrient.lang.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * @author Fangfang.Xu
 *
 */
public abstract class ClassUtils {

	/**
	 * 获取目标类的接口(可多级)的泛型
	 * 
	 * @param target       目标类
	 * @param interfaceCla 接口
	 * @return 不为null
	 */
	public static List> getGenericInterfaceTypes(Object target, Class interfaceCla) {
		return getGenericInterfaceTypes(target.getClass(), interfaceCla);
	}
	
	/**
	 * 获取目标类的接口(可多级)的泛型
	 * 
	 * @param target       目标类
	 * @param interfaceCla 接口
	 * @return 不为null
	 */
	public static List> getGenericInterfaceTypes(Class target, Class interfaceCla) {
		ParameterizedType pt = getGenericInterfaceParameterizedType(target, interfaceCla);
		if (pt != null) {
			List> list = new LinkedList>();
			for (Type type : pt.getActualTypeArguments()) {
				list.add((Class) type);
			}
			return list;
		}
		return Collections.emptyList();
	}

	public static ParameterizedType getGenericInterfaceParameterizedType(Object target, Class interfaceCla) {
		return getGenericInterfaceParameterizedType(target.getClass(), interfaceCla);
	}

	public static ParameterizedType getGenericInterfaceParameterizedType(Class target, Class interfaceCla) {
		if (target.equals(Object.class)) {
			return null;
		}
		Type[] genericInterfaces = target.getGenericInterfaces();

		for (Type genericInterface : genericInterfaces) {
			if (genericInterface instanceof ParameterizedType
					&& ((ParameterizedType) genericInterface).getRawType().equals(interfaceCla)) {
				return (ParameterizedType) genericInterface;
			}
		}
		return getGenericInterfaceParameterizedType(target.getSuperclass(), interfaceCla);
	}

//	/**
//	 * 任意字段是否有值
//	 * 
//	 * @param obj
//	 * @param ignoreFieldNames 忽略的字段
//	 * @return
//	 */
//	public static boolean anyFieldHasValue(Object obj, Collection ignoreFieldNames) {
//		Class cla = obj.getClass();
//		List allDeclaredFields = getAllDeclaredFields(cla);
//
//		return allDeclaredFields.stream().anyMatch(field -> {
//			boolean accessible = field.isAccessible();
//			field.setAccessible(true);
//			try {
//				return !ignoreFieldNames.contains(field.getName()) && field.get(obj) != null;
//			} catch (IllegalAccessException e) {
//				throw new IllegalStateException("ex on doShouldUpdate", e);
//			} finally {
//				field.setAccessible(accessible);
//			}
//		});
//	}
//
//	/**
//	 * 包含所有父类的字段
//	 * 
//	 * @param cla
//	 * @return
//	 */
//	public static List getAllDeclaredFields(Class cla) {
//		List allDeclaredFields = new LinkedList();
//		while (cla != null && cla != Object.class) {
//			Field[] declaredFields = cla.getDeclaredFields();
//			allDeclaredFields.addAll(Arrays.asList(declaredFields));
//
//			cla = cla.getSuperclass();
//		}
//		return allDeclaredFields;
//	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy