Skip to content

Commit 5732032

Browse files
author
Harold Seigel
committedAug 14, 2020
8251414: Add test that invokeinterface of a protected method in java.lang.Object throws NoSuchMethodError
Add the missing test cases to existing test InterfaceObjectTest.java Reviewed-by: lfoltan, coleenp
1 parent a963aab commit 5732032

File tree

3 files changed

+118
-10
lines changed

3 files changed

+118
-10
lines changed
 

‎src/hotspot/share/interpreter/linkResolver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
339339

340340
// JDK 8, JVMS 5.4.3.4: Interface method resolution should
341341
// ignore static and non-public methods of java.lang.Object,
342-
// like clone, finalize, registerNatives.
342+
// like clone and finalize.
343343
if (in_imethod_resolve &&
344344
result != NULL &&
345345
ik->is_interface() &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2020, 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+
// This jasm file implements the following java code:
26+
interface I { }
27+
28+
public class InterfaceObj implements I {
29+
static void f(I intf) throws Throwable {
30+
I.finalize();
31+
}
32+
public static void testFinalize() throws Throwable {
33+
f(new InterfaceObj());
34+
}
35+
static void c(I intf) throws Throwable {
36+
I.clone();
37+
}
38+
public static void testClone() throws Throwable {
39+
c(new InterfaceObj());
40+
}
41+
}
42+
*/
43+
44+
public interface I version 60:0 { } // end Class I
45+
46+
47+
super public class InterfaceObj implements I version 60:0 {
48+
49+
50+
public Method "<init>":"()V" stack 1 locals 1 {
51+
aload_0;
52+
invokespecial Method java/lang/Object."<init>":"()V";
53+
return;
54+
}
55+
56+
static Method f:"(LI;)V" throws java/lang/Throwable stack 1 locals 1 {
57+
aload_0;
58+
invokeinterface InterfaceMethod I.finalize:"()V", 1;
59+
return;
60+
}
61+
62+
public static Method testFinalize:"()V" throws java/lang/Throwable stack 2 locals 1 {
63+
new class InterfaceObj;
64+
dup;
65+
invokespecial Method "<init>":"()V";
66+
invokestatic Method f:"(LI;)V";
67+
return;
68+
}
69+
70+
static Method c:"(LI;)V" throws java/lang/Throwable stack 1 locals 1 {
71+
aload_0;
72+
invokeinterface InterfaceMethod I.clone:"()Ljava/lang/Object;", 1;
73+
return;
74+
}
75+
76+
public static Method testClone:"()V" throws java/lang/Throwable stack 2 locals 1 {
77+
new class InterfaceObj;
78+
dup;
79+
invokespecial Method "<init>":"()V";
80+
invokestatic Method c:"(LI;)V";
81+
return;
82+
}
83+
84+
} // end Class InterfaceObj

‎test/hotspot/jtreg/runtime/8026394/InterfaceObjectTest.java ‎test/hotspot/jtreg/runtime/linkResolver/InterfaceObjectTest.java

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,10 @@
2323

2424
/*
2525
* @test
26-
* @bug 8026394
27-
* @summary clone() and finalize() interface resolution should not receive IAE
26+
* @bug 8026394 8251414
27+
* @summary test interface resolution when clone and finalize are declared abstract within
28+
* an interface and when they are not
29+
* @compile InterfaceObj.jasm
2830
* @run main InterfaceObjectTest
2931
*/
3032
interface IClone extends Cloneable {
@@ -54,16 +56,38 @@ public static void tryIt(ICloneExtend o1) {
5456
Object o2 = o1.clone();
5557
o1.finalize();
5658
} catch (Throwable t) {
57-
if (t instanceof IllegalAccessError) {
58-
System.out.println("TEST FAILS - IAE resulted\n");
59-
System.exit(1);
60-
}
59+
throw new AssertionError(t);
6160
}
6261
}
6362

64-
public static void main(String[] args) {
63+
64+
public static void main(String[] args) throws Exception {
65+
// Test with abstract public clone() and finalize() methods.
6566
InterfaceObjectTest o1 = new InterfaceObjectTest();
6667
tryIt(o1);
67-
System.out.println("TEST PASSES - no IAE resulted\n");
68+
69+
70+
// Test with reflection without abstract public clone() and finalize() methods.
71+
Class cls = Class.forName("InterfaceObj");
72+
try {
73+
java.lang.reflect.Method m = cls.getMethod("testFinalize");
74+
m.invoke(cls);
75+
throw new RuntimeException("Failed to throw NoSuchMethodError for finalize()");
76+
} catch (java.lang.reflect.InvocationTargetException e) {
77+
if (!e.getCause().toString().contains("NoSuchMethodError")) {
78+
throw new RuntimeException("wrong ITE: " + e.getCause().toString());
79+
}
80+
}
81+
82+
try {
83+
java.lang.reflect.Method m = cls.getMethod("testClone");
84+
m.invoke(cls);
85+
throw new RuntimeException("Failed to throw NoSuchMethodError for clone()");
86+
} catch (java.lang.reflect.InvocationTargetException e) {
87+
if (!e.getCause().toString().contains("NoSuchMethodError")) {
88+
throw new RuntimeException("wrong ITE: " + e.getCause().toString());
89+
}
90+
}
91+
6892
}
6993
}

0 commit comments

Comments
 (0)
Please sign in to comment.