Java 反射工具類 ReflectionUtil

import lombok.extern.slf4j.Slf4j; import java.lang.reflect.*; /** * 反射的 Utils 函數集合 * 提供訪問私有變量, 獲取泛型類型 Class, 提取集合中元素屬性等 Utils 函數 */ @Slf4j public class ReflectionUtils { /** * 直接讀取對象的屬性值, 忽略 private/protected 修飾符, 也不經過 getter * * @param object * @param fieldName * @return */ public static Object getFieldValue(Object object, String fieldName) { Field field = getDeclaredField(object, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); } makeAccessible(field); Object result = null; try { result = field.get(object); } catch (IllegalAccessException e) { log.error("getFieldValue:", e); } return result; } /** * 直接設置對象屬性值, 忽略 private/protected 修飾符, 也不經過 setter * * @param object * @param fieldName * @param value */ public static void setFieldValue(Object object, String fieldName, Object value) { Field field = getDeclaredField(object, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); } makeAccessible(field); try { field.set(object, value); } catch (IllegalAccessException e) { log.error("setFieldValue:", e); } } /** * 通過反射, 獲得定義 Class 時聲明的父類的泛型參數的類型 * 如: public EmployeeDao extends BaseDao<Employee, String> * * @param clazz * @param index * @return */ @SuppressWarnings("unchecked") public static Class getSuperClassGenricType(Class clazz, int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { return Object.class; } Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } if (!(params[index] instanceof Class)) { return Object.class; } return (Class) params[index]; } /** * 通過反射, 獲得 Class 定義中聲明的父類的泛型參數類型 * 如: public EmployeeDao extends BaseDao<Employee, String> * * @param <T> * @param clazz * @return */ @SuppressWarnings("unchecked") public static <T> Class<T> getSuperGenericType(Class clazz) { return getSuperClassGenricType(clazz, 0); } /** * 循環向上轉型, 獲取對象的 DeclaredMethod * * @param object * @param methodName * @param parameterTypes * @return */ public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) { for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { try { return superClass.getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { //Method 不在當前類定義, 繼續向上轉型 } } return null; } /** * 使 filed 變爲可訪問 * * @param field */ public static void makeAccessible(Field field) { if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } } /** * 循環向上轉型, 獲取對象的 DeclaredField * * @param object * @param filedName * @return */ public static Field getDeclaredField(Object object, String filedName) { for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { try { return superClass.getDeclaredField(filedName); } catch (NoSuchFieldException e) { //Field 不在當前類定義, 繼續向上轉型 } } return null; } /** * 直接調用對象方法, 而忽略修飾符(private, protected) * * @param object * @param methodName * @param parameterTypes * @param parameters * @return * @throws InvocationTargetException * @throws IllegalArgumentException */ public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws InvocationTargetException { Method method = getDeclaredMethod(object, methodName, parameterTypes); if (method == null) { throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]"); } method.setAccessible(true); try { return method.invoke(object, parameters); } catch (IllegalAccessException e) { log.error("invokeMethod:", e); } return null; } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章