HasFinalizableSubclassTest.java revision 9111:a41fe5ffa839
1109998Smarkm/*
2109998Smarkm * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3109998Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4109998Smarkm *
5109998Smarkm * This code is free software; you can redistribute it and/or modify it
6109998Smarkm * under the terms of the GNU General Public License version 2 only, as
7109998Smarkm * published by the Free Software Foundation.
8109998Smarkm *
9109998Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT
10296341Sdelphij * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11109998Smarkm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12109998Smarkm * version 2 for more details (a copy is included in the LICENSE file that
13109998Smarkm * accompanied this code).
14109998Smarkm *
15109998Smarkm * You should have received a copy of the GNU General Public License version
16109998Smarkm * 2 along with this work; if not, write to the Free Software Foundation,
17109998Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18109998Smarkm *
19109998Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20109998Smarkm * or visit www.oracle.com if you need additional information or have any
21109998Smarkm * questions.
22109998Smarkm */
23109998Smarkm
24109998Smarkm/*
25109998Smarkm * @test
26109998Smarkm * @bug 8136421
27109998Smarkm * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
28109998Smarkm * @library / /testlibrary /../../test/lib
29109998Smarkm * @compile ../common/CompilerToVMHelper.java
30109998Smarkm * @build compiler.jvmci.compilerToVM.HasFinalizableSubclassTest
31109998Smarkm * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
32109998Smarkm * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockExperimentalVMOptions
33109998Smarkm *     -XX:+EnableJVMCI compiler.jvmci.compilerToVM.HasFinalizableSubclassTest
34109998Smarkm */
35109998Smarkm
36109998Smarkmpackage compiler.jvmci.compilerToVM;
37109998Smarkm
38109998Smarkmimport compiler.jvmci.common.testcases.AbstractClass;
39109998Smarkmimport compiler.jvmci.common.testcases.AbstractClassExtender;
40109998Smarkmimport compiler.jvmci.common.testcases.DoNotImplementInterface;
41109998Smarkmimport compiler.jvmci.common.testcases.MultipleImplementer1;
42109998Smarkmimport compiler.jvmci.common.testcases.MultipleImplementersInterface;
43109998Smarkmimport compiler.jvmci.common.testcases.SingleImplementerInterface;
44109998Smarkmimport java.util.HashSet;
45109998Smarkmimport java.util.Set;
46109998Smarkmimport java.util.stream.Stream;
47109998Smarkmimport jdk.vm.ci.hotspot.CompilerToVMHelper;
48109998Smarkmimport jdk.vm.ci.hotspot.HotSpotResolvedObjectTypeImpl;
49109998Smarkmimport jdk.test.lib.Asserts;
50109998Smarkmimport jdk.test.lib.Utils;
51109998Smarkm
52109998Smarkmpublic class HasFinalizableSubclassTest {
53109998Smarkm    public static void main(String args[]) {
54109998Smarkm        HasFinalizableSubclassTest test = new HasFinalizableSubclassTest();
55160814Ssimon        for (TestCase tcase : createTestCases()) {
56160814Ssimon            test.runTest(tcase);
57296341Sdelphij        }
58160814Ssimon    }
59160814Ssimon
60109998Smarkm    private static Set<TestCase> createTestCases() {
61109998Smarkm        Stream.of(
62109998Smarkm                    AbstractClassExtender.class,
63109998Smarkm                    SingleImplementerInterface.class,
64109998Smarkm                    MultipleImplementersInterface.class,
65296341Sdelphij                    MultipleImplementer1.class,
66296341Sdelphij                    DoNotImplementInterface.class)
67296341Sdelphij                .forEach(Utils::ensureClassIsLoaded);
68296341Sdelphij        Set<TestCase> result = new HashSet<>();
69296341Sdelphij        // iface with finalize method
70109998Smarkm        result.add(new TestCase(SingleImplementerInterface.class, false));
71296341Sdelphij        // iface with default finalize method
72296341Sdelphij        result.add(new TestCase(MultipleImplementersInterface.class, false));
73109998Smarkm        // class which implements iface w/ default finalize method
74109998Smarkm        result.add(new TestCase(MultipleImplementer1.class, true));
75296341Sdelphij        // abstract class with finalizeable subclass
76296341Sdelphij        result.add(new TestCase(AbstractClass.class, true));
77109998Smarkm        // non-implemented iface
78109998Smarkm        result.add(new TestCase(DoNotImplementInterface.class, false));
79296341Sdelphij        return result;
80296341Sdelphij    }
81109998Smarkm
82160814Ssimon    private void runTest(TestCase tcase) {
83296341Sdelphij        System.out.println(tcase);
84296341Sdelphij        HotSpotResolvedObjectTypeImpl metaspaceKlass = CompilerToVMHelper
85160814Ssimon                .lookupType(Utils.toJVMTypeSignature(tcase.aClass),
86160814Ssimon                        getClass(), /* resolve = */ true);
87296341Sdelphij        Asserts.assertEQ(tcase.expected,
88296341Sdelphij                CompilerToVMHelper.hasFinalizableSubclass(metaspaceKlass),
89160814Ssimon                        "Unexpected finalizableSubclass state for "
90296341Sdelphij                                + tcase.aClass.getName());
91296341Sdelphij    }
92296341Sdelphij
93296341Sdelphij    private static class TestCase {
94296341Sdelphij        public final Class<?> aClass;
95296341Sdelphij        public final boolean expected;
96296341Sdelphij
97296341Sdelphij        public TestCase(Class<?> clazz, boolean expected) {
98296341Sdelphij            this.aClass = clazz;
99296341Sdelphij            this.expected = expected;
100109998Smarkm        }
101109998Smarkm        @Override
102109998Smarkm        public String toString() {
103109998Smarkm            return "CASE: class= " + aClass.getName() + ", expected=" + expected;
104296341Sdelphij        }
105296341Sdelphij    }
106296341Sdelphij}
107296341Sdelphij