jdk/src/java.base/share/classes/java/io/ObjectStreamField.java
changeset 45892 0b9499e86ee6
parent 37363 329dba26ffd2
equal deleted inserted replaced
45891:55e3ff5a7b08 45892:0b9499e86ee6
    43     implements Comparable<Object>
    43     implements Comparable<Object>
    44 {
    44 {
    45 
    45 
    46     /** field name */
    46     /** field name */
    47     private final String name;
    47     private final String name;
    48     /** canonical JVM signature of field type */
    48     /** canonical JVM signature of field type, if given */
    49     private final String signature;
    49     private final String signature;
    50     /** field type (Object.class if unknown non-primitive type) */
    50     /** field type (Object.class if unknown non-primitive type) */
    51     private final Class<?> type;
    51     private final Class<?> type;
       
    52     /** lazily constructed signature for the type, if no explicit signature */
       
    53     private String typeSignature;
    52     /** whether or not to (de)serialize field values as unshared */
    54     /** whether or not to (de)serialize field values as unshared */
    53     private final boolean unshared;
    55     private final boolean unshared;
    54     /** corresponding reflective field object, if any */
    56     /** corresponding reflective field object, if any */
    55     private final Field field;
    57     private final Field field;
    56     /** offset of field value in enclosing field group */
    58     /** offset of field value in enclosing field group */
    57     private int offset = 0;
    59     private int offset;
    58 
    60 
    59     /**
    61     /**
    60      * Create a Serializable field with the specified type.  This field should
    62      * Create a Serializable field with the specified type.  This field should
    61      * be documented with a <code>serialField</code> tag.
    63      * be documented with a <code>serialField</code> tag.
    62      *
    64      *
    89             throw new NullPointerException();
    91             throw new NullPointerException();
    90         }
    92         }
    91         this.name = name;
    93         this.name = name;
    92         this.type = type;
    94         this.type = type;
    93         this.unshared = unshared;
    95         this.unshared = unshared;
    94         signature = getClassSignature(type).intern();
    96         this.field = null;
    95         field = null;
    97         this.signature = null;
    96     }
    98     }
    97 
    99 
    98     /**
   100     /**
    99      * Creates an ObjectStreamField representing a field with the given name,
   101      * Creates an ObjectStreamField representing a field with the given name,
   100      * signature and unshared setting.
   102      * signature and unshared setting.
   104             throw new NullPointerException();
   106             throw new NullPointerException();
   105         }
   107         }
   106         this.name = name;
   108         this.name = name;
   107         this.signature = signature.intern();
   109         this.signature = signature.intern();
   108         this.unshared = unshared;
   110         this.unshared = unshared;
   109         field = null;
   111         this.field = null;
   110 
   112 
   111         switch (signature.charAt(0)) {
   113         switch (signature.charAt(0)) {
   112             case 'Z': type = Boolean.TYPE; break;
   114             case 'Z': type = Boolean.TYPE; break;
   113             case 'B': type = Byte.TYPE; break;
   115             case 'B': type = Byte.TYPE; break;
   114             case 'C': type = Character.TYPE; break;
   116             case 'C': type = Character.TYPE; break;
   240      *
   242      *
   241      * @return  the typecode of the serializable field
   243      * @return  the typecode of the serializable field
   242      */
   244      */
   243     // REMIND: deprecate?
   245     // REMIND: deprecate?
   244     public char getTypeCode() {
   246     public char getTypeCode() {
   245         return signature.charAt(0);
   247         return getSignature().charAt(0);
   246     }
   248     }
   247 
   249 
   248     /**
   250     /**
   249      * Return the JVM type signature.
   251      * Return the JVM type signature.
   250      *
   252      *
   251      * @return  null if this field has a primitive type.
   253      * @return  null if this field has a primitive type.
   252      */
   254      */
   253     // REMIND: deprecate?
   255     // REMIND: deprecate?
   254     public String getTypeString() {
   256     public String getTypeString() {
   255         return isPrimitive() ? null : signature;
   257         return isPrimitive() ? null : getSignature();
   256     }
   258     }
   257 
   259 
   258     /**
   260     /**
   259      * Offset of field within instance data.
   261      * Offset of field within instance data.
   260      *
   262      *
   282      *
   284      *
   283      * @return  true if and only if this field corresponds to a primitive type
   285      * @return  true if and only if this field corresponds to a primitive type
   284      */
   286      */
   285     // REMIND: deprecate?
   287     // REMIND: deprecate?
   286     public boolean isPrimitive() {
   288     public boolean isPrimitive() {
   287         char tcode = signature.charAt(0);
   289         char tcode = getTypeCode();
   288         return ((tcode != 'L') && (tcode != '['));
   290         return ((tcode != 'L') && (tcode != '['));
   289     }
   291     }
   290 
   292 
   291     /**
   293     /**
   292      * Returns boolean value indicating whether or not the serializable field
   294      * Returns boolean value indicating whether or not the serializable field
   318 
   320 
   319     /**
   321     /**
   320      * Return a string that describes this field.
   322      * Return a string that describes this field.
   321      */
   323      */
   322     public String toString() {
   324     public String toString() {
   323         return signature + ' ' + name;
   325         return getSignature() + ' ' + name;
   324     }
   326     }
   325 
   327 
   326     /**
   328     /**
   327      * Returns field represented by this ObjectStreamField, or null if
   329      * Returns field represented by this ObjectStreamField, or null if
   328      * ObjectStreamField is not associated with an actual field.
   330      * ObjectStreamField is not associated with an actual field.
   334     /**
   336     /**
   335      * Returns JVM type signature of field (similar to getTypeString, except
   337      * Returns JVM type signature of field (similar to getTypeString, except
   336      * that signature strings are returned for primitive fields as well).
   338      * that signature strings are returned for primitive fields as well).
   337      */
   339      */
   338     String getSignature() {
   340     String getSignature() {
   339         return signature;
   341         if (signature != null) {
       
   342             return signature;
       
   343         }
       
   344 
       
   345         String sig = typeSignature;
       
   346         // This lazy calculation is safe since signature can be null iff one
       
   347         // of the public constructors are used, in which case type is always
       
   348         // initialized to the exact type we want the signature to represent.
       
   349         if (sig == null) {
       
   350             typeSignature = sig = getClassSignature(type).intern();
       
   351         }
       
   352         return sig;
   340     }
   353     }
   341 }
   354 }