1 /* 2 * Copyright (c) 2020, 2021, 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 8225056 27 * @compile GetPermittedSubclasses.jcod 28 * @compile noSubclass/BaseC.java noSubclass/BaseI.java noSubclass/Impl1.java 29 * @compile noSubclass/Impl2.java 30 * @run main GetPermittedSubclassesTest 31 */ 32 33 import java.util.ArrayList; 34 35 // Test Class GetPermittedSubtpes() and Class.isSealed() APIs. 36 public class GetPermittedSubclassesTest { 37 38 sealed class Sealed1 permits Sub1 {} 39 40 final class Sub1 extends Sealed1 implements SealedI1 {} 41 42 sealed interface SealedI1 permits NotSealed, Sub1, Extender {} 43 44 non-sealed interface Extender extends SealedI1 { } 45 46 final class FinalC implements Extender {} 47 48 final class NotSealed implements SealedI1 {} 49 50 final class Final4 {} 51 testSealedInfo(Class<?> c, String[] expected, boolean isSealed)52 public static void testSealedInfo(Class<?> c, String[] expected, boolean isSealed) { 53 var permitted = c.getPermittedSubclasses(); 54 55 if (isSealed) { 56 if (permitted.length != expected.length) { 57 throw new RuntimeException( 58 "Unexpected number of permitted subclasses for: " + c.toString() + 59 "(" + java.util.Arrays.asList(permitted)); 60 } 61 62 if (!c.isSealed()) { 63 throw new RuntimeException("Expected sealed class: " + c.toString()); 64 } 65 66 // Create ArrayList of permitted subclasses class names. 67 ArrayList<String> permittedNames = new ArrayList<String>(); 68 for (int i = 0; i < permitted.length; i++) { 69 permittedNames.add(permitted[i].getName()); 70 } 71 72 if (permittedNames.size() != expected.length) { 73 throw new RuntimeException( 74 "Unexpected number of permitted names for: " + c.toString()); 75 } 76 77 // Check that expected class names are in the permitted subclasses list. 78 for (int i = 0; i < expected.length; i++) { 79 if (!permittedNames.contains(expected[i])) { 80 throw new RuntimeException( 81 "Expected class not found in permitted subclases list, super class: " + 82 c.getName() + ", expected class: " + expected[i]); 83 } 84 } 85 } else { 86 // Must not be sealed. 87 if (c.isSealed() || permitted != null) { 88 throw new RuntimeException("Unexpected sealed class: " + c.toString()); 89 } 90 } 91 } 92 testBadSealedClass(String className, Class<?> expectedException, String expectedCFEMessage)93 public static void testBadSealedClass(String className, 94 Class<?> expectedException, 95 String expectedCFEMessage) throws Throwable { 96 try { 97 Class.forName(className); 98 throw new RuntimeException("Expected ClassFormatError exception not thrown for " + className); 99 } catch (ClassFormatError cfe) { 100 if (ClassFormatError.class != expectedException) { 101 throw new RuntimeException( 102 "Class " + className + " got unexpected exception: " + cfe.getMessage()); 103 } 104 if (!cfe.getMessage().contains(expectedCFEMessage)) { 105 throw new RuntimeException( 106 "Class " + className + " got unexpected ClassFormatError exception: " + cfe.getMessage()); 107 } 108 } catch (IncompatibleClassChangeError icce) { 109 if (IncompatibleClassChangeError.class != expectedException) { 110 throw new RuntimeException( 111 "Class " + className + " got unexpected exception: " + icce.getMessage()); 112 } 113 if (!icce.getMessage().contains(expectedCFEMessage)) { 114 throw new RuntimeException( 115 "Class " + className + " got unexpected IncompatibleClassChangeError exception: " + icce.getMessage()); 116 } 117 } 118 } 119 main(String... args)120 public static void main(String... args) throws Throwable { 121 testSealedInfo(SealedI1.class, new String[] {"GetPermittedSubclassesTest$NotSealed", 122 "GetPermittedSubclassesTest$Sub1", 123 "GetPermittedSubclassesTest$Extender"}, 124 true); 125 126 testSealedInfo(Sealed1.class, new String[] {"GetPermittedSubclassesTest$Sub1"}, true); 127 testSealedInfo(Final4.class, null, false); 128 testSealedInfo(NotSealed.class, null, false); 129 130 // Test class with PermittedSubclasses attribute but old class file version. 131 testSealedInfo(OldClassFile.class, null, false); 132 133 // Test class with an empty PermittedSubclasses attribute. 134 testSealedInfo(NoSubclasses.class, new String[]{}, true); 135 136 // Test that a class with an empty PermittedSubclasses attribute cannot be subclass-ed. 137 testBadSealedClass("SubClass", IncompatibleClassChangeError.class, 138 "SubClass cannot inherit from sealed class NoSubclasses"); 139 140 // Test returning only names of existing classes. 141 testSealedInfo(NoLoadSubclasses.class, new String[]{"ExistingClassFile" }, true); 142 143 // Test that loading a class with a corrupted PermittedSubclasses attribute 144 // causes a ClassFormatError. 145 testBadSealedClass("BadPermittedAttr", ClassFormatError.class, 146 "Permitted subclass class_info_index 15 has bad constant type"); 147 148 // Test that loading a sealed final class with a PermittedSubclasses 149 // attribute causes a ClassFormatError. 150 testBadSealedClass("SealedButFinal", ClassFormatError.class, 151 "PermittedSubclasses attribute in final class"); 152 153 // Test that loading a sealed class with an ill-formed class name in its 154 // PermittedSubclasses attribute causes a ClassFormatError. 155 testBadSealedClass("BadPermittedSubclassEntry", ClassFormatError.class, 156 "Illegal class name \"iDont;;Exist\" in class file"); 157 158 // Test that loading a sealed class with an empty class name in its PermittedSubclasses 159 // attribute causes a ClassFormatError. 160 testBadSealedClass("EmptyPermittedSubclassEntry", ClassFormatError.class, 161 "Illegal class name \"\" in class file"); 162 163 //test type enumerated in the PermittedSubclasses attribute, 164 //which are not direct subtypes of the current class are not returned: 165 testSealedInfo(noSubclass.BaseC.class, new String[] {"noSubclass.ImplCIntermediate"}, true); 166 testSealedInfo(noSubclass.BaseI.class, new String[] {"noSubclass.ImplIIntermediateI", "noSubclass.ImplIIntermediateC"}, true); 167 } 168 } 169