1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.lang.reflect; 19 20import tests.support.Support_Field; 21 22import java.lang.annotation.Annotation; 23import java.lang.annotation.ElementType; 24import java.lang.annotation.Inherited; 25import java.lang.annotation.Retention; 26import java.lang.annotation.RetentionPolicy; 27import java.lang.annotation.Target; 28import java.lang.reflect.Field; 29import java.lang.reflect.Modifier; 30import java.lang.reflect.Type; 31import java.lang.reflect.TypeVariable; 32import java.util.HashSet; 33import java.util.Set; 34 35public class FieldTest extends junit.framework.TestCase { 36 37 // BEGIN android-note 38 // This test had a couple of bugs in it. Some parts of the code were 39 // unreachable before. Also some tests expected the wrong excpetions 40 // to be thrown. This version has been validated to pass on a standard 41 // JDK 1.5. 42 // END android-note 43 44 public class TestClass { 45 @AnnotationRuntime0 46 @AnnotationRuntime1 47 @AnnotationClass0 48 @AnnotationSource0 49 public int annotatedField; 50 class Inner{} 51 } 52 53 54 @Retention(RetentionPolicy.RUNTIME) 55 @Target( {ElementType.FIELD}) 56 static @interface AnnotationRuntime0 { 57 } 58 59 @Retention(RetentionPolicy.RUNTIME) 60 @Target( { ElementType.FIELD}) 61 static @interface AnnotationRuntime1 { 62 } 63 64 @Retention(RetentionPolicy.CLASS) 65 @Target( { ElementType.FIELD}) 66 static @interface AnnotationClass0 { 67 } 68 69 @Retention(RetentionPolicy.SOURCE) 70 @Target( {ElementType.FIELD}) 71 static @interface AnnotationSource0 { 72 } 73 74 @Inherited 75 @Retention(RetentionPolicy.RUNTIME) 76 @Target( {ElementType.FIELD}) 77 static @interface InheritedRuntime { 78 } 79 80 public class GenericField<S, T extends Number> { 81 S field; 82 T boundedField; 83 int intField; 84 } 85 86 87 static class TestField { 88 public static int pubfield1; 89 90 private static int privfield1 = 123; 91 92 protected int intField = Integer.MAX_VALUE; 93 protected final int intFField = Integer.MAX_VALUE; 94 protected static int intSField = Integer.MAX_VALUE; 95 private final int intPFField = Integer.MAX_VALUE; 96 97 protected short shortField = Short.MAX_VALUE; 98 protected final short shortFField = Short.MAX_VALUE; 99 protected static short shortSField = Short.MAX_VALUE; 100 private final short shortPFField = Short.MAX_VALUE; 101 102 protected boolean booleanField = true; 103 protected static boolean booleanSField = true; 104 protected final boolean booleanFField = true; 105 private final boolean booleanPFField = true; 106 107 protected byte byteField = Byte.MAX_VALUE; 108 protected static byte byteSField = Byte.MAX_VALUE; 109 protected final byte byteFField = Byte.MAX_VALUE; 110 private final byte bytePFField = Byte.MAX_VALUE; 111 112 protected long longField = Long.MAX_VALUE; 113 protected final long longFField = Long.MAX_VALUE; 114 protected static long longSField = Long.MAX_VALUE; 115 private final long longPFField = Long.MAX_VALUE; 116 117 protected double doubleField = Double.MAX_VALUE; 118 protected static double doubleSField = Double.MAX_VALUE; 119 protected static final double doubleSFField = Double.MAX_VALUE; 120 protected final double doubleFField = Double.MAX_VALUE; 121 private final double doublePFField = Double.MAX_VALUE; 122 123 protected float floatField = Float.MAX_VALUE; 124 protected final float floatFField = Float.MAX_VALUE; 125 protected static float floatSField = Float.MAX_VALUE; 126 private final float floatPFField = Float.MAX_VALUE; 127 128 protected char charField = 'T'; 129 protected static char charSField = 'T'; 130 private final char charPFField = 'T'; 131 132 protected final char charFField = 'T'; 133 134 private static final int x = 1; 135 136 public volatile transient int y = 0; 137 138 protected static transient volatile int prsttrvol = 99; 139 } 140 141 public class TestFieldSub1 extends TestField { 142 } 143 144 public class TestFieldSub2 extends TestField { 145 } 146 147 static class A { 148 protected short shortField = Short.MAX_VALUE; 149 } 150 151 static enum TestEnum { 152 A, B, C; 153 int field; 154 } 155 156 /** 157 * java.lang.reflect.Field#equals(java.lang.Object) 158 */ 159 public void test_equalsLjava_lang_Object() { 160 // Test for method boolean 161 // java.lang.reflect.Field.equals(java.lang.Object) 162 TestField x = new TestField(); 163 Field f = null; 164 try { 165 f = x.getClass().getDeclaredField("shortField"); 166 } catch (Exception e) { 167 fail("Exception during getType test : " + e.getMessage()); 168 } 169 try { 170 assertTrue("Same Field returned false", f.equals(f)); 171 assertTrue("Inherited Field returned false", f.equals(x.getClass() 172 .getDeclaredField("shortField"))); 173 assertTrue("Identical Field from different class returned true", !f 174 .equals(A.class.getDeclaredField("shortField"))); 175 } catch (Exception e) { 176 fail("Exception during getType test : " + e.getMessage()); 177 } 178 } 179 180 /** 181 * java.lang.reflect.Field#get(java.lang.Object) 182 */ 183 public void test_getLjava_lang_Object() throws Throwable { 184 // Test for method java.lang.Object 185 // java.lang.reflect.Field.get(java.lang.Object) 186 TestField x = new TestField(); 187 Field f = x.getClass().getDeclaredField("doubleField"); 188 Double val = (Double) f.get(x); 189 190 assertTrue("Returned incorrect double field value", 191 val.doubleValue() == Double.MAX_VALUE); 192 // Test getting a static field; 193 f = x.getClass().getDeclaredField("doubleSField"); 194 f.set(x, new Double(1.0)); 195 val = (Double) f.get(x); 196 assertEquals("Returned incorrect double field value", 1.0, val 197 .doubleValue()); 198 199 // Try a get on a private field 200 boolean thrown = false; 201 try { 202 f = TestAccess.class.getDeclaredField("xxx"); 203 assertNotNull(f); 204 f.get(null); 205 fail("No expected IllegalAccessException"); 206 } catch (IllegalAccessException ok) { 207 thrown = true; 208 } 209 assertTrue("IllegalAccessException expected but not thrown", thrown); 210 211 // Try a get on a private field in nested member 212 // temporarily commented because it breaks J9 VM 213 // Regression for HARMONY-1309 214 //f = x.getClass().getDeclaredField("privfield1"); 215 //assertEquals(x.privfield1, f.get(x)); 216 217 // Try a get using an invalid class. 218 thrown = false; 219 try { 220 f = x.getClass().getDeclaredField("doubleField"); 221 f.get(new String()); 222 fail("No expected IllegalArgumentException"); 223 } catch (IllegalArgumentException exc) { 224 // Correct - Passed an Object that does not declare or inherit f 225 thrown = true; 226 } 227 assertTrue("IllegalArgumentException expected but not thrown", thrown); 228 229 //Test NPE 230 thrown = false; 231 try { 232 f = TestField.class.getDeclaredField("intField"); 233 f.get(null); 234 fail("Expected NullPointerException not thrown"); 235 } catch (NullPointerException exc) { 236 // Correct - Passed an Object that does not declare or inherit f 237 thrown = true; 238 } 239 assertTrue("NullPointerException expected but not thrown", thrown); 240 241 //Test no NPE on static fields 242 thrown = false; 243 try { 244 f = TestField.class.getDeclaredField("doubleSField"); 245 f.get(null); 246 assertTrue("Exception thrown", true); 247 } catch (Exception exc) { 248 fail("No exception expected"); 249 } 250 } 251 252 class SupportSubClass extends Support_Field { 253 254 Object getField(char primitiveType, Object o, Field f, 255 Class expectedException) { 256 Object res = null; 257 try { 258 primitiveType = Character.toUpperCase(primitiveType); 259 switch (primitiveType) { 260 case 'I': // int 261 res = new Integer(f.getInt(o)); 262 break; 263 case 'J': // long 264 res = new Long(f.getLong(o)); 265 break; 266 case 'Z': // boolean 267 res = new Boolean(f.getBoolean(o)); 268 break; 269 case 'S': // short 270 res = new Short(f.getShort(o)); 271 break; 272 case 'B': // byte 273 res = new Byte(f.getByte(o)); 274 break; 275 case 'C': // char 276 res = new Character(f.getChar(o)); 277 break; 278 case 'D': // double 279 res = new Double(f.getDouble(o)); 280 break; 281 case 'F': // float 282 res = new Float(f.getFloat(o)); 283 break; 284 default: 285 res = f.get(o); 286 } 287 if (expectedException != null) { 288 fail("expected exception " + expectedException.getName()); 289 } 290 } catch (Exception e) { 291 if (expectedException == null) { 292 fail("unexpected exception " + e); 293 } else { 294 assertTrue("expected exception " 295 + expectedException.getName() + " and got " + e, e 296 .getClass().equals(expectedException)); 297 } 298 } 299 return res; 300 } 301 302 void setField(char primitiveType, Object o, Field f, 303 Class expectedException, Object value) { 304 try { 305 primitiveType = Character.toUpperCase(primitiveType); 306 switch (primitiveType) { 307 case 'I': // int 308 f.setInt(o, ((Integer) value).intValue()); 309 break; 310 case 'J': // long 311 f.setLong(o, ((Long) value).longValue()); 312 break; 313 case 'Z': // boolean 314 f.setBoolean(o, ((Boolean) value).booleanValue()); 315 break; 316 case 'S': // short 317 f.setShort(o, ((Short) value).shortValue()); 318 break; 319 case 'B': // byte 320 f.setByte(o, ((Byte) value).byteValue()); 321 break; 322 case 'C': // char 323 f.setChar(o, ((Character) value).charValue()); 324 break; 325 case 'D': // double 326 f.setDouble(o, ((Double) value).doubleValue()); 327 break; 328 case 'F': // float 329 f.setFloat(o, ((Float) value).floatValue()); 330 break; 331 default: 332 f.set(o, value); 333 } 334 if (expectedException != null) { 335 fail("expected exception " + expectedException.getName() 336 + " for field " + f.getName() + ", value " + value); 337 } 338 } catch (Exception e) { 339 if (expectedException == null) { 340 fail("unexpected exception " + e + " for field " 341 + f.getName() + ", value " + value); 342 } else { 343 assertTrue("expected exception " 344 + expectedException.getName() + " and got " + e 345 + " for field " + f.getName() + ", value " + value, 346 e.getClass().equals(expectedException)); 347 } 348 } 349 } 350 } 351 352 /** 353 * java.lang.reflect.Field#get(java.lang.Object) 354 * java.lang.reflect.Field#getByte(java.lang.Object) 355 * java.lang.reflect.Field#getBoolean(java.lang.Object) 356 * java.lang.reflect.Field#getShort(java.lang.Object) 357 * java.lang.reflect.Field#getInt(java.lang.Object) 358 * java.lang.reflect.Field#getLong(java.lang.Object) 359 * java.lang.reflect.Field#getFloat(java.lang.Object) 360 * java.lang.reflect.Field#getDouble(java.lang.Object) 361 * java.lang.reflect.Field#getChar(java.lang.Object) 362 * java.lang.reflect.Field#set(java.lang.Object, java.lang.Object) 363 * java.lang.reflect.Field#setByte(java.lang.Object, byte) 364 * java.lang.reflect.Field#setBoolean(java.lang.Object, boolean) 365 * java.lang.reflect.Field#setShort(java.lang.Object, short) 366 * java.lang.reflect.Field#setInt(java.lang.Object, int) 367 * java.lang.reflect.Field#setLong(java.lang.Object, long) 368 * java.lang.reflect.Field#setFloat(java.lang.Object, float) 369 * java.lang.reflect.Field#setDouble(java.lang.Object, double) 370 * java.lang.reflect.Field#setChar(java.lang.Object, char) 371 */ 372 public void testProtectedFieldAccess() { 373 Class fieldClass = new Support_Field().getClass(); 374 String fieldName = null; 375 Field objectField = null; 376 Field booleanField = null; 377 Field byteField = null; 378 Field charField = null; 379 Field shortField = null; 380 Field intField = null; 381 Field longField = null; 382 Field floatField = null; 383 Field doubleField = null; 384 try { 385 fieldName = "objectField"; 386 objectField = fieldClass.getDeclaredField(fieldName); 387 388 fieldName = "booleanField"; 389 booleanField = fieldClass.getDeclaredField(fieldName); 390 391 fieldName = "byteField"; 392 byteField = fieldClass.getDeclaredField(fieldName); 393 394 fieldName = "charField"; 395 charField = fieldClass.getDeclaredField(fieldName); 396 397 fieldName = "shortField"; 398 shortField = fieldClass.getDeclaredField(fieldName); 399 400 fieldName = "intField"; 401 intField = fieldClass.getDeclaredField(fieldName); 402 403 fieldName = "longField"; 404 longField = fieldClass.getDeclaredField(fieldName); 405 406 fieldName = "floatField"; 407 floatField = fieldClass.getDeclaredField(fieldName); 408 409 fieldName = "doubleField"; 410 doubleField = fieldClass.getDeclaredField(fieldName); 411 } catch (NoSuchFieldException e) { 412 fail("missing field " + fieldName + " in test support class " 413 + fieldClass.getName()); 414 } 415 416 // create the various objects that might or might not have an instance 417 // of the field 418 Support_Field parentClass = new Support_Field(); 419 SupportSubClass subclass = new SupportSubClass(); 420 SupportSubClass otherSubclass = new SupportSubClass(); 421 Object plainObject = new Object(); 422 423 Class illegalAccessExceptionClass = new IllegalAccessException() 424 .getClass(); 425 Class illegalArgumentExceptionClass = new IllegalArgumentException() 426 .getClass(); 427 428 // The test will attempt to use pass an object to set for object, byte, 429 // short, ..., float and double fields 430 // and pass a byte to to setByte for byte, short, ..., float and double 431 // fields and so on. 432 // It will also test if IllegalArgumentException is thrown when the 433 // field does not exist in the given object and that 434 // IllegalAccessException is thrown when trying to access an 435 // inaccessible protected field. 436 // The test will also check that IllegalArgumentException is thrown for 437 // all other attempts. 438 439 // Ordered by widening conversion, except for 'L' at the beg (which 440 // stands for Object). 441 // If the object provided to set can be unwrapped to a primitive, then 442 // the set method can set 443 // primitive fields. 444 char types[] = { 'L', 'B', 'S', 'C', 'I', 'J', 'F', 'D' }; 445 Field fields[] = { objectField, byteField, shortField, charField, 446 intField, longField, floatField, doubleField }; 447 Object values[] = { new Byte((byte) 1), new Byte((byte) 1), 448 new Short((short) 1), new Character((char) 1), new Integer(1), 449 new Long(1), new Float(1), new Double(1) }; 450 451 // test set methods 452 for (int i = 0; i < types.length; i++) { 453 char type = types[i]; 454 Object value = values[i]; 455 for (int j = i; j < fields.length; j++) { 456 Field field = fields[j]; 457 fieldName = field.getName(); 458 459 if (field == charField && type != 'C') { 460 // the exception is that bytes and shorts CANNOT be 461 // converted into chars even though chars CAN be 462 // converted into ints, longs, floats and doubles 463 subclass.setField(type, subclass, field, 464 illegalArgumentExceptionClass, value); 465 } else { 466 // setting type into field); 467 subclass.setField(type, subclass, field, null, value); 468 subclass.setField(type, otherSubclass, field, null, value); 469 subclass.setField(type, parentClass, field, 470 illegalAccessExceptionClass, value); 471 subclass.setField(type, plainObject, field, 472 // Failed on JDK. 473 illegalAccessExceptionClass, value); 474 } 475 } 476 for (int j = 0; j < i; j++) { 477 Field field = fields[j]; 478 fieldName = field.getName(); 479 // not setting type into field); 480 subclass.setField(type, subclass, field, 481 illegalArgumentExceptionClass, value); 482 } 483 } 484 485 // test setBoolean 486 Boolean booleanValue = Boolean.TRUE; 487 subclass.setField('Z', subclass, booleanField, null, booleanValue); 488 subclass.setField('Z', otherSubclass, booleanField, null, booleanValue); 489 subclass.setField('Z', parentClass, booleanField, 490 illegalAccessExceptionClass, booleanValue); 491 subclass.setField('Z', plainObject, booleanField, 492 // Failed on JDK 493 illegalAccessExceptionClass, booleanValue); 494 for (int j = 0; j < fields.length; j++) { 495 Field listedField = fields[j]; 496 fieldName = listedField.getName(); 497 // not setting boolean into listedField 498 subclass.setField('Z', subclass, listedField, 499 illegalArgumentExceptionClass, booleanValue); 500 } 501 for (int i = 0; i < types.length; i++) { 502 char type = types[i]; 503 Object value = values[i]; 504 subclass.setField(type, subclass, booleanField, 505 illegalArgumentExceptionClass, value); 506 } 507 508 // We perform the analagous test on the get methods. 509 510 // ordered by widening conversion, except for 'L' at the end (which 511 // stands for Object), to which all primitives can be converted by 512 // wrapping 513 char newTypes[] = new char[] { 'B', 'S', 'C', 'I', 'J', 'F', 'D', 'L' }; 514 Field newFields[] = { byteField, shortField, charField, intField, 515 longField, floatField, doubleField, objectField }; 516 fields = newFields; 517 types = newTypes; 518 // test get methods 519 for (int i = 0; i < types.length; i++) { 520 char type = types[i]; 521 for (int j = 0; j <= i; j++) { 522 Field field = fields[j]; 523 fieldName = field.getName(); 524 if (type == 'C' && field != charField) { 525 // the exception is that bytes and shorts CANNOT be 526 // converted into chars even though chars CAN be 527 // converted into ints, longs, floats and doubles 528 subclass.getField(type, subclass, field, 529 illegalArgumentExceptionClass); 530 } else { 531 // getting type from field 532 subclass.getField(type, subclass, field, null); 533 subclass.getField(type, otherSubclass, field, null); 534 subclass.getField(type, parentClass, field, 535 illegalAccessExceptionClass); 536 subclass.getField(type, plainObject, field, 537 illegalAccessExceptionClass); 538 } 539 } 540 for (int j = i + 1; j < fields.length; j++) { 541 Field field = fields[j]; 542 fieldName = field.getName(); 543 subclass.getField(type, subclass, field, 544 illegalArgumentExceptionClass); 545 } 546 } 547 548 // test getBoolean 549 subclass.getField('Z', subclass, booleanField, null); 550 subclass.getField('Z', otherSubclass, booleanField, null); 551 subclass.getField('Z', parentClass, booleanField, 552 illegalAccessExceptionClass); 553 subclass.getField('Z', plainObject, booleanField, 554 illegalAccessExceptionClass); 555 for (int j = 0; j < fields.length; j++) { 556 Field listedField = fields[j]; 557 fieldName = listedField.getName(); 558 // not getting boolean from listedField 559 subclass.getField('Z', subclass, listedField, 560 illegalArgumentExceptionClass); 561 } 562 for (int i = 0; i < types.length - 1; i++) { 563 char type = types[i]; 564 subclass.getField(type, subclass, booleanField, 565 illegalArgumentExceptionClass); 566 } 567 Object res = subclass.getField('L', subclass, booleanField, null); 568 assertTrue("unexpected object " + res, res instanceof Boolean); 569 } 570 571 /** 572 * java.lang.reflect.Field#getBoolean(java.lang.Object) 573 */ 574 public void test_getBooleanLjava_lang_Object() { 575 TestField x = new TestField(); 576 Field f = null; 577 boolean val = false; 578 try { 579 f = x.getClass().getDeclaredField("booleanField"); 580 val = f.getBoolean(x); 581 } catch (Exception e) { 582 fail("Exception during getBoolean test: " + e.toString()); 583 } 584 assertTrue("Returned incorrect boolean field value", val); 585 586 boolean thrown = false; 587 try { 588 f = x.getClass().getDeclaredField("doubleField"); 589 f.getBoolean(x); 590 fail("IllegalArgumentException expected but not thrown"); 591 } catch (IllegalArgumentException ex) { 592 thrown = true; 593 } catch (Exception ex) { 594 fail("IllegalArgumentException expected but not thrown"); 595 } 596 assertTrue("IllegalArgumentException expected but not thrown", thrown); 597 598 thrown = false; 599 try { 600 f = x.getClass().getDeclaredField("booleanPFField"); 601 f.getBoolean(x); 602 fail("IllegalAccessException expected but not thrown"); 603 } catch (IllegalAccessException ex) { 604 thrown = true; 605 } catch (Exception ex) { 606 fail("IllegalAccessException expected but not thrown" 607 + ex.getMessage()); 608 } 609 assertTrue("IllegalAccessException expected but not thrown", thrown); 610 611 //Test NPE 612 thrown = false; 613 try { 614 f = x.getClass().getDeclaredField("booleanField"); 615 f.getBoolean(null); 616 fail("NullPointerException expected but not thrown"); 617 } catch (NullPointerException ex) { 618 thrown = true; 619 } catch (Exception ex) { 620 fail("NullPointerException expected but not thrown"); 621 } 622 assertTrue("NullPointerException expected but not thrown", thrown); 623 624 //Test no NPE on static field 625 thrown = false; 626 try { 627 f = x.getClass().getDeclaredField("booleanSField"); 628 boolean staticValue = f.getBoolean(null); 629 assertTrue("Wrong value returned", staticValue); 630 } catch (Exception ex) { 631 fail("No exception expected"); 632 } 633 } 634 635 636 /** 637 * java.lang.reflect.Field#getByte(java.lang.Object) 638 */ 639 public void test_getByteLjava_lang_Object() { 640 // Test for method byte 641 // java.lang.reflect.Field.getByte(java.lang.Object) 642 TestField x = new TestField(); 643 Field f = null; 644 byte val = 0; 645 try { 646 f = x.getClass().getDeclaredField("byteField"); 647 val = f.getByte(x); 648 } catch (Exception e) { 649 fail("Exception during getbyte test : " + e.getMessage()); 650 } 651 assertTrue("Returned incorrect byte field value", val == Byte.MAX_VALUE); 652 653 boolean thrown = false; 654 try { 655 f = x.getClass().getDeclaredField("doubleField"); 656 f.getByte(x); 657 fail("IllegalArgumentException expected but not thrown"); 658 } catch (IllegalArgumentException ex) { 659 thrown = true; 660 } catch (Exception ex) { 661 fail("IllegalArgumentException expected but not thrown"); 662 } 663 assertTrue("IllegalArgumentException expected but not thrown", thrown); 664 665 thrown = false; 666 try { 667 f = x.getClass().getDeclaredField("bytePFField"); 668 f.getByte(x); 669 fail("IllegalAccessException expected but not thrown"); 670 } catch (IllegalAccessException ex) { 671 thrown = true; 672 } catch (Exception ex) { 673 fail("IllegalAccessException expected but not thrown" 674 + ex.getMessage()); 675 } 676 assertTrue("IllegalAccessException expected but not thrown", thrown); 677 678 //Test NPE 679 thrown = false; 680 try { 681 f = x.getClass().getDeclaredField("byteField"); 682 f.getByte(null); 683 fail("NullPointerException expected but not thrown"); 684 } catch (NullPointerException ex) { 685 thrown = true; 686 } catch (Exception ex) { 687 fail("NullPointerException expected but not thrown"); 688 } 689 assertTrue("NullPointerException expected but not thrown", thrown); 690 691 //Test no NPE on static field 692 thrown = false; 693 try { 694 f = x.getClass().getDeclaredField("byteSField"); 695 byte staticValue = f.getByte(null); 696 assertEquals("Wrong value returned", Byte.MAX_VALUE, staticValue); 697 } catch (Exception ex) { 698 fail("No exception expected "+ ex.getMessage()); 699 } 700 } 701 702 /** 703 * java.lang.reflect.Field#getChar(java.lang.Object) 704 */ 705 public void test_getCharLjava_lang_Object() { 706 // Test for method char 707 // java.lang.reflect.Field.getChar(java.lang.Object) 708 TestField x = new TestField(); 709 Field f = null; 710 char val = 0; 711 try { 712 f = x.getClass().getDeclaredField("charField"); 713 val = f.getChar(x); 714 } catch (Exception e) { 715 fail("Exception during getCharacter test: " + e.toString()); 716 } 717 assertEquals("Returned incorrect char field value", 'T', val); 718 719 boolean thrown = false; 720 try { 721 f = x.getClass().getDeclaredField("doubleField"); 722 f.getChar(x); 723 fail("IllegalArgumentException expected but not thrown"); 724 } catch (IllegalArgumentException ex) { 725 thrown = true; 726 } catch (Exception ex) { 727 fail("IllegalArgumentException expected but not thrown"); 728 } 729 assertTrue("IllegalArgumentException expected but not thrown", thrown); 730 731 thrown = false; 732 try { 733 f = x.getClass().getDeclaredField("charPFField"); 734 f.getChar(x); 735 fail("IllegalAccessException expected but not thrown"); 736 } catch (IllegalAccessException ex) { 737 thrown = true; 738 } catch (Exception ex) { 739 fail("IllegalAccessException expected but not thrown" 740 + ex.getMessage()); 741 } 742 assertTrue("IllegalAccessException expected but not thrown", thrown); 743 744 //Test NPE 745 thrown = false; 746 try { 747 f = x.getClass().getDeclaredField("charField"); 748 f.getChar(null); 749 fail("NullPointerException expected but not thrown"); 750 } catch (NullPointerException ex) { 751 thrown = true; 752 } catch (Exception ex) { 753 fail("NullPointerException expected but not thrown"); 754 } 755 assertTrue("NullPointerException expected but not thrown", thrown); 756 757 //Test no NPE on static field 758 thrown = false; 759 try { 760 f = x.getClass().getDeclaredField("charSField"); 761 char staticValue = f.getChar(null); 762 assertEquals("Wrong value returned", 'T', staticValue); 763 } catch (Exception ex) { 764 fail("No exception expected "+ ex.getMessage()); 765 } 766 } 767 768 /** 769 * java.lang.reflect.Field#getDeclaringClass() 770 */ 771 public void test_getDeclaringClass() { 772 // Test for method java.lang.Class 773 // java.lang.reflect.Field.getDeclaringClass() 774 Field[] fields; 775 776 try { 777 fields = new TestField().getClass().getFields(); 778 assertTrue("Returned incorrect declaring class", fields[0] 779 .getDeclaringClass().equals(new TestField().getClass())); 780 781 // Check the case where the field is inherited to be sure the parent 782 // is returned as the declarator 783 fields = new TestFieldSub1().getClass().getFields(); 784 assertTrue("Returned incorrect declaring class", fields[0] 785 .getDeclaringClass().equals(new TestField().getClass())); 786 } catch (Exception e) { 787 fail("Exception : " + e.getMessage()); 788 } 789 } 790 791 /** 792 * java.lang.reflect.Field#getDouble(java.lang.Object) 793 */ 794 public void test_getDoubleLjava_lang_Object() { 795 // Test for method double 796 // java.lang.reflect.Field.getDouble(java.lang.Object) 797 TestField x = new TestField(); 798 Field f = null; 799 double val = 0.0; 800 try { 801 f = x.getClass().getDeclaredField("doubleField"); 802 val = f.getDouble(x); 803 } catch (Exception e) { 804 fail("Exception during getDouble test: " + e.toString()); 805 } 806 assertTrue("Returned incorrect double field value", 807 val == Double.MAX_VALUE); 808 809 boolean thrown = false; 810 try { 811 f = x.getClass().getDeclaredField("booleanField"); 812 f.getDouble(x); 813 fail("IllegalArgumentException expected but not thrown"); 814 } catch (IllegalArgumentException ex) { 815 thrown = true; 816 } catch (Exception ex) { 817 fail("IllegalArgumentException expected but not thrown " 818 + ex.getMessage()); 819 } 820 assertTrue("IllegalArgumentException expected but not thrown", thrown); 821 822 thrown = false; 823 try { 824 f = x.getClass().getDeclaredField("doublePFField"); 825 f.getDouble(x); 826 fail("IllegalAccessException expected but not thrown"); 827 } catch (IllegalAccessException ex) { 828 thrown = true; 829 } catch (Exception ex) { 830 fail("IllegalAccessException expected but not thrown" 831 + ex.getMessage()); 832 } 833 assertTrue("IllegalAccessException expected but not thrown", thrown); 834 835 //Test NPE 836 thrown = false; 837 try { 838 f = x.getClass().getDeclaredField("doubleField"); 839 f.getDouble(null); 840 fail("NullPointerException expected but not thrown"); 841 } catch (NullPointerException ex) { 842 thrown = true; 843 } catch (Exception ex) { 844 fail("NullPointerException expected but not thrown"); 845 } 846 assertTrue("NullPointerException expected but not thrown", thrown); 847 848 //Test no NPE on static field 849 thrown = false; 850 try { 851 f = x.getClass().getDeclaredField("doubleSFField"); 852 double staticValue = f.getDouble(null); 853 assertEquals("Wrong value returned", Double.MAX_VALUE, staticValue); 854 } catch (Exception ex) { 855 fail("No exception expected "+ ex.getMessage()); 856 } 857 } 858 859 /** 860 * java.lang.reflect.Field#getFloat(java.lang.Object) 861 */ 862 public void test_getFloatLjava_lang_Object() { 863 // Test for method float 864 // java.lang.reflect.Field.getFloat(java.lang.Object) 865 TestField x = new TestField(); 866 Field f = null; 867 float val = 0; 868 try { 869 f = x.getClass().getDeclaredField("floatField"); 870 val = f.getFloat(x); 871 } catch (Exception e) { 872 fail("Exception during getFloat test : " + e.getMessage()); 873 } 874 assertTrue("Returned incorrect float field value", 875 val == Float.MAX_VALUE); 876 877 boolean thrown = false; 878 try { 879 f = x.getClass().getDeclaredField("booleanField"); 880 f.getFloat(x); 881 fail("IllegalArgumentException expected but not thrown"); 882 } catch (IllegalArgumentException ex) { 883 thrown = true; 884 } catch (Exception ex) { 885 fail("IllegalArgumentException expected but not thrown " 886 + ex.getMessage()); 887 } 888 assertTrue("IllegalArgumentException expected but not thrown", thrown); 889 890 thrown = false; 891 try { 892 f = x.getClass().getDeclaredField("floatPFField"); 893 f.getFloat(x); 894 fail("IllegalAccessException expected but not thrown"); 895 } catch (IllegalAccessException ex) { 896 thrown = true; 897 } catch (Exception ex) { 898 fail("IllegalAccessException expected but not thrown" 899 + ex.getMessage()); 900 } 901 assertTrue("IllegalAccessException expected but not thrown", thrown); 902 903 //Test NPE 904 thrown = false; 905 try { 906 f = x.getClass().getDeclaredField("floatField"); 907 f.getFloat(null); 908 fail("NullPointerException expected but not thrown"); 909 } catch (NullPointerException ex) { 910 thrown = true; 911 } catch (Exception ex) { 912 fail("NullPointerException expected but not thrown"); 913 } 914 assertTrue("NullPointerException expected but not thrown", thrown); 915 916 //Test no NPE on static field 917 thrown = false; 918 try { 919 f = x.getClass().getDeclaredField("floatSField"); 920 float staticValue = f.getFloat(null); 921 assertEquals("Wrong value returned", Float.MAX_VALUE, staticValue); 922 } catch (Exception ex) { 923 fail("No exception expected "+ ex.getMessage()); 924 } 925 } 926 927 /** 928 * java.lang.reflect.Field#getInt(java.lang.Object) 929 */ 930 public void test_getIntLjava_lang_Object() { 931 // Test for method int java.lang.reflect.Field.getInt(java.lang.Object) 932 TestField x = new TestField(); 933 Field f = null; 934 int val = 0; 935 try { 936 f = x.getClass().getDeclaredField("intField"); 937 val = f.getInt(x); 938 } catch (Exception e) { 939 fail("Exception during getInt test : " + e.getMessage()); 940 } 941 assertTrue("Returned incorrect Int field value", 942 val == Integer.MAX_VALUE); 943 944 boolean thrown = false; 945 try { 946 f = x.getClass().getDeclaredField("booleanField"); 947 f.getInt(x); 948 fail("IllegalArgumentException expected but not thrown"); 949 } catch (IllegalArgumentException ex) { 950 thrown = true; 951 } catch (Exception ex) { 952 fail("IllegalArgumentException expected but not thrown " 953 + ex.getMessage()); 954 } 955 assertTrue("IllegalArgumentException expected but not thrown", thrown); 956 957 thrown = false; 958 try { 959 f = x.getClass().getDeclaredField("intPFField"); 960 f.getInt(x); 961 fail("IllegalAccessException expected but not thrown"); 962 } catch (IllegalAccessException ex) { 963 thrown = true; 964 } catch (Exception ex) { 965 fail("IllegalAccessException expected but not thrown" 966 + ex.getMessage()); 967 } 968 assertTrue("IllegalAccessException expected but not thrown", thrown); 969 970 //Test NPE 971 thrown = false; 972 try { 973 f = x.getClass().getDeclaredField("intField"); 974 f.getInt(null); 975 fail("NullPointerException expected but not thrown"); 976 } catch (NullPointerException ex) { 977 thrown = true; 978 } catch (Exception ex) { 979 fail("NullPointerException expected but not thrown"); 980 } 981 assertTrue("NullPointerException expected but not thrown", thrown); 982 983 //Test no NPE on static field 984 thrown = false; 985 try { 986 f = x.getClass().getDeclaredField("intSField"); 987 int staticValue = f.getInt(null); 988 assertEquals("Wrong value returned", Integer.MAX_VALUE, staticValue); 989 } catch (Exception ex) { 990 fail("No exception expected " + ex.getMessage()); 991 } 992 993 } 994 995 /** 996 * java.lang.reflect.Field#getLong(java.lang.Object) 997 */ 998 public void test_getLongLjava_lang_Object() { 999 // Test for method long 1000 // java.lang.reflect.Field.getLong(java.lang.Object) 1001 TestField x = new TestField(); 1002 Field f = null; 1003 long val = 0; 1004 try { 1005 f = x.getClass().getDeclaredField("longField"); 1006 val = f.getLong(x); 1007 } catch (Exception e) { 1008 fail("Exception during getLong test : " + e.getMessage()); 1009 } 1010 1011 boolean thrown = false; 1012 try { 1013 f = x.getClass().getDeclaredField("booleanField"); 1014 f.getLong(x); 1015 fail("IllegalArgumentException expected but not thrown"); 1016 } catch (IllegalArgumentException ex) { 1017 thrown = true; 1018 } catch (Exception ex) { 1019 fail("IllegalArgumentException expected but not thrown " 1020 + ex.getMessage()); 1021 } 1022 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1023 1024 thrown = false; 1025 try { 1026 f = x.getClass().getDeclaredField("longPFField"); 1027 f.getLong(x); 1028 fail("IllegalAccessException expected but not thrown"); 1029 } catch (IllegalAccessException ex) { 1030 thrown = true; 1031 } catch (Exception ex) { 1032 fail("IllegalAccessException expected but not thrown" 1033 + ex.getMessage()); 1034 } 1035 assertTrue("IllegalAccessException expected but not thrown", thrown); 1036 1037 //Test NPE 1038 thrown = false; 1039 try { 1040 f = x.getClass().getDeclaredField("longField"); 1041 f.getLong(null); 1042 fail("NullPointerException expected but not thrown"); 1043 } catch (NullPointerException ex) { 1044 thrown = true; 1045 } catch (Exception ex) { 1046 fail("NullPointerException expected but not thrown"); 1047 } 1048 assertTrue("NullPointerException expected but not thrown", thrown); 1049 1050 //Test no NPE on static field 1051 thrown = false; 1052 try { 1053 f = x.getClass().getDeclaredField("longSField"); 1054 long staticValue = f.getLong(null); 1055 assertEquals("Wrong value returned", Long.MAX_VALUE, staticValue); 1056 } catch (Exception ex) { 1057 fail("No exception expected "+ ex.getMessage()); 1058 } 1059 } 1060 1061 /** 1062 * java.lang.reflect.Field#getModifiers() 1063 */ 1064 public void test_getModifiers() { 1065 // Test for method int java.lang.reflect.Field.getModifiers() 1066 TestField x = new TestField(); 1067 Field f = null; 1068 try { 1069 f = x.getClass().getDeclaredField("prsttrvol"); 1070 } catch (Exception e) { 1071 fail("Exception during getModifiers test: " + e.toString()); 1072 } 1073 int mod = f.getModifiers(); 1074 int mask = (Modifier.PROTECTED | Modifier.STATIC) 1075 | (Modifier.TRANSIENT | Modifier.VOLATILE); 1076 int nmask = (Modifier.PUBLIC | Modifier.NATIVE); 1077 assertTrue("Returned incorrect field modifiers: ", 1078 ((mod & mask) == mask) && ((mod & nmask) == 0)); 1079 } 1080 1081 /** 1082 * java.lang.reflect.Field#getName() 1083 */ 1084 public void test_getName() { 1085 // Test for method java.lang.String java.lang.reflect.Field.getName() 1086 TestField x = new TestField(); 1087 Field f = null; 1088 try { 1089 f = x.getClass().getDeclaredField("shortField"); 1090 } catch (Exception e) { 1091 fail("Exception during getType test : " + e.getMessage()); 1092 } 1093 assertEquals("Returned incorrect field name", 1094 "shortField", f.getName()); 1095 } 1096 1097 /** 1098 * java.lang.reflect.Field#getShort(java.lang.Object) 1099 */ 1100 public void test_getShortLjava_lang_Object() { 1101 // Test for method short 1102 // java.lang.reflect.Field.getShort(java.lang.Object) 1103 TestField x = new TestField(); 1104 Field f = null; 1105 short val = 0; 1106 ; 1107 try { 1108 f = x.getClass().getDeclaredField("shortField"); 1109 val = f.getShort(x); 1110 } catch (Exception e) { 1111 fail("Exception during getShort test : " + e.getMessage()); 1112 } 1113 assertTrue("Returned incorrect short field value", 1114 val == Short.MAX_VALUE); 1115 1116 boolean thrown = false; 1117 try { 1118 f = x.getClass().getDeclaredField("booleanField"); 1119 f.getShort(x); 1120 fail("IllegalArgumentException expected but not thrown"); 1121 } catch (IllegalArgumentException ex) { 1122 thrown = true; 1123 } catch (Exception ex) { 1124 fail("IllegalArgumentException expected but not thrown " 1125 + ex.getMessage()); 1126 } 1127 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1128 1129 thrown = false; 1130 try { 1131 f = x.getClass().getDeclaredField("shortPFField"); 1132 f.getShort(x); 1133 fail("IllegalAccessException expected but not thrown"); 1134 } catch (IllegalAccessException ex) { 1135 thrown = true; 1136 } catch (Exception ex) { 1137 fail("IllegalAccessException expected but not thrown" 1138 + ex.getMessage()); 1139 } 1140 assertTrue("IllegalAccessException expected but not thrown", thrown); 1141 1142 //Test NPE 1143 thrown = false; 1144 try { 1145 f = x.getClass().getDeclaredField("shortField"); 1146 f.getShort(null); 1147 fail("NullPointerException expected but not thrown"); 1148 } catch (NullPointerException ex) { 1149 thrown = true; 1150 } catch (Exception ex) { 1151 fail("NullPointerException expected but not thrown"); 1152 } 1153 assertTrue("NullPointerException expected but not thrown", thrown); 1154 1155 //Test no NPE on static field 1156 thrown = false; 1157 try { 1158 f = x.getClass().getDeclaredField("shortSField"); 1159 short staticValue = f.getShort(null); 1160 assertEquals("Wrong value returned", Short.MAX_VALUE, staticValue); 1161 } catch (Exception ex) { 1162 fail("No exception expected "+ ex.getMessage()); 1163 } 1164 } 1165 1166 /** 1167 * java.lang.reflect.Field#getType() 1168 */ 1169 public void test_getType() { 1170 // Test for method java.lang.Class java.lang.reflect.Field.getType() 1171 TestField x = new TestField(); 1172 Field f = null; 1173 try { 1174 f = x.getClass().getDeclaredField("shortField"); 1175 } catch (Exception e) { 1176 fail("Exception during getType test : " + e.getMessage()); 1177 } 1178 assertTrue("Returned incorrect field type: " + f.getType().toString(), 1179 f.getType().equals(short.class)); 1180 } 1181 1182 /** 1183 * java.lang.reflect.Field#set(java.lang.Object, java.lang.Object) 1184 */ 1185 public void test_setLjava_lang_ObjectLjava_lang_Object() throws Exception{ 1186 // Test for method void java.lang.reflect.Field.set(java.lang.Object, 1187 // java.lang.Object) 1188 TestField x = new TestField(); 1189 Field f = null; 1190 double val = 0.0; 1191 try { 1192 f = x.getClass().getDeclaredField("doubleField"); 1193 f.set(x, new Double(1.0)); 1194 val = f.getDouble(x); 1195 } catch (Exception e) { 1196 fail("Exception during set test : " + e.getMessage()); 1197 } 1198 assertEquals("Returned incorrect double field value", 1.0, val); 1199 1200 //test wrong type 1201 boolean thrown = false; 1202 try { 1203 f = x.getClass().getDeclaredField("booleanField"); 1204 f.set(x, new Double(1.0)); 1205 fail("Accessed field of invalid type"); 1206 } catch (IllegalArgumentException ex) { 1207 thrown = true; 1208 } 1209 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1210 1211 //test not accessible 1212 thrown = false; 1213 try { 1214 f = x.getClass().getDeclaredField("doubleFField"); 1215 assertFalse(f.isAccessible()); 1216 f.set(x, new Double(1.0)); 1217 fail("Accessed inaccessible field"); 1218 } catch (IllegalAccessException ex) { 1219 thrown = true; 1220 } 1221 assertTrue("IllegalAccessException expected but not thrown", thrown); 1222 1223 //Test NPE 1224 thrown = false; 1225 try { 1226 f = x.getClass().getDeclaredField("booleanField"); 1227 f.set(null, true); 1228 fail("NullPointerException expected but not thrown"); 1229 } catch (NullPointerException ex) { 1230 thrown = true; 1231 } catch (Exception ex) { 1232 fail("NullPointerException expected but not thrown"); 1233 } 1234 assertTrue("NullPointerException expected but not thrown", thrown); 1235 1236 // Test setting a static field; 1237 f = x.getClass().getDeclaredField("doubleSField"); 1238 f.set(null, new Double(1.0)); 1239 val = f.getDouble(x); 1240 assertEquals("Returned incorrect double field value", 1.0, val); 1241 } 1242 1243 /** 1244 * java.lang.reflect.Field#setBoolean(java.lang.Object, boolean) 1245 */ 1246 public void test_setBooleanLjava_lang_ObjectZ() throws Exception{ 1247 // Test for method void 1248 // java.lang.reflect.Field.setBoolean(java.lang.Object, boolean) 1249 TestField x = new TestField(); 1250 Field f = null; 1251 boolean val = false; 1252 try { 1253 f = x.getClass().getDeclaredField("booleanField"); 1254 f.setBoolean(x, false); 1255 val = f.getBoolean(x); 1256 } catch (Exception e) { 1257 fail("Exception during setboolean test: " + e.toString()); 1258 } 1259 assertTrue("Returned incorrect float field value", !val); 1260 1261 //test wrong type 1262 boolean thrown = false; 1263 try { 1264 f = x.getClass().getDeclaredField("doubleField"); 1265 f.setBoolean(x, false); 1266 fail("Accessed field of invalid type"); 1267 } catch (IllegalArgumentException ex) { 1268 thrown = true; 1269 } 1270 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1271 1272 //test not accessible 1273 thrown = false; 1274 try { 1275 f = x.getClass().getDeclaredField("booleanPFField"); 1276 assertFalse(f.isAccessible()); 1277 f.setBoolean(x, true); 1278 fail("Accessed inaccessible field"); 1279 } catch (IllegalAccessException ex) { 1280 thrown = true; 1281 } 1282 assertTrue("IllegalAccessException expected but not thrown", thrown); 1283 1284 //Test NPE 1285 thrown = false; 1286 try { 1287 f = x.getClass().getDeclaredField("booleanField"); 1288 f.setBoolean(null, true); 1289 fail("NullPointerException expected but not thrown"); 1290 } catch (NullPointerException ex) { 1291 thrown = true; 1292 } catch (Exception ex) { 1293 fail("NullPointerException expected but not thrown"); 1294 } 1295 assertTrue("NullPointerException expected but not thrown", thrown); 1296 1297 // Test setting a static field; 1298 f = x.getClass().getDeclaredField("booleanSField"); 1299 f.setBoolean(null, false); 1300 val = f.getBoolean(x); 1301 assertFalse("Returned incorrect boolean field value", val); 1302 } 1303 1304 /** 1305 * java.lang.reflect.Field#setByte(java.lang.Object, byte) 1306 */ 1307 public void test_setByteLjava_lang_ObjectB() throws Exception{ 1308 // Test for method void 1309 // java.lang.reflect.Field.setByte(java.lang.Object, byte) 1310 TestField x = new TestField(); 1311 Field f = null; 1312 byte val = 0; 1313 try { 1314 f = x.getClass().getDeclaredField("byteField"); 1315 f.setByte(x, (byte) 1); 1316 val = f.getByte(x); 1317 } catch (Exception e) { 1318 fail("Exception during setByte test : " + e.getMessage()); 1319 } 1320 assertEquals("Returned incorrect float field value", 1, val); 1321 1322 //test wrong type 1323 boolean thrown = false; 1324 try { 1325 f = x.getClass().getDeclaredField("booleanField"); 1326 f.setByte(x, Byte.MIN_VALUE); 1327 fail("Accessed field of invalid type"); 1328 } catch (IllegalArgumentException ex) { 1329 thrown = true; 1330 } 1331 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1332 1333 //test not accessible 1334 thrown = false; 1335 try { 1336 f = x.getClass().getDeclaredField("bytePFField"); 1337 assertFalse(f.isAccessible()); 1338 f.setByte(x, Byte.MIN_VALUE); 1339 fail("Accessed inaccessible field"); 1340 } catch (IllegalAccessException ex) { 1341 thrown = true; 1342 } 1343 assertTrue("IllegalAccessException expected but not thrown", thrown); 1344 1345 //Test NPE 1346 thrown = false; 1347 try { 1348 f = x.getClass().getDeclaredField("byteField"); 1349 f.setByte(null, Byte.MIN_VALUE); 1350 fail("NullPointerException expected but not thrown"); 1351 } catch (NullPointerException ex) { 1352 thrown = true; 1353 } catch (Exception ex) { 1354 fail("NullPointerException expected but not thrown"); 1355 } 1356 assertTrue("NullPointerException expected but not thrown", thrown); 1357 1358 // Test setting a static field; 1359 f = x.getClass().getDeclaredField("byteSField"); 1360 f.setByte(null, Byte.MIN_VALUE); 1361 val = f.getByte(x); 1362 assertEquals("Returned incorrect byte field value", Byte.MIN_VALUE, 1363 val); 1364 } 1365 1366 /** 1367 * java.lang.reflect.Field#setChar(java.lang.Object, char) 1368 */ 1369 public void test_setCharLjava_lang_ObjectC() throws Exception{ 1370 // Test for method void 1371 // java.lang.reflect.Field.setChar(java.lang.Object, char) 1372 TestField x = new TestField(); 1373 Field f = null; 1374 char val = 0; 1375 try { 1376 f = x.getClass().getDeclaredField("charField"); 1377 f.setChar(x, (char) 1); 1378 val = f.getChar(x); 1379 } catch (Exception e) { 1380 fail("Exception during setChar test : " + e.getMessage()); 1381 } 1382 assertEquals("Returned incorrect float field value", 1, val); 1383 1384 //test wrong type 1385 boolean thrown = false; 1386 try { 1387 f = x.getClass().getDeclaredField("booleanField"); 1388 f.setChar(x, Character.MIN_VALUE); 1389 fail("Accessed field of invalid type"); 1390 } catch (IllegalArgumentException ex) { 1391 thrown = true; 1392 } 1393 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1394 1395 //test not accessible 1396 thrown = false; 1397 try { 1398 f = x.getClass().getDeclaredField("charPFField"); 1399 assertFalse(f.isAccessible()); 1400 f.setChar(x, Character.MIN_VALUE); 1401 fail("Accessed inaccessible field"); 1402 } catch (IllegalAccessException ex) { 1403 thrown = true; 1404 } 1405 assertTrue("IllegalAccessException expected but not thrown", thrown); 1406 1407 //Test NPE 1408 thrown = false; 1409 try { 1410 f = x.getClass().getDeclaredField("charField"); 1411 f.setChar(null, Character.MIN_VALUE); 1412 fail("NullPointerException expected but not thrown"); 1413 } catch (NullPointerException ex) { 1414 thrown = true; 1415 } catch (Exception ex) { 1416 fail("NullPointerException expected but not thrown"); 1417 } 1418 assertTrue("NullPointerException expected but not thrown", thrown); 1419 1420 // Test setting a static field; 1421 f = x.getClass().getDeclaredField("charSField"); 1422 f.setChar(null, Character.MIN_VALUE); 1423 val = f.getChar(x); 1424 assertEquals("Returned incorrect char field value", 1425 Character.MIN_VALUE, val); 1426 } 1427 1428 /** 1429 * java.lang.reflect.Field#setDouble(java.lang.Object, double) 1430 */ 1431 public void test_setDoubleLjava_lang_ObjectD() throws Exception{ 1432 // Test for method void 1433 // java.lang.reflect.Field.setDouble(java.lang.Object, double) 1434 TestField x = new TestField(); 1435 Field f = null; 1436 double val = 0.0; 1437 try { 1438 f = x.getClass().getDeclaredField("doubleField"); 1439 f.setDouble(x, Double.MIN_VALUE); 1440 val = f.getDouble(x); 1441 } catch (Exception e) { 1442 fail("Exception during setDouble test: " + e.toString()); 1443 } 1444 assertEquals("Returned incorrect double field value", Double.MIN_VALUE, 1445 val); 1446 1447 //test wrong type 1448 boolean thrown = false; 1449 try { 1450 f = x.getClass().getDeclaredField("booleanField"); 1451 f.setDouble(x, Double.MIN_VALUE); 1452 fail("Accessed field of invalid type"); 1453 } catch (IllegalArgumentException ex) { 1454 thrown = true; 1455 } 1456 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1457 1458 //test not accessible 1459 thrown = false; 1460 try { 1461 f = x.getClass().getDeclaredField("doublePFField"); 1462 assertFalse(f.isAccessible()); 1463 f.setDouble(x, Double.MIN_VALUE); 1464 fail("Accessed inaccessible field"); 1465 } catch (IllegalAccessException ex) { 1466 thrown = true; 1467 } 1468 assertTrue("IllegalAccessException expected but not thrown", thrown); 1469 1470 //Test NPE 1471 thrown = false; 1472 try { 1473 f = x.getClass().getDeclaredField("doubleField"); 1474 f.setDouble(null, Double.MIN_VALUE); 1475 fail("NullPointerException expected but not thrown"); 1476 } catch (NullPointerException ex) { 1477 thrown = true; 1478 } catch (Exception ex) { 1479 fail("NullPointerException expected but not thrown"); 1480 } 1481 assertTrue("NullPointerException expected but not thrown", thrown); 1482 1483 // Test setting a static field; 1484 f = x.getClass().getDeclaredField("doubleSField"); 1485 f.setDouble(null, Double.MIN_VALUE); 1486 val = f.getDouble(x); 1487 assertEquals("Returned incorrect double field value", 1488 Double.MIN_VALUE, val); 1489 } 1490 1491 /** 1492 * java.lang.reflect.Field#setFloat(java.lang.Object, float) 1493 */ 1494 public void test_setFloatLjava_lang_ObjectF() throws Exception{ 1495 // Test for method void 1496 // java.lang.reflect.Field.setFloat(java.lang.Object, float) 1497 TestField x = new TestField(); 1498 Field f = null; 1499 float val = 0.0F; 1500 try { 1501 f = x.getClass().getDeclaredField("floatField"); 1502 f.setFloat(x, Float.MIN_VALUE); 1503 val = f.getFloat(x); 1504 } catch (Exception e) { 1505 fail("Exception during setFloat test : " + e.getMessage()); 1506 } 1507 assertEquals("Returned incorrect float field value", Float.MIN_VALUE, 1508 val, 0.0); 1509 1510 //test wrong type 1511 boolean thrown = false; 1512 try { 1513 f = x.getClass().getDeclaredField("booleanField"); 1514 f.setFloat(x, Float.MIN_VALUE); 1515 fail("Accessed field of invalid type"); 1516 } catch (IllegalArgumentException ex) { 1517 thrown = true; 1518 } 1519 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1520 1521 //test not accessible 1522 thrown = false; 1523 try { 1524 f = x.getClass().getDeclaredField("floatPFField"); 1525 assertFalse(f.isAccessible()); 1526 f.setFloat(x, Float.MIN_VALUE); 1527 fail("Accessed inaccessible field"); 1528 } catch (IllegalAccessException ex) { 1529 thrown = true; 1530 } 1531 assertTrue("IllegalAccessException expected but not thrown", thrown); 1532 1533 //Test NPE 1534 thrown = false; 1535 try { 1536 f = x.getClass().getDeclaredField("floatField"); 1537 f.setFloat(null, Float.MIN_VALUE); 1538 fail("NullPointerException expected but not thrown"); 1539 } catch (NullPointerException ex) { 1540 thrown = true; 1541 } catch (Exception ex) { 1542 fail("NullPointerException expected but not thrown"); 1543 } 1544 assertTrue("NullPointerException expected but not thrown", thrown); 1545 1546 // Test setting a static field; 1547 f = x.getClass().getDeclaredField("floatSField"); 1548 f.setFloat(null, Float.MIN_VALUE); 1549 val = f.getFloat(x); 1550 assertEquals("Returned incorrect float field value", 1551 Float.MIN_VALUE, val); 1552 } 1553 1554 /** 1555 * java.lang.reflect.Field#setInt(java.lang.Object, int) 1556 */ 1557 public void test_setIntLjava_lang_ObjectI() throws Exception{ 1558 // Test for method void java.lang.reflect.Field.setInt(java.lang.Object, 1559 // int) 1560 TestField x = new TestField(); 1561 Field f = null; 1562 int val = 0; 1563 try { 1564 f = x.getClass().getDeclaredField("intField"); 1565 f.setInt(x, Integer.MIN_VALUE); 1566 val = f.getInt(x); 1567 } catch (Exception e) { 1568 fail("Exception during setInteger test: " + e.toString()); 1569 } 1570 assertEquals("Returned incorrect int field value", Integer.MIN_VALUE, 1571 val); 1572 1573 // test wrong type 1574 boolean thrown = false; 1575 try { 1576 f = x.getClass().getDeclaredField("booleanField"); 1577 f.setInt(x, Integer.MIN_VALUE); 1578 fail("Accessed field of invalid type"); 1579 } catch (IllegalArgumentException ex) { 1580 thrown = true; 1581 } 1582 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1583 1584 // test not accessible 1585 thrown = false; 1586 try { 1587 f = x.getClass().getDeclaredField("intPFField"); 1588 assertFalse(f.isAccessible()); 1589 f.setInt(x, Integer.MIN_VALUE); 1590 fail("Accessed inaccessible field"); 1591 } catch (IllegalAccessException ex) { 1592 thrown = true; 1593 } 1594 assertTrue("IllegalAccessException expected but not thrown", thrown); 1595 1596 // Test NPE 1597 thrown = false; 1598 try { 1599 f = x.getClass().getDeclaredField("intField"); 1600 f.setInt(null, Integer.MIN_VALUE); 1601 fail("NullPointerException expected but not thrown"); 1602 } catch (NullPointerException ex) { 1603 thrown = true; 1604 } catch (Exception ex) { 1605 fail("NullPointerException expected but not thrown"); 1606 } 1607 assertTrue("NullPointerException expected but not thrown", thrown); 1608 1609 // Test setting a static field; 1610 f = x.getClass().getDeclaredField("intSField"); 1611 f.setInt(null, Integer.MIN_VALUE); 1612 val = f.getInt(x); 1613 assertEquals("Returned incorrect int field value", 1614 Integer.MIN_VALUE, val); 1615 } 1616 1617 /** 1618 * java.lang.reflect.Field#setLong(java.lang.Object, long) 1619 */ 1620 public void test_setLongLjava_lang_ObjectJ() throws Exception{ 1621 // Test for method void 1622 // java.lang.reflect.Field.setLong(java.lang.Object, long) 1623 TestField x = new TestField(); 1624 Field f = null; 1625 long val = 0L; 1626 try { 1627 f = x.getClass().getDeclaredField("longField"); 1628 f.setLong(x, Long.MIN_VALUE); 1629 val = f.getLong(x); 1630 } catch (Exception e) { 1631 fail("Exception during setLong test : " + e.getMessage()); 1632 } 1633 assertEquals("Returned incorrect long field value", Long.MIN_VALUE, val); 1634 1635 // test wrong type 1636 boolean thrown = false; 1637 try { 1638 f = x.getClass().getDeclaredField("booleanField"); 1639 f.setLong(x, Long.MIN_VALUE); 1640 fail("Accessed field of invalid type"); 1641 } catch (IllegalArgumentException ex) { 1642 thrown = true; 1643 } 1644 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1645 1646 // test not accessible 1647 thrown = false; 1648 try { 1649 f = x.getClass().getDeclaredField("longPFField"); 1650 assertFalse(f.isAccessible()); 1651 f.setLong(x, Long.MIN_VALUE); 1652 fail("Accessed inaccessible field"); 1653 } catch (IllegalAccessException ex) { 1654 thrown = true; 1655 } 1656 assertTrue("IllegalAccessException expected but not thrown", thrown); 1657 1658 // Test NPE 1659 thrown = false; 1660 try { 1661 f = x.getClass().getDeclaredField("longField"); 1662 f.setLong(null, Long.MIN_VALUE); 1663 fail("NullPointerException expected but not thrown"); 1664 } catch (NullPointerException ex) { 1665 thrown = true; 1666 } catch (Exception ex) { 1667 fail("NullPointerException expected but not thrown"); 1668 } 1669 assertTrue("NullPointerException expected but not thrown", thrown); 1670 1671 // Test setting a static field; 1672 f = x.getClass().getDeclaredField("longSField"); 1673 f.setLong(null, Long.MIN_VALUE); 1674 val = f.getLong(x); 1675 assertEquals("Returned incorrect long field value", 1676 Long.MIN_VALUE, val); 1677 } 1678 1679 /** 1680 * java.lang.reflect.Field#setShort(java.lang.Object, short) 1681 */ 1682 public void test_setShortLjava_lang_ObjectS() throws Exception{ 1683 // Test for method void 1684 // java.lang.reflect.Field.setShort(java.lang.Object, short) 1685 TestField x = new TestField(); 1686 Field f = null; 1687 short val = 0; 1688 try { 1689 f = x.getClass().getDeclaredField("shortField"); 1690 f.setShort(x, Short.MIN_VALUE); 1691 val = f.getShort(x); 1692 } catch (Exception e) { 1693 fail("Exception during setShort test : " + e.getMessage()); 1694 } 1695 assertEquals("Returned incorrect short field value", Short.MIN_VALUE, 1696 val); 1697 1698 // test wrong type 1699 boolean thrown = false; 1700 try { 1701 f = x.getClass().getDeclaredField("booleanField"); 1702 f.setShort(x, Short.MIN_VALUE); 1703 fail("Accessed field of invalid type"); 1704 } catch (IllegalArgumentException ex) { 1705 thrown = true; 1706 } 1707 assertTrue("IllegalArgumentException expected but not thrown", thrown); 1708 1709 // test not accessible 1710 thrown = false; 1711 try { 1712 f = x.getClass().getDeclaredField("shortPFField"); 1713 assertFalse(f.isAccessible()); 1714 f.setShort(x, Short.MIN_VALUE); 1715 fail("Accessed inaccessible field"); 1716 } catch (IllegalAccessException ex) { 1717 thrown = true; 1718 } 1719 assertTrue("IllegalAccessException expected but not thrown", thrown); 1720 1721 // Test NPE 1722 thrown = false; 1723 try { 1724 f = x.getClass().getDeclaredField("shortField"); 1725 f.setShort(null, Short.MIN_VALUE); 1726 fail("NullPointerException expected but not thrown"); 1727 } catch (NullPointerException ex) { 1728 thrown = true; 1729 } catch (Exception ex) { 1730 fail("NullPointerException expected but not thrown"); 1731 } 1732 assertTrue("NullPointerException expected but not thrown", thrown); 1733 1734 // Test setting a static field; 1735 f = x.getClass().getDeclaredField("shortSField"); 1736 f.setShort(null, Short.MIN_VALUE); 1737 val = f.getShort(x); 1738 assertEquals("Returned incorrect short field value", 1739 Short.MIN_VALUE, val); 1740 } 1741 1742 /** 1743 * java.lang.reflect.Field#toString() 1744 */ 1745 public void test_toString() { 1746 // Test for method java.lang.String java.lang.reflect.Field.toString() 1747 Field f = null; 1748 1749 try { 1750 f = TestField.class.getDeclaredField("x"); 1751 } catch (Exception e) { 1752 fail("Exception getting field : " + e.getMessage()); 1753 } 1754 assertEquals("Field returned incorrect string", 1755 "private static final int tests.api.java.lang.reflect.FieldTest$TestField.x", 1756 f.toString()); 1757 } 1758 1759 public void test_getDeclaredAnnotations() throws Exception { 1760 Field field = TestClass.class.getField("annotatedField"); 1761 Annotation[] annotations = field.getDeclaredAnnotations(); 1762 assertEquals(2, annotations.length); 1763 1764 Set<Class<?>> ignoreOrder = new HashSet<Class<?>>(); 1765 ignoreOrder.add(annotations[0].annotationType()); 1766 ignoreOrder.add(annotations[1].annotationType()); 1767 1768 assertTrue("Missing @AnnotationRuntime0", ignoreOrder 1769 .contains(AnnotationRuntime0.class)); 1770 assertTrue("Missing @AnnotationRuntime1", ignoreOrder 1771 .contains(AnnotationRuntime1.class)); 1772 } 1773 1774 public void test_isEnumConstant() throws Exception { 1775 Field field = TestEnum.class.getDeclaredField("A"); 1776 assertTrue("Enum constant not recognized", field.isEnumConstant()); 1777 1778 field = TestEnum.class.getDeclaredField("field"); 1779 assertFalse("Non enum constant wrongly stated as enum constant", field 1780 .isEnumConstant()); 1781 1782 field = TestClass.class.getDeclaredField("annotatedField"); 1783 assertFalse("Non enum constant wrongly stated as enum constant", field 1784 .isEnumConstant()); 1785 } 1786 1787 public void test_isSynthetic() throws Exception { 1788 Field[] fields = TestClass.Inner.class.getDeclaredFields(); 1789 assertEquals("Not exactly one field returned", 1, fields.length); 1790 1791 assertTrue("Enum constant not recognized", fields[0].isSynthetic()); 1792 1793 Field field = TestEnum.class.getDeclaredField("field"); 1794 assertFalse("Non synthetic field wrongly stated as synthetic", field 1795 .isSynthetic()); 1796 1797 field = TestClass.class.getDeclaredField("annotatedField"); 1798 assertFalse("Non synthetic field wrongly stated as synthetic", field 1799 .isSynthetic()); 1800 } 1801 1802 1803 public void test_getGenericType() throws Exception { 1804 Field field = GenericField.class.getDeclaredField("field"); 1805 Type type = field.getGenericType(); 1806 @SuppressWarnings("unchecked") 1807 TypeVariable typeVar = (TypeVariable) type; 1808 assertEquals("Wrong type name returned", "S", typeVar.getName()); 1809 1810 Field boundedField = GenericField.class.getDeclaredField("boundedField"); 1811 Type boundedType = boundedField.getGenericType(); 1812 @SuppressWarnings("unchecked") 1813 TypeVariable boundedTypeVar = (TypeVariable) boundedType; 1814 assertEquals("Wrong type name returned", "T", boundedTypeVar.getName()); 1815 assertEquals("More than one bound found", 1, 1816 boundedTypeVar.getBounds().length); 1817 assertEquals("Wrong bound returned", Number.class, 1818 boundedTypeVar.getBounds()[0]); 1819 } 1820 1821 1822 public void test_toGenericString() throws Exception { 1823 Field field = GenericField.class.getDeclaredField("field"); 1824 assertEquals("Wrong generic string returned", 1825 "S tests.api.java.lang.reflect.FieldTest$GenericField.field", 1826 field.toGenericString()); 1827 1828 Field boundedField = GenericField.class 1829 .getDeclaredField("boundedField"); 1830 assertEquals( 1831 "Wrong generic string returned", 1832 "T tests.api.java.lang.reflect.FieldTest$GenericField.boundedField", 1833 boundedField.toGenericString()); 1834 1835 Field ordinary = GenericField.class.getDeclaredField("intField"); 1836 assertEquals( 1837 "Wrong generic string returned", 1838 "int tests.api.java.lang.reflect.FieldTest$GenericField.intField", 1839 ordinary.toGenericString()); 1840 } 1841 1842 1843 public void test_hashCode() throws Exception { 1844 Field field = TestClass.class.getDeclaredField("annotatedField"); 1845 assertEquals("Wrong hashCode returned", field.getName().hashCode() 1846 ^ field.getDeclaringClass().getName().hashCode(), field 1847 .hashCode()); 1848 } 1849 1850 1851 /** 1852 * Sets up the fixture, for example, open a network connection. This method 1853 * is called before a test is executed. 1854 */ 1855 protected void setUp() { 1856 } 1857 1858 /** 1859 * Tears down the fixture, for example, close a network connection. This 1860 * method is called after a test is executed. 1861 */ 1862 protected void tearDown() { 1863 } 1864} 1865 1866class TestAccess { 1867 private static int xxx; 1868} 1869