FileDocCategorySizeDatePackage
ObjectStreamField.javaAPI DocAndroid 1.5 API12313Wed May 06 22:41:04 BST 2009java.io

ObjectStreamField

public class ObjectStreamField extends Object implements Comparable
Describes a field for the purpose of serialization. Classes can define the collection of fields that are serialized, which may be different from the set of all declared fields.
see
ObjectOutputStream#writeFields()
see
ObjectInputStream#readFields()
since
Android 1.0

Fields Summary
private String
name
private Object
type
int
offset
private String
typeString
private boolean
unshared
private boolean
isDeserialized
Constructors Summary
public ObjectStreamField(String name, Class cl)
Constructs an ObjectStreamField with the specified name and type.

param
name the name of the field.
param
cl the type of the field.
throws
NullPointerException if {@code name} or {@code cl} is {@code null}.
since
Android 1.0

        if (name == null || cl == null) {
            throw new NullPointerException();
        }
        this.name = name;
        this.type = new WeakReference<Class<?>>(cl);
    
public ObjectStreamField(String name, Class cl, boolean unshared)
Constructs an ObjectStreamField with the specified name, type and the indication if it is unshared.

param
name the name of the field.
param
cl the type of the field.
param
unshared {@code true} if the field is written and read unshared; {@code false} otherwise.
throws
NullPointerException if {@code name} or {@code cl} is {@code null}.
see
ObjectOutputStream#writeUnshared(Object)
since
Android 1.0

        if (name == null || cl == null) {
            throw new NullPointerException();
        }
        this.name = name;
        this.type = (cl.getClassLoader() == null) ? cl
                : new WeakReference<Class<?>>(cl);
        this.unshared = unshared;
    
ObjectStreamField(String signature, String name)
Constructs an ObjectStreamField with the given name and the given type. The type may be null.

param
signature A String representing the type of the field
param
name a String, the name of the field, or null

        if (name == null) {
            throw new NullPointerException();
        }
        this.name = name;
        this.typeString = signature.replace('.", '/").intern();
        this.isDeserialized = true;
    
Methods Summary
public intcompareTo(java.lang.Object o)
Compares this field descriptor to the specified one. Checks first if one of the compared fields has a primitive type and the other one not. If so, the field with the primitive type is considered to be "smaller". If both fields are equal, their names are compared.

param
o the object to compare with.
return
-1 if this field is "smaller" than field {@code o}, 0 if both fields are equal; 1 if this field is "greater" than field {@code o}.
since
Android 1.0

        ObjectStreamField f = (ObjectStreamField) o;
        boolean thisPrimitive = this.isPrimitive();
        boolean fPrimitive = f.isPrimitive();

        // If one is primitive and the other isn't, we have enough info to
        // compare
        if (thisPrimitive != fPrimitive) {
            return thisPrimitive ? -1 : 1;
        }

        // Either both primitives or both not primitives. Compare based on name.
        return this.getName().compareTo(f.getName());
    
public java.lang.StringgetName()
Gets the name of this field.

return
the field's name.
since
Android 1.0

        return name;
    
public intgetOffset()
Gets the offset of this field in the object.

return
this field's offset.
since
Android 1.0

        return offset;
    
public java.lang.ClassgetType()
Gets the type of this field.

return
a {@code Class} object representing the type of the field.
since
Android 1.0

        Class<?> cl = getTypeInternal();
        if (isDeserialized && !cl.isPrimitive()) {
            return Object.class;
        }
        return cl;
    
public chargetTypeCode()
Gets a character code for the type of this field. The following codes are used:
B byte
C char
D double
F float
I int
J long
L class or interface
S short
Z boolean
[ array

return
the field's type code.
since
Android 1.0

        Class<?> t = getTypeInternal();
        if (t == Integer.TYPE) {
            return 'I";
        }
        if (t == Byte.TYPE) {
            return 'B";
        }
        if (t == Character.TYPE) {
            return 'C";
        }
        if (t == Short.TYPE) {
            return 'S";
        }
        if (t == Boolean.TYPE) {
            return 'Z";
        }
        if (t == Long.TYPE) {
            return 'J";
        }
        if (t == Float.TYPE) {
            return 'F";
        }
        if (t == Double.TYPE) {
            return 'D";
        }
        if (t.isArray()) {
            return '[";
        }
        return 'L";
    
java.lang.ClassgetTypeInternal()
Return the type of the field the receiver represents, this is an internal method

return
A Class object representing the type of the field

        if (type instanceof WeakReference) {
            return (Class<?>) ((WeakReference<?>) type).get();
        }
        return (Class<?>) type;
    
public java.lang.StringgetTypeString()
Gets the type signature used by the VM to represent the type of this field.

return
the signature of this field's class or {@code null} if this field's type is primitive.
since
Android 1.0

        if (isPrimitive()) {
            return null;
        }
        if (typeString == null) {
            Class<?> t = getTypeInternal();
            String typeName = t.getName().replace('.", '/");
            String str = (t.isArray()) ? typeName : ("L" + typeName + ';"); //$NON-NLS-1$
            typeString = str.intern();
        }
        return typeString;
    
public booleanisPrimitive()
Indicates whether this field's type is a primitive type.

return
{@code true} if this field's type is primitive; {@code false} if the type of this field is a regular class.
since
Android 1.0

        Class<?> t = getTypeInternal();
        return t != null && t.isPrimitive();
    
public booleanisUnshared()
Indicats whether this field is unshared.

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

        return unshared;
    
voidresolve(java.lang.ClassLoader loader)

        if (typeString.length() == 1) {
            switch (typeString.charAt(0)) {
                case 'I":
                    type = Integer.TYPE;
                    return;
                case 'B":
                    type = Byte.TYPE;
                    return;
                case 'C":
                    type = Character.TYPE;
                    return;
                case 'S":
                    type = Short.TYPE;
                    return;
                case 'Z":
                    type = Boolean.TYPE;
                    return;
                case 'J":
                    type = Long.TYPE;
                    return;
                case 'F":
                    type = Float.TYPE;
                    return;
                case 'D":
                    type = Double.TYPE;
                    return;
            }
        }
        String className = typeString.replace('/", '.");
        if (className.charAt(0) == 'L") {
            // remove L and ;
            className = className.substring(1, className.length() - 1);
        }
        try {
            Class<?> cl = Class.forName(className, false, loader);
            type = (cl.getClassLoader() == null) ? cl
                    : new WeakReference<Class<?>>(cl);
        } catch (ClassNotFoundException e) {
            // Ignored
        }
    
protected voidsetOffset(int newValue)
Sets this field's offset in the object.

param
newValue the field's new offset.
since
Android 1.0

        this.offset = newValue;
    
voidsetUnshared(boolean unshared)

        this.unshared = unshared;
    
static voidsortFields(java.io.ObjectStreamField[] fields)
Sorts the fields for dumping. Primitive types come first, then regular types.

param
fields ObjectStreamField[] fields to be sorted

        // Sort if necessary
        if (fields.length > 1) {
            Comparator<ObjectStreamField> fieldDescComparator = new Comparator<ObjectStreamField>() {
                public int compare(ObjectStreamField f1, ObjectStreamField f2) {
                    return f1.compareTo(f2);
                }
            };
            Arrays.sort(fields, fieldDescComparator);
        }
    
public java.lang.StringtoString()
Returns a string containing a concise, human-readable description of this field descriptor.

return
a printable representation of this descriptor.
since
Android 1.0

        return this.getClass().getName() + '(" + getName() + ':"
                + getTypeInternal() + ')";