author | mchung |
Tue, 13 Aug 2019 15:49:11 -0700 | |
changeset 57735 | 7ba5e49258de |
permissions | -rw-r--r-- |
57735
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
1 |
/* |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
4 |
* |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
8 |
* |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
13 |
* accompanied this code). |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
14 |
* |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
18 |
* |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
21 |
* questions. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
22 |
*/ |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
23 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
24 |
package test; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
25 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
26 |
import java.lang.invoke.MethodHandle; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
27 |
import java.lang.invoke.MethodHandles; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
28 |
import java.lang.invoke.MethodHandles.Lookup; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
29 |
import java.lang.invoke.MethodType; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
30 |
import java.lang.reflect.InvocationTargetException; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
31 |
import java.lang.reflect.Method; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
32 |
import java.util.Comparator; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
33 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
34 |
/* |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
35 |
* java.util.Comparator is in java.base. MyComparator is a Comparator |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
36 |
* subclass and it's in a different module. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
37 |
* |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
38 |
* Test findSpecial and unreflectSpecial with Comparator and MyComparator |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
39 |
* as the special caller. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
40 |
*/ |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
41 |
public class FindSpecial { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
42 |
private static final Lookup LOOKUP = MethodHandles.lookup(); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
43 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
44 |
public static void main(String... args) throws Throwable { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
45 |
findSpecialTest(); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
46 |
unreflectSpecialTest(); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
47 |
reflectMethodInvoke(); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
48 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
49 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
50 |
static void findSpecialTest() throws Throwable { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
51 |
Method m = Comparator.class.getMethod("reversed"); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
52 |
MethodType mt = MethodType.methodType(m.getReturnType(), m.getParameterTypes()); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
53 |
// refc and specialCaller are both in java.base |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
54 |
MethodHandle mh = LOOKUP.findSpecial(Comparator.class, m.getName(), mt, Comparator.class); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
55 |
// refc in java.base, specialCaller in this module |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
56 |
MethodHandle mh1 = LOOKUP.findSpecial(m.getDeclaringClass(), m.getName(), mt, |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
57 |
MyComparator.class); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
58 |
Comparator<Object> cmp = new MyComparator(); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
59 |
// invokespecial to invoke Comparator::reversed. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
60 |
Object o = mh.invoke(cmp); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
61 |
Object o1 = mh1.invoke(cmp); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
62 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
63 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
64 |
static void unreflectSpecialTest() throws Throwable { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
65 |
Method m = Comparator.class.getMethod("reversed"); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
66 |
// refc and specialCaller are both in java.base |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
67 |
MethodHandle mh = LOOKUP.unreflectSpecial(m, Comparator.class); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
68 |
// refc in java.base, specialCaller in this module |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
69 |
MethodHandle mh1 = LOOKUP.unreflectSpecial(m, MyComparator.class); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
70 |
Comparator<Object> cmp = new MyComparator(); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
71 |
// invokespecial to invoke Comparator::reversed. |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
72 |
Object o = mh.invoke(cmp); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
73 |
Object o1 = mh1.invoke(cmp); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
74 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
75 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
76 |
static void reflectMethodInvoke() throws Throwable { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
77 |
Method m = Comparator.class.getMethod("reversed"); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
78 |
try { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
79 |
// invokevirtual dispatch |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
80 |
Object o = m.invoke(new MyComparator()); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
81 |
throw new RuntimeException("should throw an exception"); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
82 |
} catch (InvocationTargetException e) { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
83 |
if (!(e.getCause() instanceof Error && |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
84 |
e.getCause().getMessage().equals("should not reach here"))) { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
85 |
throw e.getCause(); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
86 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
87 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
88 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
89 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
90 |
static class MyComparator implements Comparator<Object> { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
91 |
public int compare(Object o1, Object o2) { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
92 |
return 0; |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
93 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
94 |
|
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
95 |
@Override |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
96 |
public Comparator<Object> reversed() { |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
97 |
throw new Error("should not reach here"); |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
98 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
99 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
100 |
} |
7ba5e49258de
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents:
diff
changeset
|
101 |