< prev index next >
src/java.base/share/classes/java/lang/reflect/InvocationTargetException.java
Print this page
@@ -23,10 +23,17 @@
* questions.
*/
package java.lang.reflect;
+import jdk.internal.misc.SharedSecrets;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamField;
+
/**
* InvocationTargetException is a checked exception that wraps
* an exception thrown by an invoked method or constructor.
*
* <p>As of release 1.4, this exception has been retrofitted to conform to
@@ -45,20 +52,10 @@
* Use serialVersionUID from JDK 1.1.X for interoperability
*/
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.
*/
protected InvocationTargetException() {
super((Throwable)null); // Disallow initCause
@@ -68,24 +65,22 @@
* Constructs a InvocationTargetException with a target exception.
*
* @param target the target exception
*/
public InvocationTargetException(Throwable target) {
- super((Throwable)null); // Disallow initCause
- this.target = target;
+ super(null, target); // Disallow initCause
}
/**
* Constructs a InvocationTargetException with a target exception
* and a detail message.
*
* @param target the target exception
* @param s the detail message
*/
public InvocationTargetException(Throwable target, String s) {
- super(s, null); // Disallow initCause
- this.target = target;
+ super(s, target); // Disallow initCause
}
/**
* Get the thrown target exception.
*
@@ -94,19 +89,44 @@
* obtaining this information.
*
* @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();
}
}
< prev index next >