home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 4.9 KB | 209 lines |
- /*
- * @(#)ObjectStreamField.java 1.22 98/03/18
- *
- * Copyright 1996-1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.io;
-
- import java.lang.reflect.Field;
-
- /**
- * A description of a field in a serializable class.
- * A array of these is used to declare the persistent fields of
- * a class.
- *
- * @author Roger Riggs
- * @version 1.22, 03/18/98
- */
- public class ObjectStreamField implements Comparable {
- /**
- * Create a named field with the specified type.
- */
- public ObjectStreamField(String n, Class clazz) {
- name = n;
- this.clazz = clazz;
-
- // Compute the typecode for easy switching
- if (clazz.isPrimitive()) {
- if (clazz == Integer.TYPE) {
- type = 'I';
- } else if (clazz == Byte.TYPE) {
- type = 'B';
- } else if (clazz == Long.TYPE) {
- type = 'J';
- } else if (clazz == Float.TYPE) {
- type = 'F';
- } else if (clazz == Double.TYPE) {
- type = 'D';
- } else if (clazz == Short.TYPE) {
- type = 'S';
- } else if (clazz == Character.TYPE) {
- type = 'C';
- } else if (clazz == Boolean.TYPE) {
- type = 'Z';
- }
- } else if (clazz.isArray()) {
- type = '[';
- typeString = ObjectStreamClass.getSignature(clazz);
- } else {
- type = 'L';
- typeString = ObjectStreamClass.getSignature(clazz);
- }
- }
-
- ObjectStreamField(Field field) {
- this(field.getName(), field.getType());
- this.field = field;
- }
-
- /**
- * Create an ObjectStreamField containing a reflected Field.
- */
- ObjectStreamField(String n, char t, Field f, String ts)
- {
- // System.out.println("new field, " + n + " " + t + " " + ts);
- name = n;
- type = t;
- field = f;
- typeString = ts;
- }
-
- /**
- * Get the name of this field.
- */
- public String getName() {
- return name;
- }
-
- /**
- * Get the type of the field.
- */
- public Class getType() {
- if (clazz != null)
- return clazz;
- switch (type) {
- case 'B': clazz = Byte.TYPE;
- break;
- case 'C': clazz = Character.TYPE;
- break;
- case 'S': clazz = Short.TYPE;
- break;
- case 'I': clazz = Integer.TYPE;
- break;
- case 'J': clazz = Long.TYPE;
- break;
- case 'F': clazz = Float.TYPE;
- break;
- case 'D': clazz = Double.TYPE;
- break;
- case 'Z': clazz = Boolean.TYPE;
- break;
- case '[':
- case 'L':
- clazz = Object.class;
- break;
- }
-
- return clazz;
- }
-
- public char getTypeCode() {
- return type;
- }
-
- public String getTypeString() {
- return typeString;
- }
-
- Field getField() {
- return field;
- }
-
- void setField(Field field) {
- this.field = field;
- }
-
- public int getOffset() {
- return bufoffset;
- }
-
- public void setOffset(int offset) {
- bufoffset = offset;
- }
-
- /*
- * Default constructor creates an empty field.
- * Usually used just to get to the sort functions.
- */
- ObjectStreamField() {
- }
-
- /**
- * test if this field is a primitive or not.
- */
- public boolean isPrimitive() {
- return (type != '[' && type != 'L');
- }
-
- /**
- * Compare this with another ObjectStreamField.
- * return -1 if this is smaller, 0 if equal, 1 if greater
- * types that are primitives are "smaller" than objects.
- * if equal, the names are compared.
- */
- public int compareTo(Object o) {
- ObjectStreamField f2 = (ObjectStreamField)o;
- boolean thisprim = (this.typeString == null);
- boolean otherprim = (f2.typeString == null);
-
- if (thisprim != otherprim) {
- return (thisprim ? -1 : 1);
- }
- return this.name.compareTo(f2.name);
- }
-
- /**
- * Compare the types of two class descriptors.
- * The match if they have the same primitive types.
- * or if they are both objects and the object types match.
- */
- public boolean typeEquals(ObjectStreamField other) {
- if (other == null || type != other.type)
- return false;
-
- /* Return true if the primitive types matched */
- if (typeString == null && other.typeString == null)
- return true;
-
- return ObjectStreamClass.compareClassNames(typeString,
- other.typeString,
- '/');
- }
-
- /**
- * Return a string describing this field.
- */
- public String toString() {
- if (typeString != null)
- return typeString + " " + name;
- else
- return type + " " + name;
- }
-
- private String name; // the name of the field
- private char type; // type first byte of the type signature
- private Field field; // Reflected field
- private String typeString; // iff object, typename
- private int bufoffset; // offset in the data buffer, or index in objects
- private Class clazz; // the type of this field, if has been resolved
- }
-