< prev index next >
src/java.base/share/classes/java/security/PrivilegedActionException.java
Print this page
@@ -23,10 +23,17 @@
* questions.
*/
package java.security;
+import jdk.internal.misc.SharedSecrets;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamField;
+
/**
* This exception is thrown by
* {@code doPrivileged(PrivilegedExceptionAction)} and
* {@code doPrivileged(PrivilegedExceptionAction,
* AccessControlContext context)} to indicate
@@ -51,29 +58,23 @@
public class PrivilegedActionException extends Exception {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 4724086851538908602L;
/**
- * @serial
- */
- private Exception exception;
-
- /**
* Constructs a new PrivilegedActionException "wrapping"
* the specific Exception.
*
* @param exception The exception thrown
*/
public PrivilegedActionException(Exception exception) {
- super((Throwable)null); // Disallow initCause
- this.exception = exception;
+ super(null, exception); // Disallow initCause
}
/**
* Returns the exception thrown by the privileged computation that
* resulted in this {@code PrivilegedActionException}.
- *
+ *Memory
* <p>This method predates the general-purpose exception chaining facility.
* The {@link Throwable#getCause()} method is now the preferred means of
* obtaining this information.
*
* @return the exception thrown by the privileged computation that
@@ -82,25 +83,50 @@
* @see AccessController#doPrivileged(PrivilegedExceptionAction)
* @see AccessController#doPrivileged(PrivilegedExceptionAction,
* AccessControlContext)
*/
public Exception getException() {
- return exception;
+ return (Exception)getCause();
+ }
+
+ public String toString() {
+ String s = getClass().getName();
+ return (getCause() != null) ? (s + ": " + getCause().toString()) : s;
}
+
/**
- * Returns the cause of this exception (the exception thrown by
- * the privileged computation that resulted in this
- * {@code PrivilegedActionException}).
+ * Serializable fields for UndeclaredThrowableException.
*
- * @return the cause of this exception.
- * @since 1.4
+ * @serialField undeclaredThrowable Throwable
*/
- public Throwable getCause() {
- return exception;
+ private static final ObjectStreamField[] serialPersistentFields = {
+ new ObjectStreamField("exception", Exception.class)
+ };
+
+ /*
+ * Reconstitutes the PrivilegedActionException instance from a stream
+ * and initialize the cause properly when deserializing from an older
+ * version.
+ *
+ * The getException and getCause method returns the private "exception"
+ * field in the older implementation and PrivilegedActionException::cause
+ * was set to null.
+ */
+ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
+ ObjectInputStream.GetField fields = s.readFields();
+ Exception exception = (Exception) fields.get("exception", null);
+ if (exception != null) {
+ SharedSecrets.getJavaLangAccess().setCause(this, exception);
+ }
}
- public String toString() {
- String s = getClass().getName();
- return (exception != null) ? (s + ": " + exception.toString()) : s;
+ /*
+ * To maintain compatibility with older implementation, write a serial
+ * "exception" field with the cause as the value.
+ */
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ ObjectOutputStream.PutField fields = out.putFields();
+ fields.put("exception", getException());
+ out.writeFields();
}
}
< prev index next >