1 /*
  2  * Copyright (c) 2022, 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 /*
 26  * @test
 27  * @summary test that the JVM correctly accepts or rejects classes based on their
 28  *          ACC_VALUE/ACC_IDENTITY modifiers
 29  * @library /test/lib
 30  * @compile NeutralInterface.java ValueInterface.java IdentityInterface.java
 31  * @compile NeutralAbstract.java ValueAbstract.java IdentityAbstract.java AbstractWithField.java AbstractWithSynchMethod.java
 32  * @compile ClassesWithInvalidModifiers.jcod
 33  * @run main/othervm -XX:+EnableValhalla -XX:-EnablePrimitiveClasses -verify TestClassModifiers
 34  */
 35 
 36 import jdk.test.lib.Asserts;
 37 
 38  public class TestClassModifiers {
 39   static value class VC0 {}
 40   static value class VC1 implements NeutralInterface { }
 41   static value class VC2 implements ValueInterface { }
 42   static value class VC3 extends NeutralAbstract { }
 43   static value class VC4 extends ValueAbstract { }
 44   static value class VC5 extends NeutralAbstract implements NeutralInterface { }
 45   static value class VC6 extends NeutralAbstract implements ValueInterface { }
 46   static value class VC7 extends ValueAbstract implements NeutralInterface { }
 47   static value class VC8 extends ValueAbstract implements ValueInterface { }
 48 
 49   static identity class IC0 { }
 50   static identity class IC1 implements NeutralInterface { }
 51   static identity class IC2 implements IdentityInterface { }
 52   static identity class IC3 extends NeutralAbstract { }
 53   static identity class IC4 extends IdentityAbstract { }
 54   static identity class IC5 extends AbstractWithField { }
 55   static identity class IC6 extends AbstractWithSynchMethod { }
 56   static identity class IC7 extends NeutralAbstract implements NeutralInterface { }
 57   static identity class IC8 extends NeutralAbstract implements IdentityInterface { }
 58   static identity class IC9 extends IdentityAbstract implements NeutralInterface { }
 59   static identity class IC10 extends IdentityAbstract implements IdentityInterface { }
 60 
 61 
 62   static abstract class AC0 extends AbstractWithField implements NeutralInterface { }
 63   static abstract class AC1 extends AbstractWithField implements IdentityInterface { }
 64   static abstract class AC2 extends AbstractWithSynchMethod implements NeutralInterface { }
 65   static abstract class AC3 extends AbstractWithSynchMethod implements IdentityInterface { }
 66 
 67   static String[] validClasses = {"VC0", "VC1", "VC2", "VC3", "VC4", "VC5", "VC6", "VC7", "VC8",
 68                                   "IC0", "IC1", "IC2", "IC3", "IC4", "IC5", "IC6", "IC7", "IC8", "IC9", "IC10",
 69                                   "AC0", "AC1", "AC2", "AC3"};
 70 
 71   static String[] invalidClassesWithICCE = {"ValueClassExtendingIdentityClass", "ValueClassExtendingAbstractClassWithField",
 72                                     "ValueClassExtendingAbstractClassWithSynchMethod", "ValueClassImplementingIdentityInterface",
 73                                     "IdentityClassExtendingValueClass", "IdentityClassImplementingValueInterface",
 74                                     "AbstractClassWithFieldExtendingValueClass", "AbstractClassWithFieldImplementingValueInterface"};
 75   static String[] invalidClassesWithCFE = {"AbstractClassWithFieldWithNoIdentityModifier", "AbstractClassWithSynchMethodWithNoIdentityModifier",
 76                                            "AbstractClassWithBothModifiers", "ConcreteClassWithNoModifiers"};
 77 
 78   public static void main(String[] args) throws Exception {
 79     // testing valid cases first
 80     try {
 81       for (String name : validClasses) {
 82         System.out.println("Trying to load "+name);
 83         Class c = Class.forName("TestClassModifiers$"+name);
 84       }
 85     } catch(Throwable t) {
 86       t.printStackTrace();
 87       throw t;
 88     }
 89 
 90     // Testing invalid cases
 91     for (String name: invalidClassesWithICCE) {
 92       boolean icce = false;
 93       boolean otherException = false;
 94       Throwable exception = null;
 95       try {
 96         System.out.println("Trying to load "+name);
 97         Class c = Class.forName(name);
 98       } catch (IncompatibleClassChangeError e) {
 99         icce = true;
100       } catch (Throwable t) {
101         otherException = true;
102         exception = t;
103       }
104       Asserts.assertFalse(otherException, "Unexpected exception "+exception);
105       Asserts.assertTrue(icce, "IncompatibleClassChangeError not thrown");
106     }
107     for (String name: invalidClassesWithCFE) {
108       boolean cfe = false;
109       boolean otherException = false;
110       Throwable exception = null;
111       try {
112         System.out.println("Trying to load "+name);
113         Class c = Class.forName(name);
114       } catch (ClassFormatError e) {
115         cfe = true;
116       } catch (Throwable t) {
117         otherException = true;
118         exception = t;
119       }
120       Asserts.assertFalse(otherException, "Unexpected exception "+exception);
121       Asserts.assertTrue(cfe, "ClassFormatError not thrown");
122     }
123   }
124  }