作者:策馬踏清風 鏈接:https://www.jianshu.com/p/756778f5dc87 ReflectionUtils是spring針對反射提供的工具類。 handleReflectionException異常處理 推薦一個開源免費的 Spring Boot 實戰項目: https:/ ...
作者:策馬踏清風
鏈接:https://www.jianshu.com/p/756778f5dc87
ReflectionUtils
是spring
針對反射提供的工具類。
handleReflectionException異常處理
推薦一個開源免費的 Spring Boot 實戰項目:
源碼:
public static void handleReflectionException(Exception ex) {
if (ex instanceof NoSuchMethodException) {
throw new IllegalStateException("Method not found: " + ex.getMessage());
}
if (ex instanceof IllegalAccessException) {
throw new IllegalStateException("Could not access method: " + ex.getMessage());
}
if (ex instanceof InvocationTargetException) {
handleInvocationTargetException((InvocationTargetException) ex);
}
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new UndeclaredThrowableException(ex);
}
主要是將反射中的異常分成幾個部分,規範化輸出
boolean declaresException(Method method, Class<?> exceptionType)
判斷方法上是否聲明瞭指定的異常類型
findField查找欄位
Field findField(Class<?> clazz, String name, Class<?> type)
查找指定類的指定名稱和指定類型的方法
public static Field findField(Class<?> clazz, String name, Class<?> type) {
Class<?> searchType = clazz;
while (Object.class != searchType && searchType != null) {
Field[] fields = getDeclaredFields(searchType);
for (Field field : fields) {
if ((name == null || name.equals(field.getName())) &&
(type == null || type.equals(field.getType()))) {
return field;
}
}
searchType = searchType.getSuperclass();
}
return null;
}
獲取所有的方法,然後迴圈遍歷,知道找到滿足條件的返回
其中getDeclaredFields(searchType)
方法使用ConcurrentReferenceHashMap
將Field
緩存,並優先從緩存中取。
Field findField(Class<?> clazz, String name)
設置欄位setField
void setField(Field field, Object target, Object value)
設置指定欄位的值
直接使用Field.set/get
方法,然後格式化處理了異常Object getField(Field field, Object target)
獲取指定欄位的值
查找方法findMethod
Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
查找方法,方法的參數是一個可變長的Class
Method findMethod(Class<?> clazz, String name)
直接查,不指定參數
調用方法invokeMethod
Object invokeMethod(Method method, Object target, Object... args)
調用方法Object invokeMethod(Method method, Object target)
簡單版本
判斷類
boolean declaresException(Method method, Class<?> exceptionType)
方法上是否聲明瞭指定的異常boolean isPublicStaticFinal(Field field)
判斷欄位首付是public static final
的boolean isEqualsMethod(Method method)
判斷方法是否是equals
方法boolean isHashCodeMethod(Method method)
判斷方法是否是hashcode
方法boolean isToStringMethod(Method method)
判斷方法是否是toString
方法boolean isObjectMethod(Method method)
判斷方法是否是Object
類上的方法
操作
void makeAccessible(Field field)
使私有的欄位可寫void makeAccessible(Method method)
私有方法可調用void makeAccessible(Constructor<?> ctor)
私有構造器可調用void doWithLocalMethods(Class<?> clazz, MethodCallback mc)
遍歷類上的方法,並執行回調
public interface MethodCallback {
void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
}
void doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf)
增加了一個方法過濾器
public interface MethodFilter {
boolean matches(Method method);
}
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2022最新版)
4.別再寫滿屏的爆爆爆炸類了,試試裝飾器模式,這才是優雅的方式!!
覺得不錯,別忘了隨手點贊+轉發哦!