Most visited

Recently visited

Added in API level 1

Field

public final class Field
extends AccessibleObject implements Member

java.lang.Object
   ↳ java.lang.reflect.AccessibleObject
     ↳ java.lang.reflect.Field


一个Field提供关于和动态访问一个类或接口的单个字段的信息。 反射的字段可以是类(静态)字段或实例字段。

A Field允许在获取或设置访问操作期间扩展转换,但如果发生缩小转换,则会抛出 IllegalArgumentException

也可以看看:

Summary

Inherited constants

From interface java.lang.reflect.Member

Public methods

boolean equals(Object obj)

将此 Field与指定的对象进行比较。

Object get(Object object)

返回指定对象上由此 Field表示的字段的值。

<A extends Annotation> A getAnnotation(Class<A> annotationType)

如果存在这样的注释,则返回指定类型的此元素的注释,否则返回null。

boolean getBoolean(Object object)

获取静态或实例 boolean字段的值。

byte getByte(Object object)

获取静态或实例 byte字段的值。

char getChar(Object object)

获取类型为 char的静态或实例字段的 char或通过扩展转换可转换为类型 char的另一个基本类型的值。

Annotation[] getDeclaredAnnotations()

返回直接出现在此元素上的所有注释。

Class<?> getDeclaringClass()

返回 Class表示声明此表示的字段的类或接口对象 Field对象。

double getDouble(Object object)

获取类型为 double的静态或实例字段或通过扩展转换可转换为类型 double的另一个基本类型的值。

float getFloat(Object object)

获取类型为 float的静态或实例字段或通过扩展转换可转换为类型 float的另一个原始类型的值。

Type getGenericType()

返回表示 Type对象表示的字段的声明类型的 Field对象。

int getInt(Object object)

获取类型为 int的静态或实例字段的 int或通过扩展转换可转换为类型 int的另一个基本类型的值。

long getLong(Object object)

获取类型为 long的静态或实例字段的 long或通过扩展转换可转换为类型 long的另一个基本类型的值。

int getModifiers()

以整数形式返回此 Field对象表示的字段的Java语言修饰符。

String getName()

返回由此 Field对象表示的字段的名称。

short getShort(Object object)

获取类型为 short的静态或实例字段或通过扩展转换可转换为类型 short的另一个基本类型的值。

Class<?> getType()

返回一个 Class对象标识了此表示的字段的声明类型 Field对象。

int hashCode()

返回此 Field的哈希 Field

boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
boolean isEnumConstant()

如果此字段表示枚举类型的元素,则返回true ; 否则返回false

boolean isSynthetic()

如果此字段是合成字段,则返回true ; 否则返回false

void set(Object object, Object value)

将指定对象参数上此 Field对象表示的字段设置为指定的新值。

void setBoolean(Object object, boolean value)

将字段的值设置为指定对象上的 boolean

void setByte(Object object, byte value)

将字段的值设置为指定对象上的 byte

void setChar(Object object, char value)

将字段的值设置为指定对象上的 char

void setDouble(Object object, double value)

将字段的值设置为指定对象上的 double

void setFloat(Object object, float value)

将字段的值设置为指定对象上的 float

void setInt(Object object, int value)

将字段的值设置为指定对象上的 int

void setLong(Object object, long value)

将字段的值设置为指定对象上的 long

void setShort(Object object, short value)

将字段的值设置为指定对象上的 short

String toGenericString()

返回描述此 Field的字符串,包括其泛型类型。

String toString()

返回描述此 Field的字符串。

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

Public methods

equals

Added in API level 1
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.

get

Added in API level 1
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.

getAnnotation

Added in API level 14
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

getBoolean

Added in API level 1
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.

也可以看看:

getByte

Added in API level 1
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.

也可以看看:

getChar

Added in API level 1
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.

也可以看看:

getDeclaredAnnotations

Added in API level 1
Annotation[] getDeclaredAnnotations ()

返回直接出现在此元素上的所有注释。 与此接口中的其他方法不同,此方法会忽略继承的注释。 (如果此元素上没有注释,则返回长度为零的数组。)此方法的调用方可自由修改返回的数组; 它将不会影响返回给其他调用者的数组。

Returns
Annotation[] All annotations directly present on this element

getDeclaringClass

Added in API level 1
Class<?> getDeclaringClass ()

返回 Class表示声明此表示的字段的类或接口对象 Field对象。

Returns
Class<?> an object representing the declaring class of the underlying member

getDouble

