ReflectUtil:

/***************************
 *<pre>
 * @Package      : com.sea.xx.test
 * @File Name    : ReflectUtil
 * @Author       :  Sea
 * @Date         : 9/28/22 11:23 AM
 * @Purpose      :
 * @History      :
 *</pre>
 ***************************/
import com.google.common.collect.Maps;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class ReflectUtil {

    /**
     * custom commmon field type
     */
    public static String[] commonFieldType = {
            "int", "long", "short", "double", "float", "char", "byte", "boolean",
            "class java.lang.String", "class java.lang.Integer", "class java.lang.Long",
            "class java.lang.Short",  "class java.lang.Double",  "class java.lang.Float",
            "class java.lang.Byte",   "class java.lang.Boolean", "class java.math.BigDecimal"
    };


    /**
     * 获取类的私有属性 : eg:name
     * @param objClss
     * @param fileName
     * @throws Exception
     */
    public static Object getFiledValue(Object objClss, String fileName) throws Exception {
        Field name = objClss.getClass().getDeclaredField(fileName);
        name.setAccessible(true);
        Object o = name.get(objClss);
        return o;
    }

    /**
     * @param obj
     * @param methodName
     * @param args
     * @param parameterTypes
     * @return
     * @throws Exception
     */
    public static Object runMethod(Object obj, String methodName, Object args,Class<?>... parameterTypes) throws Exception {
        Method declaredMethod = obj.getClass().getDeclaredMethod(methodName, parameterTypes);
        declaredMethod.setAccessible(true);
        Object invoke = declaredMethod.invoke(obj, args);
        return invoke;
    }


    /**
     * 获取类的所有字段以及值
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Map<String, Object> getKeyAndValue(T t) {
        Map<String, Object> map = Maps.newHashMap();
        Class clazz = (Class) t.getClass();
        Field[] fields = clazz.getDeclaredFields();
        map = Arrays.stream(fields).collect(Collectors.toMap(Field::getName, field -> {
            Object resultObj = null;
            field.setAccessible(true);
            try {
                resultObj = field.get(t);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return Optional.ofNullable(resultObj).orElse(0);
        }, (k1, k2) -> k2));
        return map;
    }


    /**
     * 获取最简类名(去除包名)<br>
     * eg : com.csdn.util.ReflectUtil > ReflectUtil
     * @param className
     * @return simpleClassName
     */
    public static String getSimpleClassName(String className) {
        return className.substring(className.lastIndexOf(".") + 1);
    }

    /**
     * @param obj
     * @param fileName
     * @param value
     * @throws Exception
     */
    public static void setFiledValue(Object obj, String fileName, Object value) throws Exception {
        Field name = obj.getClass().getDeclaredField(fileName);
        name.setAccessible(true);
        name.set(obj,value);
    }

  /**
     * 判断指定对象中的指定属性名的类型是否属于自定义普通数据类型
     * @param obj 指定对象
     * @param fieldName 指定属性名
     * @return
     */
    public static boolean isCommonTypeField(Object obj, String fieldName) {
        boolean result = false;
        try {
            Field field = obj.getClass().getDeclaredField(fieldName);
            String type = field.getGenericType().toString();
            for ( int i = 0; i < commonFieldType.length; i++ ) {
                if ( commonFieldType[i].equals(type) ) {
                    result = true;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取指定类的所有属性名
     * @param className 指定类全名
     * @return 所有属性名
     */
    public static String[] getAllFieldName(String className) {
        String[] fieldName = null;
        try {
            Field[] fields = Class.forName(className).getDeclaredFields();
            fieldName = new String[fields.length];
            for ( int i = 0; i < fields.length; i++ ) {
                fieldName[i] = fields[i].getName();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fieldName;
    }
}

 

 
 

 


 
posted on 2019-05-31 18:56  lshan  阅读(1079)  评论(0编辑  收藏  举报