FieldTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.lang.reflect;
19
20import java.lang.reflect.Field;
21import java.lang.reflect.Modifier;
22
23import tests.support.Support_Field;
24
25public 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	 */
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	 */
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
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
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	 */
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,
357							illegalAccessExceptionClass, value);
358					subclass.setField(type, plainObject, field,
359							illegalArgumentExceptionClass, value);
360				}
361			}
362			for (int j = 0; j < i; j++) {
363				Field field = fields[j];
364				fieldName = field.getName();
365				// not setting type into field);
366				subclass.setField(type, subclass, field,
367						illegalArgumentExceptionClass, value);
368			}
369		}
370
371		// test setBoolean
372		Boolean booleanValue = Boolean.TRUE;
373		subclass.setField('Z', subclass, booleanField, null, booleanValue);
374		subclass.setField('Z', otherSubclass, booleanField, null, booleanValue);
375		subclass.setField('Z', parentClass, booleanField,
376				illegalAccessExceptionClass, booleanValue);
377		subclass.setField('Z', plainObject, booleanField,
378				illegalArgumentExceptionClass, booleanValue);
379		for (int j = 0; j < fields.length; j++) {
380			Field listedField = fields[j];
381			fieldName = listedField.getName();
382			// not setting boolean into listedField
383			subclass.setField('Z', subclass, listedField,
384					illegalArgumentExceptionClass, booleanValue);
385		}
386		for (int i = 0; i < types.length; i++) {
387			char type = types[i];
388			Object value = values[i];
389			subclass.setField(type, subclass, booleanField,
390					illegalArgumentExceptionClass, value);
391		}
392
393		// We perform the analagous test on the get methods.
394
395		// ordered by widening conversion, except for 'L' at the end (which
396		// stands for Object), to which all primitives can be converted by
397		// wrapping
398		char newTypes[] = new char[] { 'B', 'S', 'C', 'I', 'J', 'F', 'D', 'L' };
399		Field newFields[] = { byteField, shortField, charField, intField,
400				longField, floatField, doubleField, objectField };
401		fields = newFields;
402		types = newTypes;
403		// test get methods
404		for (int i = 0; i < types.length; i++) {
405			char type = types[i];
406			for (int j = 0; j <= i; j++) {
407				Field field = fields[j];
408				fieldName = field.getName();
409				if (type == 'C' && field != charField) {
410					// the exception is that bytes and shorts CANNOT be
411					// converted into chars even though chars CAN be
412					// converted into ints, longs, floats and doubles
413					subclass.getField(type, subclass, field,
414							illegalArgumentExceptionClass);
415				} else {
416					// getting type from field
417					subclass.getField(type, subclass, field, null);
418					subclass.getField(type, otherSubclass, field, null);
419					subclass.getField(type, parentClass, field,
420							illegalAccessExceptionClass);
421					subclass.getField(type, plainObject, field,
422							illegalArgumentExceptionClass);
423				}
424			}
425			for (int j = i + 1; j < fields.length; j++) {
426				Field field = fields[j];
427				fieldName = field.getName();
428				subclass.getField(type, subclass, field,
429						illegalArgumentExceptionClass);
430			}
431		}
432
433		// test getBoolean
434		subclass.getField('Z', subclass, booleanField, null);
435		subclass.getField('Z', otherSubclass, booleanField, null);
436		subclass.getField('Z', parentClass, booleanField,
437				illegalAccessExceptionClass);
438		subclass.getField('Z', plainObject, booleanField,
439				illegalArgumentExceptionClass);
440		for (int j = 0; j < fields.length; j++) {
441			Field listedField = fields[j];
442			fieldName = listedField.getName();
443			// not getting boolean from listedField
444			subclass.getField('Z', subclass, listedField,
445					illegalArgumentExceptionClass);
446		}
447		for (int i = 0; i < types.length - 1; i++) {
448			char type = types[i];
449			subclass.getField(type, subclass, booleanField,
450					illegalArgumentExceptionClass);
451		}
452		Object res = subclass.getField('L', subclass, booleanField, null);
453		assertTrue("unexpected object " + res, res instanceof Boolean);
454	}
455
456	/**
457	 * @tests java.lang.reflect.Field#getBoolean(java.lang.Object)
458	 */
459	public void test_getBooleanLjava_lang_Object() throws Exception {
460		// Test for method boolean
461		// java.lang.reflect.Field.getBoolean(java.lang.Object)
462
463		TestField x = new TestField();
464		Field f = null;
465		boolean val = false;
466                f = x.getClass().getDeclaredField("booleanField");
467                val = f.getBoolean(x);
468
469                assertTrue("Returned incorrect boolean field value", val);
470
471                try {
472                        f = x.getClass().getDeclaredField("doubleField");
473                        f.getBoolean(x);
474                } catch (IllegalArgumentException ex) {
475                        // Good, Exception should be thrown since doubleField is not a
476                        // boolean type
477                        return;
478                }
479		fail("Accessed field of invalid type");
480	}
481
482	/**
483	 * @tests java.lang.reflect.Field#getByte(java.lang.Object)
484	 */
485	public void test_getByteLjava_lang_Object() throws Exception {
486		// Test for method byte
487		// java.lang.reflect.Field.getByte(java.lang.Object)
488		TestField x = new TestField();
489		Field f = null;
490		byte val = 0;
491                f = x.getClass().getDeclaredField("byteField");
492                val = f.getByte(x);
493
494                assertTrue("Returned incorrect byte field value", val == Byte.MAX_VALUE);
495                try {
496                        f = x.getClass().getDeclaredField("booleanField");
497                        f.getByte(x);
498                } catch (IllegalArgumentException ex) {
499                        // Good, Exception should be thrown since byteField is not a
500                        // boolean type
501                        return;
502                }
503
504                fail("Accessed field of invalid type");
505	}
506
507	/**
508	 * @tests java.lang.reflect.Field#getChar(java.lang.Object)
509	 */
510	public void test_getCharLjava_lang_Object() throws Exception {
511		// Test for method char
512		// java.lang.reflect.Field.getChar(java.lang.Object)
513		TestField x = new TestField();
514		Field f = null;
515		char val = 0;
516                f = x.getClass().getDeclaredField("charField");
517                val = f.getChar(x);
518
519                assertEquals("Returned incorrect char field value", 'T', val);
520                try {
521                        f = x.getClass().getDeclaredField("booleanField");
522                        f.getChar(x);
523                } catch (IllegalArgumentException ex) {
524                        // Good, Exception should be thrown since charField is not a
525                        // boolean type
526                        return;
527                }
528
529                fail("Accessed field of invalid type");
530	}
531
532	/**
533	 * @tests java.lang.reflect.Field#getDeclaringClass()
534	 */
535	public void test_getDeclaringClass() {
536		// Test for method java.lang.Class
537		// java.lang.reflect.Field.getDeclaringClass()
538		Field[] fields;
539
540                fields = new TestField().getClass().getFields();
541                assertTrue("Returned incorrect declaring class", fields[0]
542                                .getDeclaringClass().equals(new TestField().getClass()));
543
544                // Check the case where the field is inherited to be sure the parent
545                // is returned as the declarator
546                fields = new TestFieldSub1().getClass().getFields();
547                assertTrue("Returned incorrect declaring class", fields[0]
548                                .getDeclaringClass().equals(new TestField().getClass()));
549	}
550
551	/**
552	 * @tests java.lang.reflect.Field#getDouble(java.lang.Object)
553	 */
554	public void test_getDoubleLjava_lang_Object() throws Exception {
555		// Test for method double
556		// java.lang.reflect.Field.getDouble(java.lang.Object)
557		TestField x = new TestField();
558		Field f = null;
559		double val = 0.0;
560                f = x.getClass().getDeclaredField("doubleField");
561                val = f.getDouble(x);
562
563                assertTrue("Returned incorrect double field value",
564				val == Double.MAX_VALUE);
565                try {
566                        f = x.getClass().getDeclaredField("booleanField");
567                        f.getDouble(x);
568                } catch (IllegalArgumentException ex) {
569                        // Good, Exception should be thrown since doubleField is not a
570                        // boolean type
571                        return;
572                }
573
574                fail("Accessed field of invalid type");
575	}
576
577	/**
578	 * @tests java.lang.reflect.Field#getFloat(java.lang.Object)
579	 */
580	public void test_getFloatLjava_lang_Object() throws Exception {
581		// Test for method float
582		// java.lang.reflect.Field.getFloat(java.lang.Object)
583		TestField x = new TestField();
584		Field f = null;
585		float val = 0;
586                f = x.getClass().getDeclaredField("floatField");
587                val = f.getFloat(x);
588
589                assertTrue("Returned incorrect float field value",
590				val == Float.MAX_VALUE);
591                try {
592                        f = x.getClass().getDeclaredField("booleanField");
593                        f.getFloat(x);
594                } catch (IllegalArgumentException ex) {
595                        // Good, Exception should be thrown since floatField is not a
596                        // boolean type
597                        return;
598                }
599
600                fail("Accessed field of invalid type");
601	}
602
603	/**
604	 * @tests java.lang.reflect.Field#getInt(java.lang.Object)
605	 */
606	public void test_getIntLjava_lang_Object() throws Exception {
607		// Test for method int java.lang.reflect.Field.getInt(java.lang.Object)
608		TestField x = new TestField();
609		Field f = null;
610		int val = 0;
611                f = x.getClass().getDeclaredField("intField");
612                val = f.getInt(x);
613
614                assertTrue("Returned incorrect Int field value",
615				val == Integer.MAX_VALUE);
616                try {
617                        f = x.getClass().getDeclaredField("booleanField");
618                        f.getInt(x);
619                } catch (IllegalArgumentException ex) {
620                        // Good, Exception should be thrown since IntField is not a
621                        // boolean type
622                        return;
623                }
624
625                fail("Accessed field of invalid type");
626	}
627
628	/**
629	 * @tests java.lang.reflect.Field#getLong(java.lang.Object)
630	 */
631	public void test_getLongLjava_lang_Object() throws Exception {
632		// Test for method long
633		// java.lang.reflect.Field.getLong(java.lang.Object)
634		TestField x = new TestField();
635		Field f = null;
636		long val = 0;
637                f = x.getClass().getDeclaredField("longField");
638                val = f.getLong(x);
639
640                assertTrue("Returned incorrect long field value", val == Long.MAX_VALUE);
641
642                try {
643                        f = x.getClass().getDeclaredField("booleanField");
644                        f.getLong(x);
645                } catch (IllegalArgumentException ex) {
646                        // Good, Exception should be thrown since booleanField is not a
647                        // long type
648                        return;
649                }
650
651                fail("Accessed field of invalid type");
652	}
653
654	/**
655	 * @tests java.lang.reflect.Field#getModifiers()
656	 */
657	public void test_getModifiers() throws Exception {
658		// Test for method int java.lang.reflect.Field.getModifiers()
659		TestField x = new TestField();
660		Field f = null;
661		f = x.getClass().getDeclaredField("prsttrvol");
662
663                int mod = f.getModifiers();
664		int mask = (Modifier.PROTECTED | Modifier.STATIC)
665				| (Modifier.TRANSIENT | Modifier.VOLATILE);
666		int nmask = (Modifier.PUBLIC | Modifier.NATIVE);
667		assertTrue("Returned incorrect field modifiers: ",
668				((mod & mask) == mask) && ((mod & nmask) == 0));
669	}
670
671	/**
672	 * @tests java.lang.reflect.Field#getName()
673	 */
674	public void test_getName() throws Exception {
675		// Test for method java.lang.String java.lang.reflect.Field.getName()
676		TestField x = new TestField();
677		Field f = null;
678		f = x.getClass().getDeclaredField("shortField");
679
680                assertEquals("Returned incorrect field name",
681				"shortField", f.getName());
682	}
683
684	/**
685	 * @tests java.lang.reflect.Field#getShort(java.lang.Object)
686	 */
687	public void test_getShortLjava_lang_Object() throws Exception {
688		// Test for method short
689		// java.lang.reflect.Field.getShort(java.lang.Object)
690		TestField x = new TestField();
691		Field f = null;
692		short val = 0;
693
694                f = x.getClass().getDeclaredField("shortField");
695                val = f.getShort(x);
696
697                assertTrue("Returned incorrect short field value",
698				val == Short.MAX_VALUE);
699                try {
700                        f = x.getClass().getDeclaredField("booleanField");
701                        f.getShort(x);
702                } catch (IllegalArgumentException ex) {
703                        // Good, Exception should be thrown since booleanField is not a
704                        // short type
705                        return;
706                }
707
708                fail("Accessed field of invalid type");
709	}
710
711	/**
712	 * @tests java.lang.reflect.Field#getType()
713	 */
714	public void test_getType() throws Exception {
715		// Test for method java.lang.Class java.lang.reflect.Field.getType()
716		TestField x = new TestField();
717		Field f = null;
718		f = x.getClass().getDeclaredField("shortField");
719
720                assertTrue("Returned incorrect field type: " + f.getType().toString(),
721				f.getType().equals(short.class));
722	}
723
724	/**
725	 * @tests java.lang.reflect.Field#set(java.lang.Object, java.lang.Object)
726	 */
727	public void test_setLjava_lang_ObjectLjava_lang_Object() throws Exception {
728		// Test for method void java.lang.reflect.Field.set(java.lang.Object,
729		// java.lang.Object)
730		TestField x = new TestField();
731		Field f = null;
732		double val = 0.0;
733                f = x.getClass().getDeclaredField("doubleField");
734                f.set(x, new Double(1.0));
735                val = f.getDouble(x);
736
737                assertEquals("Returned incorrect double field value", 1.0, val);
738
739                try {
740                        f = x.getClass().getDeclaredField("booleanField");
741                        f.set(x, new Double(1.0));
742                } catch (IllegalArgumentException ex) {
743                        // Good, Exception should be thrown since booleanField is not a
744                        // double type
745                        return;
746                }
747                try {
748                        f = x.getClass().getDeclaredField("doubleFField");
749                        f.set(x, new Double(1.0));
750                } catch (IllegalAccessException ex) {
751                        // Good, Exception should be thrown since doubleFField is
752                        // declared as final
753                        return;
754                }
755                // Test setting a static field;
756                f = x.getClass().getDeclaredField("doubleSField");
757                f.set(x, new Double(1.0));
758                val = f.getDouble(x);
759                assertEquals("Returned incorrect double field value", 1.0, val);
760
761                fail("Accessed field of invalid type");
762	}
763
764	/**
765	 * @tests java.lang.reflect.Field#setBoolean(java.lang.Object, boolean)
766	 */
767	public void test_setBooleanLjava_lang_ObjectZ() throws Exception {
768		// Test for method void
769		// java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)
770		TestField x = new TestField();
771		Field f = null;
772		boolean val = false;
773                f = x.getClass().getDeclaredField("booleanField");
774                f.setBoolean(x, false);
775                val = f.getBoolean(x);
776
777                assertTrue("Returned incorrect float field value", !val);
778                try {
779                        f = x.getClass().getDeclaredField("booleanField");
780                        f.setBoolean(x, true);
781                } catch (IllegalArgumentException ex) {
782                        // Good, Exception should be thrown since booleanField is not a
783                        // boolean type
784                        return;
785                }
786
787                try {
788                        f = x.getClass().getDeclaredField("booleanFField");
789                        f.setBoolean(x, true);
790                } catch (IllegalAccessException ex) {
791                        // Good, Exception should be thrown since booleanField is
792                        // declared as final
793                        return;
794                }
795
796                fail("Accessed field of invalid type");
797	}
798
799	/**
800	 * @tests java.lang.reflect.Field#setByte(java.lang.Object, byte)
801	 */
802	public void test_setByteLjava_lang_ObjectB() throws Exception {
803		// Test for method void
804		// java.lang.reflect.Field.setByte(java.lang.Object, byte)
805		TestField x = new TestField();
806		Field f = null;
807		byte val = 0;
808                f = x.getClass().getDeclaredField("byteField");
809                f.setByte(x, (byte) 1);
810                val = f.getByte(x);
811
812                assertEquals("Returned incorrect float field value", 1, val);
813
814                try {
815                        f = x.getClass().getDeclaredField("booleanField");
816                        f.setByte(x, (byte) 1);
817                } catch (IllegalArgumentException ex) {
818                        // Good, Exception should be thrown since booleanField is not a
819                        // byte type
820                        return;
821                }
822
823                try {
824                        f = x.getClass().getDeclaredField("byteFField");
825                        f.setByte(x, (byte) 1);
826                } catch (IllegalAccessException ex) {
827                        // Good, Exception should be thrown since byteFField is declared
828                        // as final
829                        return;
830                }
831
832                fail("Accessed field of invalid type");
833	}
834
835	/**
836	 * @tests java.lang.reflect.Field#setChar(java.lang.Object, char)
837	 */
838	public void test_setCharLjava_lang_ObjectC() throws Exception {
839		// Test for method void
840		// java.lang.reflect.Field.setChar(java.lang.Object, char)
841		TestField x = new TestField();
842		Field f = null;
843		char val = 0;
844                f = x.getClass().getDeclaredField("charField");
845                f.setChar(x, (char) 1);
846                val = f.getChar(x);
847
848                assertEquals("Returned incorrect float field value", 1, val);
849
850                try {
851                        f = x.getClass().getDeclaredField("booleanField");
852                        f.setChar(x, (char) 1);
853                } catch (IllegalArgumentException ex) {
854                        // Good, Exception should be thrown since booleanField is not a
855                        // char type
856                        return;
857                }
858
859                try {
860                        f = x.getClass().getDeclaredField("charFField");
861                        f.setChar(x, (char) 1);
862                } catch (IllegalAccessException ex) {
863                        // Good, Exception should be thrown since charFField is declared
864                        // as final
865                        return;
866                }
867
868		fail("Accessed field of invalid type");
869	}
870
871	/**
872	 * @tests java.lang.reflect.Field#setDouble(java.lang.Object, double)
873	 */
874	public void test_setDoubleLjava_lang_ObjectD() throws Exception {
875		// Test for method void
876		// java.lang.reflect.Field.setDouble(java.lang.Object, double)
877		TestField x = new TestField();
878		Field f = null;
879		double val = 0.0;
880                f = x.getClass().getDeclaredField("doubleField");
881                f.setDouble(x, 1.0);
882                val = f.getDouble(x);
883
884                assertEquals("Returned incorrect double field value", 1.0, val);
885
886                try {
887                        f = x.getClass().getDeclaredField("booleanField");
888                        f.setDouble(x, 1.0);
889                } catch (IllegalArgumentException ex) {
890                        // Good, Exception should be thrown since booleanField is not a
891                        // double type
892                        return;
893                }
894
895                try {
896                        f = x.getClass().getDeclaredField("doubleFField");
897                        f.setDouble(x, 1.0);
898                } catch (IllegalAccessException ex) {
899                        // Good, Exception should be thrown since doubleFField is
900                        // declared as final
901                        return;
902                }
903
904                fail("Accessed field of invalid type");
905	}
906
907	/**
908	 * @tests java.lang.reflect.Field#setFloat(java.lang.Object, float)
909	 */
910	public void test_setFloatLjava_lang_ObjectF() throws Exception {
911		// Test for method void
912		// java.lang.reflect.Field.setFloat(java.lang.Object, float)
913		TestField x = new TestField();
914		Field f = null;
915		float val = 0.0F;
916                f = x.getClass().getDeclaredField("floatField");
917                f.setFloat(x, (float) 1);
918                val = f.getFloat(x);
919
920                assertEquals("Returned incorrect float field value", 1.0, val, 0.0);
921                try {
922                        f = x.getClass().getDeclaredField("booleanField");
923                        f.setFloat(x, (float) 1);
924                } catch (IllegalArgumentException ex) {
925                        // Good, Exception should be thrown since booleanField is not a
926                        // float type
927                        return;
928                }
929                try {
930                        f = x.getClass().getDeclaredField("floatFField");
931                        f.setFloat(x, (float) 1);
932                } catch (IllegalAccessException ex) {
933                        // Good, Exception should be thrown since floatFField is
934                        // declared as final
935                        return;
936                }
937
938                fail("Accessed field of invalid type");
939	}
940
941	/**
942	 * @tests java.lang.reflect.Field#setInt(java.lang.Object, int)
943	 */
944	public void test_setIntLjava_lang_ObjectI() throws Exception {
945		// Test for method void java.lang.reflect.Field.setInt(java.lang.Object,
946		// int)
947		TestField x = new TestField();
948		Field f = null;
949		int val = 0;
950                f = x.getClass().getDeclaredField("intField");
951                f.setInt(x, (int) 1);
952                val = f.getInt(x);
953
954                assertEquals("Returned incorrect int field value", 1, val);
955
956                try {
957                        f = x.getClass().getDeclaredField("booleanField");
958                        f.setInt(x, (int) 1);
959                } catch (IllegalArgumentException ex) {
960                        // Good, Exception should be thrown since booleanField is not a
961                        // int type
962                        return;
963                }
964                try {
965                        f = x.getClass().getDeclaredField("intFField");
966                        f.setInt(x, (int) 1);
967                } catch (IllegalAccessException ex) {
968                        // Good, Exception should be thrown since intFField is declared
969                        // as final
970                        return;
971                }
972
973                fail("Accessed field of invalid type");
974	}
975
976	/**
977	 * @tests java.lang.reflect.Field#setLong(java.lang.Object, long)
978	 */
979	public void test_setLongLjava_lang_ObjectJ() throws Exception {
980		// Test for method void
981		// java.lang.reflect.Field.setLong(java.lang.Object, long)
982		TestField x = new TestField();
983		Field f = null;
984		long val = 0L;
985                f = x.getClass().getDeclaredField("longField");
986                f.setLong(x, (long) 1);
987                val = f.getLong(x);
988
989                assertEquals("Returned incorrect long field value", 1, val);
990
991                try {
992                        f = x.getClass().getDeclaredField("booleanField");
993                        f.setLong(x, (long) 1);
994                } catch (IllegalArgumentException ex) {
995                        // Good, Exception should be thrown since booleanField is not a
996                        // long type
997                        return;
998                }
999                try {
1000                        f = x.getClass().getDeclaredField("longFField");
1001                        f.setLong(x, (long) 1);
1002                } catch (IllegalAccessException ex) {
1003                        // Good, Exception should be thrown since longFField is declared
1004                        // as final
1005                        return;
1006                }
1007
1008                fail("Accessed field of invalid type");
1009	}
1010
1011	/**
1012	 * @tests java.lang.reflect.Field#setShort(java.lang.Object, short)
1013	 */
1014	public void test_setShortLjava_lang_ObjectS() throws Exception {
1015		// Test for method void
1016		// java.lang.reflect.Field.setShort(java.lang.Object, short)
1017		TestField x = new TestField();
1018		Field f = null;
1019		short val = 0;
1020                f = x.getClass().getDeclaredField("shortField");
1021                f.setShort(x, (short) 1);
1022                val = f.getShort(x);
1023
1024                assertEquals("Returned incorrect short field value", 1, val);
1025                try {
1026                        f = x.getClass().getDeclaredField("booleanField");
1027                        f.setShort(x, (short) 1);
1028                } catch (IllegalArgumentException ex) {
1029                        // Good, Exception should be thrown since booleanField is not a
1030                        // short type
1031                        return;
1032                }
1033                try {
1034                        f = x.getClass().getDeclaredField("shortFField");
1035                        f.setShort(x, (short) 1);
1036                } catch (IllegalAccessException ex) {
1037                        // Good, Exception should be thrown since shortFField is
1038                        // declared as final
1039                        return;
1040                }
1041
1042                fail("Accessed field of invalid type");
1043	}
1044
1045	/**
1046	 * @tests java.lang.reflect.Field#toString()
1047	 */
1048	public void test_toString() throws Exception {
1049        Field f = null;
1050
1051        f = TestField.class.getDeclaredField("x");
1052
1053        assertEquals(
1054                "Field returned incorrect string",
1055                "private static final int org.apache.harmony.luni.tests.java.lang.reflect.FieldTest$TestField.x",
1056                f.toString());
1057    }
1058}
1059
1060class TestAccess {
1061    private static int xxx;
1062}
1063