changeset 4950:42706fbe23df

7110700: Enhance exception throwing mechanism in ObjectStreamClass Reviewed-by: hawtin
author dmeetry
date Wed, 23 Nov 2011 17:58:19 +0400
parents ce32bd26c1ff
children 17eb32277816
files src/share/classes/java/io/ObjectStreamClass.java test/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java
diffstat 2 files changed, 45 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/io/ObjectStreamClass.java	Fri Nov 18 12:38:48 2011 -0800
+++ b/src/share/classes/java/io/ObjectStreamClass.java	Wed Nov 23 17:58:19 2011 +0400
@@ -123,14 +123,39 @@
      */
     private boolean hasBlockExternalData = true;
 
+    /**
+     * Contains information about InvalidClassException instances to be thrown
+     * when attempting operations on an invalid class. Note that instances of
+     * this class are immutable and are potentially shared among
+     * ObjectStreamClass instances.
+     */
+    private static class ExceptionInfo {
+        private final String className;
+        private final String message;
+
+        ExceptionInfo(String cn, String msg) {
+            className = cn;
+            message = msg;
+        }
+
+        /**
+         * Returns (does not throw) an InvalidClassException instance created
+         * from the information in this object, suitable for being thrown by
+         * the caller.
+         */
+        InvalidClassException newInvalidClassException() {
+            return new InvalidClassException(className, message);
+        }
+    }
+
     /** exception (if any) thrown while attempting to resolve class */
     private ClassNotFoundException resolveEx;
     /** exception (if any) to throw if non-enum deserialization attempted */
-    private InvalidClassException deserializeEx;
+    private ExceptionInfo deserializeEx;
     /** exception (if any) to throw if non-enum serialization attempted */
-    private InvalidClassException serializeEx;
+    private ExceptionInfo serializeEx;
     /** exception (if any) to throw if default serialization attempted */
-    private InvalidClassException defaultSerializeEx;
+    private ExceptionInfo defaultSerializeEx;
 
     /** serializable fields */
     private ObjectStreamField[] fields;
@@ -444,7 +469,8 @@
                         fields = getSerialFields(cl);
                         computeFieldOffsets();
                     } catch (InvalidClassException e) {
-                        serializeEx = deserializeEx = e;
+                        serializeEx = deserializeEx =
+                            new ExceptionInfo(e.classname, e.getMessage());
                         fields = NO_FIELDS;
                     }
 
@@ -483,15 +509,14 @@
 
         if (deserializeEx == null) {
             if (isEnum) {
-                deserializeEx = new InvalidClassException(name, "enum type");
+                deserializeEx = new ExceptionInfo(name, "enum type");
             } else if (cons == null) {
-                deserializeEx = new InvalidClassException(
-                    name, "no valid constructor");
+                deserializeEx = new ExceptionInfo(name, "no valid constructor");
             }
         }
         for (int i = 0; i < fields.length; i++) {
             if (fields[i].getField() == null) {
-                defaultSerializeEx = new InvalidClassException(
+                defaultSerializeEx = new ExceptionInfo(
                     name, "unmatched serializable field(s) declared");
             }
         }
@@ -601,8 +626,8 @@
                     (externalizable != localDesc.externalizable) ||
                     !(serializable || externalizable))
                 {
-                    deserializeEx = new InvalidClassException(localDesc.name,
-                        "class invalid for deserialization");
+                    deserializeEx = new ExceptionInfo(
+                        localDesc.name, "class invalid for deserialization");
                 }
             }
 
@@ -727,11 +752,7 @@
      */
     void checkDeserialize() throws InvalidClassException {
         if (deserializeEx != null) {
-            InvalidClassException ice =
-                new InvalidClassException(deserializeEx.classname,
-                                          deserializeEx.getMessage());
-            ice.initCause(deserializeEx);
-            throw ice;
+            throw deserializeEx.newInvalidClassException();
         }
     }
 
@@ -742,11 +763,7 @@
      */
     void checkSerialize() throws InvalidClassException {
         if (serializeEx != null) {
-            InvalidClassException ice =
-                new InvalidClassException(serializeEx.classname,
-                                          serializeEx.getMessage());
-            ice.initCause(serializeEx);
-            throw ice;
+            throw serializeEx.newInvalidClassException();
         }
     }
 
@@ -759,11 +776,7 @@
      */
     void checkDefaultSerialize() throws InvalidClassException {
         if (defaultSerializeEx != null) {
-            InvalidClassException ice =
-                new InvalidClassException(defaultSerializeEx.classname,
-                                          defaultSerializeEx.getMessage());
-            ice.initCause(defaultSerializeEx);
-            throw ice;
+            throw defaultSerializeEx.newInvalidClassException();
         }
     }
 
--- a/test/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java	Fri Nov 18 12:38:48 2011 -0800
+++ b/test/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java	Wed Nov 23 17:58:19 2011 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2011, 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
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 6317435
+ * @bug 6317435 7110700
  * @summary Verify that stack trace contains a proper cause of
  *          InvalidClassException (methods: checkSerialize,
  *          checkDeserialize or checkDefaultSerialize)
@@ -59,7 +59,7 @@
     private static final String SER_METHOD_NAME = "checkSerializable";
 
     public static final void main(String[] args) throws Exception {
-        System.err.println("\nRegression test for CR6317435");
+        System.err.println("\nRegression test for CRs 6317435, 7110700");
         checkSerializable(getObject());
     }
 
@@ -99,9 +99,12 @@
                 }
             }
             if (found) {
+                if (ex.getCause() != null) {
+                    throw new Error("\nTest for CR 7110700 FAILED");
+                }
                 System.err.println("\nTEST PASSED");
             } else {
-                throw new Error();
+                throw new Error("\nTest for CR 6317435 FAILED");
             }
         }
     }