public final class Field
extends AccessibleObject implements Member
| java.lang.Object | ||
| java.lang.reflect.AccessibleObject | ||
| java.lang.reflect.Field | ||
一个Field提供关于和动态访问一个类或接口的单个字段的信息。 反射的字段可以是类(静态)字段或实例字段。
A Field允许在获取或设置访问操作期间扩展转换,但如果发生缩小转换,则会抛出 IllegalArgumentException 。
Inherited constants |
|---|
java.lang.reflect.Member
|
公共方法(Public methods) |
|
|---|---|
boolean |
equals(Object obj) 将此 |
Object |
get(Object object) 返回指定对象上由此 |
<A extends Annotation> A |
getAnnotation(Class<A> annotationType) 如果存在这样的注释,则返回指定类型的此元素的注释,否则返回null。 |
boolean |
getBoolean(Object object) 获取静态或实例 |
byte |
getByte(Object object) 获取静态或实例 |
char |
getChar(Object object) 获取类型为 |
Annotation[] |
getDeclaredAnnotations() 返回直接出现在此元素上的所有注释。 |
Class<?> |
getDeclaringClass() 返回 |
double |
getDouble(Object object) 获取类型为 |
float |
getFloat(Object object) 获取类型为 |
Type |
getGenericType() 返回表示 |
int |
getInt(Object object) 获取类型为 |
long |
getLong(Object object) 获取类型为 |
int |
getModifiers() 以整数形式返回此 |
String |
getName() 返回由此 |
short |
getShort(Object object) 获取类型为 |
Class<?> |
getType() 返回一个 |
int |
hashCode() 返回此 |
boolean |
isAnnotationPresent(Class<? extends Annotation> annotationType) |
boolean |
isEnumConstant() 如果此字段表示枚举类型的元素,则返回 |
boolean |
isSynthetic() 如果此字段是合成字段,则返回 |
void |
set(Object object, Object value) 将指定对象参数上此 |
void |
setBoolean(Object object, boolean value) 将字段的值设置为指定对象上的 |
void |
setByte(Object object, byte value) 将字段的值设置为指定对象上的 |
void |
setChar(Object object, char value) 将字段的值设置为指定对象上的 |
void |
setDouble(Object object, double value) 将字段的值设置为指定对象上的 |
void |
setFloat(Object object, float value) 将字段的值设置为指定对象上的 |
void |
setInt(Object object, int value) 将字段的值设置为指定对象上的 |
void |
setLong(Object object, long value) 将字段的值设置为指定对象上的 |
void |
setShort(Object object, short value) 将字段的值设置为指定对象上的 |
String |
toGenericString() 返回描述此 |
String |
toString() 返回描述此 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.reflect.AccessibleObject
|
|
java.lang.Object
|
|
java.lang.reflect.AnnotatedElement
|
|
java.lang.reflect.Member
|
|
boolean equals (Object obj)
将此Field与指定的对象进行比较。 如果对象相同,则返回true。 如果两个Field对象由同一个类声明并具有相同的名称和类型,则它们是相同的。
| 参数(Parameters) | |
|---|---|
obj |
Object: the reference object with which to compare. |
| 返回(Returns) | |
|---|---|
boolean |
true if this object is the same as the obj argument; false otherwise. |
Object get (Object object)
返回指定对象上由此Field表示的字段的值。 如果该对象具有基本类型,则该值将自动包装在对象中。
基础领域的价值如下获得:
如果基础字段是静态字段,则忽略obj参数; 它可能为空。
否则,基础字段是一个实例字段。 如果指定的obj参数为null,则该方法将引发NullPointerException 。 如果指定的对象不是声明基础字段的类或接口的实例,则该方法抛出IllegalArgumentException 。
如果此Field对象正在实施Java语言访问控制,并且基础字段不可访问,则该方法会抛出IllegalAccessException 。 如果底层字段是静态的,那么声明该字段的类如果尚未初始化,则会进行初始化。
否则,该值将从底层实例或静态字段中检索。 如果该字段具有原始类型,则在返回之前将值包装在对象中,否则将按原样返回。
如果字段以 obj的类型隐藏,则根据上述规则获取该字段的值。
| 参数(Parameters) | |
|---|---|
object |
Object: object from which the represented field's value is to be extracted |
| 返回(Returns) | |
|---|---|
Object |
the value of the represented field in object obj; primitive values are wrapped in an appropriate object before being returned |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof). |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
A getAnnotation (Class<A> annotationType)
如果存在这样的注释,则返回指定类型的此元素的注释,否则返回null。
| 参数(Parameters) | |
|---|---|
annotationType |
Class: the Class object corresponding to the annotation type |
| 返回(Returns) | |
|---|---|
A |
this element's annotation for the specified annotation type if present on this element, else null |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
|
boolean getBoolean (Object object)
获取静态或实例 boolean字段的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the boolean value from |
| 返回(Returns) | |
|---|---|
boolean |
the value of the boolean field |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type boolean by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
byte getByte (Object object)
获取静态或实例 byte字段的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the byte value from |
| 返回(Returns) | |
|---|---|
byte |
the value of the byte field |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type byte by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
char getChar (Object object)
通过扩展转换获取类型为 char的静态或实例字段或可转换为类型 char的另一个原始类型的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the char value from |
| 返回(Returns) | |
|---|---|
char |
the value of the field converted to type char |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type char by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
Annotation[] getDeclaredAnnotations ()
返回直接出现在此元素上的所有注释。 与此接口中的其他方法不同,此方法会忽略继承的注释。 (如果此元素上没有注释,则返回长度为零的数组。)此方法的调用方可自由修改返回的数组; 它将不会影响返回给其他调用者的数组。
| 返回(Returns) | |
|---|---|
Annotation[] |
All annotations directly present on this element |
Class<?> getDeclaringClass ()
返回 Class表示声明此表示的字段的类或接口对象 Field对象。
| 返回(Returns) | |
|---|---|
Class<?> |
an object representing the declaring class of the underlying member |
double getDouble (Object object)
通过扩展转换获取类型为 double的静态或实例字段的 double或可转换为类型 double的另一个基本类型的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the double value from |
| 返回(Returns) | |
|---|---|
double |
the value of the field converted to type double |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type double by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
float getFloat (Object object)
获取类型为 float的静态或实例字段或通过扩展转换可转换为类型 float的另一个基本类型的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the float value from |
| 返回(Returns) | |
|---|---|
float |
the value of the field converted to type float |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type float by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
Type getGenericType ()
返回表示 Type对象表示的字段的声明类型的 Field对象。
如果 Type是参数化类型,则返回的 Type对象必须准确反映源代码中使用的实际类型参数。
如果基础字段的类型是类型变量或参数化类型,则会创建它。 否则,它已解决。
| 返回(Returns) | |
|---|---|
Type |
a Type object that represents the declared type for the field represented by this Field object |
| 抛出异常(Throws) | |
|---|---|
GenericSignatureFormatError |
if the generic field signature does not conform to the format specified in The JavaTM Virtual Machine Specification |
TypeNotPresentException |
if the generic type signature of the underlying field refers to a non-existent type declaration |
MalformedParameterizedTypeException |
if the generic signature of the underlying field refers to a parameterized type that cannot be instantiated for any reason |
int getInt (Object object)
通过扩展转换获取类型为 int的静态或实例字段或可转换为类型 int的另一个基本类型的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the int value from |
| 返回(Returns) | |
|---|---|
int |
the value of the field converted to type int |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type int by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
long getLong (Object object)
获取类型为 long的静态或实例字段的 long或通过扩展转换可转换为类型 long的另一个基本类型的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the long value from |
| 返回(Returns) | |
|---|---|
long |
the value of the field converted to type long |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type long by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
int getModifiers ()
以整数形式返回此Field对象表示的字段的Java语言修饰符。 应该使用Modifier类来解码修饰符。
| 返回(Returns) | |
|---|---|
int |
the Java language modifiers for the underlying member |
也可以看看:
String getName ()
返回由此 Field对象表示的字段的名称。
| 返回(Returns) | |
|---|---|
String |
the simple name of the underlying member |
short getShort (Object object)
通过扩展转换获取类型为 short的静态或实例字段或可转换为类型为 short的另一个原始类型的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object to extract the short value from |
| 返回(Returns) | |
|---|---|
short |
the value of the field converted to type short |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is inaccessible. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if the field value cannot be converted to the type short by a widening conversion. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
Class<?> getType ()
返回一个 Class对象标识了此表示的字段的声明类型 Field对象。
| 返回(Returns) | |
|---|---|
Class<?> |
a Class object identifying the declared type of the field represented by this object |
int hashCode ()
返回此Field的哈希Field 。 这被计算为基础字段的声明类名称及其名称的排他值或散列码。
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
boolean isAnnotationPresent (Class<? extends Annotation> annotationType)
| 参数(Parameters) | |
|---|---|
annotationType |
Class
|
| 返回(Returns) | |
|---|---|
boolean |
|
boolean isEnumConstant ()
如果此字段表示枚举类型的元素,则返回true ; 否则返回false 。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this field represents an element of an enumerated type. |
boolean isSynthetic ()
如果此字段是合成字段,则返回true ; 否则返回false 。
| 返回(Returns) | |
|---|---|
boolean |
true if and only if this field is a synthetic field as defined by the Java Language Specification. |
void set (Object object, Object value)
将指定对象参数上此Field对象表示的字段设置为指定的新值。 如果基础字段具有原始类型,则新值将自动解包。
操作过程如下:
如果底层字段是静态的,则obj参数将被忽略; 它可能为空。
否则,基础字段是一个实例字段。 如果指定的对象参数为null,则该方法将引发NullPointerException 。 如果指定的对象参数不是声明基础字段的类或接口的实例,则该方法会抛出IllegalArgumentException 。
如果此 Field对象正在实施Java语言访问控制,并且基础字段不可访问,则该方法会抛出 IllegalAccessException 。
如果底层字段是最终的,则该方法将抛出IllegalAccessException除非setAccessible(true)对于此Field对象已成功并且该字段是非静态的。 以这种方式设置最后一个字段仅在反序列化或重构带有空白最终字段的类的实例期间才有意义,然后它们可供程序的其他部分访问。 在任何其他情况下使用可能会产生不可预知的影响,包括程序的其他部分继续使用此字段的原始值的情况。
如果底层字段是原始类型,则会尝试展开转换以将新值转换为基元类型的值。 如果此尝试失败,该方法将抛出IllegalArgumentException 。
如果在可能的解包之后,新值无法通过身份或扩展转换转换为基础字段的类型,则此方法会抛出 IllegalArgumentException 。
如果底层字段是静态的,那么声明该字段的类如果尚未初始化,则会进行初始化。
该字段设置为可能展开和扩大的新值。
如果该字段以 obj类型隐藏,则根据上述规则设置该字段的值。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
Object: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
void setBoolean (Object object, boolean value)
将字段的值设置为指定对象上的boolean 。 此方法相当于set(obj, zObj) ,其中zObj是Boolean对象和zObj.booleanValue() == z 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
boolean: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
void setByte (Object object, byte value)
将字段的值设置为指定对象上的byte 。 此方法相当于set(obj, bObj) ,其中bObj是Byte对象和bObj.byteValue() == b 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
byte: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
void setChar (Object object, char value)
将字段的值设置为指定对象上的char 。 此方法相当于set(obj, cObj) ,其中cObj是Character对象和cObj.charValue() == c 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
char: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
void setDouble (Object object, double value)
将字段的值设置为指定对象上的double 。 此方法相当于set(obj, dObj) ,其中dObj是Double对象和dObj.doubleValue() == d 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
double: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
void setFloat (Object object, float value)
将字段的值设置为指定对象上的float 。 此方法相当于set(obj, fObj) ,其中fObj是Float对象和fObj.floatValue() == f 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
float: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
void setInt (Object object, int value)
将字段的值设置为指定对象上的int 。 此方法相当于set(obj, iObj) ,其中iObj是Integer对象和iObj.intValue() == i 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
int: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
void setLong (Object object, long value)
将字段的值设置为指定对象上的long 。 此方法相当于set(obj, lObj) ,其中lObj是Long对象和lObj.longValue() == l 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
long: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
void setShort (Object object, short value)
将字段的值设置为指定对象上的short 。 此方法相当于set(obj, sObj) ,其中sObj是Short对象和sObj.shortValue() == s 。
| 参数(Parameters) | |
|---|---|
object |
Object: the object whose field should be modified |
value |
short: the new value for the field of obj being modified |
| 抛出异常(Throws) | |
|---|---|
IllegalAccessException |
if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. |
IllegalArgumentException |
if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails. |
NullPointerException |
if the specified object is null and the field is an instance field. |
ExceptionInInitializerError |
if the initialization provoked by this method fails. |
也可以看看:
String toGenericString ()
返回描述此Field的字符串,包括其泛型类型。 格式是字段的访问修饰符(如果有的话),后面是通用字段类型,后跟一个空格,随后是声明字段的类的完全限定名称,后跟句点,后跟名称领域。
修饰符按照“Java语言规范”中规定的规范顺序放置。 这是public , protected或private以下顺序,然后再其它改性剂: static , final , transient , volatile 。
| 返回(Returns) | |
|---|---|
String |
a string describing this Field, including its generic type |
String toString ()
返回描述这个Field的字符串。 格式是字段的访问修饰符(如果有的话),后面是字段类型,后跟空格,后面是声明字段的类的完全限定名称,后跟句点,后跟字段的名称。 例如:
public static final int java.lang.Thread.MIN_PRIORITY
private int java.io.FileDescriptor.fd
修饰符按照“Java语言规范”中规定的规范顺序放置。 这是public , protected或private以下顺序,然后再其它改性剂: static , final , transient , volatile 。
| 返回(Returns) | |
|---|---|
String |
a string representation of the object. |