J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4250960 5044412 |
| 27 | * @summary Should not be able to set final fields through reflection unless setAccessible(true) passes and is not static |
| 28 | * @author David Bowen (modified by Doug Lea) |
| 29 | */ |
| 30 | |
| 31 | import java.lang.reflect.*; |
| 32 | |
| 33 | public class Set { |
| 34 | public static void main(String[] argv) throws Throwable { |
| 35 | boolean failed = false; |
| 36 | Test t = new Test(); |
| 37 | if (!t.testPrimitive()) { |
| 38 | failed = true; System.out.println("FAILED: testPrimitive()"); |
| 39 | } |
| 40 | if (!t.testAccessiblePrimitive()) { |
| 41 | failed = true; System.out.println("FAILED: testAccessiblePrimitive()"); |
| 42 | } |
| 43 | if (!t.testVolatilePrimitive()) { |
| 44 | failed = true; System.out.println("FAILED: testVolatilePrimitive()"); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | if (!t.testStaticPrimitive()) { |
| 49 | failed = true; System.out.println("FAILED: testStaticPrimitive()"); |
| 50 | } |
| 51 | if (!t.testAccessibleStaticPrimitive()) { |
| 52 | failed = true; System.out.println("FAILED: testAccessibleStaticPrimitive()"); |
| 53 | } |
| 54 | |
| 55 | if (!t.testObject()) { |
| 56 | failed = true; System.out.println("FAILED: testObject()"); |
| 57 | } |
| 58 | if (!t.testAccessibleObject()) { |
| 59 | failed = true; System.out.println("FAILED: testAccessibleObject()"); |
| 60 | } |
| 61 | |
| 62 | if (!t.testVolatileObject()) { |
| 63 | failed = true; System.out.println("FAILED: testVolatileObject()"); |
| 64 | } |
| 65 | |
| 66 | if (!t.testStaticObject()) { |
| 67 | failed = true; System.out.println("FAILED: testStaticObject()"); |
| 68 | } |
| 69 | if (!t.testAccessibleStaticObject()) { |
| 70 | failed = true; System.out.println("FAILED: testAccessibleStaticObject()"); |
| 71 | } |
| 72 | |
| 73 | if (failed) { |
| 74 | throw( new Throwable("Test for Field.set FAILED")); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | class Test { |
| 81 | |
| 82 | private final int i; |
| 83 | private final Object o; |
| 84 | |
| 85 | public final int ni; |
| 86 | public final Object no; |
| 87 | |
| 88 | public volatile int vi; |
| 89 | public volatile Object vo; |
| 90 | |
| 91 | private static final int si = 408-343-1407;; |
| 92 | private static final Object so = new Object(); |
| 93 | |
| 94 | Test() { |
| 95 | i = 911; |
| 96 | ni = i; |
| 97 | vi = i; |
| 98 | o = new Object(); |
| 99 | no = o; |
| 100 | vo = o; |
| 101 | } |
| 102 | |
| 103 | boolean testPrimitive() |
| 104 | throws NoSuchFieldException |
| 105 | { |
| 106 | try { |
| 107 | Field f = this.getClass().getDeclaredField("ni"); |
| 108 | f.setInt(this, 7); |
| 109 | if (ni != 7) { |
| 110 | System.out.println("setInt() did not work"); |
| 111 | } |
| 112 | return false; // FAILED |
| 113 | } catch (IllegalAccessException iae) { |
| 114 | return true; // PASSED |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | boolean testStaticPrimitive() |
| 119 | throws NoSuchFieldException |
| 120 | { |
| 121 | try { |
| 122 | Field f = this.getClass().getDeclaredField("si"); |
| 123 | f.setInt(this, 13); |
| 124 | if (si != 13) { |
| 125 | System.out.println("setInt() did not work for static"); |
| 126 | } |
| 127 | return false; // FAILED |
| 128 | } catch (IllegalAccessException iae) { |
| 129 | return true; // PASSED |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | boolean testObject() |
| 134 | throws NoSuchFieldException |
| 135 | { |
| 136 | Object saved = no; |
| 137 | try { |
| 138 | Field f = this.getClass().getDeclaredField("no"); |
| 139 | f.set(this, new Object()); |
| 140 | if (no == saved) { |
| 141 | System.out.println("set() did not work"); |
| 142 | } |
| 143 | return false; // FAILED |
| 144 | } catch (IllegalAccessException iae) { |
| 145 | return true; // PASSED |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | boolean testStaticObject() |
| 150 | throws NoSuchFieldException |
| 151 | { |
| 152 | Object saved = so; |
| 153 | try { |
| 154 | Field f = this.getClass().getDeclaredField("so"); |
| 155 | f.set(this, new Object()); |
| 156 | if (so == saved) { |
| 157 | System.out.println("set() did not work for static"); |
| 158 | } |
| 159 | return false; // FAILED |
| 160 | } catch (IllegalAccessException iae) { |
| 161 | return true; // PASSED |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | boolean testAccessiblePrimitive() |
| 166 | throws NoSuchFieldException |
| 167 | { |
| 168 | try { |
| 169 | Field f = this.getClass().getDeclaredField("i"); |
| 170 | f.setAccessible(true); |
| 171 | f.setInt(this, 7); |
| 172 | if (i != 7) { |
| 173 | System.out.println("setInt() did not work"); |
| 174 | } |
| 175 | return true; // PASSED |
| 176 | } catch (IllegalAccessException iae) { |
| 177 | return false; // FAILED |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | boolean testAccessibleStaticPrimitive() |
| 182 | throws NoSuchFieldException |
| 183 | { |
| 184 | try { |
| 185 | Field f = this.getClass().getDeclaredField("si"); |
| 186 | f.setAccessible(true); |
| 187 | f.setInt(this, 13); |
| 188 | if (si != 13) { |
| 189 | System.out.println("setInt() did not work for static"); |
| 190 | } |
| 191 | return false; // FAILED |
| 192 | } catch (IllegalAccessException iae) { |
| 193 | return true; // PASSED |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | boolean testAccessibleObject() |
| 198 | throws NoSuchFieldException |
| 199 | { |
| 200 | Object saved = o; |
| 201 | try { |
| 202 | Field f = this.getClass().getDeclaredField("o"); |
| 203 | f.setAccessible(true); |
| 204 | f.set(this, new Object()); |
| 205 | if (o == saved) { |
| 206 | System.out.println("set() did not work"); |
| 207 | } |
| 208 | return true; // PASSED |
| 209 | } catch (IllegalAccessException iae) { |
| 210 | return false; // FAILED |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | boolean testAccessibleStaticObject() |
| 215 | throws NoSuchFieldException |
| 216 | { |
| 217 | Object saved = so; |
| 218 | try { |
| 219 | Field f = this.getClass().getDeclaredField("so"); |
| 220 | f.setAccessible(true); |
| 221 | f.set(this, new Object()); |
| 222 | if (so == saved) { |
| 223 | System.out.println("set() did not work for static"); |
| 224 | } |
| 225 | return false; // FAILED |
| 226 | } catch (IllegalAccessException iae) { |
| 227 | return true; // PASSED |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | boolean testVolatilePrimitive() |
| 232 | throws NoSuchFieldException |
| 233 | { |
| 234 | try { |
| 235 | Field f = this.getClass().getDeclaredField("vi"); |
| 236 | f.setAccessible(true); |
| 237 | f.setInt(this, 7); |
| 238 | if (vi != 7) { |
| 239 | System.out.println("setInt() did not work"); |
| 240 | } |
| 241 | return true; // PASSED |
| 242 | } catch (IllegalAccessException iae) { |
| 243 | return false; // FAILED |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | |
| 248 | boolean testVolatileObject() |
| 249 | throws NoSuchFieldException |
| 250 | { |
| 251 | Object saved = vo; |
| 252 | try { |
| 253 | Field f = this.getClass().getDeclaredField("vo"); |
| 254 | f.setAccessible(true); |
| 255 | f.set(this, new Object()); |
| 256 | if (vo == saved) { |
| 257 | System.out.println("set() did not work"); |
| 258 | } |
| 259 | return true; // PASSED |
| 260 | } catch (IllegalAccessException iae) { |
| 261 | return false; // FAILED |
| 262 | } |
| 263 | } |
| 264 | } |