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

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

There is a newer version: 1.5.0
Show 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 org.apache.commons.lang3.StringUtils;

public final class ClzUtils {

	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) {
	        e.printStackTrace();
        }
		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()) {
			try {
				return supClz.getDeclaredField(prop);
			} catch (Exception e) {
			}
		}
		return null;
	}
	public static Field getField(Object obj, String prop){
		Class clz = null;
		if(obj instanceof Class){
			clz = (Class)obj;
		}else {
			clz = 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) {
	        e.printStackTrace();
        }
	    return result;
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy