MethodHandles.lookup()
返回一个工具,能够访问与调用此函数的调用者相同的方法/函数/构造函数。具体而言,如果调用者可以访问某些私有数据,则该MethodHandles.Lookup工具也可以访问。下面的代码证明了这种说法是错误的。我错在哪里了?public class MethodHandlerAccessTest {
private static class NestedClass {
private static void foo(){}
}
@Test
public void testPrivateAccess() throws Throwable {
NestedClass.foo(); //compiles and executes perfectly
MethodType type = MethodType.methodType(void.class);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findStatic(NestedClass.class, "foo", type);
}
}
Edit:
以下是错误信息:
java.lang.IllegalAccessException: member is private: MethodHandlerAccessTest$NestedClass.foo()void, from MethodHandlerAccessTest at java.lang.invoke.MemberName.makeAccessException(MemberName.java:507) at java.lang.invoke.MethodHandles$Lookup.checkAccess(MethodHandles.java:1182) at java.lang.invoke.MethodHandles$Lookup.checkMethod(MethodHandles.java:1162) at java.lang.invoke.MethodHandles$Lookup.accessStatic(MethodHandles.java:591) at java.lang.invoke.MethodHandles$Lookup.findStatic(MethodHandles.java:587) at MethodHandlerAccessTest.testPrivateAccess(MethodHandlerAccessTest.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
foo()
,也无权这样做。通过添加额外的方法并调用它,语言能够赋予你“特殊”的权限来这样做,但这只是语言问题。从虚拟机的角度来看,调用者(外部类)确实无法访问foo()
。 - Jon SkeetMethodHandles.lookup()
替换为MethodHandles.lookup().in(NestedClass.class)
。通过这种方式在外部类和内部类之间更改上下文类时,私有访问能力得以保留,因此之后可以访问嵌套类的private
成员。除此之外,从Java 11开始,这个access$...
的东西不再必要,但它也需要使用JDK 11目标编译代码。 - Holger