Skip to content

Commit

Permalink
8210721: Replace legacy serial exception field with Throwable::cause
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs, lancea
  • Loading branch information
Mandy Chung committed Sep 17, 2018
1 parent b27f471 commit b72ab42
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 105 deletions.
57 changes: 38 additions & 19 deletions src/java.base/share/classes/java/lang/ClassNotFoundException.java
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -92,8 +88,7 @@ public ClassNotFoundException(String s) {
* @since 1.2
*/
public ClassNotFoundException(String s, Throwable ex) {
super(s, null); // Disallow initCause
this.ex = ex;
super(s, ex); // Disallow initCause
}

/**
Expand All @@ -108,18 +103,42 @@ public ClassNotFoundException(String s, Throwable ex) {
* @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
*/
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.
*/
public Throwable getCause() {
return ex;
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("ex", super.getCause());
out.writeFields();
}
}
Expand Up @@ -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
Expand All @@ -47,24 +52,14 @@ 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
* throwable object.
* A detail message is a String that describes this particular exception.
*/
public ExceptionInInitializerError() {
initCause(null); // Disallow subsequent initCause
initCause(null); // Disallow subsequent initCause
}

/**
Expand All @@ -76,23 +71,20 @@ public ExceptionInInitializerError() {
* @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
}

/**
Expand All @@ -109,18 +101,43 @@ public ExceptionInInitializerError(String s) {
* 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();
}

}
3 changes: 3 additions & 0 deletions src/java.base/share/classes/java/lang/System.java
Expand Up @@ -2201,6 +2201,9 @@ public byte[] getBytesUTF8NoRepl(String s) {
return StringCoding.getBytesUTF8NoRepl(s);
}

public void setCause(Throwable t, Throwable cause) {
t.setCause(cause);
}
});
}
}
10 changes: 10 additions & 0 deletions src/java.base/share/classes/java/lang/Throwable.java
Expand Up @@ -466,6 +466,16 @@ public synchronized Throwable initCause(Throwable cause) {
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:
Expand Down
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -70,8 +66,7 @@ protected InvocationTargetException() {
* @param target the target exception
*/
public InvocationTargetException(Throwable target) {
super((Throwable)null); // Disallow initCause
this.target = target;
super(null, target); // Disallow initCause
}

/**
Expand All @@ -82,8 +77,7 @@ public InvocationTargetException(Throwable target) {
* @param s the detail message
*/
public InvocationTargetException(Throwable target, String s) {
super(s, null); // Disallow initCause
this.target = target;
super(s, target); // Disallow initCause
}

/**
Expand All @@ -96,17 +90,42 @@ public InvocationTargetException(Throwable target, String s) {
* @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
*/
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.
*/
public Throwable getCause() {
return target;
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("target", super.getCause());
out.writeFields();
}
}

0 comments on commit b72ab42

Please sign in to comment.