Skip to content

Commit 74dc50b

Browse files
author
Mandy Chung
committed
8301721: lookup.findSpecial fails on Object method call from interface
Reviewed-by: alanb
1 parent 0f0fda7 commit 74dc50b

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4071,7 +4071,7 @@ private MethodHandle getDirectMethodCommon(byte refKind, Class<?> refc, MemberNa
40714071

40724072
if (refKind == REF_invokeSpecial &&
40734073
refc != lookupClass() &&
4074-
!refc.isInterface() &&
4074+
!refc.isInterface() && !lookupClass().isInterface() &&
40754075
refc != lookupClass().getSuperclass() &&
40764076
refc.isAssignableFrom(lookupClass())) {
40774077
assert(!method.getName().equals(ConstantDescs.INIT_NAME)); // not this code path
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8301721
27+
* @library /test/lib
28+
* @run main FindSpecialObjectMethod
29+
* @summary Test findSpecial on Object methods calling from a class or interface.
30+
*/
31+
32+
import java.lang.invoke.MethodHandle;
33+
import java.lang.invoke.MethodHandles;
34+
import java.util.Objects;
35+
36+
import static java.lang.invoke.MethodType.*;
37+
import static jdk.test.lib.Asserts.*;
38+
39+
public class FindSpecialObjectMethod {
40+
static class C {
41+
public static Object test(C o) throws Throwable {
42+
MethodHandles.Lookup lookup = MethodHandles.lookup();
43+
MethodHandle mh = lookup.findSpecial(Object.class, "toString", methodType(String.class), C.class);
44+
return mh.invoke(o);
45+
}
46+
47+
public String toString() {
48+
return "C";
49+
}
50+
}
51+
52+
interface I {
53+
static Object test(I o) throws Throwable {
54+
MethodHandles.Lookup lookup = MethodHandles.lookup();
55+
MethodHandle mh = lookup.findSpecial(Object.class, "toString", methodType(String.class), I.class);
56+
return mh.invoke(o);
57+
}
58+
59+
static void noAccess() throws Throwable {
60+
try {
61+
MethodHandles.Lookup lookup = MethodHandles.lookup();
62+
MethodHandle mh = lookup.findSpecial(String.class, "hashCode", methodType(int.class), I.class);
63+
throw new RuntimeException("IllegalAccessException not thrown");
64+
} catch (IllegalAccessException ex) {}
65+
}
66+
}
67+
68+
public static void main(String... args) throws Throwable {
69+
// Object.toString can be called from invokespecial from within
70+
// a special caller class C or interface I
71+
C c = new C();
72+
I i = new I() {};
73+
assertEquals(C.test(c), Objects.toIdentityString(c));
74+
assertEquals(I.test(i), Objects.toIdentityString(i));
75+
76+
// I has no access to methods in other class besides Object
77+
I.noAccess();
78+
}
79+
}

0 commit comments

Comments
 (0)