路人战队|新型反射应用

【路人战队|新型反射应用】public class Main {public static void main(String[] args) {try {MethodHandles.Lookup lookup = MethodHandles.lookup();MethodType type = MethodType.methodType(void.class, String.class);MethodHandle mh = lookup.findVirtual(Test.class, "say", type);ConstantCallSite callSite = new ConstantCallSite(mh);MethodHandle invoker = callSite.dynamicInvoker();invoker.invoke(new Test(), "hello world");} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (Throwable e) {e.printStackTrace();}}}class Test implements Cloneable, Serializable {private static final long serialVersionUID = -8655076206703674175L;public Test() {//throw new Error("不能创建实例");}public void say(String str) {System.out.println(str);}public Object clone() throws CloneNotSupportedException {return super.clone();}}