java.io

Class ObjectStreamField

Implemented Interfaces:
Comparable<T>

public class ObjectStreamField
extends Object
implements Comparable<T>

A description of a Serializable field from a Serializable class. An array of ObjectStreamFields is used to declare the Serializable fields of a class.
Since:
1.2
See Also:
ObjectStreamClass

Constructor Summary

ObjectStreamField(String name, Class type)
Create a Serializable field with the specified type.
ObjectStreamField(String name, Class type, boolean unshared)
Creates an ObjectStreamField representing a serializable field with the given name and type.

Method Summary

int
compareTo(Object obj)
Compare this field with another ObjectStreamField.
String
getName()
Get the name of this field.
int
getOffset()
Offset of field within instance data.
Class
getType()
Get the type of the field.
char
getTypeCode()
Returns character encoding of field type.
String
getTypeString()
Return the JVM type signature.
boolean
isPrimitive()
Return true if this field has a primitive type.
boolean
isUnshared()
Returns boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared.
protected void
setOffset(int offset)
Offset within instance data.
String
toString()
Return a string that describes this field.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

ObjectStreamField

public ObjectStreamField(String name,
                         Class type)
Create a Serializable field with the specified type. This field should be documented with a serialField tag.
Parameters:
name - the name of the serializable field
type - the Class object of the serializable field
Usages and Demos :

View More Examples of ObjectStreamField(String name,Class type)
   1:    {
   2:       new ObjectStreamField("jndiName", String.class),
   3:         ...
   4:       new ObjectStreamField("jndiEnv", Hashtable.class)
   5:    };
   6: 
   7:    private Hashtable jndiEnv;

View Full Code Here
   1:         private static final ObjectStreamField[] serialPersistentFields = { 
   2:             new ObjectStreamField("count", Integer.TYPE),
   3:         ...
   4:             new ObjectStreamField("streamVersion", Byte.TYPE),
   5:         ...
   6:             new ObjectStreamField("parent", ExpressionPool.class) 
   7:         }; 

