Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Field/Set.java
38828 views
/*1* Copyright (c) 1999, 2004, 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 4250960 504441226* @summary Should not be able to set final fields through reflection unless setAccessible(true) passes and is not static27* @author David Bowen (modified by Doug Lea)28*/2930import java.lang.reflect.*;3132public class Set {33public static void main(String[] argv) throws Throwable {34boolean failed = false;35Test t = new Test();36if (!t.testPrimitive()) {37failed = true; System.out.println("FAILED: testPrimitive()");38}39if (!t.testAccessiblePrimitive()) {40failed = true; System.out.println("FAILED: testAccessiblePrimitive()");41}42if (!t.testVolatilePrimitive()) {43failed = true; System.out.println("FAILED: testVolatilePrimitive()");44}454647if (!t.testStaticPrimitive()) {48failed = true; System.out.println("FAILED: testStaticPrimitive()");49}50if (!t.testAccessibleStaticPrimitive()) {51failed = true; System.out.println("FAILED: testAccessibleStaticPrimitive()");52}5354if (!t.testObject()) {55failed = true; System.out.println("FAILED: testObject()");56}57if (!t.testAccessibleObject()) {58failed = true; System.out.println("FAILED: testAccessibleObject()");59}6061if (!t.testVolatileObject()) {62failed = true; System.out.println("FAILED: testVolatileObject()");63}6465if (!t.testStaticObject()) {66failed = true; System.out.println("FAILED: testStaticObject()");67}68if (!t.testAccessibleStaticObject()) {69failed = true; System.out.println("FAILED: testAccessibleStaticObject()");70}7172if (failed) {73throw( new Throwable("Test for Field.set FAILED"));74}75}7677}7879class Test {8081private final int i;82private final Object o;8384public final int ni;85public final Object no;8687public volatile int vi;88public volatile Object vo;8990private static final int si = 408-343-1407;;91private static final Object so = new Object();9293Test() {94i = 911;95ni = i;96vi = i;97o = new Object();98no = o;99vo = o;100}101102boolean testPrimitive()103throws NoSuchFieldException104{105try {106Field f = this.getClass().getDeclaredField("ni");107f.setInt(this, 7);108if (ni != 7) {109System.out.println("setInt() did not work");110}111return false; // FAILED112} catch (IllegalAccessException iae) {113return true; // PASSED114}115}116117boolean testStaticPrimitive()118throws NoSuchFieldException119{120try {121Field f = this.getClass().getDeclaredField("si");122f.setInt(this, 13);123if (si != 13) {124System.out.println("setInt() did not work for static");125}126return false; // FAILED127} catch (IllegalAccessException iae) {128return true; // PASSED129}130}131132boolean testObject()133throws NoSuchFieldException134{135Object saved = no;136try {137Field f = this.getClass().getDeclaredField("no");138f.set(this, new Object());139if (no == saved) {140System.out.println("set() did not work");141}142return false; // FAILED143} catch (IllegalAccessException iae) {144return true; // PASSED145}146}147148boolean testStaticObject()149throws NoSuchFieldException150{151Object saved = so;152try {153Field f = this.getClass().getDeclaredField("so");154f.set(this, new Object());155if (so == saved) {156System.out.println("set() did not work for static");157}158return false; // FAILED159} catch (IllegalAccessException iae) {160return true; // PASSED161}162}163164boolean testAccessiblePrimitive()165throws NoSuchFieldException166{167try {168Field f = this.getClass().getDeclaredField("i");169f.setAccessible(true);170f.setInt(this, 7);171if (i != 7) {172System.out.println("setInt() did not work");173}174return true; // PASSED175} catch (IllegalAccessException iae) {176return false; // FAILED177}178}179180boolean testAccessibleStaticPrimitive()181throws NoSuchFieldException182{183try {184Field f = this.getClass().getDeclaredField("si");185f.setAccessible(true);186f.setInt(this, 13);187if (si != 13) {188System.out.println("setInt() did not work for static");189}190return false; // FAILED191} catch (IllegalAccessException iae) {192return true; // PASSED193}194}195196boolean testAccessibleObject()197throws NoSuchFieldException198{199Object saved = o;200try {201Field f = this.getClass().getDeclaredField("o");202f.setAccessible(true);203f.set(this, new Object());204if (o == saved) {205System.out.println("set() did not work");206}207return true; // PASSED208} catch (IllegalAccessException iae) {209return false; // FAILED210}211}212213boolean testAccessibleStaticObject()214throws NoSuchFieldException215{216Object saved = so;217try {218Field f = this.getClass().getDeclaredField("so");219f.setAccessible(true);220f.set(this, new Object());221if (so == saved) {222System.out.println("set() did not work for static");223}224return false; // FAILED225} catch (IllegalAccessException iae) {226return true; // PASSED227}228}229230boolean testVolatilePrimitive()231throws NoSuchFieldException232{233try {234Field f = this.getClass().getDeclaredField("vi");235f.setAccessible(true);236f.setInt(this, 7);237if (vi != 7) {238System.out.println("setInt() did not work");239}240return true; // PASSED241} catch (IllegalAccessException iae) {242return false; // FAILED243}244}245246247boolean testVolatileObject()248throws NoSuchFieldException249{250Object saved = vo;251try {252Field f = this.getClass().getDeclaredField("vo");253f.setAccessible(true);254f.set(this, new Object());255if (vo == saved) {256System.out.println("set() did not work");257}258return true; // PASSED259} catch (IllegalAccessException iae) {260return false; // FAILED261}262}263}264265266