反射知識點

  • s.getClass().getInterfaces() : 表示獲取該對象的全部實現的接口數組
public class ReflectUtil {

    /**
     * 反射執行方法
     *
     * @param classLoader
     * @param className
     * @param methodName
     * @param receiver
     * @param classes
     * @param params
     * @return
     * @throws Throwable
     */
    public static Object invoke(ClassLoader classLoader, String className, String methodName, Object receiver, Class[] classes, Object[] params) throws Throwable {
        return classLoader.loadClass(className).getMethod(methodName, classes).invoke(receiver, params);
    }

    /**
     * 獲取泛型類型
     *
     * @param o
     * @return
     */
    public static Class getTClass(Object o) {
        try {
            Type genType = o.getClass().getGenericSuperclass();
            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
            Class entityClass = (Class) params[0];
            return entityClass;
        } catch (Exception e) {
        }
        try {
            Type[] genType = o.getClass().getGenericInterfaces();
            Type[] params = ((ParameterizedType) genType[0]).getActualTypeArguments();
            Class entityClass = (Class) params[0];
            return entityClass;
        } catch (Exception e) {
        }
        return null;
    }

    public static Type[] getTClasses(Object o) {
        Type genType = o.getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        return params;
    }

    /**
     * 複製屬性
     *
     * @param toObj
     * @param fromObj
     */
    public static void copyFields(Object toObj, Object fromObj) {
        Class toClass = toObj.getClass();

        Class fromClass = fromObj.getClass();

        HashSet<Field> fields = new HashSet<>();

        for (Field field : toClass.getFields()) {
            fields.add(field);
        }
        for (Field field : toClass.getDeclaredFields()) {
            fields.add(field);
        }

        for (Field toField : fields) {
            toField.setAccessible(true);
            try {
                Field fromField = fromClass.getField(toField.getName());
                if (fromField.getType() == toField.getType()) {
                    fromField.setAccessible(true);
                    toField.set(toObj, fromField.get(fromObj));
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static void copyFieldsByAnnotation(Object toObj, Object fromObj) {
        Class toClass = toObj.getClass();

        Class fromClass = fromObj.getClass();

        HashSet<Field> fields = new HashSet<>();

        for (Field field : toClass.getFields()) {
            fields.add(field);
        }
        for (Field field : toClass.getDeclaredFields()) {
            fields.add(field);
        }

        for (Field toField : fields) {
            toField.setAccessible(true);
            for (Annotation annotation : toField.getAnnotations()) {
                if (annotation instanceof FromOld) {
                    FromOld fromOld = (FromOld) annotation;
                    try {
                        String oldName = fromOld.oldName();
                        if (TextUtils.isEmpty(oldName)) {
                            oldName = toField.getName();
                        }
                        Field fromField = fromClass.getField(oldName);
                        if (fromField.getType() == toField.getType()) {
                            fromField.setAccessible(true);
                            toField.set(toObj, fromField.get(fromObj));
                        }
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

    public static Object getField(Object obj, Class<?> cl, String field)
            throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Field localField = cl.getDeclaredField(field);
        localField.setAccessible(true);
        return localField.get(obj);
    }

    public static void setField(Object obj, Class<?> cl, String field, Object value)
            throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Field localField = cl.getDeclaredField(field);
        localField.setAccessible(true);
        localField.set(obj, value);
    }
}
相關文章
相關標籤/搜索