jdk/src/share/classes/javax/management/MBeanException.java
changeset 1712 8664f15a755b
parent 2 90ce3da70b43
child 4156 acaa49a2768a
--- a/jdk/src/share/classes/javax/management/MBeanException.java	Tue Dec 09 20:51:59 2008 +0100
+++ b/jdk/src/share/classes/javax/management/MBeanException.java	Wed Dec 10 11:59:32 2008 +0100
@@ -25,6 +25,8 @@
 
 package javax.management;
 
+import javax.management.openmbean.CompositeData;
+
 
 /**
  * Represents "user defined" exceptions thrown by MBean methods
@@ -41,6 +43,26 @@
     private static final long serialVersionUID = 4066342430588744142L;
 
     /**
+     * @serial This field is null for instances of this class that were
+     * produced by its public constructors.  It is non-null for instances
+     * of this class that represent serialized instances of {@link
+     * GenericMBeanException}.
+     *
+     * @see GenericMBeanException#getErrorCode()
+     */
+    final String errorCode;
+
+    /**
+     * @serial This field is null for instances of this class that were
+     * produced by its public constructors.  It may be non-null for instances
+     * of this class that represent serialized instances of {@link
+     * GenericMBeanException}.
+     *
+     * @see GenericMBeanException#getUserData()
+     */
+    final CompositeData userData;
+
+    /**
      * @serial Encapsulated {@link Exception}
      */
     private java.lang.Exception exception ;
@@ -51,9 +73,8 @@
      *
      * @param e the wrapped exception.
      */
-    public MBeanException(java.lang.Exception e) {
-        super() ;
-        exception = e ;
+    public MBeanException(Exception e) {
+        this(null, null, null, e);
     }
 
     /**
@@ -63,11 +84,19 @@
      * @param e the wrapped exception.
      * @param message the detail message.
      */
-    public MBeanException(java.lang.Exception e, String message) {
-        super(message) ;
-        exception = e ;
+    public MBeanException(Exception e, String message) {
+        this(message, null, null, e);
     }
 
+    MBeanException(
+            String message, String errorCode, CompositeData userData, Throwable cause) {
+        super(message);
+        initCause(cause);
+        if (cause instanceof Exception)
+            this.exception = (Exception) cause;
+        this.errorCode = errorCode;
+        this.userData = userData;
+    }
 
     /**
      * Return the actual {@link Exception} thrown.
@@ -79,11 +108,24 @@
     }
 
     /**
-     * Return the actual {@link Exception} thrown.
-     *
-     * @return the wrapped exception.
+     * This method is invoked when deserializing instances of this class.
+     * If the {@code errorCode} field of the deserialized instance is not
+     * null, this method returns an instance of {@link GenericMBeanException}
+     * instead.  Otherwise it returns {@code this}.
+     * @return {@code this}, or a {@code GenericMBeanException}.
      */
-    public Throwable getCause() {
-        return exception;
+    Object readResolve() {
+        if (errorCode == null) {
+            // serial compatibility: earlier versions did not set
+            // Throwable.cause because they overrode getCause().
+            if (getCause() == null && exception != null)
+                initCause(exception);
+            return this;
+        } else {
+            Throwable t = new GenericMBeanException(
+                    getMessage(), errorCode, userData, getCause());
+            t.setStackTrace(this.getStackTrace());
+            return t;
+        }
     }
 }