public final class Field extends AccessibleObject implements Member
IllegalArgumentException if
a narrowing conversion would be necessary. You can query for information
on this Field regardless of location, but get and set access may be limited
by Java language access controls. If you can't do it in the compiler, you
can't normally do it here either.
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.
Member,
Class,
Class.getField(String),
Class.getDeclaredField(String),
Class.getFields(),
Class.getDeclaredFields()| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(java.lang.Object o)
Compare two objects to see if they are semantically equivalent.
|
java.lang.Object |
get(java.lang.Object o)
Get the value of this Field.
|
<T extends java.lang.annotation.Annotation> |
getAnnotation(java.lang.Class<T> annotationClass)
Returns the element's annotation for the specified annotation type,
or
null if no such annotation exists. |
boolean |
getBoolean(java.lang.Object o)
Get the value of this boolean Field.
|
byte |
getByte(java.lang.Object o)
Get the value of this byte Field.
|
char |
getChar(java.lang.Object o)
Get the value of this Field as a char.
|
java.lang.annotation.Annotation[] |
getDeclaredAnnotations()
Returns all annotations directly defined by the element.
|
java.lang.Class<?> |
getDeclaringClass()
Gets the class that declared this field, or the class where this field
is a non-inherited member.
|
double |
getDouble(java.lang.Object o)
Get the value of this Field as a double.
|
float |
getFloat(java.lang.Object o)
Get the value of this Field as a float.
|
Type |
getGenericType()
Return the generic type of the field.
|
int |
getInt(java.lang.Object o)
Get the value of this Field as an int.
|
long |
getLong(java.lang.Object o)
Get the value of this Field as a long.
|
int |
getModifiers()
Gets the modifiers this field uses.
|
java.lang.String |
getName()
Gets the name of this field.
|
short |
getShort(java.lang.Object o)
Get the value of this Field as a short.
|
java.lang.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(java.lang.Object o,
java.lang.Object value)
Set the value of this Field.
|
void |
setBoolean(java.lang.Object o,
boolean value)
Set this boolean Field.
|
void |
setByte(java.lang.Object o,
byte value)
Set this byte Field.
|
void |
setChar(java.lang.Object o,
char value)
Set this char Field.
|
void |
setDouble(java.lang.Object o,
double value)
Set this double Field.
|
void |
setFloat(java.lang.Object o,
float value)
Set this float Field.
|
void |
setInt(java.lang.Object o,
int value)
Set this int Field.
|
void |
setLong(java.lang.Object o,
long value)
Set this long Field.
|
void |
setShort(java.lang.Object o,
short value)
Set this short Field.
|
java.lang.String |
toGenericString() |
java.lang.String |
toString()
Get a String representation of the Field.
|
getAnnotations, isAccessible, isAnnotationPresent, setAccessible, setAccessiblepublic java.lang.Class<?> getDeclaringClass()
getDeclaringClass in interface Memberpublic java.lang.String getName()
public int getModifiers()
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.getModifiers in interface MemberModifierpublic boolean isSynthetic()
isSynthetic in interface Memberpublic boolean isEnumConstant()
public java.lang.Class<?> getType()
public boolean equals(java.lang.Object o)
equals in class java.lang.Objecto - the object to compare totrue if they are equal; false if notObject.hashCode()public int hashCode()
hashCode in class java.lang.ObjectObject.equals(Object),
System.identityHashCode(Object)public java.lang.String toString()
public transient boolean gnu.parse.Parser.parseCompletetoString in class java.lang.ObjectObject.getClass(),
Object.hashCode(),
Class.getName(),
Integer.toHexString(int)public java.lang.String toGenericString()
public java.lang.Object get(java.lang.Object o) throws java.lang.IllegalAccessException
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.
o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if o is not an instance of
the class or interface declaring this fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedgetBoolean(Object),
getByte(Object),
getChar(Object),
getShort(Object),
getInt(Object),
getLong(Object),
getFloat(Object),
getDouble(Object)public boolean getBoolean(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not a boolean field of
o, or if o is not an instance of the
declaring class of this fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public byte getByte(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not a byte field of
o, or if o is not an instance of the
declaring class of this fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public char getChar(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.java.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not a char field of
o, or if o is not an instance
of the declaring class of this fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public short getShort(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public int getInt(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public long getLong(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public float getFloat(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public double getDouble(java.lang.Object o) throws java.lang.IllegalAccessException
o will be ignored.o - the object to get the value of this Field fromjava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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
fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedget(Object)public void set(java.lang.Object o, java.lang.Object value) throws java.lang.IllegalAccessException
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.
o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedsetBoolean(Object, boolean),
setByte(Object, byte),
setChar(Object, char),
setShort(Object, short),
setInt(Object, int),
setLong(Object, long),
setFloat(Object, float),
setDouble(Object, double)public void setBoolean(java.lang.Object o, boolean value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not a boolean field, or if
o is not an instance of the class declaring this
fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public void setByte(java.lang.Object o, byte value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public void setChar(java.lang.Object o, char value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public void setShort(java.lang.Object o, short value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.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 fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public void setInt(java.lang.Object o, int value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not an int, long, float, or
double field, or if o is not an instance of the
class declaring this fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public void setLong(java.lang.Object o, long value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not a long, float, or double
field, or if o is not an instance of the class
declaring this fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public void setFloat(java.lang.Object o, float value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not a float or long field, or
if o is not an instance of the class declaring this
fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public void setDouble(java.lang.Object o, double value) throws java.lang.IllegalAccessException
o will be
ignored.o - the object to set this Field onvalue - the value to set this Field tojava.lang.IllegalAccessException - if you could not normally access this field
(i.e. it is not public)java.lang.IllegalArgumentException - if this is not a double field, or if
o is not an instance of the class declaring this
fieldjava.lang.NullPointerException - if o is null and this field
requires an instancejava.lang.ExceptionInInitializerError - if accessing a static field triggered
class initialization, which then failedset(Object, Object)public Type getGenericType()
getType().GenericSignatureFormatError - if the generic signature does
not conform to the format specified in the Virtual Machine
specification, version 3.public <T extends java.lang.annotation.Annotation> T getAnnotation(java.lang.Class<T> annotationClass)
null if no such annotation exists.getAnnotation in interface AnnotatedElementgetAnnotation in class AccessibleObjectannotationClass - the type of annotation to look for.null if no such annotation exists.java.lang.NullPointerException - if the annotation class is null.public java.lang.annotation.Annotation[] getDeclaredAnnotations()
getDeclaredAnnotations in interface AnnotatedElementgetDeclaredAnnotations in class AccessibleObject