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