直接上代码 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(); } }