Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8282696: Add constructors taking a cause to InvalidObjectException and InvalidClassException #7711

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/java.base/share/classes/java/io/InvalidClassException.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -73,6 +73,30 @@ public InvalidClassException(String cname, String reason) {
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.
*/
Expand Down
16 changes: 15 additions & 1 deletion src/java.base/share/classes/java/io/InvalidObjectException.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -48,4 +48,18 @@ public class InvalidObjectException extends ObjectStreamException {
public InvalidObjectException(String reason) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
jddarcy marked this conversation as resolved.
Show resolved Hide resolved
super(reason);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be: super(reason, cause)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; good catch -- will fix in the next push. Thanks.

}
}
45 changes: 16 additions & 29 deletions src/java.base/share/classes/java/io/ObjectInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1429,9 +1429,7 @@ private void filterCheck(Class<?> clazz, int arrayLength)
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);
}
}

Expand Down Expand Up @@ -1996,14 +1994,10 @@ private ObjectStreamClass readProxyDesc(boolean unshared)
} 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
Expand All @@ -2016,10 +2010,8 @@ private ObjectStreamClass readProxyDesc(boolean unshared)
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--;
}
Expand Down Expand Up @@ -2050,8 +2042,8 @@ private ObjectStreamClass readNonProxyDesc(boolean unshared)
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;
Expand Down Expand Up @@ -2221,9 +2213,9 @@ private Enum<?> readEnum(boolean unshared) throws IOException {
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be reflowed to be on two lines instead of 3? Your call.

}
if (!unshared) {
handles.setObject(enumHandle, result);
Expand Down Expand Up @@ -2262,9 +2254,8 @@ private Object readOrdinaryObject(boolean unshared)
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);
Expand Down Expand Up @@ -2388,16 +2379,12 @@ private Object readRecord(ObjectStreamClass desc) throws IOException {
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);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/java.base/share/classes/java/io/ObjectStreamClass.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -709,8 +709,9 @@ void readNonProxy(ObjectInputStream in)
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();
Expand Down
24 changes: 23 additions & 1 deletion src/java.base/share/classes/java/io/ObjectStreamException.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -44,10 +44,32 @@ protected ObjectStreamException(String message) {
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);
}
}