天天看點

java 反射内部匿名内部類_android-反射的使用(反射靜态内部類、非靜态内部類、匿名内部類等)...

直接上代碼 com.**.routerdemo 替換成包名

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

public class ReflectionUtils {

public static void invokeInnerStaticClass() {

try {

Class clazz = Class.forName("com.**.routerdemo.Test$InnerStaticClass");

Constructor constructor = clazz.getDeclaredConstructor();

constructor.setAccessible(true);

Object instance = constructor.newInstance();

Method printM = clazz.getDeclaredMethod("print", String.class);

printM.setAccessible(true);

printM.invoke(instance, "I am InnerStaticClass");

Method getNameM = clazz.getDeclaredMethod("getName");

getNameM.setAccessible(true);

System.out.println("cxx getclassname=" + getNameM.invoke(instance));

} catch (Exception e) {

System.out.println("cxx Exception >>" + e.getMessage());

}

}

public static void invokeNormalStaticClass() {

try {

Class outerCls = Class.forName("com.**.routerdemo.Test");

Constructor outerConstructor = outerCls.getDeclaredConstructor();

outerConstructor.setAccessible(true);

Object outerInstance = outerConstructor.newInstance();

//正常内部類執行個體化 new Test().new InnerNormalClass();

Class clazz = Class.forName("com.**.routerdemo.Test$InnerNormalClass");

Constructor constructor = clazz.getDeclaredConstructor(outerCls);

constructor.setAccessible(true);

Object instance = constructor.newInstance(outerInstance);

Method printM = clazz.getDeclaredMethod("print", String.class);

printM.setAccessible(true);

printM.invoke(instance, "I am InnerStaticClass");

Method getNameM = clazz.getDeclaredMethod("getName");

getNameM.setAccessible(true);

System.out.println("cxx getclassname=" + getNameM.invoke(instance));

} catch (Exception e) {

System.out.println("cxx Exception >>" + e.getMessage());

}

}

public static void invokeMethod() {

try {

Bean bean1 = new Bean();

bean1.setVerCode(1);

Bean bean2 = new Bean();

bean2.setVerCode(1);

Class cls = Class.forName("com.**.routerdemo.Test");

Constructor constructor = cls.getDeclaredConstructor();

Method method = cls.getDeclaredMethod("equal", Bean.class, Bean.class);

method.setAccessible(true);

System.out.println("cxx is equal>>" + method.invoke(constructor.newInstance(), bean1, bean2));

} catch (Exception e) {

System.out.println("cxx e>>>" + e.getMessage());

}

}

public static void invokeAnonymousClass() {

try {

Class clazz = Class.forName("com.**.routerdemo.Test");

Constructor constructor = clazz.getDeclaredConstructor();

constructor.setAccessible(true);

Object outerInstance = constructor.newInstance();

Field field = clazz.getDeclaredField("runnable");

field.setAccessible(true);

Runnable r = (Runnable) field.get(outerInstance);

r.run();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void InvokeInnerClasses() {

try {

Class clazz = Class.forName("com.**.routerdemo.Test");

Object container = clazz.newInstance();

Class innerClazz[] = clazz.getDeclaredClasses();

for (Class cls : innerClazz) {

int mod = cls.getModifiers();

String modifier = Modifier.toString(mod);

if (modifier.contains("static")) {

//構造靜态内部類執行個體

Constructor innerStatic = cls.getDeclaredConstructor();

innerStatic.setAccessible(true);

Method printM = cls.getDeclaredMethod("print", String.class);

printM.setAccessible(true);

printM.invoke(innerStatic.newInstance(), "I am InnerStaticClass");

} else {

Constructor innerNormal = cls.getDeclaredConstructor(clazz);

innerNormal.setAccessible(true);

Method print = cls.getDeclaredMethod("print", String.class);

print.setAccessible(true);

print.invoke(innerNormal.newInstance(container), "I am innerNormal");

}

}

// 擷取匿名内部類執行個體

Field field = clazz.getDeclaredField("runnable");

field.setAccessible(true);

Runnable r = (Runnable) field.get(container);

r.run();

// 擷取靜态成員變量

Field staticField = clazz.getDeclaredField("paramStatic");

staticField.setAccessible(true);

staticField.setBoolean(container, true);

System.out.println("cxx test staticParam " + Test.paramStatic);

// 擷取非靜态成員變量

Field normalField = clazz.getDeclaredField("paramNormal");

normalField.setAccessible(true);

normalField.setBoolean(container, true);

System.out.println("cxx test paramNormal " + ((Test) container).paramNormal);

} catch (Exception e) {

System.out.println("cxx Exception >>" + e.getMessage());

}

}

}

被反射的類:

public class Test {

private static class InnerStaticClass {

private void print(String str) {

System.out.println("cxx 靜态内部類print帶參方法,參數" + str);

}

private String getName() {

return InnerStaticClass.class.getName();

}

}

private class InnerNormalClass {

private void print(String str) {

System.out.println("cxx 非靜态内部類print帶參方法,參數" + str);

}

private String getName() {

return InnerNormalClass.class.getName();

}

}

private Runnable runnable = new Runnable() {

@Override

public void run() {

System.out.println("cxx 匿名内部類run方法");

}

};

public static boolean paramStatic = false;

public boolean paramNormal = false;

private boolean equal(Bean a, Bean b) {

return a.getVerCode() == b.getVerCode();

}

}