jAVA反射機制java
JAVA反射機制是在運行狀態中,對於任意一個類,都可以知道這個類的全部屬性和方法;對於任意一個對象,都可以調用它的任意一個方法;這種動態獲取的信息以及動態調用對象的方法的功能稱爲java語言的反射機制。
Java反射機制主要提供瞭如下功能: 在運行時判斷任意一個對象所屬的類;在運行時構造任意一個類的對象;在運行時判斷任意一個類所具備的成員變量和方法;在運行時調用任意一個對象的方法;生成動態代理。
1. 獲得某個對象的屬性數組
public Object getProperty(Object owner, String fieldName) throws Exception { 函數
Class ownerClass = owner.getClass(); spa
Field field = ownerClass.getField(fieldName); .net
Object property = field.get(owner); 代理
return property; orm
} 對象
public Object getProperty(Object owner, String fieldName) throws Exception { blog
Class ownerClass = owner.getClass(); get
Field field = ownerClass.getField(fieldName);
Object property = field.get(owner);
return property;
}
[java] view plaincopy
public Object getProperty(Object owner, String fieldName) throws Exception {
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(fieldName);
Object property = field.get(owner);
return property;
}
Class ownerClass = owner.getClass():獲得該對象的Class。
Field field = ownerClass.getField(fieldName):經過Class獲得類聲明的屬性。
Object property = field.get(owner):經過對象獲得該屬性的實例,若是這個屬性是非公有的,這裏會報IllegalAccessException。
2. 獲得某個類的靜態屬性
public Object getStaticProperty(String className, String fieldName)
throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
public Object getStaticProperty(String className, String fieldName)
throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
[java] view plaincopy
public Object getStaticProperty(String className, String fieldName)
throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
Class ownerClass = Class.forName(className) :首先獲得這個類的Class。
Field field = ownerClass.getField(fieldName):和上面同樣,經過Class獲得類聲明的屬性。
Object property = field.get(ownerClass) :這裏和上面有些不一樣,由於該屬性是靜態的,因此直接從類的Class裏取。
3. 執行某對象的方法
public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(owner, args);
}
public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(owner, args);
}
[java] view plaincopy
public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(owner, args);
}
Class owner_class = owner.getClass() :首先仍是必須獲得這個對象的Class。
5~9行:配置參數的Class數組,做爲尋找Method的條件。
Method method = ownerClass.getMethod(methodName, argsClass):經過methodName和參數的argsClass(方法中的參數類型集合)數組獲得要執行的Method。
method.invoke(owner, args):執行該Method.invoke方法的參數是執行這個方法的對象owner,和參數數組args,能夠這麼理解:owner對象中帶有參數args的method方法。返回值是Object,也既是該方法的返回值。
4. 執行某個類的靜態方法
public Object invokeStaticMethod(String className, String methodName,
Object[] args) throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(null, args);
}
public Object invokeStaticMethod(String className, String methodName,
Object[] args) throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(null, args);
}
[java] view plaincopy
public Object invokeStaticMethod(String className, String methodName,
Object[] args) throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(null, args);
}
基本的原理和實例3相同,不一樣點是最後一行,invoke的一個參數是null,由於這是靜態方法,不須要藉助實例運行。
5. 新建實例
public Object newInstance(String className, Object[] args) throws Exception {
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
}
public Object newInstance(String className, Object[] args) throws Exception {
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
}
[java] view plaincopy
public Object newInstance(String className, Object[] args) throws Exception {
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
}
這裏說的方法是執行帶參數的構造函數來新建實例的方法。若是不須要參數,能夠直接使用newoneClass.newInstance()來實現。
Class newoneClass = Class.forName(className):第一步,獲得要構造的實例的Class。
第5~第9行:獲得參數的Class數組。
Constructor cons = newoneClass.getConstructor(argsClass):獲得構造子。
cons.newInstance(args):新建實例。
6. 判斷是否爲某個類的實例
public boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
public boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
[java] view plaincopy
public boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
7. 獲得數組中的某個元素
public Object getByArray(Object array, int index) {
return Array.get(array,index);
}