home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / ObjectStreamField.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  4.9 KB  |  209 lines

  1. /*
  2.  * @(#)ObjectStreamField.java    1.22 98/03/18
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.lang.reflect.Field;
  18.  
  19. /**
  20.  * A description of a field in a serializable class.
  21.  * A array of these is used to declare the persistent fields of
  22.  * a class.
  23.  *
  24.  * @author  Roger Riggs
  25.  * @version 1.22, 03/18/98
  26.  */
  27. public class ObjectStreamField implements Comparable {
  28.     /**
  29.      * Create a named field with the specified type.
  30.      */
  31.     public ObjectStreamField(String n, Class clazz) {
  32.         name = n;
  33.         this.clazz = clazz;
  34.  
  35.     // Compute the typecode for easy switching
  36.     if (clazz.isPrimitive()) {
  37.         if (clazz == Integer.TYPE) {
  38.         type = 'I';
  39.         } else if (clazz == Byte.TYPE) {
  40.         type = 'B';
  41.         } else if (clazz == Long.TYPE) {
  42.         type = 'J';
  43.         } else if (clazz == Float.TYPE) {
  44.         type = 'F';
  45.         } else if (clazz == Double.TYPE) {
  46.         type = 'D';
  47.         } else if (clazz == Short.TYPE) {
  48.         type = 'S';
  49.         } else if (clazz == Character.TYPE) {
  50.         type = 'C';
  51.         } else if (clazz == Boolean.TYPE) {
  52.         type = 'Z';
  53.         }
  54.     } else if (clazz.isArray()) {
  55.         type = '[';
  56.         typeString = ObjectStreamClass.getSignature(clazz);
  57.     } else {
  58.         type = 'L';
  59.         typeString = ObjectStreamClass.getSignature(clazz);
  60.     }
  61.     }
  62.  
  63.     ObjectStreamField(Field field) {
  64.     this(field.getName(), field.getType());
  65.     this.field = field;
  66.     }
  67.  
  68.     /**
  69.      * Create an ObjectStreamField containing a reflected Field.
  70.      */
  71.     ObjectStreamField(String n, char t, Field f, String ts)
  72.     {
  73.     //    System.out.println("new field, " + n + " " + t + " " + ts);
  74.     name = n;
  75.     type = t;
  76.     field = f;
  77.     typeString = ts;
  78.     }
  79.  
  80.     /**
  81.      * Get the name of this field.
  82.      */
  83.     public String getName() {
  84.         return name;
  85.     }
  86.  
  87.     /**
  88.      * Get the type of the field.
  89.      */
  90.     public Class getType() {
  91.         if (clazz != null)
  92.             return clazz;
  93.     switch (type) {
  94.     case 'B': clazz = Byte.TYPE;
  95.         break;
  96.     case 'C': clazz = Character.TYPE;
  97.         break;
  98.     case 'S': clazz = Short.TYPE;
  99.         break;
  100.     case 'I': clazz = Integer.TYPE;
  101.         break;
  102.     case 'J': clazz = Long.TYPE;
  103.         break;
  104.     case 'F': clazz = Float.TYPE;
  105.         break;
  106.     case 'D': clazz = Double.TYPE;
  107.         break;
  108.     case 'Z': clazz = Boolean.TYPE;
  109.         break;
  110.     case '[':
  111.     case 'L':
  112.         clazz = Object.class;
  113.         break;
  114.     }
  115.  
  116.         return clazz;
  117.     }
  118.  
  119.     public char getTypeCode() {
  120.     return type;
  121.     }
  122.  
  123.     public String getTypeString() {
  124.     return typeString;
  125.     }
  126.  
  127.     Field getField() {
  128.      return field;
  129.     }
  130.  
  131.     void setField(Field field) {
  132.      this.field = field;
  133.     }
  134.  
  135.     public int getOffset() {
  136.     return bufoffset;
  137.     }
  138.  
  139.     public void setOffset(int offset) {
  140.     bufoffset = offset;
  141.     }
  142.  
  143.     /*
  144.      * Default constructor creates an empty field.
  145.      * Usually used just to get to the sort functions.
  146.      */ 
  147.     ObjectStreamField() {
  148.     }
  149.  
  150.     /**
  151.      * test if this field is a primitive or not.
  152.      */
  153.     public boolean isPrimitive() {
  154.     return (type != '[' && type != 'L');
  155.     }
  156.  
  157.     /**
  158.      * Compare this with another ObjectStreamField.
  159.      * return -1 if this is smaller, 0 if equal, 1 if greater
  160.      * types that are primitives are "smaller" than objects.
  161.      * if equal, the names are compared.
  162.      */
  163.     public int compareTo(Object o) {
  164.     ObjectStreamField f2 = (ObjectStreamField)o;
  165.     boolean thisprim = (this.typeString == null);
  166.     boolean otherprim = (f2.typeString == null);
  167.  
  168.     if (thisprim != otherprim) {
  169.         return (thisprim ? -1 : 1);
  170.     }
  171.     return this.name.compareTo(f2.name);
  172.     }
  173.  
  174.     /**
  175.      * Compare the types of two class descriptors.
  176.      * The match if they have the same primitive types.
  177.      * or if they are both objects and the object types match.
  178.      */
  179.     public boolean typeEquals(ObjectStreamField other) {
  180.     if (other == null || type != other.type)
  181.       return false;
  182.  
  183.     /* Return true if the primitive types matched */
  184.     if (typeString == null && other.typeString == null)
  185.         return true;
  186.  
  187.     return ObjectStreamClass.compareClassNames(typeString,
  188.                            other.typeString,
  189.                            '/');
  190.     }
  191.  
  192.     /**
  193.      * Return a string describing this field.
  194.      */
  195.     public String toString() {
  196.     if (typeString != null)
  197.         return typeString + " " + name;
  198.     else
  199.         return type + " " + name;
  200.     }
  201.  
  202.     private String name;        // the name of the field
  203.     private char type;            // type first byte of the type signature
  204.     private Field field;        // Reflected field
  205.     private String typeString;        // iff object, typename
  206.     private int bufoffset;        // offset in the data buffer, or index in objects
  207.     private Class clazz;        // the type of this field, if has been resolved
  208. }
  209.