blob: 72ed6b5533b22b6039d260ef6dbd0f5a2217a41f [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();
Narayan Kamath3e0dce02016-10-31 13:55:55 +000060 testExceptionDetailMessages();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +010061 }
62
63 public static void testfindSpecial_invokeSuperBehaviour() throws Throwable {
64 // This is equivalent to an invoke-super instruction where the referrer
65 // is B.class.
66 MethodHandle mh1 = B.lookup.findSpecial(A.class /* refC */, "foo",
67 MethodType.methodType(void.class), B.class /* specialCaller */);
68
69 A aInstance = new A();
70 B bInstance = new B();
71 C cInstance = new C();
72
73 // This should be as if an invoke-super was called from one of B's methods.
74 mh1.invokeExact(bInstance);
75 mh1.invoke(bInstance);
76
77 // This should not work. The receiver type in the handle will be suitably
78 // restricted to B and subclasses.
79 try {
80 mh1.invoke(aInstance);
81 System.out.println("mh1.invoke(aInstance) should not succeeed");
82 } catch (ClassCastException expected) {
83 }
84
85 try {
86 mh1.invokeExact(aInstance);
87 System.out.println("mh1.invoke(aInstance) should not succeeed");
88 } catch (WrongMethodTypeException expected) {
89 } catch (ClassCastException workaround) {
90 // TODO(narayan): ART treats all invokes as if they were non-exact. We
91 // should throw a WMTE if we execute an invoke-polymorphic instruction whose
92 // target method is MethodHandle.invokeExact.
93 }
94
95 // This should *still* be as if an invoke-super was called from one of C's
96 // methods, despite the fact that we're operating on a C.
97 mh1.invoke(cInstance);
98
99 // Now that C is the special caller, the next invoke will call B.foo.
100 MethodHandle mh2 = C.lookup.findSpecial(A.class /* refC */, "foo",
101 MethodType.methodType(void.class), C.class /* specialCaller */);
102 mh2.invokeExact(cInstance);
103
104 // Shouldn't allow invoke-super semantics from an unrelated special caller.
105 try {
106 C.lookup.findSpecial(A.class, "foo",
107 MethodType.methodType(void.class), D.class /* specialCaller */);
108 System.out.println("findSpecial(A.class, foo, .. D.class) unexpectedly succeeded.");
109 } catch (IllegalAccessException expected) {
110 }
111 }
112
113 public static void testfindSpecial_invokeDirectBehaviour() throws Throwable {
114 D dInstance = new D();
115
116 MethodHandle mh3 = D.lookup.findSpecial(D.class, "privateRyan",
117 MethodType.methodType(void.class), D.class /* specialCaller */);
118 mh3.invoke(dInstance);
119
120 // The private method shouldn't be accessible from any special caller except
121 // itself...
122 try {
123 D.lookup.findSpecial(D.class, "privateRyan", MethodType.methodType(void.class), C.class);
124 System.out.println("findSpecial(privateRyan, C.class) unexpectedly succeeded");
125 } catch (IllegalAccessException expected) {
126 }
127
128 // ... or from any lookup context except its own.
129 try {
130 E.lookup.findSpecial(D.class, "privateRyan", MethodType.methodType(void.class), E.class);
131 System.out.println("findSpecial(privateRyan, E.class) unexpectedly succeeded");
132 } catch (IllegalAccessException expected) {
133 }
134 }
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000135
136 public static void testExceptionDetailMessages() throws Throwable {
137 MethodHandle handle = MethodHandles.lookup().findVirtual(String.class, "concat",
138 MethodType.methodType(String.class, String.class));
139
140 try {
141 handle.invokeExact("a", new Object());
142 System.out.println("invokeExact(\"a\", new Object()) unexpectedly succeeded.");
143 } catch (WrongMethodTypeException ex) {
144 System.out.println("Received exception: " + ex.getMessage());
145 }
146 }
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100147}
148
149