Java 反射总结
程序可以訪問,檢測和修改它本身狀態或行為的一種能力,并能根據自身行為的狀態和結果,調整或修改應用所描述行為的狀態和相關的語義。
?
二、反射的類
1)反射類
java.lang.Class ? ? ? ??
java.lang.reflect.Constructor
java.lang.reflect.Field ? ? ?
java.lang.reflect.Method
java.lang.reflect.Modifier
?2)相關類
AccessibleObject
Array
Proxy
?
三、Class
(1)獲取Class對象的三種方式
- 類名.class
- Class.forName()
- 對象.getClass()
(2)Class的成員方法
1)構造方法
getConstructor
getConstructors
getDeclaredConstructor
getDeclaredConstructors
2)變量域
getField
getFields
getDeclaredField
getDeclaredFields
3)方法
getMethod
getMethods
getDeclaredMethod
getDeclaredMethods
4)成員類
getClasses
getDeclaredClasses
5)類注解
getAnnnotation
getAnnnotations
getAnnnotationsByTpe
6)父類
getSuperClass
getGenericSuperClass ? ?獲取包含泛型的父類
getAnnotatedSuperclass ? 獲取extends父類時的標注的注解和父類,如class ?B extends @Anno A { ?}
7)接口
getInterfaces
getGenericInterfaces ? ? ?獲取包含泛型的接口
getAnnotatedInterfaces ?獲取implements接口時的標注的注解和接口,如class ?C implements @AnnoA A,@AnnoB B { ?}
8)立即封閉類
getDeclaringClass ?返回Class對象,表示this類在return類中被聲明,this類對象所表示的類或接口是return類的成員。
9)封閉
getEnclosingClass
getEnclosingConstructor
getEnclosingMethod
10)類型判斷
isInstance
isPrimitive
isArray
isMemberClass ? ? ? ? ? ? ?成員類
isLocalClass ? ? ? ? ? ? ? ? ?局部類
isAnnonymousClass ? ? ?匿名類
isInterface
isEnum
isAnnotation
11)類名
getName ? ? ? ? ? ? ? ? ? ? 內部名 ? ? ? ? ?
getSimpleName ? ? ? ? ?縮寫名
getCanonicalName ? ?全稱名
12)其他方法
newInstance ? ? ? ? ? ? ? ? ? ? 無參構造方法創建對象,沒有無參構造方法拋異常InstantiationException
getClassLoader
getTypeParameters ? ? ? ? 泛型參數
getModifiers ? ? ? ? ? ? ? ? ? ? 修飾符
getPackage
getResource ? ? ? ? ? ? ? ? ? ? ? ? 獲取資源返回url
getResourceAsStream ? ? ? ? 獲取資源返回InputStream
?
四、constructor
1)特殊
getName
getModifiers
isVarArgs ? ? ?可變參
isSynthetic ? ? ? ? ? ??由編譯器自動添加,在非靜態成員類用于傳人外部類對象的構造方法是復合構造方法
newInstance
2)參數
getGenericParameterTypes ? ? ? 形參泛型參數類型
getParameterTypes ? ? ? ? ? ? ? ? ? ?形參參數類型
getTypeParameters ? ? ? ? ? ? ? ? ? ?泛型參數占位符?
3)異常
getExceptionTypes
getGenericExceptionTypes
4)注解
getAnnotation
getDeclaredAnnotations
getParameterAnnotations ? ?注解在形參
?
五、Field
1)特殊
getName
getDeclaringClass ? 獲取Field聲明所在的類
getModifiers
isEnumConstant ? ?是否枚舉
isSynthetic ? ? ? ? ? ? 由編譯器自動添加,在非靜態成員類用于訪問外部類的變量是復合變量
2)get基本類型與Object
getXXX
get
3)set基本類型與Object
setXXX
set
4)類型
getType
getGenericType
5)注解
getAnnotation
getDeclaredAnnotations
?
六、Method
?1)特殊
getName
invoke ? ? ? ?異步執行
getDeclaringClass ? 獲取Method聲明所在的類
getModifiers
isVarArgs ? ? ? ?可變參
getDefaultValue
isSynthetic ? ? 由編譯器自動添加,在非靜態成員類用于訪問外部類的方法是復合方法
2)參數
getGenericParameterTypes ? ? ? 形參泛型參數類型
getParameterTypes ? ? ? ? ? ? ? ? ? ?形參參數類型
3)返回值
getReturnType
getGenericReturnType
4)注解
getAnnotation
getDeclaredAnnotations
getParameterAnnotations
5)異常
getExceptionTypes
getGenericExceptionTypes
?
七、Modifier
1)靜態字段
PUBLIC
FINAL
STRICT strictfp
VOLATILE
...
2)方法
isPublic
isFinal
isStrict
isVolatile
...
?
八、AccessibleObject
getAnnotation
getAnnotations
getDeclaredAnnotations
isAccessible
isAnnotationPresent ? ? ? ? ? ?如果指定類型的注釋存在于此元素上,則返回 true,否則返回 false。
setAccessible ? ? ? ? ? ? ? ? ? ? ?靜態方法
setAccessible
?
九、Array
靜態方法
get
getXXX
getLength
newInstance ? ? ? ? ? ? 有指定數組維度
set
setXXX
?
十、Proxy
靜態方法
getInvocationHandler
getProxyClass
isProxyClass
newProxyInstance
?
總結
- 上一篇: 笔记本电脑硬件配置数据能更改吗?让鲁大师
- 下一篇: Java 注解总结