< prev index next >

src/java.base/share/classes/java/io/ObjectStreamField.java

Print this page

        

@@ -89,11 +89,11 @@
             throw new NullPointerException();
         }
         this.name = name;
         this.type = type;
         this.unshared = unshared;
-        signature = getClassSignature(type).intern();
+        signature = null; // will be lazily obtained from type
         field = null;
     }
 
     /**
      * Creates an ObjectStreamField representing a field with the given name,

@@ -122,62 +122,10 @@
             default: throw new IllegalArgumentException("illegal signature");
         }
     }
 
     /**
-     * Returns JVM type signature for given primitive.
-     */
-    private static String getPrimitiveSignature(Class<?> cl) {
-        if (cl == Integer.TYPE)
-            return "I";
-        else if (cl == Byte.TYPE)
-            return "B";
-        else if (cl == Long.TYPE)
-            return "J";
-        else if (cl == Float.TYPE)
-            return "F";
-        else if (cl == Double.TYPE)
-            return "D";
-        else if (cl == Short.TYPE)
-            return "S";
-        else if (cl == Character.TYPE)
-            return "C";
-        else if (cl == Boolean.TYPE)
-            return "Z";
-        else if (cl == Void.TYPE)
-            return "V";
-        else
-            throw new InternalError();
-    }
-
-    /**
-     * Returns JVM type signature for given class.
-     */
-    static String getClassSignature(Class<?> cl) {
-        if (cl.isPrimitive()) {
-            return getPrimitiveSignature(cl);
-        } else {
-            return appendClassSignature(new StringBuilder(), cl).toString();
-        }
-    }
-
-    static StringBuilder appendClassSignature(StringBuilder sbuf, Class<?> cl) {
-        while (cl.isArray()) {
-            sbuf.append('[');
-            cl = cl.getComponentType();
-        }
-
-        if (cl.isPrimitive()) {
-            sbuf.append(getPrimitiveSignature(cl));
-        } else {
-            sbuf.append('L').append(cl.getName().replace('.', '/')).append(';');
-        }
-
-        return sbuf;
-    }
-
-    /**
      * Creates an ObjectStreamField representing the given field with the
      * specified unshared setting.  For compatibility with the behavior of
      * earlier serialization implementations, a "showType" parameter is
      * necessary to govern whether or not a getType() call on this
      * ObjectStreamField (if non-primitive) will return Object.class (as

@@ -186,12 +134,17 @@
     ObjectStreamField(Field field, boolean unshared, boolean showType) {
         this.field = field;
         this.unshared = unshared;
         name = field.getName();
         Class<?> ftype = field.getType();
-        type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
-        signature = getClassSignature(ftype).intern();
+        if (showType || ftype.isPrimitive()) {
+            type = ftype;
+            signature = null; // will be lazily obtained from type
+        } else {
+            type = Object.class;
+            signature = ftype.getJvmTypeSignature();
+        }
     }
 
     /**
      * Get the name of this field.
      *

@@ -240,21 +193,21 @@
      *
      * @return  the typecode of the serializable field
      */
     // REMIND: deprecate?
     public char getTypeCode() {
-        return signature.charAt(0);
+        return getSignature().charAt(0);
     }
 
     /**
      * Return the JVM type signature.
      *
      * @return  null if this field has a primitive type.
      */
     // REMIND: deprecate?
     public String getTypeString() {
-        return isPrimitive() ? null : signature;
+        return isPrimitive() ? null : getSignature();
     }
 
     /**
      * Offset of field within instance data.
      *

@@ -282,11 +235,11 @@
      *
      * @return  true if and only if this field corresponds to a primitive type
      */
     // REMIND: deprecate?
     public boolean isPrimitive() {
-        char tcode = signature.charAt(0);
+        char tcode = getSignature().charAt(0);
         return ((tcode != 'L') && (tcode != '['));
     }
 
     /**
      * Returns boolean value indicating whether or not the serializable field

@@ -318,11 +271,11 @@
 
     /**
      * Return a string that describes this field.
      */
     public String toString() {
-        return signature + ' ' + name;
+        return getSignature() + ' ' + name;
     }
 
     /**
      * Returns field represented by this ObjectStreamField, or null if
      * ObjectStreamField is not associated with an actual field.

@@ -334,8 +287,8 @@
     /**
      * Returns JVM type signature of field (similar to getTypeString, except
      * that signature strings are returned for primitive fields as well).
      */
     String getSignature() {
-        return signature;
+        return signature == null ? type.getJvmTypeSignature() : signature;
     }
 }
< prev index next >