春雨将半,各梦所欢。——送花的人走了

最近在看MethodHandle相关内容,我们将MethodHandle转反射的Executable对象,可以这么使用:

1
final Executable executable = MethodHandles.reflectAs(Executable.class, methodHandle);

当然你也可以这么用:

1
2
3
4
5
final MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle methodHandle = lookup
.findStatic(Test.class, "myMethod", MethodType.methodType(resultType, paramerType));
Method method = lookup.revealDirect(methodHandle).reflectAs(Method.class, lookup);
System.out.println(method);

javadoc apihttps://docs.oracle.com/javase/8/docs/api/java/lang/invoke/MethodHandles.html

image-20220811135138437