1 /* java.lang.reflect.Field - reflection of Java fields
2 Copyright (C) 1998, 2001, 2005, 2008 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.lang.reflect;
41 import gnu.java.lang.ClassHelper;
42 import gnu.java.lang.CPStringBuilder;
44 import gnu.java.lang.reflect.FieldSignatureParser;
46 import java.lang.annotation.Annotation;
49 * The Field class represents a member variable of a class. It also allows
50 * dynamic access to a member, via reflection. This works for both
51 * static and instance fields. Operations on Field objects know how to
52 * do widening conversions, but throw {@link IllegalArgumentException} if
53 * a narrowing conversion would be necessary. You can query for information
54 * on this Field regardless of location, but get and set access may be limited
55 * by Java language access controls. If you can't do it in the compiler, you
56 * can't normally do it here either.<p>
58 * <B>Note:</B> This class returns and accepts types as Classes, even
59 * primitive types; there are Class types defined that represent each
60 * different primitive type. They are <code>java.lang.Boolean.TYPE,
61 * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class,
62 * byte.class</code>, etc. These are not to be confused with the
63 * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are
66 * Also note that this is not a serializable class. It is entirely feasible
67 * to make it serializable using the Externalizable interface, but this is
71 * @author Eric Blake <ebb9@email.byu.edu>
74 * @see Class#getField(String)
75 * @see Class#getDeclaredField(String)
76 * @see Class#getFields()
77 * @see Class#getDeclaredFields()
79 * @status updated to 1.4
81 public final class Field
82 extends AccessibleObject implements Member
84 static final int FIELD_MODIFIERS
85 = Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED
86 | Modifier.PUBLIC | Modifier.STATIC | Modifier.TRANSIENT
89 private FieldSignatureParser p;
94 * This class is uninstantiable outside the package.
103 * Gets the class that declared this field, or the class where this field
104 * is a non-inherited member.
105 * @return the class that declared this member
107 @SuppressWarnings("unchecked")
108 public Class<?> getDeclaringClass()
110 return (Class<?>) f.getDeclaringClass();
114 * Gets the name of this field.
115 * @return the name of this field
117 public String getName()
123 * Gets the modifiers this field uses. Use the <code>Modifier</code>
124 * class to interpret the values. A field can only have a subset of the
125 * following modifiers: public, private, protected, static, final,
126 * transient, and volatile.
128 * @return an integer representing the modifiers to this Member
131 public int getModifiers()
133 return f.getModifiersInternal() & FIELD_MODIFIERS;
137 * Return true if this field is synthetic, false otherwise.
140 public boolean isSynthetic()
142 return (f.getModifiersInternal() & Modifier.SYNTHETIC) != 0;
146 * Return true if this field represents an enum constant,
150 public boolean isEnumConstant()
152 return (f.getModifiersInternal() & Modifier.ENUM) != 0;
156 * Gets the type of this field.
157 * @return the type of this field
159 public Class<?> getType()
165 * Compare two objects to see if they are semantically equivalent.
166 * Two Fields are semantically equivalent if they have the same declaring
167 * class, name, and type. Since you can't creat a Field except through
168 * the VM, this is just the == relation.
170 * @param o the object to compare to
171 * @return <code>true</code> if they are equal; <code>false</code> if not
173 public boolean equals(Object o)
179 * Get the hash code for the Field. The Field hash code is the hash code
180 * of its name XOR'd with the hash code of its class name.
182 * @return the hash code for the object.
184 public int hashCode()
186 return f.getDeclaringClass().getName().hashCode() ^ f.getName().hashCode();
190 * Get a String representation of the Field. A Field's String
191 * representation is "<modifiers> <type>
192 * <class>.<fieldname>".<br> Example:
193 * <code>public transient boolean gnu.parse.Parser.parseComplete</code>
195 * @return the String representation of the Field
197 public String toString()
199 // 64 is a reasonable buffer initial size for field
200 CPStringBuilder sb = new CPStringBuilder(64);
201 Modifier.toString(getModifiers(), sb).append(' ');
202 sb.append(ClassHelper.getUserName(getType())).append(' ');
203 sb.append(getDeclaringClass().getName()).append('.');
204 sb.append(getName());
205 return sb.toString();
208 public String toGenericString()
210 CPStringBuilder sb = new CPStringBuilder(64);
211 Modifier.toString(getModifiers(), sb).append(' ');
212 sb.append(getGenericType()).append(' ');
213 sb.append(getDeclaringClass().getName()).append('.');
214 sb.append(getName());
215 return sb.toString();
219 * Get the value of this Field. If it is primitive, it will be wrapped
220 * in the appropriate wrapper type (boolean = java.lang.Boolean).<p>
222 * If the field is static, <code>o</code> will be ignored. Otherwise, if
223 * <code>o</code> is null, you get a <code>NullPointerException</code>,
224 * and if it is incompatible with the declaring class of the field, you
225 * get an <code>IllegalArgumentException</code>.<p>
227 * Next, if this Field enforces access control, your runtime context is
228 * evaluated, and you may have an <code>IllegalAccessException</code> if
229 * you could not access this field in similar compiled code. If the field
230 * is static, and its class is uninitialized, you trigger class
231 * initialization, which may end in a
232 * <code>ExceptionInInitializerError</code>.<p>
234 * Finally, the field is accessed, and primitives are wrapped (but not
235 * necessarily in new objects). This method accesses the field of the
236 * declaring class, even if the instance passed in belongs to a subclass
237 * which declares another field to hide this one.
239 * @param o the object to get the value of this Field from
240 * @return the value of the Field
241 * @throws IllegalAccessException if you could not normally access this field
242 * (i.e. it is not public)
243 * @throws IllegalArgumentException if <code>o</code> is not an instance of
244 * the class or interface declaring this field
245 * @throws NullPointerException if <code>o</code> is null and this field
246 * requires an instance
247 * @throws ExceptionInInitializerError if accessing a static field triggered
248 * class initialization, which then failed
249 * @see #getBoolean(Object)
250 * @see #getByte(Object)
251 * @see #getChar(Object)
252 * @see #getShort(Object)
253 * @see #getInt(Object)
254 * @see #getLong(Object)
255 * @see #getFloat(Object)
256 * @see #getDouble(Object)
258 public Object get(Object o)
259 throws IllegalAccessException
265 * Get the value of this boolean Field. If the field is static,
266 * <code>o</code> will be ignored.
268 * @param o the object to get the value of this Field from
269 * @return the value of the Field
270 * @throws IllegalAccessException if you could not normally access this field
271 * (i.e. it is not public)
272 * @throws IllegalArgumentException if this is not a boolean field of
273 * <code>o</code>, or if <code>o</code> is not an instance of the
274 * declaring class of this field
275 * @throws NullPointerException if <code>o</code> is null and this field
276 * requires an instance
277 * @throws ExceptionInInitializerError if accessing a static field triggered
278 * class initialization, which then failed
281 public boolean getBoolean(Object o)
282 throws IllegalAccessException
284 return f.getBoolean(o);
288 * Get the value of this byte Field. If the field is static,
289 * <code>o</code> will be ignored.
291 * @param o the object to get the value of this Field from
292 * @return the value of the Field
293 * @throws IllegalAccessException if you could not normally access this field
294 * (i.e. it is not public)
295 * @throws IllegalArgumentException if this is not a byte field of
296 * <code>o</code>, or if <code>o</code> is not an instance of the
297 * declaring class of this field
298 * @throws NullPointerException if <code>o</code> is null and this field
299 * requires an instance
300 * @throws ExceptionInInitializerError if accessing a static field triggered
301 * class initialization, which then failed
304 public byte getByte(Object o)
305 throws IllegalAccessException
311 * Get the value of this Field as a char. If the field is static,
312 * <code>o</code> will be ignored.
314 * @throws IllegalAccessException if you could not normally access this field
315 * (i.e. it is not public)
316 * @throws IllegalArgumentException if this is not a char field of
317 * <code>o</code>, or if <code>o</code> is not an instance
318 * of the declaring class of this field
319 * @throws NullPointerException if <code>o</code> is null and this field
320 * requires an instance
321 * @throws ExceptionInInitializerError if accessing a static field triggered
322 * class initialization, which then failed
325 public char getChar(Object o)
326 throws IllegalAccessException
332 * Get the value of this Field as a short. If the field is static,
333 * <code>o</code> will be ignored.
335 * @param o the object to get the value of this Field from
336 * @return the value of the Field
337 * @throws IllegalAccessException if you could not normally access this field
338 * (i.e. it is not public)
339 * @throws IllegalArgumentException if this is not a byte or short
340 * field of <code>o</code>, or if <code>o</code> is not an instance
341 * of the declaring class of this field
342 * @throws NullPointerException if <code>o</code> is null and this field
343 * requires an instance
344 * @throws ExceptionInInitializerError if accessing a static field triggered
345 * class initialization, which then failed
348 public short getShort(Object o)
349 throws IllegalAccessException
351 return f.getShort(o);
355 * Get the value of this Field as an int. If the field is static,
356 * <code>o</code> will be ignored.
358 * @param o the object to get the value of this Field from
359 * @return the value of the Field
360 * @throws IllegalAccessException if you could not normally access this field
361 * (i.e. it is not public)
362 * @throws IllegalArgumentException if this is not a byte, short, char, or
363 * int field of <code>o</code>, or if <code>o</code> is not an
364 * instance of the declaring class of this field
365 * @throws NullPointerException if <code>o</code> is null and this field
366 * requires an instance
367 * @throws ExceptionInInitializerError if accessing a static field triggered
368 * class initialization, which then failed
371 public int getInt(Object o)
372 throws IllegalAccessException
378 * Get the value of this Field as a long. If the field is static,
379 * <code>o</code> will be ignored.
381 * @param o the object to get the value of this Field from
382 * @return the value of the Field
383 * @throws IllegalAccessException if you could not normally access this field
384 * (i.e. it is not public)
385 * @throws IllegalArgumentException if this is not a byte, short, char, int,
386 * or long field of <code>o</code>, or if <code>o</code> is not an
387 * instance of the declaring class of this field
388 * @throws NullPointerException if <code>o</code> is null and this field
389 * requires an instance
390 * @throws ExceptionInInitializerError if accessing a static field triggered
391 * class initialization, which then failed
394 public long getLong(Object o)
395 throws IllegalAccessException
401 * Get the value of this Field as a float. If the field is static,
402 * <code>o</code> will be ignored.
404 * @param o the object to get the value of this Field from
405 * @return the value of the Field
406 * @throws IllegalAccessException if you could not normally access this field
407 * (i.e. it is not public)
408 * @throws IllegalArgumentException if this is not a byte, short, char, int,
409 * long, or float field of <code>o</code>, or if <code>o</code> is
410 * not an instance of the declaring class of this field
411 * @throws NullPointerException if <code>o</code> is null and this field
412 * requires an instance
413 * @throws ExceptionInInitializerError if accessing a static field triggered
414 * class initialization, which then failed
417 public float getFloat(Object o)
418 throws IllegalAccessException
420 return f.getFloat(o);
424 * Get the value of this Field as a double. If the field is static,
425 * <code>o</code> will be ignored.
427 * @param o the object to get the value of this Field from
428 * @return the value of the Field
429 * @throws IllegalAccessException if you could not normally access this field
430 * (i.e. it is not public)
431 * @throws IllegalArgumentException if this is not a byte, short, char, int,
432 * long, float, or double field of <code>o</code>, or if
433 * <code>o</code> is not an instance of the declaring class of this
435 * @throws NullPointerException if <code>o</code> is null and this field
436 * requires an instance
437 * @throws ExceptionInInitializerError if accessing a static field triggered
438 * class initialization, which then failed
441 public double getDouble(Object o)
442 throws IllegalAccessException
444 return f.getDouble(o);
448 * Set the value of this Field. If it is a primitive field, the value
449 * will be unwrapped from the passed object (boolean = java.lang.Boolean).<p>
451 * If the field is static, <code>o</code> will be ignored. Otherwise, if
452 * <code>o</code> is null, you get a <code>NullPointerException</code>,
453 * and if it is incompatible with the declaring class of the field, you
454 * get an <code>IllegalArgumentException</code>.<p>
456 * Next, if this Field enforces access control, your runtime context is
457 * evaluated, and you may have an <code>IllegalAccessException</code> if
458 * you could not access this field in similar compiled code. This also
459 * occurs whether or not there is access control if the field is final.
460 * If the field is primitive, and unwrapping your argument fails, you will
461 * get an <code>IllegalArgumentException</code>; likewise, this error
462 * happens if <code>value</code> cannot be cast to the correct object type.
463 * If the field is static, and its class is uninitialized, you trigger class
464 * initialization, which may end in a
465 * <code>ExceptionInInitializerError</code>.<p>
467 * Finally, the field is set with the widened value. This method accesses
468 * the field of the declaring class, even if the instance passed in belongs
469 * to a subclass which declares another field to hide this one.
471 * @param o the object to set this Field on
472 * @param value the value to set this Field to
473 * @throws IllegalAccessException if you could not normally access this field
474 * (i.e. it is not public)
475 * @throws IllegalArgumentException if <code>value</code> cannot be
476 * converted by a widening conversion to the underlying type of
477 * the Field, or if <code>o</code> is not an instance of the class
478 * declaring this field
479 * @throws NullPointerException if <code>o</code> is null and this field
480 * requires an instance
481 * @throws ExceptionInInitializerError if accessing a static field triggered
482 * class initialization, which then failed
483 * @see #setBoolean(Object, boolean)
484 * @see #setByte(Object, byte)
485 * @see #setChar(Object, char)
486 * @see #setShort(Object, short)
487 * @see #setInt(Object, int)
488 * @see #setLong(Object, long)
489 * @see #setFloat(Object, float)
490 * @see #setDouble(Object, double)
492 public void set(Object o, Object value)
493 throws IllegalAccessException
499 * Set this boolean Field. If the field is static, <code>o</code> will be
502 * @param o the object to set this Field on
503 * @param value the value to set this Field to
504 * @throws IllegalAccessException if you could not normally access this field
505 * (i.e. it is not public)
506 * @throws IllegalArgumentException if this is not a boolean field, or if
507 * <code>o</code> is not an instance of the class declaring this
509 * @throws NullPointerException if <code>o</code> is null and this field
510 * requires an instance
511 * @throws ExceptionInInitializerError if accessing a static field triggered
512 * class initialization, which then failed
513 * @see #set(Object, Object)
515 public void setBoolean(Object o, boolean value)
516 throws IllegalAccessException
518 f.setBoolean(o, value);
522 * Set this byte Field. If the field is static, <code>o</code> will be
525 * @param o the object to set this Field on
526 * @param value the value to set this Field to
527 * @throws IllegalAccessException if you could not normally access this field
528 * (i.e. it is not public)
529 * @throws IllegalArgumentException if this is not a byte, short, int, long,
530 * float, or double field, or if <code>o</code> is not an instance
531 * of the class declaring this field
532 * @throws NullPointerException if <code>o</code> is null and this field
533 * requires an instance
534 * @throws ExceptionInInitializerError if accessing a static field triggered
535 * class initialization, which then failed
536 * @see #set(Object, Object)
538 public void setByte(Object o, byte value)
539 throws IllegalAccessException
545 * Set this char Field. If the field is static, <code>o</code> will be
548 * @param o the object to set this Field on
549 * @param value the value to set this Field to
550 * @throws IllegalAccessException if you could not normally access this field
551 * (i.e. it is not public)
552 * @throws IllegalArgumentException if this is not a char, int, long,
553 * float, or double field, or if <code>o</code> is not an instance
554 * of the class declaring this field
555 * @throws NullPointerException if <code>o</code> is null and this field
556 * requires an instance
557 * @throws ExceptionInInitializerError if accessing a static field triggered
558 * class initialization, which then failed
559 * @see #set(Object, Object)
561 public void setChar(Object o, char value)
562 throws IllegalAccessException
568 * Set this short Field. If the field is static, <code>o</code> will be
571 * @param o the object to set this Field on
572 * @param value the value to set this Field to
573 * @throws IllegalAccessException if you could not normally access this field
574 * (i.e. it is not public)
575 * @throws IllegalArgumentException if this is not a short, int, long,
576 * float, or double field, or if <code>o</code> is not an instance
577 * of the class declaring this field
578 * @throws NullPointerException if <code>o</code> is null and this field
579 * requires an instance
580 * @throws ExceptionInInitializerError if accessing a static field triggered
581 * class initialization, which then failed
582 * @see #set(Object, Object)
584 public void setShort(Object o, short value)
585 throws IllegalAccessException
587 f.setShort(o, value);
591 * Set this int Field. If the field is static, <code>o</code> will be
594 * @param o the object to set this Field on
595 * @param value the value to set this Field to
596 * @throws IllegalAccessException if you could not normally access this field
597 * (i.e. it is not public)
598 * @throws IllegalArgumentException if this is not an int, long, float, or
599 * double field, or if <code>o</code> is not an instance of the
600 * class declaring this field
601 * @throws NullPointerException if <code>o</code> is null and this field
602 * requires an instance
603 * @throws ExceptionInInitializerError if accessing a static field triggered
604 * class initialization, which then failed
605 * @see #set(Object, Object)
607 public void setInt(Object o, int value)
608 throws IllegalAccessException
614 * Set this long Field. If the field is static, <code>o</code> will be
617 * @param o the object to set this Field on
618 * @param value the value to set this Field to
619 * @throws IllegalAccessException if you could not normally access this field
620 * (i.e. it is not public)
621 * @throws IllegalArgumentException if this is not a long, float, or double
622 * field, or if <code>o</code> is not an instance of the class
623 * declaring this field
624 * @throws NullPointerException if <code>o</code> is null and this field
625 * requires an instance
626 * @throws ExceptionInInitializerError if accessing a static field triggered
627 * class initialization, which then failed
628 * @see #set(Object, Object)
630 public void setLong(Object o, long value)
631 throws IllegalAccessException
637 * Set this float Field. If the field is static, <code>o</code> will be
640 * @param o the object to set this Field on
641 * @param value the value to set this Field to
642 * @throws IllegalAccessException if you could not normally access this field
643 * (i.e. it is not public)
644 * @throws IllegalArgumentException if this is not a float or long field, or
645 * if <code>o</code> is not an instance of the class declaring this
647 * @throws NullPointerException if <code>o</code> is null and this field
648 * requires an instance
649 * @throws ExceptionInInitializerError if accessing a static field triggered
650 * class initialization, which then failed
651 * @see #set(Object, Object)
653 public void setFloat(Object o, float value)
654 throws IllegalAccessException
656 f.setFloat(o, value);
660 * Set this double Field. If the field is static, <code>o</code> will be
663 * @param o the object to set this Field on
664 * @param value the value to set this Field to
665 * @throws IllegalAccessException if you could not normally access this field
666 * (i.e. it is not public)
667 * @throws IllegalArgumentException if this is not a double field, or if
668 * <code>o</code> is not an instance of the class declaring this
670 * @throws NullPointerException if <code>o</code> is null and this field
671 * requires an instance
672 * @throws ExceptionInInitializerError if accessing a static field triggered
673 * class initialization, which then failed
674 * @see #set(Object, Object)
676 public void setDouble(Object o, double value)
677 throws IllegalAccessException
679 f.setDouble(o, value);
683 * Return the generic type of the field. If the field type is not a generic
684 * type, the method returns the same as <code>getType()</code>.
686 * @throws GenericSignatureFormatError if the generic signature does
687 * not conform to the format specified in the Virtual Machine
688 * specification, version 3.
691 public Type getGenericType()
695 String signature = f.getSignature();
696 if (signature == null)
698 p = new FieldSignatureParser(getDeclaringClass(),
701 return p.getFieldType();
705 * Returns the element's annotation for the specified annotation type,
706 * or <code>null</code> if no such annotation exists.
708 * @param annotationClass the type of annotation to look for.
709 * @return this element's annotation for the specified type, or
710 * <code>null</code> if no such annotation exists.
711 * @throws NullPointerException if the annotation class is <code>null</code>.
713 @SuppressWarnings("unchecked")
714 public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
716 return (T) f.getAnnotation(annotationClass);
720 * Returns all annotations directly defined by the element. If there are
721 * no annotations directly associated with the element, then a zero-length
722 * array will be returned. The returned array may be modified by the client
723 * code, but this will have no effect on the annotation content of this
724 * class, and hence no effect on the return value of this method for
727 * @return the annotations directly defined by the element.
730 public Annotation[] getDeclaredAnnotations()
732 return f.getDeclaredAnnotations();