Reflection是Java 程序開發語言的特征之一
Java 的這一能力在實際應用中也許用得不是很多
JavaBean 是 reflection 的實際應用之一
考慮下面這個簡單的例子
import java
public class DumpMethods {
public static void main(String args[]) {
try {
Class c = Class
Method m[] = c
for (int i =
System
} catch (Throwable e) {
System
}
}
}
按如下語句執行
java DumpMethods java
它的結果輸出為
public java
public synchronized java
public synchronized java
public boolean java
public synchronized int java
這樣就列出了java
這個程序使用 Class
用於 reflection 的類
下面就是獲得一個 Class 對象的方法之一
Class c = Class
這條語句得到一個 String 類的類對象
Class c = int
或者
Class c = Integer
它們可獲得基本類型的類信息
第二步是調用諸如 getDeclaredMethods 的方法
一旦取得這個信息
Class c = Class
Method m[] = c
System
它將以文本方式打印出 String 中定義的第一個方法的原型
在下面的例子中
模擬 instanceof 操作符
得到類信息之後
class A {
}
public class instance
public static void main(String args[]) {
try {
Class cls = Class
boolean b
System
boolean b
System
} catch (Throwable e) {
System
}
}
}
在這個例子中創建了一個 A 類的 Class 對象
找出一個類中定義了些什麼方法
import java
public class method
private int f
if (p == null)
throw new NullPointerException();
return x;
}
public static void main(String args[]) {
try {
Class cls = Class
Method methlist[] = cls
for (int i =
Method m = methlist[i];
System
System
Class pvec[] = m
for (int j =
System
Class evec[] = m
for (int j =
System
System
System
}
} catch (Throwable e) {
System
}
}
}
這個程序首先取得 method
取得了 Method 對象列表之後
輸出的結果如下
name = f
decl class = class method
param #
param #
exc #
return type = int
name = main
decl class = class method
param #
return type = void
獲取類構造器的用法與上述獲取方法的用法類似
import java
public class constructor
public constructor
}
protected constructor
}
public static void main(String args[]) {
try {
Class cls = Class
Constructor ctorlist[] = cls
for (int i =
Constructor ct = ctorlist[i];
System
System
Class pvec[] = ct
for (int j =
System
Class evec[] = ct
for (int j =
System
System
}
} catch (Throwable e) {
System
}
}
}
這個例子中沒能獲得返回類型的相關信息
這個程序運行的結果是
name = constructor
decl class = class constructor
name = constructor
decl class = class constructor
param #
param #
找出一個類中定義了哪些數據字段也是可能的
import java
public class field
private double d;
public static final int i =
String s =
public static void main(String args[]) {
try {
Class cls = Class
Field fieldlist[] = cls
for (int i =
Field fld = fieldlist[i];
System
System
System
int mod = fld
System
System
}
} catch (Throwable e) {
System
}
}
}
這個例子和前面那個例子非常相似
name = d
decl class = class field
type = double
modifiers = private
name = i
decl class = class field
type = int
modifiers = public static final
name = s
decl class = class field
type = class java
modifiers =
和獲取方法的情況一下
文本到這裡
import java
public class method
public int add(int a
return a + b;
}
public static void main(String args[]) {
try {
Class cls = Class
Class partypes[] = new Class[
partypes[
partypes[
Method meth = cls
method
Object arglist[] = new Object[
arglist[
arglist[
Object retobj = meth
Integer retval = (Integer) retobj;
System
} catch (Throwable e) {
System
}
}
}
假如一個程序在執行的某處的時候才知道需要執行某個方法
上例中
對於構造器
import java
public class constructor
public constructor
}
public constructor
System
}
public static void main(String args[]) {
try {
Class cls = Class
Class partypes[] = new Class[
partypes[
partypes[
Constructor ct = cls
Object arglist[] = new Object[
arglist[
arglist[
Object retobj = ct
} catch (Throwable e) {
System
}
}
}
根據指定的參數類型找到相應的構造函數並執行它
reflection 的還有一個用處就是改變對象數據字段的值
import java
public class field
public double d;
public static void main(String args[]) {
try {
Class cls = Class
Field fld = cls
field
System
fld
System
} catch (Throwable e) {
System
}
}
}
這個例子中
本文介紹的 reflection 的最後一種用法是創建的操作數組
import java
public class array
public static void main(String args[]) {
try {
Class cls = Class
Object arr = Array
Array
String s = (String) Array
System
} catch (Throwable e) {
System
}
}
}
例中創建了
下面這段代碼提供了一個更復雜的例子
import java
public class array
public static void main(String args[]) {
int dims[] = new int[]{
Object arr = Array
Object arrobj = Array
Class cls = arrobj
System
arrobj = Array
Array
int arrcast[][][] = (int[][][]) arr;
System
}
}
例中創建了一個
注意創建數組時的類型是動態的
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26193.html