Skip to content
Permalink
Browse files
8282696: Add constructors taking a cause to InvalidObjectException an…
…d InvalidClassException

Reviewed-by: lancea
  • Loading branch information
jddarcy committed Mar 7, 2022
1 parent 6fc73f7 commit 104e3cb
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 36 deletions.
@@ -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 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.
*/
@Override
public String getMessage() {
if (classname == null)
return super.getMessage();
@@ -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);
}
}
@@ -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);
}
}

@@ -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
@@ -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--;
}
@@ -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;
@@ -2221,9 +2213,8 @@ 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);
}
if (!unshared) {
handles.setObject(enumHandle, result);
@@ -2262,9 +2253,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);
@@ -2388,16 +2378,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);
}
}

@@ -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 @@ 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();
@@ -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 @@ 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);
}
}
@@ -0,0 +1,58 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8282696
* @summary Verify message and cause handling of InvalidClassException
*/
import java.io.*;
import java.util.Objects;

public class TestIceConstructors {
public static void main(String... args) {
String reason = "reason";
Throwable cause = new RuntimeException();

testException(new InvalidClassException(reason),
reason, null);
testException(new InvalidClassException(reason, cause),
reason, cause);
testException(new InvalidClassException("prefix", reason, cause),
"prefix" + "; " + reason, cause);
}

private static void testException(InvalidClassException ice,
String expectedMessage,
Throwable expectedCause) {
var message = ice.getMessage();
if (!Objects.equals(message, expectedMessage)) {
throw new RuntimeException("Unexpected message " + message);
}

var cause = ice.getCause();
if (cause != expectedCause) {
throw new RuntimeException("Unexpected cause");
}
}
}
@@ -0,0 +1,56 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8282696
* @summary Verify message and cause handling of InvalidObjectException
*/
import java.io.*;
import java.util.Objects;

public class TestIoeConstructors {
public static void main(String... args) {
String reason = "reason";
Throwable cause = new RuntimeException();

testException(new InvalidObjectException(reason),
reason, null);
testException(new InvalidObjectException(reason, cause),
reason, cause);
}

private static void testException(InvalidObjectException ioe,
String expectedMessage,
Throwable expectedCause) {
var message = ioe.getMessage();
if (!Objects.equals(message, expectedMessage)) {
throw new RuntimeException("Unexpected message " + message);
}

var cause = ioe.getCause();
if (cause != expectedCause) {
throw new RuntimeException("Unexpected cause");
}
}
}

1 comment on commit 104e3cb

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.