Issue #2075 cleanup MultiException to better use suppressed (#2105)

* Issue #2075 cleanup multiexceptio to better use suppressed
* Update MultiException.java fixes from review
Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-01-23 08:34:07 +01:00 committed by GitHub
parent c1d566a56d
commit ad8f2abbb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 113 additions and 62 deletions

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.util; package org.eclipse.jetty.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -30,8 +31,6 @@ import java.util.List;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class MultiException extends Exception public class MultiException extends Exception
{ {
private List<Throwable> nested;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public MultiException() public MultiException()
{ {
@ -44,69 +43,105 @@ public class MultiException extends Exception
if (e==null) if (e==null)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if(nested == null) if (e instanceof MultiException)
Arrays.stream(MultiException.class.cast(e).getSuppressed()).forEach(this::add);
else
{ {
if (getCause()==null)
initCause(e); initCause(e);
nested = new ArrayList<>();
}
else else
addSuppressed(e); addSuppressed(e);
if (e instanceof MultiException)
{
MultiException me = (MultiException)e;
nested.addAll(me.nested);
} }
else
nested.add(e);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public int size() public int size()
{ {
return (nested ==null)?0:nested.size(); if (getCause()==null)
return 0;
return 1+getSuppressed().length;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public List<Throwable> getThrowables() public List<Throwable> getThrowables()
{ {
if(nested == null) if (getCause()==null)
return Collections.emptyList(); return Collections.emptyList();
return nested;
Throwable[] suppressed = getSuppressed();
List<Throwable> list = new ArrayList<>(suppressed.length+1);
list.add(getCause());
Arrays.stream(suppressed).forEach(list::add);
return list;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public Throwable getThrowable(int i) public Throwable getThrowable(int i)
{ {
return nested.get(i); if (getCause()==null)
throw new ArrayIndexOutOfBoundsException();
if (i==0)
return getCause();
return getSuppressed()[i-1];
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** Throw a multiexception. /** Throw a multiexception.
* If this multi exception is empty then no action is taken. If it * If this multi exception is empty then no action is taken. If it
* contains a single exception that is thrown, otherwise the this * contains a single exception then that is thrown, otherwise the this
* multi exception is thrown. * multi exception is thrown.
* @exception Exception the Error or Exception if nested is 1, or the MultiException itself if nested is more than 1. * @exception Exception the Error or Exception if nested is 1, or
* the MultiException itself if nested is more than 1.
*/ */
public void ifExceptionThrow() public void ifExceptionThrow()
throws Exception throws Exception
{ {
if(nested == null) Throwable cause=getCause();
if (cause==null)
return; return;
switch (nested.size()) Throwable[] suppressed = getSuppressed();
if (suppressed.length==0)
{ {
case 0: if (cause instanceof Error)
break; throw (Error)cause;
case 1: if (cause instanceof Exception)
Throwable th=nested.get(0); throw (Exception)cause;
if (th instanceof Error) }
throw (Error)th;
if (th instanceof Exception)
throw (Exception)th;
default:
throw this; throw this;
} }
/* ------------------------------------------------------------ */
/** Throw an Exception, potentially with suppress.
* If this multi exception is empty then no action is taken. If the first
* exception added is an Error or Exception, then that is throw with
* any additional exceptions added as suppressed. Otherwise this exception
* is thrown.
* @exception Exception the Error or Exception if at least one is added.
*/
public void ifExceptionThrowSuppressed()
throws Exception
{
Throwable cause=getCause();
if (cause==null)
return;
if (cause instanceof Error)
{
Arrays.stream(getSuppressed()).forEach(cause::addSuppressed);
throw (Error)cause;
}
if (cause instanceof Exception)
{
Arrays.stream(getSuppressed()).forEach(cause::addSuppressed);
throw (Exception)cause;
}
throw this;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -121,40 +156,36 @@ public class MultiException extends Exception
public void ifExceptionThrowRuntime() public void ifExceptionThrowRuntime()
throws Error throws Error
{ {
if(nested == null) Throwable cause = getCause();
if (cause==null)
return; return;
switch (nested.size()) Throwable[] nested = getSuppressed();
if (nested.length==0)
{ {
case 0: if (cause instanceof Error)
break; throw (Error)cause;
case 1: if (cause instanceof RuntimeException)
Throwable th=nested.get(0); throw (RuntimeException)cause;
if (th instanceof Error) throw new RuntimeException(cause);
throw (Error)th;
else if (th instanceof RuntimeException)
throw (RuntimeException)th;
else
throw new RuntimeException(th);
default:
throw new RuntimeException(this);
} }
throw new RuntimeException(this);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** Throw a multiexception. /** Throw a MultiException.
* If this multi exception is empty then no action is taken. If it * If this multi exception is empty then no action is taken. If it
* contains a any exceptions then this * contains a any exceptions then this multi exception is thrown.
* multi exception is thrown.
* @throws MultiException the multiexception if there are nested exception * @throws MultiException the multiexception if there are nested exception
*/ */
public void ifExceptionThrowMulti() public void ifExceptionThrowMulti()
throws MultiException throws MultiException
{ {
if(nested == null) if (getCause()==null)
return; return;
if (nested.size()>0)
throw this; throw this;
} }
@ -164,11 +195,7 @@ public class MultiException extends Exception
{ {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
str.append(MultiException.class.getSimpleName()); str.append(MultiException.class.getSimpleName());
if((nested == null) || (nested.size()<=0)) { str.append(Arrays.asList(getSuppressed()));
str.append("[]");
} else {
str.append(nested);
}
return str.toString(); return str.toString();
} }

View File

@ -37,6 +37,7 @@ public class MultiExceptionTest
me.ifExceptionThrow(); me.ifExceptionThrow();
me.ifExceptionThrowMulti(); me.ifExceptionThrowMulti();
me.ifExceptionThrowRuntime(); me.ifExceptionThrowRuntime();
me.ifExceptionThrowSuppressed();
} }
@Test @Test
@ -78,6 +79,16 @@ public class MultiExceptionTest
assertTrue(e.getCause()==io); assertTrue(e.getCause()==io);
} }
try
{
me.ifExceptionThrowSuppressed();
assertTrue(false);
}
catch(IOException e)
{
assertTrue(e==io);
}
me = new MultiException(); me = new MultiException();
RuntimeException run = new RuntimeException("one"); RuntimeException run = new RuntimeException("one");
me.add(run); me.add(run);
@ -98,7 +109,7 @@ public class MultiExceptionTest
{ {
MultiException me = new MultiException(); MultiException me = new MultiException();
IOException io = new IOException("one"); IOException io = new IOException("one");
RuntimeException run = new RuntimeException("one"); RuntimeException run = new RuntimeException("two");
me.add(io); me.add(io);
me.add(run); me.add(run);
@ -134,6 +145,19 @@ public class MultiExceptionTest
assertTrue(e.getCause()==me); assertTrue(e.getCause()==me);
} }
try
{
me.ifExceptionThrowSuppressed();
assertTrue(false);
}
catch(IOException e)
{
assertTrue(e==io);
assertEquals(1,e.getSuppressed().length);
assertTrue(e.getSuppressed()[0]==run);
}
me = new MultiException(); me = new MultiException();
me.add(run); me.add(run);
me.add(run); me.add(run);