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