8282696: Add constructors taking a cause to InvalidObjectException and InvalidClassException

Reviewed-by: lancea
This commit is contained in:
Joe Darcy 2022-03-07 14:47:52 +00:00
parent 6fc73f709b
commit 104e3cb24b
7 changed files with 198 additions and 36 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -73,9 +73,34 @@ public class InvalidClassException extends ObjectStreamException {
classname = cname;
}
/**
* Report an InvalidClassException for the reason and cause specified.
*
* @param reason String describing the reason for the exception.
* @param cause the cause
* @since 19
*/
public InvalidClassException(String reason, Throwable cause) {
super(reason, cause);
}
/**
* Report an InvalidClassException for the reason and cause specified.
*
* @param cname a String naming the invalid class.
* @param reason String describing the reason for the exception.
* @param cause the cause
* @since 19
*/
public InvalidClassException(String cname, String reason, Throwable cause) {
super(reason, cause);
classname = cname;
}
/**
* Produce the message and include the classname, if present.
*/
@Override
public String getMessage() {
if (classname == null)
return super.getMessage();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -45,7 +45,21 @@ public class InvalidObjectException extends ObjectStreamException {
*
* @see ObjectInputValidation
*/
public InvalidObjectException(String reason) {
public InvalidObjectException(String reason) {
super(reason);
}
/**
* Constructs an {@code InvalidObjectException} with the given
* reason and cause.
*
* @param reason Detailed message explaining the reason for the failure.
* @param cause the cause
*
* @see ObjectInputValidation
* @since 19
*/
public InvalidObjectException(String reason, Throwable cause) {
super(reason, cause);
}
}

View file

@ -1429,9 +1429,7 @@ public class ObjectInputStream
event.commit();
}
if (serialFilter != null && (status == null || status == ObjectInputFilter.Status.REJECTED)) {
InvalidClassException ice = new InvalidClassException("filter status: " + status);
ice.initCause(ex);
throw ice;
throw new InvalidClassException("filter status: " + status, ex);
}
}
@ -1996,14 +1994,10 @@ public class ObjectInputStream
} catch (ClassNotFoundException ex) {
resolveEx = ex;
} catch (IllegalAccessError aie) {
IOException ice = new InvalidClassException(aie.getMessage());
ice.initCause(aie);
throw ice;
throw new InvalidClassException(aie.getMessage(), aie);
} catch (OutOfMemoryError memerr) {
IOException ex = new InvalidObjectException("Proxy interface limit exceeded: " +
Arrays.toString(ifaces));
ex.initCause(memerr);
throw ex;
throw new InvalidObjectException("Proxy interface limit exceeded: " +
Arrays.toString(ifaces), memerr);
}
// Call filterCheck on the class before reading anything else
@ -2016,10 +2010,8 @@ public class ObjectInputStream
depth++;
desc.initProxy(cl, resolveEx, readClassDesc(false));
} catch (OutOfMemoryError memerr) {
IOException ex = new InvalidObjectException("Proxy interface limit exceeded: " +
Arrays.toString(ifaces));
ex.initCause(memerr);
throw ex;
throw new InvalidObjectException("Proxy interface limit exceeded: " +
Arrays.toString(ifaces), memerr);
} finally {
depth--;
}
@ -2050,8 +2042,8 @@ public class ObjectInputStream
try {
readDesc = readClassDescriptor();
} catch (ClassNotFoundException ex) {
throw (IOException) new InvalidClassException(
"failed to read class descriptor").initCause(ex);
throw new InvalidClassException("failed to read class descriptor",
ex);
}
Class<?> cl = null;
@ -2221,9 +2213,8 @@ public class ObjectInputStream
Enum<?> en = Enum.valueOf((Class)cl, name);
result = en;
} catch (IllegalArgumentException ex) {
throw (IOException) new InvalidObjectException(
"enum constant " + name + " does not exist in " +
cl).initCause(ex);
throw new InvalidObjectException("enum constant " +
name + " does not exist in " + cl, ex);
}
if (!unshared) {
handles.setObject(enumHandle, result);
@ -2262,9 +2253,8 @@ public class ObjectInputStream
try {
obj = desc.isInstantiable() ? desc.newInstance() : null;
} catch (Exception ex) {
throw (IOException) new InvalidClassException(
desc.forClass().getName(),
"unable to create instance").initCause(ex);
throw new InvalidClassException(desc.forClass().getName(),
"unable to create instance", ex);
}
passHandle = handles.assign(unshared ? unsharedMarker : obj);
@ -2388,16 +2378,12 @@ public class ObjectInputStream
try {
return (Object) ctrMH.invokeExact(fieldValues.primValues, fieldValues.objValues);
} catch (Exception e) {
InvalidObjectException ioe = new InvalidObjectException(e.getMessage());
ioe.initCause(e);
throw ioe;
throw new InvalidObjectException(e.getMessage(), e);
} catch (Error e) {
throw e;
} catch (Throwable t) {
ObjectStreamException ose = new InvalidObjectException(
"ReflectiveOperationException during deserialization");
ose.initCause(t);
throw ose;
throw new InvalidObjectException("ReflectiveOperationException " +
"during deserialization", t);
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -709,8 +709,9 @@ public class ObjectStreamClass implements Serializable {
try {
fields[i] = new ObjectStreamField(fname, signature, false);
} catch (RuntimeException e) {
throw (IOException) new InvalidClassException(name,
"invalid descriptor for field " + fname).initCause(e);
throw new InvalidClassException(name,
"invalid descriptor for field " +
fname, e);
}
}
computeFieldOffsets();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -44,10 +44,32 @@ public abstract class ObjectStreamException extends IOException {
super(message);
}
/**
* Create an ObjectStreamException with the specified message and
* cause.
*
* @param message the detailed message for the exception
* @param cause the cause
* @since 19
*/
protected ObjectStreamException(String message, Throwable cause) {
super(message, cause);
}
/**
* Create an ObjectStreamException.
*/
protected ObjectStreamException() {
super();
}
/**
* Create an ObjectStreamException with the specified cause.
*
* @param cause the cause
* @since 19
*/
protected ObjectStreamException(Throwable cause) {
super(cause);
}
}