+-
java-将MethodHandles.publicLookup()与Method.setAccessible(true)组合
我知道publicLookup()比public方法的lookup()更快,我想利用它.如果我要在非固有公共方法但已调用setAccessible(true)的方法上使用MethodHandles.publicLookup().unreflect(Method),它将起作用吗?
最佳答案
由于每个人都可以调用成功调用setAccessible(true)的Method,因此可以像其他任何Lookup对象一样,使用MethodHandles.publicLookup()将其设置为未反射.

毕竟,这是在MethodHandles上使用访问覆盖的唯一方法,因为java.lang.invoke本身并不提供任何访问覆盖功能.

下面的演示使用字段而不是方法,但具有令人印象深刻的结果:

Field m = String.class.getDeclaredField("value");
m.setAccessible(true);
MethodHandle mh = MethodHandles.publicLookup().unreflectGetter(m);
char[] ch = (char[])mh.invoke("hello");
Arrays.fill(ch, '*');
System.out.println("hello");
点击查看更多相关文章

转载注明原文:java-将MethodHandles.publicLookup()与Method.setAccessible(true)组合 - 乐贴网