1 /*
 2  * Copyright (c) 2018, 2024, 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  * @ignore Fix JDK-8328416
26  * @test
27  * @summary test that the right exceptions get thrown for bad inline type
28  *          class files.
29  * @enablePreview
30  * @compile cfpTests.jcod
31  * @run main/othervm -Xverify:remote BadInlineTypes
32  */
33 
34 public class BadInlineTypes {
35 
36     public static void runTest(String test_name, String message) throws Exception {
37         System.out.println("Testing: " + test_name);
38         try {
39             Class newClass = Class.forName(test_name);
40         } catch (java.lang.ClassFormatError e) {
41             if (!e.getMessage().contains(message)) {
42                 throw new RuntimeException( "Wrong ClassFormatError: " + e.getMessage());
43             }
44         }
45     }
46 
47     public static void main(String[] args) throws Exception {
48 
49         // Test that ACC_PRIMITIVE with ACC_ABSTRACT is illegal.
50         runTest("ValueAbstract", "Illegal class modifiers in class ValueAbstract");
51 
52         // Test that ACC_PRIMITIVE with ACC_ENUM is illegal.
53         runTest("ValueEnum", "Illegal class modifiers in class ValueEnum");
54 
55         // Test that inline type fields must be final.
56         runTest("ValueFieldNotFinal", "Illegal field modifiers in class ValueFieldNotFinal");
57 
58         // Test that ACC_PRIMITIVE with ACC_INTERFACE is illegal.
59         runTest("ValueInterface", "Illegal class modifiers in class ValueInterface");
60 
61         // Test that inline type instance methods cannot be synchronized.
62         runTest("ValueMethodSynch",
63                 "Method getInt in class ValueMethodSynch (not an identity class) has illegal modifiers: 0x20");
64 
65         // Test that a class with an old class file version cannot contain a Q signature.
66         runTest("OldClassWithQSig",
67                 "Field \"this\" in class OldClassWithQSig has illegal signature \"QOldClassWithQSig;\"");
68 
69         // Test that a class with an old class file version cannot contain an array Q signature.
70         runTest("OldClassWithQArraySig",
71                 "Field \"ia\" in class OldClassWithQArraySig has illegal signature \"[Qjava/lang/Integer;\"");
72 
73         // Test that ClassCircularityError gets detected for instance fields.
74         System.out.println("Testing ClassCircularityError for instance fields");
75         try {
76             Class newClass = Class.forName("Circ");
77             throw new RuntimeException( "java.lang.ClassCircularityError exception not thrown!");
78         } catch (java.lang.ClassCircularityError e) {
79              if (!e.getMessage().contains("Circ")) {
80                  throw new RuntimeException( "Wrong ClassCircularityError: " + e.getMessage());
81              }
82          }
83 
84         // Test that ClassCircularityError isn't detected for static fields.
85         System.out.println("Testing ClassCircularityError for static fields");
86         try {
87             Class newClass = Class.forName("CircStaticB");
88         } catch (java.lang.ClassCircularityError e) {
89              throw new RuntimeException( "java.lang.ClassCircularityError exception thrown!");
90          }
91 
92         runTest("ValueCloneable", "Inline Types do not support Cloneable");
93 
94         runTest("SuperIsZero", "Invalid superclass index 0 in class file SuperIsZero");
95 
96         runTest("QInOldClass",
97                 "Class name contains illegal Q-signature in descriptor in class file QInOldClass");
98     }
99 }