Class runtimeClass = Runtime.class;
Method m1 = runtimeClass.getMethod("getRuntime",new Class[]{});//new Class[]{}==new Class[0]
((Runtime)m1.invoke(null,new Object[]{})).exec("/System/Applications/Calculator.app/Contents/MacOS/Calculator");
关于反射可以无限套娃的,我把之前的processBuilder命令执行反射调用改了改,变得更加复杂,当然搞懂之后反射也变得更好理解。
Class clazz = Class.forName("java.lang.ProcessBuilder");
Class classFath = clazz.getClass();
Class classFath2 = classFath.getClass();
Class classFath3 = classFath2.getClass();
Class classFath4 = classFath3.getClass();
Method a2 = classFath4.getMethod("getMethod",new Class[]{String.class,Class[].class});
Method t1 = (Method)a2.getClass().getMethod("invoke",new Class[]{Object.class,Object[].class});
Method a1 = (Method) t1.invoke(a2,new Object[]{classFath3,new Object[]{"getMethod",new Class[]{String.class,Class[].class}}});
Method a = (Method)a1.invoke(classFath2,new Object[]{"getMethod",new Class[]{String.class,Class[].class}});
Method getConstructorMethod = (Method) a.invoke(classFath,new Object[]{"getConstructor",new Class[]{Class[].class}});
Object b = getConstructorMethod.invoke(clazz,new Object[]{new Class[]{String[].class}});
Method newInstanceMethod = b.getClass().getMethod("newInstance",new Class[]{Object[].class});
Object d = newInstanceMethod.invoke(b,new Object[]{new String[][]{{"/System/Applications/Calculator.app/Contents/MacOS/Calculator"}}});
Method startMethod = d.getClass().getMethod("start",new Class[]{});
startMethod.invoke(d,new Object[]{});
通过动态调试发现,getClass不是我们想象中的那种无限向上追溯,而是获取到根Class对象后就会停止追溯,什么意思呢简单来说的,我们最多获取到图中a1这个对象。