blob: 2802dfa4cccf9533b2f7fb9d9c3c8986534ec9a0 [file] [log] [blame]
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.lang.invoke.MethodHandle;
18import java.lang.invoke.MethodHandles;
19import java.lang.invoke.MethodHandles.Lookup;
20import java.lang.invoke.MethodType;
21import java.lang.invoke.WrongMethodTypeException;
22
23public class Main {
24
25 public static class A {
26 public void foo() {
27 System.out.println("foo_A");
28 }
29
30 public static final Lookup lookup = MethodHandles.lookup();
31 }
32
33 public static class B extends A {
34 public void foo() {
35 System.out.println("foo_B");
36 }
37
38 public static final Lookup lookup = MethodHandles.lookup();
39 }
40
41 public static class C extends B {
42 public static final Lookup lookup = MethodHandles.lookup();
43 }
44
45 public static class D {
46 private final void privateRyan() {
47 System.out.println("privateRyan_D");
48 }
49
50 public static final Lookup lookup = MethodHandles.lookup();
51 }
52
53 public static class E extends D {
54 public static final Lookup lookup = MethodHandles.lookup();
55 }
56
57 public static void main(String[] args) throws Throwable {
58 testfindSpecial_invokeSuperBehaviour();
59 testfindSpecial_invokeDirectBehaviour();
60 }
61
62 public static void testfindSpecial_invokeSuperBehaviour() throws Throwable {
63 // This is equivalent to an invoke-super instruction where the referrer
64 // is B.class.
65 MethodHandle mh1 = B.lookup.findSpecial(A.class /* refC */, "foo",
66 MethodType.methodType(void.class), B.class /* specialCaller */);
67
68 A aInstance = new A();
69 B bInstance = new B();
70 C cInstance = new C();
71
72 // This should be as if an invoke-super was called from one of B's methods.
73 mh1.invokeExact(bInstance);
74 mh1.invoke(bInstance);
75
76 // This should not work. The receiver type in the handle will be suitably
77 // restricted to B and subclasses.
78 try {
79 mh1.invoke(aInstance);
80 System.out.println("mh1.invoke(aInstance) should not succeeed");
81 } catch (ClassCastException expected) {
82 }
83
84 try {
85 mh1.invokeExact(aInstance);
86 System.out.println("mh1.invoke(aInstance) should not succeeed");
87 } catch (WrongMethodTypeException expected) {
88 } catch (ClassCastException workaround) {
89 // TODO(narayan): ART treats all invokes as if they were non-exact. We
90 // should throw a WMTE if we execute an invoke-polymorphic instruction whose
91 // target method is MethodHandle.invokeExact.
92 }
93
94 // This should *still* be as if an invoke-super was called from one of C's
95 // methods, despite the fact that we're operating on a C.
96 mh1.invoke(cInstance);
97
98 // Now that C is the special caller, the next invoke will call B.foo.
99 MethodHandle mh2 = C.lookup.findSpecial(A.class /* refC */, "foo",
100 MethodType.methodType(void.class), C.class /* specialCaller */);
101 mh2.invokeExact(cInstance);
102
103 // Shouldn't allow invoke-super semantics from an unrelated special caller.
104 try {
105 C.lookup.findSpecial(A.class, "foo",
106 MethodType.methodType(void.class), D.class /* specialCaller */);
107 System.out.println("findSpecial(A.class, foo, .. D.class) unexpectedly succeeded.");
108 } catch (IllegalAccessException expected) {
109 }
110 }
111
112 public static void testfindSpecial_invokeDirectBehaviour() throws Throwable {
113 D dInstance = new D();
114
115 MethodHandle mh3 = D.lookup.findSpecial(D.class, "privateRyan",
116 MethodType.methodType(void.class), D.class /* specialCaller */);
117 mh3.invoke(dInstance);
118
119 // The private method shouldn't be accessible from any special caller except
120 // itself...
121 try {
122 D.lookup.findSpecial(D.class, "privateRyan", MethodType.methodType(void.class), C.class);
123 System.out.println("findSpecial(privateRyan, C.class) unexpectedly succeeded");
124 } catch (IllegalAccessException expected) {
125 }
126
127 // ... or from any lookup context except its own.
128 try {
129 E.lookup.findSpecial(D.class, "privateRyan", MethodType.methodType(void.class), E.class);
130 System.out.println("findSpecial(privateRyan, E.class) unexpectedly succeeded");
131 } catch (IllegalAccessException expected) {
132 }
133 }
134}
135
136