基本反射技术
--摘自《android插件化开发指南》
No1:
String str = "abc"; Class c1 = str.getClass(); System.out.print(c1.toString()); ------------- class java.lang.String
No2:
try { Class c2 = Class.forName("java.lang.String"); Class c3 = Class.forName("android.widget.Button"); Class c5 = c3.getSuperclass(); System.out.println(c2.toString()); System.out.println(c3.toString()); System.out.println(c5.toString()); } catch (ClassNotFoundException e) { e.printStackTrace(); } -------------- class java.lang.String class android.widget.Button class android.widget.TextView
No3:
Class c6 = String.class; Class c7 = java.lang.String.class; Class c8 = MainActivity.class; Class c9 = int.class; Class c10 = int[].class; System.out.println(c6.toString()); System.out.println(c7.toString()); System.out.println(c8.toString()); System.out.println(c9.toString()); System.out.println(c10.toString()); ---------------- class java.lang.String class java.lang.String class com.example.jinke.joor.MainActivity int class [I
No4:
Class c11 = Boolean.TYPE; Class c12 = Byte.TYPE; Class c13 = Character.TYPE; Class c14 = Short.TYPE; Class c15 = Integer.TYPE; Class c16 = Long.TYPE; Class c17 = Float.TYPE; Class c18 = Double.TYPE; Class c19 = Void.TYPE; System.out.println(c11.toString()); System.out.println(c12.toString()); System.out.println(c13.toString()); System.out.println(c14.toString()); System.out.println(c15.toString()); System.out.println(c16.toString()); System.out.println(c17.toString()); System.out.println(c18.toString()); System.out.println(c19.toString()); ---------------------- boolean byte char short int long float double void
No5:
Class的getDeclaredConstructors方法可以获取所有构造函数,包括public和private的
public class TestClassCtor { String name; public TestClassCtor() { name = "baobao"; } public TestClassCtor(int a) { } public TestClassCtor(int a, String b) { name = b; } private TestClassCtor(int a, double c) { } }
public static void main(String[] args) { TestClassCtor r = new TestClassCtor(); Class temp = r.getClass(); String className = temp.getName(); Constructor[] constructors = temp.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) { //mod是访问权限 int mod = constructors[i].getModifiers(); System.out.print(Modifier.toString(mod) + " " + className + "("); //parameterTypes是一个构造函数的参数类型 Class[] parameterTypes = constructors[i].getParameterTypes(); for (int j = 0; j < parameterTypes.length; j++) { System.out.print(parameterTypes[j].getName()); if (parameterTypes.length > j + 1) { System.out.print(","); } } System.out.println(")"); } } ------------------------ private com.example.reflectiontest.TestClassCtor(int,double) public com.example.reflectiontest.TestClassCtor(int,java.lang.String) public com.example.reflectiontest.TestClassCtor(int) public com.example.reflectiontest.TestClassCtor()
No6:
Class的getConstructors方法只能获得所有的public构造函数
public class TestClassCtor { String name; public TestClassCtor() { name = "baobao"; } public TestClassCtor(int a) { } public TestClassCtor(int a, String b) { name = b; } private TestClassCtor(int a, double c) { } }
public static void main(String[] args) { TestClassCtor r = new TestClassCtor(); Class temp = r.getClass(); try { //获取无参构造函数 Constructor c1 = temp.getDeclaredConstructor(); System.out.println(c1.toString()); //获取一个参数(int型)构造函数 Class[] p2 = {int.class}; Constructor c2 = temp.getDeclaredConstructor(p2); System.out.println(c2.toString()); //获取两个参数(int型、String型) Class[] p3 = {int.class, String.class}; Constructor c3 = temp.getDeclaredConstructor(p3); System.out.println(c3.toString()); } catch (NoSuchMethodException e) { e.printStackTrace(); } } --------------- public com.example.reflectiontest.TestClassCtor() public com.example.reflectiontest.TestClassCtor(int) public com.example.reflectiontest.TestClassCtor(int,java.lang.String)
No7:
调用构造函数
public class TestClassCtor { 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 = " + name; } }
try { Class r = Class.forName("com.example.reflectiontest.TestClassCtor"); //含参 Class[] p3 = {int.class, String.class}; Constructor ctor = r.getDeclaredConstructor(p3); Object obj = ctor.newInstance(1, "bjq"); System.out.println(obj.toString()); //无参1 Constructor ctor2 = r.getDeclaredConstructor(); Object obj2 = ctor2.newInstance(); System.out.println(obj2.toString()); //无参2 Object obj4 = r.newInstance(); System.out.println(obj4.toString()); } catch (Exception e) { e.printStackTrace(); } -------------------- name = bjq name = baobao name = baobao
No8:
调用私有实例方法
public class TestClassCtor { 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; } private String doSomething2(String d, String e) { System.out.println("TestClassCtor,doSomething"); return "123+" + d + "+" + e; } }
try { Class r = Class.forName("com.example.reflectiontest.TestClassCtor"); Class[] p3 = {int.class, String.class}; Constructor ctor = r.getDeclaredConstructor(p3); Object obj = ctor.newInstance(1, "bjq"); //调用私有方法1 Class[] p4 = {String.class}; Method method = r.getDeclaredMethod("doSomething", p4); method.setAccessible(true); Object argList[] = {"jianqiang"}; Object result = method.invoke(obj, argList); System.out.println(result.toString()); //调用私有方法2 Class[] p5 = {String.class, String.class}; Method method2 = r.getDeclaredMethod("doSomething2", p5); method2.setAccessible(true); Object argList2[] = {"jianqiang", "baobao"}; Object result2 = method2.invoke(obj, argList2); System.out.println(result2.toString()); } catch (Exception e) { e.printStackTrace(); } --------------- TestClassCtor,doSomething 123jianqiang TestClassCtor,doSomething 123+jianqiang+baobao
我尝试了一下,如果doSomething是静态方法,上述代码照样适合
No9:
调用静态私有方法
public class TestClassCtor { 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"); } }
try { Class r = Class.forName("com.example.reflectiontest.TestClassCtor"); Method method = r.getDeclaredMethod("work"); method.setAccessible(true); method.invoke(null); } catch (Exception e) { e.printStackTrace(); } ----------- TestClassCtor,work
这里没有创建实例,所以只能调静态方法
No10:
修改私有实例字段
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) { } public String getName() { return name; } }
try { Class r = Class.forName("com.example.reflectiontest.TestClassCtor"); Class[] p3 = {int.class, String.class}; Constructor ctor = r.getDeclaredConstructor(p3); Object obj = ctor.newInstance(1, "bjq"); Field field = r.getDeclaredField("name"); field.setAccessible(true); Object fieldObject = field.get(obj); System.out.println(fieldObject.toString()); field.set(obj, "jianqiang1982"); fieldObject = field.get(obj); System.out.println(fieldObject.toString()); } catch (Exception e) { e.printStackTrace(); } ---------------------- bjq jianqiang1982
我尝试了一下,如果name是静态私有变量,上述代码照样合适
No11:
修改私有静态字段
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) { } public String getName() { return name; } public static void printAddress() { System.out.println(address); } }
try { Class r = Class.forName("com.example.reflectiontest.TestClassCtor"); Field field = r.getDeclaredField("address"); field.setAccessible(true); Object fieldObject = field.get(null); field.set(fieldObject, "ABCD"); System.out.println(field.get(fieldObject).toString()); TestClassCtor.printAddress(); } catch (Exception e) { e.printStackTrace(); } --------------------- ABCD ABCD
No12:
对反省的反射
public abstract class Singleton { private T mInstance; protected abstract T create(); public final T get() { synchronized (this) { if (mInstance == null) { mInstance = create(); } return mInstance; } } }
try { Class<?> singleton = Class.forName("com.example.reflectiontest.Singleton"); Field mInstanceField = singleton.getDeclaredField("mInstance"); mInstanceField.setAccessible(true); } catch (Exception e) { e.printStackTrace(); }
以上