8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
authorredestad
Tue, 01 Dec 2015 09:22:01 +0100
changeset 34357 231fdaed751a
parent 34356 5390ced223f0
child 34378 129633be5db2
8143926: ObjectStreamField constructor eagerly load ObjectStreamClass Reviewed-by: chegar, alanb, shade
jdk/src/java.base/share/classes/java/io/ObjectStreamClass.java
jdk/src/java.base/share/classes/java/io/ObjectStreamField.java
--- a/jdk/src/java.base/share/classes/java/io/ObjectStreamClass.java	Tue Dec 01 15:07:10 2015 +0800
+++ b/jdk/src/java.base/share/classes/java/io/ObjectStreamClass.java	Tue Dec 01 09:22:01 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -54,6 +54,8 @@
 import sun.reflect.ReflectionFactory;
 import sun.reflect.misc.ReflectUtil;
 
+import static java.io.ObjectStreamField.*;
+
 /**
  * Serialization's descriptor for classes.  It contains the name and
  * serialVersionUID of the class.  The ObjectStreamClass for a specific class
@@ -1519,62 +1521,15 @@
      * if class names equal, false otherwise.
      */
     private static boolean classNamesEqual(String name1, String name2) {
-        name1 = name1.substring(name1.lastIndexOf('.') + 1);
-        name2 = name2.substring(name2.lastIndexOf('.') + 1);
-        return name1.equals(name2);
+        int idx1 = name1.lastIndexOf('.') + 1;
+        int idx2 = name2.lastIndexOf('.') + 1;
+        int len1 = name1.length() - idx1;
+        int len2 = name2.length() - idx2;
+        return len1 == len2 &&
+                name1.regionMatches(idx1, name2, idx2, len1);
     }
 
     /**
-     * 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();
-    }
-
-    private 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;
-   }
-
-    /**
      * Returns JVM type signature for given list of parameters and return type.
      */
     private static String getMethodSignature(Class<?>[] paramTypes,
--- a/jdk/src/java.base/share/classes/java/io/ObjectStreamField.java	Tue Dec 01 15:07:10 2015 +0800
+++ b/jdk/src/java.base/share/classes/java/io/ObjectStreamField.java	Tue Dec 01 09:22:01 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -91,7 +91,7 @@
         this.name = name;
         this.type = type;
         this.unshared = unshared;
-        signature = ObjectStreamClass.getClassSignature(type).intern();
+        signature = getClassSignature(type).intern();
         field = null;
     }
 
@@ -124,6 +124,58 @@
     }
 
     /**
+     * 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
@@ -137,7 +189,7 @@
         name = field.getName();
         Class<?> ftype = field.getType();
         type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
-        signature = ObjectStreamClass.getClassSignature(ftype).intern();
+        signature = getClassSignature(ftype).intern();
     }
 
     /**