--摘自《android插件化开发指南》

1.jOOR库就一个Reflect.java类很重要

2.Reflect.java包括6个核心方法

  1)on:包裹一个类或者对象,表示在这个类或对象上进行反射

  2)create:用来调用之前的类的构造方法

  3)call:方法调用

  4)get:获取(field和method返回)值

  5)set:设置属性值

No1:

根据字符串获取一个类

import static com.example.reflectiontest.Reflect.on;

Reflect r1 = on(Object.class);
Reflect r2 = on("java.lang.Object");
Reflect r3 = on("java.lang.Object",ClassLoader.getSystemClassLoader()); Object o1 = on(Object.class).<Object>get();
Object o2 = on("java.lang.Object").get(); String j2 = on((Object)"abc").get();
int j3 = on(1).get(); System.out.println(r1.toString());
System.out.println(r2.toString());
System.out.println(r3.toString());
System.out.println(o1.toString());
System.out.println(o2.toString());
System.out.println(j2.toString());
System.out.println(j3);
----------------------
class java.lang.Object
class java.lang.Object
class java.lang.Object
class java.lang.Object
class java.lang.Object
abc
1

No2:

调用类的构造函数

public class TestClassCtor {
private static String name; public TestClassCtor() {
name = "baobao";
} public TestClassCtor(int a) { } public TestClassCtor(int a, String b) {
name = b;
} private TestClassCtor(int a, double c) { } @Override
public String toString() {
return name;
} public static String getName() {
return name;
}
}
TestClassCtor r = new TestClassCtor();
Class temp = r.getClass();
String className = temp.getName();
System.out.println(className); //public构造函数
Object obj = on(temp).create().get();//无参
Object obj2 = on(temp).create(1, "abc").get();//有参
System.out.println("obj:" + obj.toString());
System.out.println("obj2:" + obj2.toString()); //private构造函数
TestClassCtor obj3 = on(TestClassCtor.class).create(1, 1.1).get();
String a = obj3.getName();
System.out.println("a:" + a);
----------------
com.example.reflectiontest.TestClassCtor
obj:abc
obj2:abc
a:abc

No3:

调用类的私有方法

public class TestClassCtor {
private static String name; public TestClassCtor() {
name = "baobao";
} public TestClassCtor(int a) { } public TestClassCtor(int a, String b) {
name = b;
} private TestClassCtor(int a, double c) { } private String doSomething(String d) {
System.out.println("TestClassCtor,doSomething");
return "123" + d;
}
}
TestClassCtor r = new TestClassCtor();
Class temp = r.getClass();
Reflect reflect = on(temp).create(); String a1 = reflect.call("doSomething", "param1").get();
System.out.println(a1);
------------------
TestClassCtor,doSomething
123param1

No4:

调用类的私有静态方法

public class TestClassCtor {
private static String name; public TestClassCtor() {
name = "baobao";
} public TestClassCtor(int a) { } public TestClassCtor(int a, String b) {
name = b;
} private TestClassCtor(int a, double c) { } private static void work() {
System.out.println("TestClassCtor,work");
}
}
TestClassCtor r = new TestClassCtor();
Class temp = r.getClass();
Reflect reflect = on(temp).create(); on(TestClassCtor.class).call("work").get();
-------------------
TestClassCtor,work

No5:

修改类的私有实例字段

public class TestClassCtor {
private String name; public TestClassCtor() {
name = "baobao";
} public TestClassCtor(int a) { } public TestClassCtor(int a, String b) {
name = b;
} private TestClassCtor(int a, double c) { }
}
Reflect obj = on("com.example.reflectiontest.TestClassCtor").create(1, 1.1);
obj.set("name", "jianqiang");
Object obj1 = obj.get("name");
System.out.println(obj1.toString());
----------------------
jianqiang

No6:

修改类的私有静态字段

public class TestClassCtor {
private String name;
private static String address; public TestClassCtor() {
name = "baobao";
} public TestClassCtor(int a) { } public TestClassCtor(int a, String b) {
name = b;
} private TestClassCtor(int a, double c) { }
}
on("com.example.reflectiontest.TestClassCtor").set("address","avccccc");
Object obj2 = on("com.example.reflectiontest.TestClassCtor").get("address");
System.out.println(obj2.toString());
------------------
avccccc

问题:为什么jOOR不支持final字段

因为jOOR的Reflect的set方法会在遇到final时,尝试反射出Field类的modifiers字段,在Java环境下是有这个字段的,但是Android版本的Field并没有这个字段,于是就报错NoSuchFeildException

05-06 21:13