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 |
---|
From interface 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 |
|
---|---|
From class java.lang.reflect.AccessibleObject
|
|
From class java.lang.Object
|
|
From interface java.lang.reflect.AnnotatedElement
|
|
From interface 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 Java™ 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. |