java.lang.reflect
public final class Field extends AccessibleObject implements Member
Note: This class returns and accepts types as Classes, even
primitive types; there are Class types defined that represent each
different primitive type. They are java.lang.Boolean.TYPE,
java.lang.Byte.TYPE,
, also available as boolean.class,
byte.class
, etc. These are not to be confused with the
classes java.lang.Boolean, java.lang.Byte
, etc., which are
real classes.
Also note that this is not a serializable class. It is entirely feasible to make it serializable using the Externalizable interface, but this is on Sun, not me.
Since: 1.1
See Also: Member Class getField getDeclaredField getFields getDeclaredFields
UNKNOWN: updated to 1.4
Method Summary | |
---|---|
boolean | equals(Object fld)
Compare two objects to see if they are semantically equivalent.
|
Object | get(Object obj)
Get the value of this Field. |
<T extends Annotation> T | getAnnotation(Class<T> annoClass) |
boolean | getBoolean(Object obj)
Get the value of this boolean Field. |
byte | getByte(Object obj)
Get the value of this byte Field. |
char | getChar(Object obj)
Get the value of this Field as a char. |
Annotation[] | getDeclaredAnnotations() |
Class<?> | getDeclaringClass()
Gets the class that declared this field, or the class where this field
is a non-inherited member. |
double | getDouble(Object obj)
Get the value of this Field as a double. |
float | getFloat(Object obj)
Get the value of this Field as a float. |
Type | getGenericType()
Return the generic type of the field. |
int | getInt(Object obj)
Get the value of this Field as an int. |
long | getLong(Object obj)
Get the value of this Field as a long. |
int | getModifiers()
Gets the modifiers this field uses. |
String | getName()
Gets the name of this field. |
short | getShort(Object obj)
Get the value of this Field as a short. |
Class<?> | getType()
Gets the type of this field. |
int | hashCode()
Get the hash code for the Field. |
boolean | isEnumConstant()
Return true if this field represents an enum constant,
false otherwise. |
boolean | isSynthetic()
Return true if this field is synthetic, false otherwise. |
void | set(Object object, Object value)
Set the value of this Field. |
void | setBoolean(Object obj, boolean b)
Set this boolean Field. |
void | setByte(Object obj, byte b)
Set this byte Field. |
void | setChar(Object obj, char c)
Set this char Field. |
void | setDouble(Object obj, double d)
Set this double Field. |
void | setFloat(Object obj, float f)
Set this float Field. |
void | setInt(Object obj, int i)
Set this int Field. |
void | setLong(Object obj, long l)
Set this long Field. |
void | setShort(Object obj, short s)
Set this short Field. |
String | toGenericString() |
String | toString()
Get a String representation of the Field. |
Parameters: o the object to compare to
Returns: true
if they are equal; false
if not
If the field is static, o
will be ignored. Otherwise, if
o
is null, you get a NullPointerException
,
and if it is incompatible with the declaring class of the field, you
get an IllegalArgumentException
.
Next, if this Field enforces access control, your runtime context is
evaluated, and you may have an IllegalAccessException
if
you could not access this field in similar compiled code. If the field
is static, and its class is uninitialized, you trigger class
initialization, which may end in a
ExceptionInInitializerError
.
Finally, the field is accessed, and primitives are wrapped (but not necessarily in new objects). This method accesses the field of the declaring class, even if the instance passed in belongs to a subclass which declares another field to hide this one.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if o
is not an instance of
the class or interface declaring this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: getBoolean getByte getChar getShort getInt getLong getFloat getDouble
o
will be ignored.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a boolean field of
o
, or if o
is not an instance of the
declaring class of this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
o
will be ignored.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a byte field of
o
, or if o
is not an instance of the
declaring class of this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
o
will be ignored.
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a char field of
o
, or if o
is not an instance
of the declaring class of this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
Returns: the class that declared this member
o
will be ignored.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a byte, short, char, int,
long, float, or double field of o
, or if
o
is not an instance of the declaring class of this
field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
o
will be ignored.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a byte, short, char, int,
long, or float field of o
, or if o
is
not an instance of the declaring class of this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
getType()
.
Throws: GenericSignatureFormatError if the generic signature does not conform to the format specified in the Virtual Machine specification, version 3.
Since: 1.5
o
will be ignored.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a byte, short, char, or
int field of o
, or if o
is not an
instance of the declaring class of this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
o
will be ignored.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a byte, short, char, int,
or long field of o
, or if o
is not an
instance of the declaring class of this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
Modifier
class to interpret the values. A field can only have a subset of the
following modifiers: public, private, protected, static, final,
transient, and volatile.
Returns: an integer representing the modifiers to this Member
See Also: Modifier
Returns: the name of this field
o
will be ignored.
Parameters: o the object to get the value of this Field from
Returns: the value of the Field
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a byte or short
field of o
, or if o
is not an instance
of the declaring class of this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: get
Returns: the type of this field
Returns: the hash code for the object.
Since: 1.5
Since: 1.5
If the field is static, o
will be ignored. Otherwise, if
o
is null, you get a NullPointerException
,
and if it is incompatible with the declaring class of the field, you
get an IllegalArgumentException
.
Next, if this Field enforces access control, your runtime context is
evaluated, and you may have an IllegalAccessException
if
you could not access this field in similar compiled code. This also
occurs whether or not there is access control if the field is final.
If the field is primitive, and unwrapping your argument fails, you will
get an IllegalArgumentException
; likewise, this error
happens if value
cannot be cast to the correct object type.
If the field is static, and its class is uninitialized, you trigger class
initialization, which may end in a
ExceptionInInitializerError
.
Finally, the field is set with the widened value. This method accesses the field of the declaring class, even if the instance passed in belongs to a subclass which declares another field to hide this one.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if value
cannot be
converted by a widening conversion to the underlying type of
the Field, or if o
is not an instance of the class
declaring this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a boolean field, or if
o
is not an instance of the class declaring this
field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a byte, short, int, long,
float, or double field, or if o
is not an instance
of the class declaring this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a char, int, long,
float, or double field, or if o
is not an instance
of the class declaring this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a double field, or if
o
is not an instance of the class declaring this
field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a float or long field, or
if o
is not an instance of the class declaring this
field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not an int, long, float, or
double field, or if o
is not an instance of the
class declaring this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a long, float, or double
field, or if o
is not an instance of the class
declaring this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
o
will be
ignored.
Parameters: o the object to set this Field on value the value to set this Field to
Throws: IllegalAccessException if you could not normally access this field
(i.e. it is not public) IllegalArgumentException if this is not a short, int, long,
float, or double field, or if o
is not an instance
of the class declaring this field NullPointerException if o
is null and this field
requires an instance ExceptionInInitializerError if accessing a static field triggered
class initialization, which then failed
See Also: Field
public transient boolean gnu.parse.Parser.parseComplete
Returns: the String representation of the Field