1 /* 2 * Copyright (c) 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 * @test 26 * @enablePreview 27 * @compile strictFields.jasm 28 * @run main/othervm -Xverify:remote StrictFields 29 */ 30 31 public class StrictFields { 32 public static void main(String[] args) throws Throwable { 33 34 // First load a class where strict should be ignored 35 Class<?> c = Class.forName("StrictIgnore"); 36 37 // Now load a well-formed strict-using value class 38 c = Class.forName("StrictBase"); 39 40 // Now a bad class 41 try { 42 c = Class.forName("PostInitStrict"); 43 throw new Error("VerifyError was not thrown as expected!"); 44 } catch (VerifyError ve) { 45 // Once strict non-final is possible, expect "Illegal use of putfield on a strict field" 46 if (!ve.getMessage().startsWith("All strict final fields must be initialized before super()")) { 47 throw new Error("Wrong VerifyError thrown", ve); 48 } else { 49 System.out.println("Expected VerifyError was thrown"); 50 } 51 } 52 53 // Now a bad class that tries to write to a super class's strict field 54 // in the preinit phase 55 try { 56 c = Class.forName("BadStrictSubPreInit"); 57 throw new Error("VerifyError was not thrown as expected!"); 58 } catch (VerifyError ve) { 59 if (!ve.getMessage().startsWith("Bad type on operand stack")) { 60 throw new Error("Wrong VerifyError thrown", ve); 61 } else { 62 System.out.println("Expected VerifyError was thrown"); 63 } 64 } 65 66 // Now a bad class that tries to write to a super class's strict field 67 // in the post phase. This is not a verification error but we test it 68 // here for completeness.Expected exception: 69 // java.lang.IllegalAccessError: Update to non-static final field 70 // BadStrictSubPostInit.x attempted from a different class 71 // (BadStrictSubPostInit) than the field's declaring class 72 try { 73 c = Class.forName("BadStrictSubPostInit"); 74 Object o = c.newInstance(); 75 throw new Error("IllegalAccessErrorError was not thrown as expected!"); 76 } catch (IllegalAccessError iae) { 77 if (!iae.getMessage().startsWith("Update to non-static final field")) { 78 throw new Error("Wrong IllegalAccessError thrown", iae); 79 } else { 80 System.out.println("Expected IllegalAccessError was thrown"); 81 } 82 } 83 84 } 85 }