FileDocCategorySizeDatePackage
Field.javaAPI DocAndroid 1.5 API38054Wed May 06 22:41:04 BST 2009java.lang.reflect

Field

public final class Field extends AccessibleObject implements Member
This class represents a field. Information about the field can be accessed, and the field's value can be accessed dynamically.
since
Android 1.0

Fields Summary
private Class
declaringClass
private Class
type
private Type
genericType
private volatile boolean
genericTypesAreInitialized
private String
name
private int
slot
private static final int
TYPE_BOOLEAN
private static final int
TYPE_BYTE
private static final int
TYPE_CHAR
private static final int
TYPE_SHORT
private static final int
TYPE_INTEGER
private static final int
TYPE_FLOAT
private static final int
TYPE_LONG
private static final int
TYPE_DOUBLE
Constructors Summary
Field(Field orig)
Construct a clone of the given instance.

param
orig non-null; the original instance to clone


                        
    /*package*/   
        this(orig.declaringClass, orig.type, orig.name, orig.slot);
    
        // Copy the accessible flag.
        if (orig.flag) {
            this.flag = true;
        }
    
private Field(Class declaringClass, Class type, String name, int slot)

        this.declaringClass = declaringClass;
        this.type = type;
        this.name = name;
        this.slot = slot;
    
Methods Summary
public booleanequals(java.lang.Object object)
Indicates whether or not the specified {@code object} is equal to this field. To be equal, the specified object must be an instance of {@code Field} with the same declaring class, type and name as this field.

param
object the object to compare
return
{@code true} if the specified object is equal to this method, {@code false} otherwise
see
#hashCode
since
Android 1.0

        return object instanceof Field && toString().equals(object.toString());
    
public java.lang.Objectget(java.lang.Object object)
Returns the value of the field in the specified object. This reproduces the effect of {@code object.fieldName}

If the type of this field is a primitive type, the field value is automatically wrapped.

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value, possibly wrapped
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getField(object, declaringClass, type, slot, flag);
    
private native bytegetBField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

public booleangetBoolean(java.lang.Object object)
Returns the value of the field in the specified object as a {@code boolean}. This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getZField(object, declaringClass, type, slot, flag, TYPE_BOOLEAN);
    
public bytegetByte(java.lang.Object object)
Returns the value of the field in the specified object as a {@code byte}. This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getBField(object, declaringClass, type, slot, flag, TYPE_BYTE);
    
private native chargetCField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

public chargetChar(java.lang.Object object)
Returns the value of the field in the specified object as a {@code char}. This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getCField(object, declaringClass, type, slot, flag, TYPE_CHAR);
    
private native doublegetDField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

public java.lang.annotation.Annotation[]getDeclaredAnnotations()

        return getDeclaredAnnotations(declaringClass, slot);
    
private native java.lang.annotation.Annotation[]getDeclaredAnnotations(java.lang.Class declaringClass, int slot)

public java.lang.ClassgetDeclaringClass()
Returns the class that declares this field.

return
the declaring class
since
Android 1.0

        return declaringClass;
    
public doublegetDouble(java.lang.Object object)
Returns the value of the field in the specified object as a {@code double}. This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getDField(object, declaringClass, type, slot, flag, TYPE_DOUBLE);
    
private native floatgetFField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

private native java.lang.ObjectgetField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck)

private native intgetFieldModifiers(java.lang.Class declaringClass, int slot)

public floatgetFloat(java.lang.Object object)
Returns the value of the field in the specified object as a {@code float} . This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getFField(object, declaringClass, type, slot, flag, TYPE_FLOAT);
    
public java.lang.reflect.TypegetGenericType()
Returns the generic type of this field.

return
the generic type
throws
GenericSignatureFormatError if the generic field signature is invalid
throws
TypeNotPresentException if the generic type points to a missing type
throws
MalformedParameterizedTypeException if the generic type points to a type that cannot be instantiated for some reason
since
Android 1.0

        initGenericType();
        return Types.getType(genericType);
    
private native intgetIField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

public intgetInt(java.lang.Object object)
Returns the value of the field in the specified object as an {@code int}. This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getIField(object, declaringClass, type, slot, flag, TYPE_INTEGER);
    
private native longgetJField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

public longgetLong(java.lang.Object object)
Returns the value of the field in the specified object as a {@code long}. This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getJField(object, declaringClass, type, slot, flag, TYPE_LONG);
    
public intgetModifiers()
Returns the modifiers for this field. The {@link Modifier} class should be used to decode the result.

return
the modifiers for this field
see
Modifier
since
Android 1.0

        return getFieldModifiers(declaringClass, slot);
    
public java.lang.StringgetName()
Returns the name of this field.

return
the name of this field
since
Android 1.0

        return name;
    
private native shortgetSField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

public shortgetShort(java.lang.Object object)
Returns the value of the field in the specified object as a {@code short} . This reproduces the effect of {@code object.fieldName}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

param
object the object to access
return
the field value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        return getSField(object, declaringClass, type, slot, flag, TYPE_SHORT);
    
private java.lang.StringgetSignature()
Returns the constructor's signature in non-printable form. This is called (only) from IO native code and needed for deriving the serialVersionUID of the class

return
the constructor's signature.
since
Android 1.0

        return getSignature(type);
    
private native java.lang.Object[]getSignatureAnnotation(java.lang.Class declaringClass, int slot)
Get the Signature annotation for this field. Returns null if not found.

java.lang.StringgetSignatureAttribute()
{@inheritDoc}

        Object[] annotation = getSignatureAnnotation(declaringClass, slot);

        if (annotation == null) {
            return null;
        }

        return StringUtils.combineStrings(annotation);
    
