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 18 package org.apache.harmony.luni.tests.java.lang.reflect; 19 20 import java.lang.reflect.Field; 21 import java.lang.reflect.Modifier; 22 23 import tests.support.Support_Field; 24 25 public class FieldTest extends junit.framework.TestCase { 26 27 static class TestField { 28 public static int pubfield1; 29 30 protected static double doubleSField = Double.MAX_VALUE; 31 32 private static int privfield1 = 123; 33 34 protected int intField = Integer.MAX_VALUE; 35 36 protected short shortField = Short.MAX_VALUE; 37 38 protected boolean booleanField = true; 39 40 protected byte byteField = Byte.MAX_VALUE; 41 42 protected long longField = Long.MAX_VALUE; 43 44 protected double doubleField = Double.MAX_VALUE; 45 46 protected float floatField = Float.MAX_VALUE; 47 48 protected char charField = 'T'; 49 50 protected final int intFField = Integer.MAX_VALUE; 51 52 protected final short shortFField = Short.MAX_VALUE; 53 54 protected final boolean booleanFField = true; 55 56 protected final byte byteFField = Byte.MAX_VALUE; 57 58 protected final long longFField = Long.MAX_VALUE; 59 60 protected final double doubleFField = Double.MAX_VALUE; 61 62 protected final float floatFField = Float.MAX_VALUE; 63 64 protected final char charFField = 'T'; 65 66 private static final int x = 1; 67 68 public volatile transient int y = 0; 69 70 protected static transient volatile int prsttrvol = 99; 71 } 72 73 public class TestFieldSub1 extends TestField { 74 } 75 76 public class TestFieldSub2 extends TestField { 77 } 78 79 static class A { 80 protected short shortField = Short.MAX_VALUE; 81 } 82 83 /** 84 * @tests java.lang.reflect.Field#equals(java.lang.Object) 85 */ test_equalsLjava_lang_Object()86 public void test_equalsLjava_lang_Object() throws Exception { 87 // Test for method boolean 88 // java.lang.reflect.Field.equals(java.lang.Object) 89 TestField x = new TestField(); 90 Field f = null; 91 f = x.getClass().getDeclaredField("shortField"); 92 93 assertTrue("Same Field returned false", f.equals(f)); 94 assertTrue("Inherited Field returned false", f.equals(x.getClass() 95 .getDeclaredField("shortField"))); 96 assertTrue("Identical Field from different class returned true", !f 97 .equals(A.class.getDeclaredField("shortField"))); 98 } 99 100 /** 101 * @tests java.lang.reflect.Field#get(java.lang.Object) 102 */ test_getLjava_lang_Object()103 public void test_getLjava_lang_Object() throws Throwable { 104 // Test for method java.lang.Object 105 // java.lang.reflect.Field.get(java.lang.Object) 106 TestField x = new TestField(); 107 Field f = x.getClass().getDeclaredField("doubleField"); 108 Double val = (Double) f.get(x); 109 110 assertTrue("Returned incorrect double field value", 111 val.doubleValue() == Double.MAX_VALUE); 112 // Test getting a static field; 113 f = x.getClass().getDeclaredField("doubleSField"); 114 f.set(x, new Double(1.0)); 115 val = (Double) f.get(x); 116 assertEquals("Returned incorrect double field value", 1.0, val 117 .doubleValue()); 118 119 // Try a get on a private field 120 try { 121 f = TestAccess.class.getDeclaredField("xxx"); 122 assertNotNull(f); 123 f.get(null); 124 fail("No expected IllegalAccessException"); 125 } catch (IllegalAccessException ok) {} 126 127 // Try a get on a private field in nested member 128 // temporarily commented because it breaks J9 VM 129 // Regression for HARMONY-1309 130 //f = x.getClass().getDeclaredField("privfield1"); 131 //assertEquals(x.privfield1, f.get(x)); 132 133 // Try a get using an invalid class. 134 try { 135 f = x.getClass().getDeclaredField("doubleField"); 136 f.get(new String()); 137 fail("No expected IllegalArgumentException"); 138 } catch (IllegalArgumentException exc) { 139 // Correct - Passed an Object that does not declare or inherit f 140 } 141 } 142 143 class SupportSubClass extends Support_Field { 144 getField(char primitiveType, Object o, Field f, Class expectedException)145 Object getField(char primitiveType, Object o, Field f, 146 Class expectedException) { 147 Object res = null; 148 try { 149 primitiveType = Character.toUpperCase(primitiveType); 150 switch (primitiveType) { 151 case 'I': // int 152 res = new Integer(f.getInt(o)); 153 break; 154 case 'J': // long 155 res = new Long(f.getLong(o)); 156 break; 157 case 'Z': // boolean 158 res = new Boolean(f.getBoolean(o)); 159 break; 160 case 'S': // short 161 res = new Short(f.getShort(o)); 162 break; 163 case 'B': // byte 164 res = new Byte(f.getByte(o)); 165 break; 166 case 'C': // char 167 res = new Character(f.getChar(o)); 168 break; 169 case 'D': // double 170 res = new Double(f.getDouble(o)); 171 break; 172 case 'F': // float 173 res = new Float(f.getFloat(o)); 174 break; 175 default: 176 res = f.get(o); 177 } 178 if (expectedException != null) { 179 fail("expected exception " + expectedException.getName()); 180 } 181 } catch (Exception e) { 182 if (expectedException == null) { 183 fail("unexpected exception " + e); 184 } else { 185 assertTrue("expected exception " 186 + expectedException.getName() + " and got " + e, e 187 .getClass().equals(expectedException)); 188 } 189 } 190 return res; 191 } 192 setField(char primitiveType, Object o, Field f, Class expectedException, Object value)193 void setField(char primitiveType, Object o, Field f, 194 Class expectedException, Object value) { 195 try { 196 primitiveType = Character.toUpperCase(primitiveType); 197 switch (primitiveType) { 198 case 'I': // int 199 f.setInt(o, ((Integer) value).intValue()); 200 break; 201 case 'J': // long 202 f.setLong(o, ((Long) value).longValue()); 203 break; 204 case 'Z': // boolean 205 f.setBoolean(o, ((Boolean) value).booleanValue()); 206 break; 207 case 'S': // short 208 f.setShort(o, ((Short) value).shortValue()); 209 break; 210 case 'B': // byte 211 f.setByte(o, ((Byte) value).byteValue()); 212 break; 213 case 'C': // char 214 f.setChar(o, ((Character) value).charValue()); 215 break; 216 case 'D': // double 217 f.setDouble(o, ((Double) value).doubleValue()); 218 break; 219 case 'F': // float 220 f.setFloat(o, ((Float) value).floatValue()); 221 break; 222 default: 223 f.set(o, value); 224 } 225 if (expectedException != null) { 226 fail("expected exception " + expectedException.getName()); 227 } 228 } catch (Exception e) { 229 if (expectedException == null) { 230 fail("unexpected exception " + e); 231 } else { 232 assertTrue("expected exception " 233 + expectedException.getName() + " and got " + e, e 234 .getClass().equals(expectedException)); 235 } 236 } 237 } 238 } 239 240 /** 241 * @tests java.lang.reflect.Field#get(java.lang.Object) 242 * @tests java.lang.reflect.Field#getByte(java.lang.Object) 243 * @tests java.lang.reflect.Field#getBoolean(java.lang.Object) 244 * @tests java.lang.reflect.Field#getShort(java.lang.Object) 245 * @tests java.lang.reflect.Field#getInt(java.lang.Object) 246 * @tests java.lang.reflect.Field#getLong(java.lang.Object) 247 * @tests java.lang.reflect.Field#getFloat(java.lang.Object) 248 * @tests java.lang.reflect.Field#getDouble(java.lang.Object) 249 * @tests java.lang.reflect.Field#getChar(java.lang.Object) 250 * @tests java.lang.reflect.Field#set(java.lang.Object, java.lang.Object) 251 * @tests java.lang.reflect.Field#setByte(java.lang.Object, byte) 252 * @tests java.lang.reflect.Field#setBoolean(java.lang.Object, boolean) 253 * @tests java.lang.reflect.Field#setShort(java.lang.Object, short) 254 * @tests java.lang.reflect.Field#setInt(java.lang.Object, int) 255 * @tests java.lang.reflect.Field#setLong(java.lang.Object, long) 256 * @tests java.lang.reflect.Field#setFloat(java.lang.Object, float) 257 * @tests java.lang.reflect.Field#setDouble(java.lang.Object, double) 258 * @tests java.lang.reflect.Field#setChar(java.lang.Object, char) 259 */ testProtectedFieldAccess()260 public void testProtectedFieldAccess() { 261 Class fieldClass = new Support_Field().getClass(); 262 String fieldName = null; 263 Field objectField = null; 264 Field booleanField = null; 265 Field byteField = null; 266 Field charField = null; 267 Field shortField = null; 268 Field intField = null; 269 Field longField = null; 270 Field floatField = null; 271 Field doubleField = null; 272 try { 273 fieldName = "objectField"; 274 objectField = fieldClass.getDeclaredField(fieldName); 275 276 fieldName = "booleanField"; 277 booleanField = fieldClass.getDeclaredField(fieldName); 278 279 fieldName = "byteField"; 280 byteField = fieldClass.getDeclaredField(fieldName); 281 282 fieldName = "charField"; 283 charField = fieldClass.getDeclaredField(fieldName); 284 285 fieldName = "shortField"; 286 shortField = fieldClass.getDeclaredField(fieldName); 287 288 fieldName = "intField"; 289 intField = fieldClass.getDeclaredField(fieldName); 290 291 fieldName = "longField"; 292 longField = fieldClass.getDeclaredField(fieldName); 293 294 fieldName = "floatField"; 295 floatField = fieldClass.getDeclaredField(fieldName); 296 297 fieldName = "doubleField"; 298 doubleField = fieldClass.getDeclaredField(fieldName); 299 } catch (NoSuchFieldException e) { 300 fail("missing field " + fieldName + " in test support class " 301 + fieldClass.getName()); 302 } 303 304 // create the various objects that might or might not have an instance 305 // of the field 306 Support_Field parentClass = new Support_Field(); 307 SupportSubClass subclass = new SupportSubClass(); 308 SupportSubClass otherSubclass = new SupportSubClass(); 309 Object plainObject = new Object(); 310 311 Class illegalAccessExceptionClass = new IllegalAccessException() 312 .getClass(); 313 Class illegalArgumentExceptionClass = new IllegalArgumentException() 314 .getClass(); 315 316 // The test will attempt to use pass an object to set for object, byte, 317 // short, ..., float and double fields 318 // and pass a byte to setByte for byte, short, ..., float and double 319 // fields and so on. 320 // It will also test if IllegalArgumentException is thrown when the 321 // field does not exist in the given object and that 322 // IllegalAccessException is thrown when trying to access an 323 // inaccessible protected field. 324 // The test will also check that IllegalArgumentException is thrown for 325 // all other attempts. 326 327 // Ordered by widening conversion, except for 'L' at the beg (which 328 // stands for Object). 329 // If the object provided to set can be unwrapped to a primitive, then 330 // the set method can set 331 // primitive fields. 332 char types[] = { 'L', 'B', 'S', 'C', 'I', 'J', 'F', 'D' }; 333 Field fields[] = { objectField, byteField, shortField, charField, 334 intField, longField, floatField, doubleField }; 335 Object values[] = { new Byte((byte) 1), new Byte((byte) 1), 336 new Short((short) 1), new Character((char) 1), new Integer(1), 337 new Long(1), new Float(1), new Double(1) }; 338 339 // test set methods 340 for (int i = 0; i < types.length; i++) { 341 char type = types[i]; 342 Object value = values[i]; 343 for (int j = i; j < fields.length; j++) { 344 Field field = fields[j]; 345 fieldName = field.getName(); 346 if (field == charField && type != 'C') { 347 // the exception is that bytes and shorts CANNOT be 348 // converted into chars even though chars CAN be 349 // converted into ints, longs, floats and doubles 350 subclass.setField(type, subclass, field, 351 illegalArgumentExceptionClass, value); 352 } else { 353 // setting type into field); 354 subclass.setField(type, subclass, field, null, value); 355 subclass.setField(type, otherSubclass, field, null, value); 356 subclass.setField(type, parentClass, field, illegalAccessExceptionClass, value); 357 subclass.setField(type, plainObject, field, illegalAccessExceptionClass, value); 358 } 359 } 360 for (int j = 0; j < i; j++) { 361 Field field = fields[j]; 362 fieldName = field.getName(); 363 // not setting type into field); 364 subclass.setField(type, subclass, field, 365 illegalArgumentExceptionClass, value); 366 } 367 } 368 369 // test setBoolean 370 Boolean booleanValue = Boolean.TRUE; 371 subclass.setField('Z', subclass, booleanField, null, booleanValue); 372 subclass.setField('Z', otherSubclass, booleanField, null, booleanValue); 373 subclass.setField('Z', parentClass, booleanField, illegalAccessExceptionClass, booleanValue); 374 subclass.setField('Z', plainObject, booleanField, illegalAccessExceptionClass, booleanValue); 375 for (int j = 0; j < fields.length; j++) { 376 Field listedField = fields[j]; 377 fieldName = listedField.getName(); 378 // not setting boolean into listedField 379 subclass.setField('Z', subclass, listedField, 380 illegalArgumentExceptionClass, booleanValue); 381 } 382 for (int i = 0; i < types.length; i++) { 383 char type = types[i]; 384 Object value = values[i]; 385 subclass.setField(type, subclass, booleanField, 386 illegalArgumentExceptionClass, value); 387 } 388 389 // We perform the analagous test on the get methods. 390 391 // ordered by widening conversion, except for 'L' at the end (which 392 // stands for Object), to which all primitives can be converted by 393 // wrapping 394 char newTypes[] = new char[] { 'B', 'S', 'C', 'I', 'J', 'F', 'D', 'L' }; 395 Field newFields[] = { byteField, shortField, charField, intField, 396 longField, floatField, doubleField, objectField }; 397 fields = newFields; 398 types = newTypes; 399 // test get methods 400 for (int i = 0; i < types.length; i++) { 401 char type = types[i]; 402 for (int j = 0; j <= i; j++) { 403 Field field = fields[j]; 404 fieldName = field.getName(); 405 if (type == 'C' && field != charField) { 406 // the exception is that bytes and shorts CANNOT be 407 // converted into chars even though chars CAN be 408 // converted into ints, longs, floats and doubles 409 subclass.getField(type, subclass, field, 410 illegalArgumentExceptionClass); 411 } else { 412 // getting type from field 413 subclass.getField(type, subclass, field, null); 414 subclass.getField(type, otherSubclass, field, null); 415 subclass.getField(type, parentClass, field, illegalAccessExceptionClass); 416 subclass.getField(type, plainObject, field, illegalAccessExceptionClass); 417 } 418 } 419 for (int j = i + 1; j < fields.length; j++) { 420 Field field = fields[j]; 421 fieldName = field.getName(); 422 subclass.getField(type, subclass, field, 423 illegalArgumentExceptionClass); 424 } 425 } 426 427 // test getBoolean 428 subclass.getField('Z', subclass, booleanField, null); 429 subclass.getField('Z', otherSubclass, booleanField, null); 430 subclass.getField('Z', parentClass, booleanField, illegalAccessExceptionClass); 431 subclass.getField('Z', plainObject, booleanField, illegalAccessExceptionClass); 432 for (int j = 0; j < fields.length; j++) { 433 Field listedField = fields[j]; 434 fieldName = listedField.getName(); 435 // not getting boolean from listedField 436 subclass.getField('Z', subclass, listedField, 437 illegalArgumentExceptionClass); 438 } 439 for (int i = 0; i < types.length - 1; i++) { 440 char type = types[i]; 441 subclass.getField(type, subclass, booleanField, 442 illegalArgumentExceptionClass); 443 } 444 Object res = subclass.getField('L', subclass, booleanField, null); 445 assertTrue("unexpected object " + res, res instanceof Boolean); 446 } 447 448 /** 449 * @tests java.lang.reflect.Field#getBoolean(java.lang.Object) 450 */ test_getBooleanLjava_lang_Object()451 public void test_getBooleanLjava_lang_Object() throws Exception { 452 // Test for method boolean 453 // java.lang.reflect.Field.getBoolean(java.lang.Object) 454 455 TestField x = new TestField(); 456 Field f = null; 457 boolean val = false; 458 f = x.getClass().getDeclaredField("booleanField"); 459 val = f.getBoolean(x); 460 461 assertTrue("Returned incorrect boolean field value", val); 462 463 try { 464 f = x.getClass().getDeclaredField("doubleField"); 465 f.getBoolean(x); 466 } catch (IllegalArgumentException ex) { 467 // Good, Exception should be thrown since doubleField is not a 468 // boolean type 469 return; 470 } 471 fail("Accessed field of invalid type"); 472 } 473 474 /** 475 * @tests java.lang.reflect.Field#getByte(java.lang.Object) 476 */ test_getByteLjava_lang_Object()477 public void test_getByteLjava_lang_Object() throws Exception { 478 // Test for method byte 479 // java.lang.reflect.Field.getByte(java.lang.Object) 480 TestField x = new TestField(); 481 Field f = null; 482 byte val = 0; 483 f = x.getClass().getDeclaredField("byteField"); 484 val = f.getByte(x); 485 486 assertTrue("Returned incorrect byte field value", val == Byte.MAX_VALUE); 487 try { 488 f = x.getClass().getDeclaredField("booleanField"); 489 f.getByte(x); 490 } catch (IllegalArgumentException ex) { 491 // Good, Exception should be thrown since byteField is not a 492 // boolean type 493 return; 494 } 495 496 fail("Accessed field of invalid type"); 497 } 498 499 /** 500 * @tests java.lang.reflect.Field#getChar(java.lang.Object) 501 */ test_getCharLjava_lang_Object()502 public void test_getCharLjava_lang_Object() throws Exception { 503 // Test for method char 504 // java.lang.reflect.Field.getChar(java.lang.Object) 505 TestField x = new TestField(); 506 Field f = null; 507 char val = 0; 508 f = x.getClass().getDeclaredField("charField"); 509 val = f.getChar(x); 510 511 assertEquals("Returned incorrect char field value", 'T', val); 512 try { 513 f = x.getClass().getDeclaredField("booleanField"); 514 f.getChar(x); 515 } catch (IllegalArgumentException ex) { 516 // Good, Exception should be thrown since charField is not a 517 // boolean type 518 return; 519 } 520 521 fail("Accessed field of invalid type"); 522 } 523 524 /** 525 * @tests java.lang.reflect.Field#getDeclaringClass() 526 */ test_getDeclaringClass()527 public void test_getDeclaringClass() { 528 // Test for method java.lang.Class 529 // java.lang.reflect.Field.getDeclaringClass() 530 Field[] fields; 531 532 fields = new TestField().getClass().getFields(); 533 assertTrue("Returned incorrect declaring class", fields[0] 534 .getDeclaringClass().equals(new TestField().getClass())); 535 536 // Check the case where the field is inherited to be sure the parent 537 // is returned as the declarator 538 fields = new TestFieldSub1().getClass().getFields(); 539 assertTrue("Returned incorrect declaring class", fields[0] 540 .getDeclaringClass().equals(new TestField().getClass())); 541 } 542 543 /** 544 * @tests java.lang.reflect.Field#getDouble(java.lang.Object) 545 */ test_getDoubleLjava_lang_Object()546 public void test_getDoubleLjava_lang_Object() throws Exception { 547 // Test for method double 548 // java.lang.reflect.Field.getDouble(java.lang.Object) 549 TestField x = new TestField(); 550 Field f = null; 551 double val = 0.0; 552 f = x.getClass().getDeclaredField("doubleField"); 553 val = f.getDouble(x); 554 555 assertTrue("Returned incorrect double field value", 556 val == Double.MAX_VALUE); 557 try { 558 f = x.getClass().getDeclaredField("booleanField"); 559 f.getDouble(x); 560 } catch (IllegalArgumentException ex) { 561 // Good, Exception should be thrown since doubleField is not a 562 // boolean type 563 return; 564 } 565 566 fail("Accessed field of invalid type"); 567 } 568 569 /** 570 * @tests java.lang.reflect.Field#getFloat(java.lang.Object) 571 */ test_getFloatLjava_lang_Object()572 public void test_getFloatLjava_lang_Object() throws Exception { 573 // Test for method float 574 // java.lang.reflect.Field.getFloat(java.lang.Object) 575 TestField x = new TestField(); 576 Field f = null; 577 float val = 0; 578 f = x.getClass().getDeclaredField("floatField"); 579 val = f.getFloat(x); 580 581 assertTrue("Returned incorrect float field value", 582 val == Float.MAX_VALUE); 583 try { 584 f = x.getClass().getDeclaredField("booleanField"); 585 f.getFloat(x); 586 } catch (IllegalArgumentException ex) { 587 // Good, Exception should be thrown since floatField is not a 588 // boolean type 589 return; 590 } 591 592 fail("Accessed field of invalid type"); 593 } 594 595 /** 596 * @tests java.lang.reflect.Field#getInt(java.lang.Object) 597 */ test_getIntLjava_lang_Object()598 public void test_getIntLjava_lang_Object() throws Exception { 599 // Test for method int java.lang.reflect.Field.getInt(java.lang.Object) 600 TestField x = new TestField(); 601 Field f = null; 602 int val = 0; 603 f = x.getClass().getDeclaredField("intField"); 604 val = f.getInt(x); 605 606 assertTrue("Returned incorrect Int field value", 607 val == Integer.MAX_VALUE); 608 try { 609 f = x.getClass().getDeclaredField("booleanField"); 610 f.getInt(x); 611 } catch (IllegalArgumentException ex) { 612 // Good, Exception should be thrown since IntField is not a 613 // boolean type 614 return; 615 } 616 617 fail("Accessed field of invalid type"); 618 } 619 620 /** 621 * @tests java.lang.reflect.Field#getLong(java.lang.Object) 622 */ test_getLongLjava_lang_Object()623 public void test_getLongLjava_lang_Object() throws Exception { 624 // Test for method long 625 // java.lang.reflect.Field.getLong(java.lang.Object) 626 TestField x = new TestField(); 627 Field f = null; 628 long val = 0; 629 f = x.getClass().getDeclaredField("longField"); 630 val = f.getLong(x); 631 632 assertTrue("Returned incorrect long field value", val == Long.MAX_VALUE); 633 634 try { 635 f = x.getClass().getDeclaredField("booleanField"); 636 f.getLong(x); 637 } catch (IllegalArgumentException ex) { 638 // Good, Exception should be thrown since booleanField is not a 639 // long type 640 return; 641 } 642 643 fail("Accessed field of invalid type"); 644 } 645 646 /** 647 * @tests java.lang.reflect.Field#getModifiers() 648 */ test_getModifiers()649 public void test_getModifiers() throws Exception { 650 // Test for method int java.lang.reflect.Field.getModifiers() 651 TestField x = new TestField(); 652 Field f = null; 653 f = x.getClass().getDeclaredField("prsttrvol"); 654 655 int mod = f.getModifiers(); 656 int mask = (Modifier.PROTECTED | Modifier.STATIC) 657 | (Modifier.TRANSIENT | Modifier.VOLATILE); 658 int nmask = (Modifier.PUBLIC | Modifier.NATIVE); 659 assertTrue("Returned incorrect field modifiers: ", 660 ((mod & mask) == mask) && ((mod & nmask) == 0)); 661 } 662 663 /** 664 * @tests java.lang.reflect.Field#getName() 665 */ test_getName()666 public void test_getName() throws Exception { 667 // Test for method java.lang.String java.lang.reflect.Field.getName() 668 TestField x = new TestField(); 669 Field f = null; 670 f = x.getClass().getDeclaredField("shortField"); 671 672 assertEquals("Returned incorrect field name", 673 "shortField", f.getName()); 674 } 675 676 /** 677 * @tests java.lang.reflect.Field#getShort(java.lang.Object) 678 */ test_getShortLjava_lang_Object()679 public void test_getShortLjava_lang_Object() throws Exception { 680 // Test for method short 681 // java.lang.reflect.Field.getShort(java.lang.Object) 682 TestField x = new TestField(); 683 Field f = null; 684 short val = 0; 685 686 f = x.getClass().getDeclaredField("shortField"); 687 val = f.getShort(x); 688 689 assertTrue("Returned incorrect short field value", 690 val == Short.MAX_VALUE); 691 try { 692 f = x.getClass().getDeclaredField("booleanField"); 693 f.getShort(x); 694 } catch (IllegalArgumentException ex) { 695 // Good, Exception should be thrown since booleanField is not a 696 // short type 697 return; 698 } 699 700 fail("Accessed field of invalid type"); 701 } 702 703 /** 704 * @tests java.lang.reflect.Field#getType() 705 */ test_getType()706 public void test_getType() throws Exception { 707 // Test for method java.lang.Class java.lang.reflect.Field.getType() 708 TestField x = new TestField(); 709 Field f = null; 710 f = x.getClass().getDeclaredField("shortField"); 711 712 assertTrue("Returned incorrect field type: " + f.getType().toString(), 713 f.getType().equals(short.class)); 714 } 715 716 /** 717 * @tests java.lang.reflect.Field#set(java.lang.Object, java.lang.Object) 718 */ test_setLjava_lang_ObjectLjava_lang_Object()719 public void test_setLjava_lang_ObjectLjava_lang_Object() throws Exception { 720 // Test for method void java.lang.reflect.Field.set(java.lang.Object, 721 // java.lang.Object) 722 TestField x = new TestField(); 723 Field f = null; 724 double val = 0.0; 725 f = x.getClass().getDeclaredField("doubleField"); 726 f.set(x, new Double(1.0)); 727 val = f.getDouble(x); 728 729 assertEquals("Returned incorrect double field value", 1.0, val); 730 731 try { 732 f = x.getClass().getDeclaredField("booleanField"); 733 f.set(x, new Double(1.0)); 734 } catch (IllegalArgumentException ex) { 735 // Good, Exception should be thrown since booleanField is not a 736 // double type 737 return; 738 } 739 try { 740 f = x.getClass().getDeclaredField("doubleFField"); 741 f.set(x, new Double(1.0)); 742 } catch (IllegalAccessException ex) { 743 // Good, Exception should be thrown since doubleFField is 744 // declared as final 745 return; 746 } 747 // Test setting a static field; 748 f = x.getClass().getDeclaredField("doubleSField"); 749 f.set(x, new Double(1.0)); 750 val = f.getDouble(x); 751 assertEquals("Returned incorrect double field value", 1.0, val); 752 753 fail("Accessed field of invalid type"); 754 } 755 756 /** 757 * @tests java.lang.reflect.Field#setBoolean(java.lang.Object, boolean) 758 */ test_setBooleanLjava_lang_ObjectZ()759 public void test_setBooleanLjava_lang_ObjectZ() throws Exception { 760 // Test for method void 761 // java.lang.reflect.Field.setBoolean(java.lang.Object, boolean) 762 TestField x = new TestField(); 763 Field f = null; 764 boolean val = false; 765 f = x.getClass().getDeclaredField("booleanField"); 766 f.setBoolean(x, false); 767 val = f.getBoolean(x); 768 769 assertTrue("Returned incorrect float field value", !val); 770 try { 771 f = x.getClass().getDeclaredField("booleanField"); 772 f.setBoolean(x, true); 773 } catch (IllegalArgumentException ex) { 774 // Good, Exception should be thrown since booleanField is not a 775 // boolean type 776 return; 777 } 778 779 try { 780 f = x.getClass().getDeclaredField("booleanFField"); 781 f.setBoolean(x, true); 782 } catch (IllegalAccessException ex) { 783 // Good, Exception should be thrown since booleanField is 784 // declared as final 785 return; 786 } 787 788 fail("Accessed field of invalid type"); 789 } 790 791 /** 792 * @tests java.lang.reflect.Field#setByte(java.lang.Object, byte) 793 */ test_setByteLjava_lang_ObjectB()794 public void test_setByteLjava_lang_ObjectB() throws Exception { 795 // Test for method void 796 // java.lang.reflect.Field.setByte(java.lang.Object, byte) 797 TestField x = new TestField(); 798 Field f = null; 799 byte val = 0; 800 f = x.getClass().getDeclaredField("byteField"); 801 f.setByte(x, (byte) 1); 802 val = f.getByte(x); 803 804 assertEquals("Returned incorrect float field value", 1, val); 805 806 try { 807 f = x.getClass().getDeclaredField("booleanField"); 808 f.setByte(x, (byte) 1); 809 } catch (IllegalArgumentException ex) { 810 // Good, Exception should be thrown since booleanField is not a 811 // byte type 812 return; 813 } 814 815 try { 816 f = x.getClass().getDeclaredField("byteFField"); 817 f.setByte(x, (byte) 1); 818 } catch (IllegalAccessException ex) { 819 // Good, Exception should be thrown since byteFField is declared 820 // as final 821 return; 822 } 823 824 fail("Accessed field of invalid type"); 825 } 826 827 /** 828 * @tests java.lang.reflect.Field#setChar(java.lang.Object, char) 829 */ test_setCharLjava_lang_ObjectC()830 public void test_setCharLjava_lang_ObjectC() throws Exception { 831 // Test for method void 832 // java.lang.reflect.Field.setChar(java.lang.Object, char) 833 TestField x = new TestField(); 834 Field f = null; 835 char val = 0; 836 f = x.getClass().getDeclaredField("charField"); 837 f.setChar(x, (char) 1); 838 val = f.getChar(x); 839 840 assertEquals("Returned incorrect float field value", 1, val); 841 842 try { 843 f = x.getClass().getDeclaredField("booleanField"); 844 f.setChar(x, (char) 1); 845 } catch (IllegalArgumentException ex) { 846 // Good, Exception should be thrown since booleanField is not a 847 // char type 848 return; 849 } 850 851 try { 852 f = x.getClass().getDeclaredField("charFField"); 853 f.setChar(x, (char) 1); 854 } catch (IllegalAccessException ex) { 855 // Good, Exception should be thrown since charFField is declared 856 // as final 857 return; 858 } 859 860 fail("Accessed field of invalid type"); 861 } 862 863 /** 864 * @tests java.lang.reflect.Field#setDouble(java.lang.Object, double) 865 */ test_setDoubleLjava_lang_ObjectD()866 public void test_setDoubleLjava_lang_ObjectD() throws Exception { 867 // Test for method void 868 // java.lang.reflect.Field.setDouble(java.lang.Object, double) 869 TestField x = new TestField(); 870 Field f = null; 871 double val = 0.0; 872 f = x.getClass().getDeclaredField("doubleField"); 873 f.setDouble(x, 1.0); 874 val = f.getDouble(x); 875 876 assertEquals("Returned incorrect double field value", 1.0, val); 877 878 try { 879 f = x.getClass().getDeclaredField("booleanField"); 880 f.setDouble(x, 1.0); 881 } catch (IllegalArgumentException ex) { 882 // Good, Exception should be thrown since booleanField is not a 883 // double type 884 return; 885 } 886 887 try { 888 f = x.getClass().getDeclaredField("doubleFField"); 889 f.setDouble(x, 1.0); 890 } catch (IllegalAccessException ex) { 891 // Good, Exception should be thrown since doubleFField is 892 // declared as final 893 return; 894 } 895 896 fail("Accessed field of invalid type"); 897 } 898 899 /** 900 * @tests java.lang.reflect.Field#setFloat(java.lang.Object, float) 901 */ test_setFloatLjava_lang_ObjectF()902 public void test_setFloatLjava_lang_ObjectF() throws Exception { 903 // Test for method void 904 // java.lang.reflect.Field.setFloat(java.lang.Object, float) 905 TestField x = new TestField(); 906 Field f = null; 907 float val = 0.0F; 908 f = x.getClass().getDeclaredField("floatField"); 909 f.setFloat(x, (float) 1); 910 val = f.getFloat(x); 911 912 assertEquals("Returned incorrect float field value", 1.0, val, 0.0); 913 try { 914 f = x.getClass().getDeclaredField("booleanField"); 915 f.setFloat(x, (float) 1); 916 } catch (IllegalArgumentException ex) { 917 // Good, Exception should be thrown since booleanField is not a 918 // float type 919 return; 920 } 921 try { 922 f = x.getClass().getDeclaredField("floatFField"); 923 f.setFloat(x, (float) 1); 924 } catch (IllegalAccessException ex) { 925 // Good, Exception should be thrown since floatFField is 926 // declared as final 927 return; 928 } 929 930 fail("Accessed field of invalid type"); 931 } 932 933 /** 934 * @tests java.lang.reflect.Field#setInt(java.lang.Object, int) 935 */ test_setIntLjava_lang_ObjectI()936 public void test_setIntLjava_lang_ObjectI() throws Exception { 937 // Test for method void java.lang.reflect.Field.setInt(java.lang.Object, 938 // int) 939 TestField x = new TestField(); 940 Field f = null; 941 int val = 0; 942 f = x.getClass().getDeclaredField("intField"); 943 f.setInt(x, (int) 1); 944 val = f.getInt(x); 945 946 assertEquals("Returned incorrect int field value", 1, val); 947 948 try { 949 f = x.getClass().getDeclaredField("booleanField"); 950 f.setInt(x, (int) 1); 951 } catch (IllegalArgumentException ex) { 952 // Good, Exception should be thrown since booleanField is not a 953 // int type 954 return; 955 } 956 try { 957 f = x.getClass().getDeclaredField("intFField"); 958 f.setInt(x, (int) 1); 959 } catch (IllegalAccessException ex) { 960 // Good, Exception should be thrown since intFField is declared 961 // as final 962 return; 963 } 964 965 fail("Accessed field of invalid type"); 966 } 967 968 /** 969 * @tests java.lang.reflect.Field#setLong(java.lang.Object, long) 970 */ test_setLongLjava_lang_ObjectJ()971 public void test_setLongLjava_lang_ObjectJ() throws Exception { 972 // Test for method void 973 // java.lang.reflect.Field.setLong(java.lang.Object, long) 974 TestField x = new TestField(); 975 Field f = null; 976 long val = 0L; 977 f = x.getClass().getDeclaredField("longField"); 978 f.setLong(x, (long) 1); 979 val = f.getLong(x); 980 981 assertEquals("Returned incorrect long field value", 1, val); 982 983 try { 984 f = x.getClass().getDeclaredField("booleanField"); 985 f.setLong(x, (long) 1); 986 } catch (IllegalArgumentException ex) { 987 // Good, Exception should be thrown since booleanField is not a 988 // long type 989 return; 990 } 991 try { 992 f = x.getClass().getDeclaredField("longFField"); 993 f.setLong(x, (long) 1); 994 } catch (IllegalAccessException ex) { 995 // Good, Exception should be thrown since longFField is declared 996 // as final 997 return; 998 } 999 1000 fail("Accessed field of invalid type"); 1001 } 1002 1003 /** 1004 * @tests java.lang.reflect.Field#setShort(java.lang.Object, short) 1005 */ test_setShortLjava_lang_ObjectS()1006 public void test_setShortLjava_lang_ObjectS() throws Exception { 1007 // Test for method void 1008 // java.lang.reflect.Field.setShort(java.lang.Object, short) 1009 TestField x = new TestField(); 1010 Field f = null; 1011 short val = 0; 1012 f = x.getClass().getDeclaredField("shortField"); 1013 f.setShort(x, (short) 1); 1014 val = f.getShort(x); 1015 1016 assertEquals("Returned incorrect short field value", 1, val); 1017 try { 1018 f = x.getClass().getDeclaredField("booleanField"); 1019 f.setShort(x, (short) 1); 1020 } catch (IllegalArgumentException ex) { 1021 // Good, Exception should be thrown since booleanField is not a 1022 // short type 1023 return; 1024 } 1025 try { 1026 f = x.getClass().getDeclaredField("shortFField"); 1027 f.setShort(x, (short) 1); 1028 } catch (IllegalAccessException ex) { 1029 // Good, Exception should be thrown since shortFField is 1030 // declared as final 1031 return; 1032 } 1033 1034 fail("Accessed field of invalid type"); 1035 } 1036 1037 /** 1038 * @tests java.lang.reflect.Field#toString() 1039 */ test_toString()1040 public void test_toString() throws Exception { 1041 Field f = null; 1042 1043 f = TestField.class.getDeclaredField("x"); 1044 1045 assertEquals( 1046 "Field returned incorrect string", 1047 "private static final int org.apache.harmony.luni.tests.java.lang.reflect.FieldTest$TestField.x", 1048 f.toString()); 1049 } 1050 } 1051 1052 class TestAccess { 1053 private static int xxx; 1054 } 1055