blob: 58ba55d6b335f4de7b9e2ee610d07b8c9065a261 [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) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +010089 }
90
91 // This should *still* be as if an invoke-super was called from one of C's
92 // methods, despite the fact that we're operating on a C.
93 mh1.invoke(cInstance);
94
95 // Now that C is the special caller, the next invoke will call B.foo.
96 MethodHandle mh2 = C.lookup.findSpecial(A.class /* refC */, "foo",
97 MethodType.methodType(void.class), C.class /* specialCaller */);
98 mh2.invokeExact(cInstance);
99
100 // Shouldn't allow invoke-super semantics from an unrelated special caller.
101 try {
102 C.lookup.findSpecial(A.class, "foo",
103 MethodType.methodType(void.class), D.class /* specialCaller */);
104 System.out.println("findSpecial(A.class, foo, .. D.class) unexpectedly succeeded.");
105 } catch (IllegalAccessException expected) {
106 }
107 }
108
109 public static void testfindSpecial_invokeDirectBehaviour() throws Throwable {
110 D dInstance = new D();
111
112 MethodHandle mh3 = D.lookup.findSpecial(D.class, "privateRyan",
113 MethodType.methodType(void.class), D.class /* specialCaller */);
114 mh3.invoke(dInstance);
115
116 // The private method shouldn't be accessible from any special caller except
117 // itself...
118 try {
119 D.lookup.findSpecial(D.class, "privateRyan", MethodType.methodType(void.class), C.class);
120 System.out.println("findSpecial(privateRyan, C.class) unexpectedly succeeded");
121 } catch (IllegalAccessException expected) {
122 }
123
124 // ... or from any lookup context except its own.
125 try {
126 E.lookup.findSpecial(D.class, "privateRyan", MethodType.methodType(void.class), E.class);
127 System.out.println("findSpecial(privateRyan, E.class) unexpectedly succeeded");
128 } catch (IllegalAccessException expected) {
129 }
130 }
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000131
132 public static void testExceptionDetailMessages() throws Throwable {
133 MethodHandle handle = MethodHandles.lookup().findVirtual(String.class, "concat",
134 MethodType.methodType(String.class, String.class));
135
136 try {
137 handle.invokeExact("a", new Object());
138 System.out.println("invokeExact(\"a\", new Object()) unexpectedly succeeded.");
139 } catch (WrongMethodTypeException ex) {
140 System.out.println("Received exception: " + ex.getMessage());
141 }
142 }
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100143}
144
145