/* * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @bug 4250960 5044412 * @summary Should not be able to set final fields through reflection unless setAccessible(true) passes and is not static * @author David Bowen (modified by Doug Lea)
*/
import java.lang.reflect.*;
publicclass Set { publicstaticvoid main(String[] argv) throws Throwable { boolean failed = false;
Test t = new Test(); if (!t.testPrimitive()) {
failed = true; System.out.println("FAILED: testPrimitive()");
} if (!t.testAccessiblePrimitive()) {
failed = true; System.out.println("FAILED: testAccessiblePrimitive()");
} if (!t.testVolatilePrimitive()) {
failed = true; System.out.println("FAILED: testVolatilePrimitive()");
}
if (!t.testStaticPrimitive()) {
failed = true; System.out.println("FAILED: testStaticPrimitive()");
} if (!t.testAccessibleStaticPrimitive()) {
failed = true; System.out.println("FAILED: testAccessibleStaticPrimitive()");
}
if (!t.testObject()) {
failed = true; System.out.println("FAILED: testObject()");
} if (!t.testAccessibleObject()) {
failed = true; System.out.println("FAILED: testAccessibleObject()");
}
if (!t.testVolatileObject()) {
failed = true; System.out.println("FAILED: testVolatileObject()");
}
if (!t.testStaticObject()) {
failed = true; System.out.println("FAILED: testStaticObject()");
} if (!t.testAccessibleStaticObject()) {
failed = true; System.out.println("FAILED: testAccessibleStaticObject()");
}
if (failed) { throw( new Throwable("Test for Field.set FAILED"));
}
}
}
class Test {
privatefinalint i; privatefinal Object o;
publicfinalint ni; publicfinal Object no;
publicvolatileint vi; publicvolatile Object vo;
privatestaticfinalint si = 408-343-1407;; privatestaticfinal Object so = new Object();
Test() {
i = 911;
ni = i;
vi = i;
o = new Object();
no = o;
vo = o;
}
boolean testPrimitive() throws NoSuchFieldException
{ try {
Field f = this.getClass().getDeclaredField("ni");
f.setInt(this, 7); if (ni != 7) {
System.out.println("setInt() did not work");
} returnfalse; // FAILED
} catch (IllegalAccessException iae) { returntrue; // PASSED
}
}
boolean testStaticPrimitive() throws NoSuchFieldException
{ try {
Field f = this.getClass().getDeclaredField("si");
f.setInt(this, 13); if (si != 13) {
System.out.println("setInt() did not work for static");
} returnfalse; // FAILED
} catch (IllegalAccessException iae) { returntrue; // PASSED
}
}
boolean testObject() throws NoSuchFieldException
{
Object saved = no; try {
Field f = this.getClass().getDeclaredField("no");
f.set(this, new Object()); if (no == saved) {
System.out.println("set() did not work");
} returnfalse; // FAILED
} catch (IllegalAccessException iae) { returntrue; // PASSED
}
}
boolean testStaticObject() throws NoSuchFieldException
{
Object saved = so; try {
Field f = this.getClass().getDeclaredField("so");
f.set(this, new Object()); if (so == saved) {
System.out.println("set() did not work for static");
} returnfalse; // FAILED
} catch (IllegalAccessException iae) { returntrue; // PASSED
}
}
boolean testAccessiblePrimitive() throws NoSuchFieldException
{ try {
Field f = this.getClass().getDeclaredField("i");
f.setAccessible(true);
f.setInt(this, 7); if (i != 7) {
System.out.println("setInt() did not work");
} returntrue; // PASSED
} catch (IllegalAccessException iae) { returnfalse; // FAILED
}
}
boolean testAccessibleStaticPrimitive() throws NoSuchFieldException
{ try {
Field f = this.getClass().getDeclaredField("si");
f.setAccessible(true);
f.setInt(this, 13); if (si != 13) {
System.out.println("setInt() did not work for static");
} returnfalse; // FAILED
} catch (IllegalAccessException iae) { returntrue; // PASSED
}
}
boolean testAccessibleObject() throws NoSuchFieldException
{
Object saved = o; try {
Field f = this.getClass().getDeclaredField("o");
f.setAccessible(true);
f.set(this, new Object()); if (o == saved) {
System.out.println("set() did not work");
} returntrue; // PASSED
} catch (IllegalAccessException iae) { returnfalse; // FAILED
}
}
boolean testAccessibleStaticObject() throws NoSuchFieldException
{
Object saved = so; try {
Field f = this.getClass().getDeclaredField("so");
f.setAccessible(true);
f.set(this, new Object()); if (so == saved) {
System.out.println("set() did not work for static");
} returnfalse; // FAILED
} catch (IllegalAccessException iae) { returntrue; // PASSED
}
}
boolean testVolatilePrimitive() throws NoSuchFieldException
{ try {
Field f = this.getClass().getDeclaredField("vi");
f.setAccessible(true);
f.setInt(this, 7); if (vi != 7) {
System.out.println("setInt() did not work");
} returntrue; // PASSED
} catch (IllegalAccessException iae) { returnfalse; // FAILED
}
}
boolean testVolatileObject() throws NoSuchFieldException
{
Object saved = vo; try {
Field f = this.getClass().getDeclaredField("vo");
f.setAccessible(true);
f.set(this, new Object()); if (vo == saved) {
System.out.println("set() did not work");
} returntrue; // PASSED
} catch (IllegalAccessException iae) { returnfalse; // FAILED
}
}
}
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.