View Full Code Here
   1:         private static final ObjectStreamField[] serialPersistentFields = {
   2:             new ObjectStreamField("theName", String.class),
   3:         ...
   4:             new ObjectStreamField("theNumber", int.class),
   5:         ...
   6:             new ObjectStreamField("theSoftware", Software.class),
   7:             new ObjectStreamField("thePolymorphic", Object.class),

View Full Code Here
   1: 
   2:     private final static ObjectStreamField[] serialPersistentFields = {new ObjectStreamField("num1", Integer.class)};
   3: 
   4:     public Integer getNum1() {
   5:       return num1;

View Full Code Here

ObjectStreamField

public ObjectStreamField(String name,
                         Class type,
                         boolean unshared)
Creates an ObjectStreamField representing a serializable field with the given name and type. If unshared is false, values of the represented field are serialized and deserialized in the default manner--if the field is non-primitive, object values are serialized and deserialized as if they had been written and read by calls to writeObject and readObject. If unshared is true, values of the represented field are serialized and deserialized as if they had been written and read by calls to writeUnshared and readUnshared.
Parameters:
name - field name
type - field type
unshared - if false, write/read field values in the same manner as writeObject/readObject; if true, write/read in the same manner as writeUnshared/readUnshared

Method Details

compareTo

public int compareTo(Object obj)
Compare this field with another ObjectStreamField. Return -1 if this is smaller, 0 if equal, 1 if greater. Types that are primitives are "smaller" than object types. If equal, the field names are compared.

getName

public String getName()
Get the name of this field.
Returns:
a String representing the name of the serializable field
Usages and Demos :

View More Examples of getName()
   1:             Class type = (Class)entry.getValue();
   2:             System.err.println("\t"+type.getName()+" "+name);
   3:         }
   4:         ...
   5:             Class type = (Class)entry.getValue();
   6:             if (DEBUG) System.err.println("\tChecking: "+type.getName()+" "+name);
   7:             Field field = null;
   8:         ...
   9:             if (field.getType()!=type) {
  10:                 throw new InvalidClassException("unmatched serializable field(s) declared - '"+name+"' in "+clazz+" is of type "+field.getType().getName()+", but the serializable field was declared to be of type "+type.getName()+" (necessarily declared in 'serialPersistentFields')");
  11:             }
  12:         ...
  13:         HashMap map = new HashMap();
  14:         ObjectStreamField[] osfs = null;

View Full Code Here
   1: import java.io.ObjectStreamClass;
   2: import java.io.ObjectStreamField;
   3: import java.io.Serializable;
   4:         ...
   5:                     String name = (String) iterator.next();
   6:                     ObjectStreamField field = objectStreamClass.getField(name);
   7:                     Object value = fields.get(name);
   8:         ...
   9:                     if (field == null) {
  10:                         throw new ObjectAccessException("Class " + value.getClass().getName()
  11:                                 + " may not write a field named '" + name + "'");
  12:         ...
  13: 
  14:                 ObjectStreamField[] fields = objectStreamClass.getFields();

View Full Code Here
   1:       f.setAccessible(true);
   2:       ObjectStreamField[] serialPersistentFields = (ObjectStreamField[]) f.get(null);
   3: 
   4:         ...
   5:       for (int i = 0; i < serialPersistentFields.length; i++) {
   6:         ObjectStreamField spf = serialPersistentFields[i];
   7: 
   8:         ...
   9:         Field thisf = c.getDeclaredField(spf.getName());
  10:         if (!((thisf.getType() == spf.getType()) && ((thisf.getModifiers() & Modifier.STATIC) == 0))) {
  11:         ...
  12: 
  13:     writer.attribute("base", o.getClass().getComponentType().getName());

View Full Code Here

getOffset

public int getOffset()
Offset of field within instance data.
Returns:
the offset of this field

getType

public Class getType()
Get the type of the field. If the type is non-primitive and this ObjectStreamField was obtained from a deserialized ObjectStreamClass instance, then Object.class is returned. Otherwise, the Class object for the type of the field is returned.
Returns:
a Class object representing the type of the serializable field
Usages and Demos :

View More Examples of getType()
   1:             }
   2:             if (field.getType()!=type) {
   3:         ...
   4:                 throw new InvalidClassException("unmatched serializable field(s) declared - '"+name+"' in "+clazz+" is of type "+field.getType().getName()+", but the serializable field was declared to be of type "+type.getName()+" (necessarily declared in 'serialPersistentFields')");
   5:             }
   6:         ...
   7:         HashMap map = new HashMap();
   8:         ObjectStreamField[] osfs = null;
   9:         try {
  10:         ...
  11:         for (int i=0; i<osfs.length; i++) {
  12:             ObjectStreamField f = osfs[i];

View Full Code Here
   1:         System.out.println("ObjectStreamFields");
   2:         ObjectStreamField[] fields = desc.getFields();
   3:         int numPrim = 0;
   4:         ...
   5:         for (int i = 0; i < fields.length; i++) {
   6:             ObjectStreamField f = fields[i];
   7:             String fieldName = "<fieldName unknown>";
   8:         ...
   9:                 numPrim++;
  10:                 System.out.println("" + i + ". " + f.getType().getName() + " " + fieldName);
  11:             } else {

View Full Code Here
   1: import java.io.ObjectStreamClass;
   2: import java.io.ObjectStreamField;
   3: import java.io.Serializable;
   4:         ...
   5:                     String name = (String) iterator.next();
   6:                     ObjectStreamField field = objectStreamClass.getField(name);
   7:                     Object value = fields.get(name);
   8:         ...
   9:                         writer.startNode(mapper.serializedMember(currentType[0], name));
  10:                         if (field.getType() != value.getClass() && !field.getType().isPrimitive()) {
  11:                             writer.addAttribute(ATTRIBUTE_CLASS, mapper.serializedClass(value.getClass()));
  12:         ...
  13: 
  14:                 ObjectStreamField[] fields = objectStreamClass.getFields();

View Full Code Here

getTypeCode

public char getTypeCode()
Returns character encoding of field type. The encoding is as follows:
 B            byte
 C            char
 D            double
 F            float
 I            int
 J            long
 L            class or interface
 S            short
 Z            boolean
 [            array
 
Returns:
the typecode of the serializable field
Usages and Demos :

View More Examples of getTypeCode()
   1: import java.io.ObjectStreamClass;
   2: import java.io.ObjectStreamField;
   3: import java.lang.reflect.Method;
   4:         ...
   5: 
   6:         java.io.ObjectStreamField[] fields = desc.getFields();
   7:         int numPrimFields = fields.length - objVals.length;
   8:         ...
   9:     {
  10:         ObjectStreamField field = getField(name, type);
  11:         if (field == null)
  12:         ...
  13: 
  14:             switch (f.getTypeCode())

View Full Code Here
   1:     private int numObjFields = 0;
   2:     private ObjectStreamField[] _fields = null;
   3: 
   4:         ...
   5:         {
   6:             Class osFieldClass = java.io.ObjectStreamField.class;
   7:             Class[] params = new Class[1];
   8:         ...
   9:         
  10:         java.io.ObjectStreamField[] fields = desc.getFields();
  11:         int numPrimFields = fields.length - objVals.length;
  12:         ...
  13:             
  14:             switch (f.getTypeCode())

View Full Code Here

getTypeString

public String getTypeString()
Return the JVM type signature.
Returns:
null if this field has a primitive type.
Usages and Demos :

View More Examples of getTypeString()
   1:         System.out.println("ObjectStreamFields");
   2:         ObjectStreamField[] fields = desc.getFields();
   3:         int numPrim = 0;
   4:         ...
   5:         for (int i = 0; i < fields.length; i++) {
   6:             ObjectStreamField f = fields[i];
   7:             String fieldName = "<fieldName unknown>";
   8:         ...
   9:                 try {
  10:                     ts = f.getTypeString();
  11:                 } catch (NoSuchMethodError e) {

View Full Code Here

isPrimitive

public boolean isPrimitive()
Return true if this field has a primitive type.
Returns:
true if and only if this field corresponds to a primitive type

isUnshared

public boolean isUnshared()
Returns boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared.

setOffset

protected void setOffset(int offset)
Offset within instance data.
Parameters:
offset - the offset of the field
See Also:
getOffset()

toString

public String toString()
Return a string that describes this field.
Overrides:
toString in interface Object