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