public java.lang.ClassgetType()
Return the {@link Class} associated with the type of this field.

return
the type of this field
since
Android 1.0

        return type;
    
private native booleangetZField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no)

public inthashCode()
Returns an integer hash code for this field. Objects which are equal return the same value for this method.

The hash code for a Field is the exclusive-or combination of the hash code of the field's name and the hash code of the name of its declaring class.

return
the hash code for this field
see
#equals
since
Android 1.0

        // BEGIN android-changed
        return name.hashCode() ^ getDeclaringClass().getName().hashCode();
        // END android-changed
    
private synchronized voidinitGenericType()

        if (!genericTypesAreInitialized) {
            String signatureAttribute = getSignatureAttribute();
            GenericSignatureParser parser = new GenericSignatureParser(
                    VMStack.getCallingClassLoader2());
            parser.parseForField(this.declaringClass, signatureAttribute);
            genericType = parser.fieldType;
            if (genericType == null) {
                genericType = getType();
            }
            genericTypesAreInitialized = true;
        }
    
public booleanisEnumConstant()
Indicates whether or not this field is an enumeration constant.

return
{@code true} if this field is an enumeration constant, {@code false} otherwise
since
Android 1.0

        int flags = getFieldModifiers(declaringClass, slot);
        return (flags & Modifier.ENUM) != 0;
    
public booleanisSynthetic()
Indicates whether or not this field is synthetic.

return
{@code true} if this field is synthetic, {@code false} otherwise
since
Android 1.0

        int flags = getFieldModifiers(declaringClass, slot);
        return (flags & Modifier.SYNTHETIC) != 0;
    
public voidset(java.lang.Object object, java.lang.Object value)
Sets the value of the field in the specified object to the value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the field type is a primitive type, the value is automatically unwrapped. If the unwrap fails, an IllegalArgumentException is thrown. If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setField(object, declaringClass, type, slot, flag, value);
    
private native voidsetBField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, byte b)

public voidsetBoolean(java.lang.Object object, boolean value)
Sets the value of the field in the specified object to the {@code boolean} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setZField(object, declaringClass, type, slot, flag, TYPE_BOOLEAN, value);
    
public voidsetByte(java.lang.Object object, byte value)
Sets the value of the field in the specified object to the {@code byte} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setBField(object, declaringClass, type, slot, flag, TYPE_BYTE, value);
    
private native voidsetCField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, char c)

public voidsetChar(java.lang.Object object, char value)
Sets the value of the field in the specified object to the {@code char} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setCField(object, declaringClass, type, slot, flag, TYPE_CHAR, value);
    
private native voidsetDField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, double v)

public voidsetDouble(java.lang.Object object, double value)
Sets the value of the field in the specified object to the {@code double} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setDField(object, declaringClass, type, slot, flag, TYPE_DOUBLE, value);
    
private native voidsetFField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, float f)

private native voidsetField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, java.lang.Object value)

public voidsetFloat(java.lang.Object object, float value)
Sets the value of the field in the specified object to the {@code float} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setFField(object, declaringClass, type, slot, flag, TYPE_FLOAT, value);
    
private native voidsetIField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, int i)

public voidsetInt(java.lang.Object object, int value)
Set the value of the field in the specified object to the {@code int} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setIField(object, declaringClass, type, slot, flag, TYPE_INTEGER, value);
    
private native voidsetJField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, long j)

public voidsetLong(java.lang.Object object, long value)
Sets the value of the field in the specified object to the {@code long} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setJField(object, declaringClass, type, slot, flag, TYPE_LONG, value);
    
private native voidsetSField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, short s)

public voidsetShort(java.lang.Object object, short value)
Sets the value of the field in the specified object to the {@code short} value. This reproduces the effect of {@code object.fieldName = value}

If this field is static, the object argument is ignored. Otherwise, if the object is {@code null}, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

param
object the object to access
param
value the new value
throws
NullPointerException if the object is {@code null} and the field is non-static
throws
IllegalArgumentException if the object is not compatible with the declaring class
throws
IllegalAccessException if this field is not accessible
since
Android 1.0

        setSField(object, declaringClass, type, slot, flag, TYPE_SHORT, value);
    
private native voidsetZField(java.lang.Object o, java.lang.Class declaringClass, java.lang.Class type, int slot, boolean noAccessCheck, int type_no, boolean z)

public java.lang.StringtoGenericString()
Returns the string representation of this field, including the field's generic type.

return
the string representation of this field
since
Android 1.0

        StringBuilder sb = new StringBuilder(80);
        // append modifiers if any
        int modifier = getModifiers();
        if (modifier != 0) {
            sb.append(Modifier.toString(modifier)).append(' ");
        }
        // append generic type
        appendGenericType(sb, getGenericType());
        sb.append(' ");
        // append full field name
        sb.append(getDeclaringClass().getName()).append('.").append(getName());
        return sb.toString();
    
public java.lang.StringtoString()
Returns a string containing a concise, human-readable description of this field.

The format of the string is:

  1. modifiers (if any)
  2. type
  3. declaring class name
  4. '.'
  5. field name

For example: {@code public static java.io.InputStream java.lang.System.in}

return
a printable representation for this field
since
Android 1.0

        StringBuffer result = new StringBuffer(Modifier.toString(getModifiers()));

        if (result.length() != 0)
            result.append(' ");
        result.append(type.getName());
        result.append(' ");
        result.append(declaringClass.getName());
        result.append('.");
        result.append(name);

        return result.toString();