8210721: Replace legacy serial exception field with Throwable::cause

Reviewed-by: dfuchs, lancea
This commit is contained in:
Mandy Chung 2018-09-17 15:22:46 -07:00
parent b27f471bdd
commit b72ab42e49
9 changed files with 486 additions and 108 deletions

View file

@ -25,6 +25,11 @@
package java.lang;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
/**
* Thrown when an application tries to load in a class through its
* string name using:
@ -56,15 +61,6 @@ public class ClassNotFoundException extends ReflectiveOperationException {
*/
private static final long serialVersionUID = 9176873029745254542L;
/**
* This field holds the exception ex if the
* ClassNotFoundException(String s, Throwable ex) constructor was
* used to instantiate the object
* @serial
* @since 1.2
*/
private Throwable ex;
/**
* Constructs a <code>ClassNotFoundException</code> with no detail message.
*/
@ -92,8 +88,7 @@ public class ClassNotFoundException extends ReflectiveOperationException {
* @since 1.2
*/
public ClassNotFoundException(String s, Throwable ex) {
super(s, null); // Disallow initCause
this.ex = ex;
super(s, ex); // Disallow initCause
}
/**
@ -108,18 +103,42 @@ public class ClassNotFoundException extends ReflectiveOperationException {
* @since 1.2
*/
public Throwable getException() {
return ex;
return super.getCause();
}
/**
* Returns the cause of this exception (the exception that was raised
* if an error occurred while attempting to load the class; otherwise
* {@code null}).
* Serializable fields for ClassNotFoundException.
*
* @return the cause of this exception.
* @since 1.4
* @serialField ex Throwable
*/
public Throwable getCause() {
return ex;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("ex", Throwable.class)
};
/*
* Reconstitutes the ClassNotFoundException instance from a stream
* and initialize the cause properly when deserializing from an older
* version.
*
* The getException and getCause method returns the private "ex" field
* in the older implementation and ClassNotFoundException::cause
* was set to null.
*/
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = s.readFields();
Throwable exception = (Throwable) fields.get("ex", null);
if (exception != null) {
setCause(exception);
}
}
/*
* To maintain compatibility with older implementation, write a serial
* "ex" field with the cause as the value.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("ex", super.getCause());
out.writeFields();
}
}

View file

@ -25,6 +25,11 @@
package java.lang;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
/**
* Signals that an unexpected exception has occurred in a static initializer.
* An <code>ExceptionInInitializerError</code> is thrown to indicate that an
@ -47,16 +52,6 @@ public class ExceptionInInitializerError extends LinkageError {
*/
private static final long serialVersionUID = 1521711792217232256L;
/**
* This field holds the exception if the
* ExceptionInInitializerError(Throwable thrown) constructor was
* used to instantiate the object
*
* @serial
*
*/
private Throwable exception;
/**
* Constructs an <code>ExceptionInInitializerError</code> with
* <code>null</code> as its detail message string and with no saved
@ -64,7 +59,7 @@ public class ExceptionInInitializerError extends LinkageError {
* A detail message is a String that describes this particular exception.
*/
public ExceptionInInitializerError() {
initCause(null); // Disallow subsequent initCause
initCause(null); // Disallow subsequent initCause
}
/**
@ -76,23 +71,20 @@ public class ExceptionInInitializerError extends LinkageError {
* @param thrown The exception thrown
*/
public ExceptionInInitializerError(Throwable thrown) {
initCause(null); // Disallow subsequent initCause
this.exception = thrown;
super(null, thrown); // Disallow subsequent initCause
}
/**
* Constructs an ExceptionInInitializerError with the specified detail
* Constructs an {@code ExceptionInInitializerError} with the specified detail
* message string. A detail message is a String that describes this
* particular exception. The detail message string is saved for later
* retrieval by the {@link Throwable#getMessage()} method. There is no
* saved throwable object.
*
*
* @param s the detail message
*/
public ExceptionInInitializerError(String s) {
super(s);
initCause(null); // Disallow subsequent initCause
super(s, null); // Disallow subsequent initCause
}
/**
@ -109,18 +101,43 @@ public class ExceptionInInitializerError extends LinkageError {
* throwable object.
*/
public Throwable getException() {
return exception;
return super.getCause();
}
/**
* Returns the cause of this error (the exception that occurred
* during a static initialization that caused this error to be created).
* Serializable fields for ExceptionInInitializerError.
*
* @return the cause of this error or <code>null</code> if the
* cause is nonexistent or unknown.
* @since 1.4
* @serialField exception Throwable
*/
public Throwable getCause() {
return exception;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("exception", Throwable.class)
};
/*
* Reconstitutes the ExceptionInInitializerError 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 ExceptionInInitializerError::cause
* was set to null.
*/
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = s.readFields();
Throwable exception = (Throwable) fields.get("exception", null);
if (exception != null) {
setCause(exception);
}
}
/*
* 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", super.getCause());
out.writeFields();
}
}

View file

@ -2201,6 +2201,9 @@ public final class System {
return StringCoding.getBytesUTF8NoRepl(s);
}
public void setCause(Throwable t, Throwable cause) {
t.setCause(cause);
}
});
}
}

View file

@ -466,6 +466,16 @@ public class Throwable implements Serializable {
return this;
}
/*
* This is called by readObject of a few exceptions such as
* ClassNotFoundException and ExceptionInInitializerError to deserialize
* a stream output from an older runtime version where the cause may
* have set to null.
*/
final void setCause(Throwable t) {
this.cause = t;
}
/**
* Returns a short description of this throwable.
* The result is the concatenation of:

View file

@ -25,6 +25,12 @@
package java.lang.reflect;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import jdk.internal.misc.SharedSecrets;
/**
* InvocationTargetException is a checked exception that wraps
* an exception thrown by an invoked method or constructor.
@ -46,16 +52,6 @@ public class InvocationTargetException extends ReflectiveOperationException {
*/
private static final long serialVersionUID = 4085088731926701167L;
/**
* This field holds the target if the
* InvocationTargetException(Throwable target) constructor was
* used to instantiate the object
*
* @serial
*
*/
private Throwable target;
/**
* Constructs an {@code InvocationTargetException} with
* {@code null} as the target exception.
@ -70,8 +66,7 @@ public class InvocationTargetException extends ReflectiveOperationException {
* @param target the target exception
*/
public InvocationTargetException(Throwable target) {
super((Throwable)null); // Disallow initCause
this.target = target;
super(null, target); // Disallow initCause
}
/**
@ -82,8 +77,7 @@ public class InvocationTargetException extends ReflectiveOperationException {
* @param s the detail message
*/
public InvocationTargetException(Throwable target, String s) {
super(s, null); // Disallow initCause
this.target = target;
super(s, target); // Disallow initCause
}
/**
@ -96,17 +90,42 @@ public class InvocationTargetException extends ReflectiveOperationException {
* @return the thrown target exception (cause of this exception).
*/
public Throwable getTargetException() {
return target;
return super.getCause();
}
/**
* Returns the cause of this exception (the thrown target exception,
* which may be {@code null}).
* Serializable fields for UndeclaredThrowableException.
*
* @return the cause of this exception.
* @since 1.4
* @serialField target Throwable
*/
public Throwable getCause() {
return target;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("target", Throwable.class)
};
/*
* Reconstitutes the InvocationTargetException instance from a stream
* and initialize the cause properly when deserializing from an older
* version.
*
* The getException and getCause method returns the private "target" field
* in the older implementation and InvocationTargetException::cause
* was set to null.
*/
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = s.readFields();
Throwable exception = (Throwable) fields.get("target", null);
if (exception != null) {
SharedSecrets.getJavaLangAccess().setCause(this, exception);
}
}
/*
* To maintain compatibility with older implementation, write a serial
* "target" field with the cause as the value.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("target", super.getCause());
out.writeFields();
}
}

View file

@ -25,6 +25,12 @@
package java.lang.reflect;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import jdk.internal.misc.SharedSecrets;
/**
* Thrown by a method invocation on a proxy instance if its invocation
* handler's {@link InvocationHandler#invoke invoke} method throws a
@ -58,12 +64,6 @@ package java.lang.reflect;
public class UndeclaredThrowableException extends RuntimeException {
static final long serialVersionUID = 330127114055056639L;
/**
* the undeclared checked exception that was thrown
* @serial
*/
private Throwable undeclaredThrowable;
/**
* Constructs an {@code UndeclaredThrowableException} with the
* specified {@code Throwable}.
@ -72,8 +72,7 @@ public class UndeclaredThrowableException extends RuntimeException {
* that was thrown
*/
public UndeclaredThrowableException(Throwable undeclaredThrowable) {
super((Throwable) null); // Disallow initCause
this.undeclaredThrowable = undeclaredThrowable;
super(null, undeclaredThrowable); // Disallow initCause
}
/**
@ -87,8 +86,7 @@ public class UndeclaredThrowableException extends RuntimeException {
public UndeclaredThrowableException(Throwable undeclaredThrowable,
String s)
{
super(s, null); // Disallow initCause
this.undeclaredThrowable = undeclaredThrowable;
super(s, undeclaredThrowable); // Disallow initCause
}
/**
@ -102,18 +100,38 @@ public class UndeclaredThrowableException extends RuntimeException {
* @return the undeclared checked exception that was thrown
*/
public Throwable getUndeclaredThrowable() {
return undeclaredThrowable;
return super.getCause();
}
/**
* Returns the cause of this exception (the {@code Throwable}
* instance wrapped in this {@code UndeclaredThrowableException},
* which may be {@code null}).
* Serializable fields for UndeclaredThrowableException.
*
* @return the cause of this exception.
* @since 1.4
* @serialField undeclaredThrowable Throwable
*/
public Throwable getCause() {
return undeclaredThrowable;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("undeclaredThrowable", Throwable.class)
};
/*
* Reconstitutes the UndeclaredThrowableException instance from a stream
* and initialize the cause properly when deserializing from an older
* version.
*/
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = s.readFields();
Throwable exception = (Throwable) fields.get("undeclaredThrowable", null);
if (exception != null) {
SharedSecrets.getJavaLangAccess().setCause(this, exception);
}
}
/*
* To maintain compatibility with older implementation, write a serial
* "ex" field with the cause as the value.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("undeclaredThrowable", super.getCause());
out.writeFields();
}
}

View file

@ -25,6 +25,12 @@
package java.security;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import jdk.internal.misc.SharedSecrets;
/**
* This exception is thrown by
* {@code doPrivileged(PrivilegedExceptionAction)} and
@ -52,11 +58,6 @@ 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 &quot;wrapping&quot;
* the specific Exception.
@ -64,8 +65,7 @@ public class PrivilegedActionException extends Exception {
* @param exception The exception thrown
*/
public PrivilegedActionException(Exception exception) {
super((Throwable)null); // Disallow initCause
this.exception = exception;
super(null, exception); // Disallow initCause
}
/**
@ -84,23 +84,49 @@ public class PrivilegedActionException extends Exception {
* AccessControlContext)
*/
public Exception getException() {
return exception;
}
/**
* Returns the cause of this exception (the exception thrown by
* the privileged computation that resulted in this
* {@code PrivilegedActionException}).
*
* @return the cause of this exception.
* @since 1.4
*/
public Throwable getCause() {
return exception;
return (Exception)super.getCause();
}
public String toString() {
String s = getClass().getName();
return (exception != null) ? (s + ": " + exception.toString()) : s;
Throwable cause = super.getCause();
return (cause != null) ? (s + ": " + cause.toString()) : s;
}
/**
* Serializable fields for UndeclaredThrowableException.
*
* @serialField undeclaredThrowable Throwable
*/
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);
}
}
/*
* 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", super.getCause());
out.writeFields();
}
}