Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MethodHandle Lookup facility

As far as JavaDoc states MethodHandles.lookup() returns facility that have ability to access the same method/functions/constructor as the caller of this function. Specifically, if the caller can access some private data, so as this MethodHandles.Lookup facility. Code below demonstrates that this is false. Where I get it wrong?

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:

This is what I get:

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)

like image 596
alexsmail Avatar asked Apr 27 '13 07:04

alexsmail


1 Answers

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.

like image 140
Jon Skeet Avatar answered Oct 13 '22 04:10

Jon Skeet