mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8210721: Replace legacy serial exception field with Throwable::cause
Reviewed-by: dfuchs, lancea
This commit is contained in:
parent
b27f471bdd
commit
b72ab42e49
9 changed files with 486 additions and 108 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue