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

com.github.xiaoyuge5201.reflect.ReflectUtil Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
package com.github.xiaoyuge5201.reflect;

import java.lang.reflect.Method;
import java.math.BigDecimal;

/**
 * @author: 小余哥
 *  2020-04-21 13:57
 **/
public class ReflectUtil {

    /**
     * 处理字符串  如:  abc_dex ---> abcDex
     *
     * @param str
     * @return
     */
    public static String removeLine(String str) {
        if (null != str && str.contains("_")) {
            int i = str.indexOf("_");
            char ch = str.charAt(i + 1);
            char newCh = (ch + "").substring(0, 1).toUpperCase().toCharArray()[0];
            String newStr = str.replace(str.charAt(i + 1), newCh);
            String newStr2 = newStr.replace("_", "");
            return newStr2;
        }
        return str;
    }

    /**
     * 根据属性,拿到set方法,并把值set到对象中
     *
     * @param obj       对象
     * @param clazz     对象的class
     * @param filedName 需要设置值得属性
     * @param typeClass 对象
     * @param value 值
     */
    public static void setValue(Object obj, Class clazz, String filedName, Class typeClass, Object value) {
        filedName = removeLine(filedName);
        String methodName = "set" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1);
        try {
            Method method = clazz.getDeclaredMethod(methodName, new Class[]{typeClass});
            method.invoke(obj, new Object[]{getClassTypeValue(typeClass, value)});
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 根据属性,获取get方法
     *
     * @param ob   对象
     * @param name 属性名
     * @return 結果
     * @throws Exception
     */
    public static Object getGetMethod(Object ob, String name) throws Exception {
        Method[] m = ob.getClass().getMethods();
        for (Method method : m) {
            if (("get" + name).toLowerCase().equals(method.getName().toLowerCase())) {
                return method.invoke(ob);
            }
        }
        return null;
    }

    /**
     * 通过class类型获取获取对应类型的值
     *
     * @param typeClass class类型
     * @param value     值
     * @return Object
     */
    private static Object getClassTypeValue(Class typeClass, Object value) {
        if (typeClass == int.class || value instanceof Integer) {
            if (null == value) {
                return 0;
            }
            return value;
        } else if (typeClass == short.class) {
            if (null == value) {
                return 0;
            }
            return value;
        } else if (typeClass == byte.class) {
            if (null == value) {
                return 0;
            }
            return value;
        } else if (typeClass == double.class) {
            if (null == value) {
                return 0;
            }
            return value;
        } else if (typeClass == long.class) {
            if (null == value) {
                return 0;
            }
            return value;
        } else if (typeClass == String.class) {
            if (null == value) {
                return "";
            }
            return value;
        } else if (typeClass == boolean.class) {
            if (null == value) {
                return true;
            }
            return value;
        } else if (typeClass == BigDecimal.class) {
            if (null == value) {
                return new BigDecimal(0);
            }
            return new BigDecimal(value + "");
        } else {
            return typeClass.cast(value);
        }
    }


    //    @Test
//    public void getColumn() throws Exception {
//        InfoResDirectoryEntity ee = new InfoResDirectoryEntity();
//        ee.setResourceCategory ("小弟");
//        //get
//        Object object = com.ygb.essearch.test.ReflectUtil.getGetMethod(ee, "resourceCategory");
//        //set
//        com.ygb.essearch.test.ReflectUtil.setValue (ee,ee.getClass (),"resourceCategory", InfoResDirectoryEntity.class.getDeclaredField("resourceCategory").getType (),"大哥");
//        System.out.println (ee);
//        Field[] fields = InfoResDirectoryEntity.class.getDeclaredFields();
//        for(Field f : fields){
//            System.out.println (f.getName ());
//        }
//    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy