Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Field/4490864/StaticFieldTest.java
38889 views
/*1* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@bug 449086426@summary Verify reflective static field accesses (sanity check)27@author Kenneth Russell28*/2930import java.lang.reflect.*;3132public class StaticFieldTest {33private static byte byteField;34private static short shortField;35private static char charField;36private static int intField;37private static long longField;38private static float floatField;39private static double doubleField;40private static String stringField;4142private static Field getAccessibleField(String name) throws NoSuchFieldException {43Field f = StaticFieldTest.class.getDeclaredField(name);44f.setAccessible(true);45return f;46}4748public static void main(String[] args) throws Exception {49Field byteField = getAccessibleField("byteField");50Field shortField = getAccessibleField("shortField");51Field charField = getAccessibleField("charField");52Field intField = getAccessibleField("intField");53Field longField = getAccessibleField("longField");54Field floatField = getAccessibleField("floatField");55Field doubleField = getAccessibleField("doubleField");56Field stringField = getAccessibleField("stringField");5758byteField.setByte (null, (byte) 77);59shortField.setShort (null, (short) 77);60charField.setChar (null, (char) 77);61intField.setInt (null, (int) 77);62longField.setLong (null, (long) 77);63floatField.setFloat (null, (float) 77);64doubleField.setDouble(null, (double) 77);65String myString = "Hello, world";66stringField.set (null, myString);6768if (byteField.getByte(null) != 77) throw new RuntimeException("Test failed");69if (shortField.getShort(null) != 77) throw new RuntimeException("Test failed");70if (charField.getChar(null) != 77) throw new RuntimeException("Test failed");71if (intField.getInt(null) != 77) throw new RuntimeException("Test failed");72if (longField.getLong(null) != 77) throw new RuntimeException("Test failed");73if (floatField.getFloat(null) != 77) throw new RuntimeException("Test failed");74if (doubleField.getDouble(null) != 77) throw new RuntimeException("Test failed");75if (stringField.get(null) != myString) throw new RuntimeException("Test failed");7677// Test passed.78}79}808182