Added in API level 1
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.

也可以看看:

getFloat

Added in API level 1
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.

也可以看看:

getGenericType

Added in API level 1
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

getInt

Added in API level 1
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.

也可以看看:

getLong

Added in API level 1
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.

也可以看看:

getModifiers

Added in API level 1
int getModifiers ()

以整数形式返回此Field对象表示的字段的Java语言修饰符。 应该使用Modifier类来解码修饰符。

Returns
int the Java language modifiers for the underlying member

也可以看看:

getName

Added in API level 1
String getName ()

返回由此 Field对象表示的字段的名称。

Returns
String the simple name of the underlying member

getShort

Added in API level 1
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.

也可以看看:

getType

Added in API level 1
Class<?> getType ()

返回一个 Class对象标识了此表示的字段的声明类型 Field对象。

Returns
Class<?> a Class object identifying the declared type of the field represented by this object

hashCode

Added in API level 1
int hashCode ()

返回此Field的哈希Field 这被计算为基础字段的声明类名称及其名称的排他值或散列码。

Returns
int a hash code value for this object.

isAnnotationPresent

Added in API level 1
boolean isAnnotationPresent (Class<? extends Annotation> annotationType)

Parameters
annotationType Class
Returns
boolean

isEnumConstant

Added in API level 1
boolean isEnumConstant ()

如果此字段表示枚举类型的元素,则返回true ; 否则返回false

Returns
boolean true if and only if this field represents an element of an enumerated type.

isSynthetic

Added in API level 1
boolean isSynthetic ()

如果此字段是合成字段,则返回true ; 否则返回false

Returns
boolean true if and only if this field is a synthetic field as defined by the Java Language Specification.

set

Added in API level 1
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.

setBoolean

Added in API level 1
void setBoolean (Object object, 
                boolean value)

将字段的值设置为指定对象上的boolean 此方法相当于set(obj, zObj) ,其中zObjBoolean对象和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.

也可以看看:

setByte

Added in API level 1
void setByte (Object object, 
                byte value)

将字段的值设置为指定对象上的byte 此方法相当于set(obj, bObj) ,其中bObjByte对象和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.

也可以看看:

setChar

Added in API level 1
void setChar (Object object, 
                char value)

将字段的值设置为指定对象上的char 此方法相当于set(obj, cObj) ,其中cObjCharacter对象和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.

也可以看看:

setDouble

Added in API level 1
void setDouble (Object object, 
                double value)

将字段的值设置为指定对象上的double 此方法相当于set(obj, dObj) ,其中dObjDouble对象和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.

也可以看看:

setFloat

Added in API level 1
void setFloat (Object object, 
                float value)

将字段的值设置为指定对象上的float 此方法相当于set(obj, fObj) ,其中fObjFloat对象和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.

也可以看看:

setInt

Added in API level 1
void setInt (Object object, 
                int value)

将字段的值设置为指定对象上的int 此方法相当于set(obj, iObj) ,其中iObjInteger对象和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.

也可以看看:

setLong

Added in API level 1
void setLong (Object object, 
                long value)

将字段的值设置为指定对象上的long 此方法相当于set(obj, lObj) ,其中lObjLong对象和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.

也可以看看:

setShort

Added in API level 1
void setShort (Object object, 
                short value)

将字段的值设置为指定对象上的short 此方法相当于set(obj, sObj) ,其中sObjShort对象和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.

也可以看看:

toGenericString

Added in API level 1
String toGenericString ()

返回描述此Field的字符串,包括其泛型类型。 格式是字段的访问修饰符(如果有的话),后面是通用字段类型,后跟一个空格,随后是声明字段的类的完全限定名称,后跟句点,后跟名称领域。

修饰符按照“Java语言规范”中规定的规范顺序放置。 这是publicprotectedprivate以下顺序,然后再其它改性剂: staticfinaltransientvolatile

Returns
String a string describing this Field, including its generic type

toString

Added in API level 1
String toString ()

返回描述这个Field的字符串。 格式是字段的访问修饰符(如果有的话),后面是字段类型,后跟空格,后面是声明字段的类的完全限定名称,后跟句点,后跟字段的名称。 例如:

    public static final int java.lang.Thread.MIN_PRIORITY
    private int java.io.FileDescriptor.fd
 

修饰符按照“Java语言规范”中规定的规范顺序放置。 这是publicprotectedprivate以下顺序,然后再其它改性剂: staticfinaltransientvolatile

Returns
String a string representation of the object.

Hooray!