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

devutility.internal.lang.ClassHelper Maven / Gradle / Ivy

There is a newer version: 1.3.8.1
Show newest version
package devutility.internal.lang;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import devutility.internal.lang.models.EntityField;
import devutility.internal.lang.models.EntityFieldUtils;
import devutility.internal.util.CollectionUtils;

public class ClassHelper {
	/**
	 * Create a new instance.
	 * @param clazz: Class object
	 * @return {@code  T}
	 * @throws ReflectiveOperationException
	 */
	public static  T newInstance(Class clazz) throws ReflectiveOperationException {
		return clazz.getDeclaredConstructor().newInstance();
	}

	/**
	 * Create a new instance.
	 * @param clazz: Class object
	 * @return {@code  T}
	 */
	public static  T instance(Class clazz) {
		try {
			return newInstance(clazz);
		} catch (ReflectiveOperationException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Get super classes include itself.
	 * @param clazz: Class object
	 * @return {@code List>}
	 */
	public static  List> getSuperClasses(Class clazz) {
		List> list = new LinkedList<>();

		if (clazz == null) {
			return list;
		}

		Class temp = clazz;

		while (temp != null && !"java.lang.Object".equals(temp.getCanonicalName())) {
			list.add(temp);
			temp = temp.getSuperclass();
		}

		return list;
	}

	public static List getAllDeclaredFields(Class clazz) {
		List list = new ArrayList<>();
		List> classes = getSuperClasses(clazz);

		if (classes.size() == 0) {
			return list;
		}

		for (Class cl : classes) {
			list.addAll(Arrays.asList(cl.getDeclaredFields()));
		}

		return list;
	}

	public static List getAllDeclaredMethods(Class clazz) {
		List list = new ArrayList<>();
		List> classes = getSuperClasses(clazz);

		if (classes.size() == 0) {
			return list;
		}

		for (Class cl : classes) {
			list.addAll(Arrays.asList(cl.getDeclaredMethods()));
		}

		return list;
	}

	/**
	 * Get EntityFields and include specified fields.
	 * @param includeFields: Fields want to include.
	 * @param clazz: Class object
	 * @return {@code List}
	 */
	public static List getIncludedEntityFields(List includeFields, Class clazz) {
		List list = getEntityFields(clazz);
		return EntityFieldUtils.includeEntityFields(list, includeFields);
	}

	/**
	 * Get EntityFields and exclude specified fields.
	 * @param clazz: Class object
	 * @param excludeFields: Fields want to exclude.
	 * @return {@code List}
	 */
	public static List getNonExcludedEntityFields(List excludeFields, Class clazz) {
		List list = getEntityFields(clazz);
		return EntityFieldUtils.excludeEntityFields(list, excludeFields);
	}

	/**
	 * Get EntityFields and exclude specified annotations.
	 * @param clazz: Class object
	 * @param excludeAnnotations: Annotations want to be excluded.
	 * @return {@code List}
	 */
	public static List getNonExcludedEntityFields(Annotation[] excludeAnnotations, Class clazz) {
		List list = getEntityFields(clazz);
		List annotations = Arrays.asList(excludeAnnotations);
		return CollectionUtils.list(list, i -> i.containAnnotations(annotations));
	}

	/**
	 * Get EntityFields.
	 * @param clazz: Class object
	 * @return {@code List}
	 */
	public static List getEntityFields(Class clazz) {
		if (clazz == null) {
			return new ArrayList<>();
		}

		List declaredFields = getAllDeclaredFields(clazz);
		List declaredMethods = getAllDeclaredMethods(clazz);
		List list = new ArrayList<>(declaredFields.size());

		for (Field declaredField : declaredFields) {
			Method setter = getSetter(declaredField.getName(), declaredMethods);
			Method getter = getGetter(declaredField, declaredMethods);

			if (setter == null || getter == null) {
				continue;
			}

			EntityField entityField = new EntityField();
			entityField.setField(declaredField);
			entityField.setSetter(setter);
			entityField.setGetter(getter);
			list.add(entityField);
		}

		list.sort((ef1, ef2) -> ef1.getField().getName().compareTo(ef2.getField().getName()));
		return list;
	}

	public static boolean isGetterField(Field field, List methods) {
		String name = String.format("get%s", StringHelper.upperFirstCase(field.getName()));

		if (methods.contains(name)) {
			return true;
		}

		return isBoolField(field, methods);
	}

	private static boolean isBoolField(Field field, List methods) {
		if (field.getType() != Boolean.class) {
			return false;
		}

		String name = String.format("is%s", StringHelper.upperFirstCase(field.getName()));
		return methods.contains(name);
	}

	public static boolean isSetterField(String field, List methods) {
		String name = String.format("set%s", StringHelper.upperFirstCase(field));
		return methods.contains(name);
	}

	public static Method getSetter(String field, List methods) {
		String name = String.format("set%s", StringHelper.upperFirstCase(field));

		for (Method method : methods) {
			if (name.equals(method.getName())) {
				return method;
			}
		}

		return null;
	}

	public static Method getGetter(Field field, List methods) {
		Class fieldType = field.getType();
		String name = String.format("get%s", StringHelper.upperFirstCase(field.getName()));

		if (fieldType == Boolean.class) {
			name = String.format("is%s", StringHelper.upperFirstCase(field.getName()));
		}

		for (Method method : methods) {
			if (name.equals(method.getName())) {
				return method;
			}
		}

		return null;
	}

	public static Class getGenericClass(Type genericType) {
		if (genericType != null && genericType instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) genericType;
			return (Class) parameterizedType.getActualTypeArguments()[0];
		}

		return null;
	}

	/**
	 * Check whether object is instance of clazz?
	 * @param object: Object need check.
	 * @param clazz: Target Class object.
	 * @return boolean
	 */
	public static boolean isInstanceOf(Object object, Class clazz) {
		return clazz.isInstance(object) || clazz.isAssignableFrom(object.getClass());
	}

	/**
	 * Whether clazz is Jave class
	 * @param clazz: Class object need check
	 * @return boolean
	 */
	public static boolean isJaveClass(Class clazz) {
		return clazz != null && clazz.getClassLoader() == null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy