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 if (!ve.getMessage().startsWith("Illegal use of putfield on a strict field")) { 46 throw new Error("Wrong VerifyError thrown", ve); 47 } else { 48 System.out.println("Expected VerifyError was thrown"); 49 } 50 } 51 52 // Now a bad class that tries to write to a super class's strict field 53 // in the preinit phase 54 try { 55 c = Class.forName("BadStrictSubPreInit"); 56 throw new Error("VerifyError was not thrown as expected!"); 57 } catch (VerifyError ve) { 58 if (!ve.getMessage().startsWith("Bad type on operand stack")) { 59 throw new Error("Wrong VerifyError thrown", ve); 60 } else { 61 System.out.println("Expected VerifyError was thrown"); 62 } 63 } 64 65 // Now a bad class that tries to write to a super class's strict field 66 // in the post phase. This is not a verification error but we test it 67 // here for completeness.Expected exception: 68 // java.lang.IllegalAccessError: Update to non-static final field 69 // BadStrictSubPostInit.x attempted from a different class 70 // (BadStrictSubPostInit) than the field's declaring class 71 try { 72 c = Class.forName("BadStrictSubPostInit"); 73 Object o = c.newInstance(); 74 throw new Error("IllegalAccessErrorError was not thrown as expected!"); 75 } catch (IllegalAccessError iae) { 76 if (!iae.getMessage().startsWith("Update to non-static final field")) { 77 throw new Error("Wrong IllegalAccessError thrown", iae); 78 } else { 79 System.out.println("Expected IllegalAccessError was thrown"); 80 } 81 } 82 83 } 84 }