7

就 JavaDoc 状态而言,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);
        }

 }

编辑:

这就是我得到的:

java.lang.IllegalAccessException:成员是私有的:MethodHandlerAccessTest$NestedClass.foo()void,来自 java.lang.invoke.MemberName.makeAccessException(MemberName.java:507) 处 java.lang.invoke.MethodHandles$Lookup.checkAccess 的 MethodHandlerAccessTest (MethodHandles.java:1182) 在 java.lang.invoke.MethodHandles$Lookup.checkMethod(MethodHandles.java:1162) 在 java.lang.invoke.MethodHandles$Lookup.accessStatic(MethodHandles.java:591) 在 java.lang。在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 57) 在 sun.reflect.DelegatingMethodAccessorImpl。在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit 的 java.lang.reflect.Method.invoke(Method.java:601) 调用(DelegatingMethodAccessorImpl.java:43)。 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) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java:50) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner .java:229) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.ParentRunner.run(ParentRunner.java:309)。 jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal。 junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner。 RemoteTestRunner.main(RemoteTestRunner.爪哇:197)

4

1 回答 1

8

The problem is that your test method doesn't really call NestedClass.foo(). This line:

NestedClass.foo();

... is actually transformed into a call to a synthetic method which is generated in foo, like this:

NestedClass.access$000();

Where access$000 looks like this:

// Note package access
static void access$000() {
    foo();
}

You can validate this by using javap -c to look at the actual bytecode.

At the JVM level, your outer class doesn't have access to foo(). The Java compiler just synthesizes access to it by creating access$000 and calling it from your outer class whenever the source code calls foo().

At execution time, the reflection libraries don't do the same thing, hence your error.

于 2013-04-29T16:20:19.883 回答