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

com.webapp.utils.clz.ClzUtils Maven / Gradle / Ivy

The newest version!
package com.webapp.utils.clz;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ClzUtils {

	private static final Logger logger = LoggerFactory.getLogger(ClzUtils.class);

	public static Class superGeneric(Class clz) {
		return superGeneric(clz, 0);
	}
	public static Class superGeneric(Class clz, int first) {
		Type superType = clz.getGenericSuperclass();
		if (superType instanceof ParameterizedType) {
			Type type = ((ParameterizedType)superType).getActualTypeArguments()[first];
			if(type instanceof ParameterizedType){//多级泛型
				return (Class)((ParameterizedType)type).getRawType();
			}else if(type instanceof Class){
				Class typeClz = ((Class)type);
				return typeClz.isArray() ? typeClz.getComponentType() : typeClz;
			}
		}
		return null;
	}

	public static Class interGeneric(Class clz) {
		return interGeneric(clz, 0, 0);
	}
	public static Class interGeneric(Class clz, int first, int num) {
		Type[] gInter = clz.getGenericInterfaces();
		if(gInter.length <= 0) return null;

		Type baseType = gInter[first];
        Type modelType = ((ParameterizedType) baseType).getActualTypeArguments()[num];
        return ((Class) modelType);
	}

	public static boolean hasAnno(Class clz, Class anno) {
		return clz.isAnnotationPresent(anno);
    }

	public static boolean hasAnno(Method method, Class anno) {
		return method.isAnnotationPresent(anno);
    }

	public static  T getAnno(Method method, Class anno) {
		return method.getAnnotation(anno);
    }

	public static  T getAnno(Class clz, Class anno) {
		return clz.getAnnotation(anno);
    }

	public static  T getAnnoIfClz(Class clz, Method method, Class anno) {
		T tAnno = null;
		if(clz.isAnnotationPresent(anno)){
			tAnno = clz.getAnnotation(anno);
		}else if (method.isAnnotationPresent(anno)) {
			tAnno = method.getAnnotation(anno);
		}
		return tAnno;
    }

	public static  T getAnnoIfMethod(Class clz, Method method, Class anno) {
		T tAnno = null;
		if(method.isAnnotationPresent(anno)){
			tAnno = method.getAnnotation(anno);
		}else if (clz.isAnnotationPresent(anno)) {
			tAnno = clz.getAnnotation(anno);
		}
		return tAnno;
    }

	public static  Method getMethod(Class clz, String prop, Class... paramTypes){
		Method method = null;
		try {
	        method = clz.getMethod(prop, paramTypes);
        } catch (NoSuchMethodException | SecurityException e) {
	        e.printStackTrace();
        }
		return method;
	}

	public static Method getGetMethod(Class clz, String prop) {
		String get = "get" + StringUtils.capitalize(prop);
		return getMethod(clz, get);

    }

	@SuppressWarnings("unchecked")
    public static  T invoke(Method method, Object instance, Class returnClz) {
		Object result = null;
		try {
	        result = method.invoke(instance);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        	logger.error("invoke error", e);
        }
		return (T)result;
    }

	public static boolean hasField(Class clz, String prop){
		return getField(clz, prop) != null ? true : false;
	}

	public static Field getField(Class clz, String prop){
		for (Class supClz = clz; supClz != Object.class; supClz = supClz.getSuperclass()) {
			if(supClz == null) break;
			try {
				return supClz.getDeclaredField(prop);
			} catch (Exception e) {
				logger.debug(String.format("%1$s does not exist %2$s", supClz.getName(), prop));
			}
		}
		return null;
	}
	public static Field getField(Object obj, String prop){
		Class clz = (obj instanceof Class) ? (Class)obj : obj.getClass();
		return getField(clz, prop);
	}

	public static Object getFieldVal(String field, Object obj) {
	    return getFieldVal(getField(obj, field), obj);
    }

	public static Object getFieldVal(Field field, Object obj) {
		Object result = null;
	    try {
	    	boolean org = field.isAccessible();
	    	field.setAccessible(true);
	    	result = field.get(obj);
	    	field.setAccessible(org);
        } catch (IllegalAccessException | SecurityException e) {
        	logger.error("getFieldVal error", e);
        }
	    return result;
    }

	public static boolean setFieldVal(Object obj, String fieldName, Object value){
		boolean result = false;
		try {
			Field field = obj.getClass().getDeclaredField(fieldName);
			boolean access = field.isAccessible();
			field.setAccessible(true);
			field.set(obj, value);
			field.setAccessible(access);
			result = true;
		} catch (ReflectiveOperationException e) {
			logger.error("setFieldVal error", e);
		}
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy