1 /*
2  * Copyright (c) 2013, 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  * @test
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
30  * @run main InterfaceObjectTest
31  */
32 interface IClone extends Cloneable {
finalize()33     void finalize() throws Throwable;
clone()34     Object clone();
35 }
36 
37 interface ICloneExtend extends IClone { }
38 
39 public class InterfaceObjectTest implements ICloneExtend {
40 
clone()41     public Object clone() {
42         System.out.println("In InterfaceObjectTest's clone() method\n");
43         return null;
44     }
45 
finalize()46     public void finalize() throws Throwable {
47         try {
48             System.out.println("In InterfaceObjectTest's finalize() method\n");
49         } catch (Throwable t) {
50             throw new AssertionError(t);
51         }
52     }
53 
tryIt(ICloneExtend o1)54     public static void tryIt(ICloneExtend o1) {
55         try {
56             Object o2 = o1.clone();
57             o1.finalize();
58         } catch (Throwable t) {
59             throw new AssertionError(t);
60         }
61     }
62 
63 
main(String[] args)64     public static void main(String[] args) throws Exception {
65         // Test with abstract public clone() and finalize() methods.
66         InterfaceObjectTest o1 = new InterfaceObjectTest();
67         tryIt(o1);
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 
92     }
93 }
94