Java反射之java.lang.reflect.Method
前一篇文章講了Class中的成員變數(java.lang.reflect.Field
)的常用使用方式以及其注意事項。我們接著講Class中的方法(java.lang.reflect.Method
)。
介紹
方法就是一段可執行的程式碼,可以是被繼承而來的,也可以進行過載和重寫,或者被編譯器強制隱藏。但是相反的,反射程式碼是使方法選擇被限制在一個特定的類中,而不考慮它的父類,雖然我們有辦法查詢到它的父類,但是這不是方法的反射能做到的,所以這裡很容易引起問題。
獲取Method的宣告
方法的宣告包括方法名稱、描述符、引數、返回型別和異常表。類java.lang.reflect.Method
1. 獲取方法的名稱
String getName()
2. 獲取方法的描述符
int getModifiers()
返回值可以參見上一篇文章的介紹。 3. 返回方法的返回值型別
Class<?> getReturnType()
Type getGenericReturnType
4. 返回方法的引數(列表)
Class<?>[] getParameterTypes()
Type[] getGenericParameterTypes()
5. 返回方法的異常資訊
Class<?>[] getExceptionTypes()
Type[] getGenericExceptionTypes()
為什麼獲取方法的引數、返回值型別和異常表會有兩個方法呢?因為帶有Generic是返回宣告的型別,即使這個宣告的型別是泛型,也會返回泛型識別符號,而不會返回真正的型別,上一篇文章 講過的泛型擦除有介紹這點。
口說無憑,舉個例子吧。
public class MethodSpy {
private static final String fmt = "%24s: %s%n";
// for the morbidly curious
<E extends RuntimeException> void genericThrow() throws E {}
public static void main(String... args) {
try {
Class<?> c = Class.forName(args[0]);
Method[] allMethods = c.getDeclaredMethods();
for (Method m : allMethods) {
if (!m.getName().equals(args[1])) {
continue;
}
out.format("%s%n", m.toGenericString());
out.format(fmt, "ReturnType", m.getReturnType());
out.format(fmt, "GenericReturnType", m.getGenericReturnType());
Class<?>[] pType = m.getParameterTypes();
Type[] gpType = m.getGenericParameterTypes();
for (int i = 0; i < pType.length; i++) {
out.format(fmt,"ParameterType", pType[i]);
out.format(fmt,"GenericParameterType", gpType[i]);
}
Class<?>[] xType = m.getExceptionTypes();
Type[] gxType = m.getGenericExceptionTypes();
for (int i = 0; i < xType.length; i++) {
out.format(fmt,"ExceptionType", xType[i]);
out.format(fmt,"GenericExceptionType", gxType[i]);
}
}
} catch (ClassNotFoundException x) {
x.printStackTrace();
}
}
}
這個例子的大致意思是輸入一個類,以及要獲取方法資訊的方法名。
輸入結果:
1. java.lang.Class的getConstructor方法
$ java MethodSpy java.lang.Class getConstructor
public java.lang.reflect.Constructor<T> java.lang.Class.getConstructor
(java.lang.Class<?>[]) throws java.lang.NoSuchMethodException,
java.lang.SecurityException
ReturnType: class java.lang.reflect.Constructor
GenericReturnType: java.lang.reflect.Constructor<T>
ParameterType: class [Ljava.lang.Class;
GenericParameterType: java.lang.Class<?>[]
ExceptionType: class java.lang.NoSuchMethodException
GenericExceptionType: class java.lang.NoSuchMethodException
ExceptionType: class java.lang.SecurityException
GenericExceptionType: class java.lang.SecurityException
- java.lang.Class的cast方法
$ java MethodSpy java.lang.Class cast
public T java.lang.Class.cast(java.lang.Object)
ReturnType: class java.lang.Object
GenericReturnType: T
ParameterType: class java.lang.Object
GenericParameterType: class java.lang.Object
cast方法的返回值是就是泛型,識別符號是”T”,所以getGenericReturnType()方法會返回T,而getReturnType()則返回java.lang.Object,也就是泛型擦除之後的型別。
3. java.io.PrintStream的format方法
$ java MethodSpy java.io.PrintStream format
public java.io.PrintStream java.io.PrintStream.format
(java.util.Locale,java.lang.String,java.lang.Object[])
ReturnType: class java.io.PrintStream
GenericReturnType: class java.io.PrintStream
ParameterType: class java.util.Locale
GenericParameterType: class java.util.Locale
ParameterType: class java.lang.String
GenericParameterType: class java.lang.String
ParameterType: class [Ljava.lang.Object;
GenericParameterType: class [Ljava.lang.Object;
public java.io.PrintStream java.io.PrintStream.format
(java.lang.String,java.lang.Object[])
ReturnType: class java.io.PrintStream
GenericReturnType: class java.io.PrintStream
ParameterType: class java.lang.String
GenericParameterType: class java.lang.String
ParameterType: class [Ljava.lang.Object;
GenericParameterType: class [Ljava.lang.Object;
獲取引數的資訊
我們單獨拿出一章講解獲取引數的資訊是因為引數比較特殊,為了安全和記憶體考慮,class的位元組碼檔案裡並不會儲存引數的名稱,比如一些引數名,如secret或password,可能會公開有關安全敏感方法的資訊,比如很長的引數,儲存其引數名會引起記憶體暴增。
當然我們執行時加上-parameters引數,可以強制儲存其引數名稱,預設情況下是不會儲存的。
官方有一個列印引數的demo程式碼:
public class MethodParameterSpy {
private static final String fmt = "%24s: %s%n";
// for the morbidly curious
<E extends RuntimeException> void genericThrow() throws E {}
public static void printClassConstructors(Class c) {
Constructor[] allConstructors = c.getConstructors();
out.format(fmt, "Number of constructors", allConstructors.length);
for (Constructor currentConstructor : allConstructors) {
printConstructor(currentConstructor);
}
Constructor[] allDeclConst = c.getDeclaredConstructors();
out.format(fmt, "Number of declared constructors",
allDeclConst.length);
for (Constructor currentDeclConst : allDeclConst) {
printConstructor(currentDeclConst);
}
}
public static void printClassMethods(Class c) {
Method[] allMethods = c.getDeclaredMethods();
out.format(fmt, "Number of methods", allMethods.length);
for (Method m : allMethods) {
printMethod(m);
}
}
public static void printConstructor(Constructor c) {
out.format("%s%n", c.toGenericString());
Parameter[] params = c.getParameters();
out.format(fmt, "Number of parameters", params.length);
for (int i = 0; i < params.length; i++) {
printParameter(params[i]);
}
}
public static void printMethod(Method m) {
out.format("%s%n", m.toGenericString());
out.format(fmt, "Return type", m.getReturnType());
out.format(fmt, "Generic return type", m.getGenericReturnType());
Parameter[] params = m.getParameters();
for (int i = 0; i < params.length; i++) {
printParameter(params[i]);
}
}
public static void printParameter(Parameter p) {
out.format(fmt, "Parameter class", p.getType());
out.format(fmt, "Parameter name", p.getName());
out.format(fmt, "Modifiers", p.getModifiers());
out.format(fmt, "Is implicit?", p.isImplicit());
out.format(fmt, "Is name present?", p.isNamePresent());
out.format(fmt, "Is synthetic?", p.isSynthetic());
}
public static void main(String... args) {
try {
printClassConstructors(Class.forName(args[0]));
printClassMethods(Class.forName(args[0]));
} catch (ClassNotFoundException x) {
x.printStackTrace();
}
}
}
獲取到引數是Parameter,和Field一樣,同樣有這幾種方法:
getType():引數型別
getName(): 引數名稱,如果編譯器加了引數-parameters,則會返回真正的引數名,如果沒有加,則引數會是argN的形式,N是第幾個引數。
getModifiers(): 引數識別符號,不詳細介紹了。
isImplicit():如果是隱式宣告,則返回true。比如內部類會隱式宣告parent成員變數和構造器。
我們的程式碼:
public class MethodParameterExamples {
public class InnerClass { }
}
編譯器真正生成的程式碼:
public class MethodParameterExamples {
public class InnerClass {
final MethodParameterExamples parent;
InnerClass(final MethodParameterExamples this$0) {
parent = this$0;
}
}
}
isNamePresent(): 名稱是否可以同樣跟-parameters有關。
isSynthetic(): 是否是編譯器生成的。
比如用上面的程式碼獲取下面類的方法資訊:
public class ExampleMethods<T> {
public boolean simpleMethod(String stringParam, int intParam) {
System.out.println("String: " + stringParam + ", integer: " + intParam);
return true;
}
public int varArgsMethod(String... manyStrings) {
return manyStrings.length;
}
public boolean methodWithList(List<String> listParam) {
return listParam.isEmpty();
}
public <T> void genericMethod(T[] a, Collection<T> c) {
System.out.println("Length of array: " + a.length);
System.out.println("Size of collection: " + c.size());
}
}
編譯器加-parameters的執行結果如下:
Number of constructors: 1
Constructor #1
public ExampleMethods()
Number of declared constructors: 1
Declared constructor #1
public ExampleMethods()
Number of methods: 4
Method #1
public boolean ExampleMethods.simpleMethod(java.lang.String,int)
Return type: boolean
Generic return type: boolean
Parameter class: class java.lang.String
Parameter name: stringParam
Modifiers: 0
Is implicit?: false
Is name present?: true
Is synthetic?: false
Parameter class: int
Parameter name: intParam
Modifiers: 0
Is implicit?: false
Is name present?: true
Is synthetic?: false
Method #2
public int ExampleMethods.varArgsMethod(java.lang.String...)
Return type: int
Generic return type: int
Parameter class: class [Ljava.lang.String;
Parameter name: manyStrings
Modifiers: 0
Is implicit?: false
Is name present?: true
Is synthetic?: false
Method #3
public boolean ExampleMethods.methodWithList(java.util.List<java.lang.String>)
Return type: boolean
Generic return type: boolean
Parameter class: interface java.util.List
Parameter name: listParam
Modifiers: 0
Is implicit?: false
Is name present?: true
Is synthetic?: false
Method #4
public <T> void ExampleMethods.genericMethod(T[],java.util.Collection<T>)
Return type: void
Generic return type: void
Parameter class: class [Ljava.lang.Object;
Parameter name: a
Modifiers: 0
Is implicit?: false
Is name present?: true
Is synthetic?: false
Parameter class: interface java.util.Collection
Parameter name: c
Modifiers: 0
Is implicit?: false
Is name present?: true
Is synthetic?: false
不加引數的執行結果:
Number of constructors: 1
public reflect.ExampleMethods()
Number of parameters: 0
Number of declared constructors: 1
public reflect.ExampleMethods()
Number of parameters: 0
Number of methods: 4
public boolean reflect.ExampleMethods.simpleMethod(java.lang.String,int)
Return type: boolean
Generic return type: boolean
Parameter class: class java.lang.String
Parameter name: arg0
Modifiers: 0
Is implicit?: false
Is name present?: false
Is synthetic?: false
Parameter class: int
Parameter name: arg1
Modifiers: 0
Is implicit?: false
Is name present?: false
Is synthetic?: false
public int reflect.ExampleMethods.varArgsMethod(java.lang.String...)
Return type: int
Generic return type: int
Parameter class: class [Ljava.lang.String;
Parameter name: arg0
Modifiers: 0
Is implicit?: false
Is name present?: false
Is synthetic?: false
public boolean reflect.ExampleMethods.methodWithList(java.util.List<java.lang.String>)
Return type: boolean
Generic return type: boolean
Parameter class: interface java.util.List
Parameter name: arg0
Modifiers: 0
Is implicit?: false
Is name present?: false
Is synthetic?: false
public <T> void reflect.ExampleMethods.genericMethod(T[],java.util.Collection<T>)
Return type: void
Generic return type: void
Parameter class: class [Ljava.lang.Object;
Parameter name: arg0
Modifiers: 0
Is implicit?: false
Is name present?: false
Is synthetic?: false
Parameter class: interface java.util.Collection
Parameter name: arg1
Modifiers: 0
Is implicit?: false
Is name present?: false
Is synthetic?: false
獲取方法的識別符號
int getModifiers
獲取識別符號,這裡舉個例子就好,不做詳細介紹。
public class MethodModifierSpy {
private static int count;
private static synchronized void inc() { count++; }
private static synchronized int cnt() { return count; }
public static void main(String... args) {
try {
Class<?> c = Class.forName(args[0]);
Method[] allMethods = c.getDeclaredMethods();
for (Method m : allMethods) {
if (!m.getName().equals(args[1])) {
continue;
}
out.format("%s%n", m.toGenericString());
out.format(" Modifiers: %s%n",
Modifier.toString(m.getModifiers()));
out.format(" [ synthetic=%-5b var_args=%-5b bridge=%-5b ]%n",
m.isSynthetic(), m.isVarArgs(), m.isBridge());
inc();
}
out.format("%d matching overload%s found%n", cnt(),
(cnt() == 1 ? "" : "s"));
} catch (ClassNotFoundException x) {
x.printStackTrace();
}
}
}
輸出結果:
$ java MethodModifierSpy java.lang.Object wait
public final void java.lang.Object.wait() throws java.lang.InterruptedException
Modifiers: public final
[ synthetic=false var_args=false bridge=false ]
public final void java.lang.Object.wait(long,int)
throws java.lang.InterruptedException
Modifiers: public final
[ synthetic=false var_args=false bridge=false ]
public final native void java.lang.Object.wait(long)
throws java.lang.InterruptedException
Modifiers: public final native
[ synthetic=false var_args=false bridge=false ]
3 matching overloads found
$ java MethodModifierSpy java.lang.StrictMath toRadians
public static double java.lang.StrictMath.toRadians(double)
Modifiers: public static strictfp
[ synthetic=false var_args=false bridge=false ]
1 matching overload found
$ java MethodModifierSpy MethodModifierSpy inc
private synchronized void MethodModifierSpy.inc()
Modifiers: private synchronized
[ synthetic=false var_args=false bridge=false ]
1 matching overload found
$ java MethodModifierSpy java.lang.Class getConstructor
public java.lang.reflect.Constructor<T> java.lang.Class.getConstructor
(java.lang.Class<T>[]) throws java.lang.NoSuchMethodException,
java.lang.SecurityException
Modifiers: public transient
[ synthetic=false var_args=true bridge=false ]
1 matching overload found
$ java MethodModifierSpy java.lang.String compareTo
public int java.lang.String.compareTo(java.lang.String)
Modifiers: public
[ synthetic=false var_args=false bridge=false ]
public int java.lang.String.compareTo(java.lang.Object)
Modifiers: public volatile
[ synthetic=true var_args=false bridge=true ]
2 matching overloads found
執行方法
使用反射執行方法(Invoking Methods)是很簡單的事情,呼叫:
public Object invoke(Object obj,
Object... args)
throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException
即可,obj是擁有方法的Class的例項,args是方法的引數。
注意方法呼叫可能丟擲 IllegalAccessException、IllegalArgumentException、InvocationTargetException異常。
反射之Method的一些注意事項
- 查詢方法時c.getMethod(mName, cArg) 注意會丟擲異常。
- 私有方法呼叫時會丟擲IllegalAccessException,但是這個可以通過AccessibleObject.setAccessible()設定成功後可以呼叫成功。
- 方法呼叫時丟擲IllegalArgumentException,丟擲這個異常的原因是引數不合法。
- 方法呼叫時丟擲InvocationTargetException,這個異常比較特殊,方法呼叫成功了,但是方法內部丟擲了異常,所有invoke()會丟擲這個異常,可以通過Throwable cause = IllegalArgumentException.getCause()和cause.getMessage獲取到異常資訊。