|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectdk.rode.thesis.meta.util.Throwables
public class Throwables
Utilities to manipulate throwable types and objects.
Constructor Summary | |
---|---|
private |
Throwables()
Private off-limit constructor. |
Method Summary | ||
---|---|---|
static List<Throwable> |
getCauses(Throwable throwable)
Returns a list containing the throwable cause chain acquired from throwable . |
|
static List<Exception> |
getCheckedExceptions(Collection<?> objects)
Returns a list containing the checked exceptions from objects . |
|
static List<Exception> |
getCheckedExceptions(Object... objects)
Returns a list containing the checked exceptions from objects . |
|
static List<Class<? extends Exception>> |
getCheckedExceptionTypes(Class<?>... types)
Returns a list containing the checked exception types from types . |
|
static List<Class<? extends Exception>> |
getCheckedExceptionTypes(Collection<Class<?>> types)
Returns a list containing the checked exception types from types . |
|
static int |
getIndex(StackTraceElement[] trace,
CharSequence... tokens)
Returns the last index + 1 in the supplied stack trace where one of the supplied tokens is found. |
|
static List<Throwable> |
getUncheckedThrowables(Collection<?> objects)
Returns a list containing the unchecked throwables from objects . |
|
static List<Throwable> |
getUncheckedThrowables(Object... objects)
Returns a list containing the unchecked throwables from objects . |
|
static List<Class<? extends Throwable>> |
getUncheckedThrowableTypes(Class<?>... types)
Returns a list containing the unchecked throwable types from types . |
|
static List<Class<? extends Throwable>> |
getUncheckedThrowableTypes(Collection<Class<?>> types)
Returns a list containing the unchecked throwable types from types . |
|
static boolean |
isChecked(Throwable throwable)
Returns true if throwable is a checked exception, false if not. |
|
static Exception |
toCheckedException(Throwable throwable)
Returns a checked exception based on throwable . |
|
static Exception |
toCheckedException(Throwable throwable,
boolean unwind)
Returns a checked exception based on throwable . |
|
static
|
toCheckedException(Throwable throwable,
Class<T> type)
Returns a checked exception of the type supplied as type
based on throwable . |
|
static
|
toCheckedException(Throwable throwable,
Class<T> type,
boolean unwind)
Returns a checked exception of the type supplied as type
based on throwable . |
|
static RuntimeException |
toRuntimeException(Throwable throwable)
Returns a runtime exception based on throwable . |
|
static RuntimeException |
toRuntimeException(Throwable throwable,
boolean unwind)
Returns a runtime exception based on throwable . |
|
static
|
toRuntimeException(Throwable throwable,
Class<T> type)
Returns a runtime exception of the type supplied as type
based on throwable . |
|
static
|
toRuntimeException(Throwable throwable,
Class<T> type,
boolean unwind)
Returns a runtime exception of the type supplied as type
based on throwable . |
|
static CharSequence |
toStackTrace(Throwable throwable)
Serializes the stack trace from throwable into
the returned char sequence. |
|
static
|
toThrowableType(Throwable throwable,
Class<T> type)
Returns a throwable of the type supplied as type
based on throwable . |
|
static
|
toThrowableType(Throwable throwable,
Class<T> type,
boolean unwind)
Returns a throwable of the type supplied as type
based on throwable . |
|
static
|
unwind(Throwable throwable,
Class<T> type)
Returns the first throwable assignable to type in the
cause chain acquired from throwable , if any. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
private Throwables()
Method Detail |
---|
public static final List<Throwable> getCauses(Throwable throwable)
throwable
.
The first index contains throwable
, the second index
the cause A of throwable
, the third the cause B
of A, etc.
throwable
- The throwable to supply the cause chain; cannot be null.
NullPointerException
- If throwable
is null.public static final List<Exception> getCheckedExceptions(Collection<?> objects)
objects
. The returned list may contain duplicates.
objects
- The objects; cannot be null, but may contain null entries.
objects
; never null.
NullPointerException
- If objects
is null.public static final List<Exception> getCheckedExceptions(Object... objects)
objects
. The returned list may contain duplicates.
objects
- The objects; cannot be null, but may contain null entries.
objects
; never null.
NullPointerException
- If objects
is null.public static final List<Class<? extends Exception>> getCheckedExceptionTypes(Class<?>... types)
types
. The returned list may contain duplicates.
types
- The types; cannot be null, but may contain null entries.
types
; never null.
NullPointerException
- If types
is null.public static final List<Class<? extends Exception>> getCheckedExceptionTypes(Collection<Class<?>> types)
types
. The returned list may contain duplicates.
types
- The types; cannot be null, but may contain null entries.
types
; never null.
NullPointerException
- If types
is null.public static final int getIndex(StackTraceElement[] trace, CharSequence... tokens)
The search for a token is done in the class name available via
a stack trace element using
Strings.indexOfIgnoreCase(CharSequence, CharSequence)
.
Hence, the search is case-insensitive, and the search for tokens
is done in the order the tokens are supplied.
trace
- The stack trace; cannot be null.tokens
- The list of tokens to search for.
trace.length
if no token could be found.
NullPointerException
- If trace
is null.public static final List<Throwable> getUncheckedThrowables(Collection<?> objects)
objects
. The returned list may contain duplicates.
objects
- The objects; cannot be null, but may contain null entries.
objects
; never null.
NullPointerException
- If objects
is null.public static final List<Throwable> getUncheckedThrowables(Object... objects)
objects
. The returned list may contain duplicates.
objects
- The objects; cannot be null, but may contain null entries.
objects
; never null.
NullPointerException
- If objects
is null.public static final List<Class<? extends Throwable>> getUncheckedThrowableTypes(Class<?>... types)
types
. The returned list may contain duplicates.
types
- The types; cannot be null, but may contain null entries.
types
; never null.
NullPointerException
- If types
is null.public static final List<Class<? extends Throwable>> getUncheckedThrowableTypes(Collection<Class<?>> types)
types
. The returned list may contain duplicates.
types
- The types; cannot be null, but may contain null entries.
types
; never null.
NullPointerException
- If types
is null.public static final boolean isChecked(Throwable throwable)
throwable
is a checked exception, false if not.
throwable
- The throwable to test; can be null, in which case false is returned.
throwable
is a checked exception, false if not.public static final Exception toCheckedException(Throwable throwable)
throwable
.
If throwable
is already a checked exception, it will
simply be returned. Otherwise a new checked exception is
created, throwable
is set as the cause, and the
created checked exception is returned.
throwable
- The throwable; cannot be null.
NullPointerException
- If throwable
is null.public static final Exception toCheckedException(Throwable throwable, boolean unwind)
throwable
.
If throwable
is already a checked exception,
it will simply be returned. If not and if unwind
is true, a checked exception is tried unwound from the cause chain
acquired from throwable
and if found
returned. Otherwise a new checked exception is created,
throwable
is set as the cause, and the created checked
exception is returned.
throwable
- The throwable; cannot be null.unwind
- True to try and unwind a checked exception from cause
chain, false to create a new checked exception if needed
(a new instance will also be created if no checked
exception can be found in the cause chain).
NullPointerException
- If throwable
is null.public static final <T extends Exception> T toCheckedException(Throwable throwable, Class<T> type)
type
based on throwable
.
If throwable
is already a checked exception of
the desired type, it will simply be returned. Otherwise a new
checked exception of the desired type is created, throwable
is set as the cause, and the created checked exception is returned.
T
- The type of throwable.throwable
- The throwable; cannot be null.type
- The specific checked exception type as a class token;
cannot be null.
NullPointerException
- If either parameter is null.
RuntimeException
- If the current context to does have
access to create a checked exception of the desired type;
or if the desired type does not have a default constructor,
or the construction fails.public static final <T extends Exception> T toCheckedException(Throwable throwable, Class<T> type, boolean unwind)
type
based on throwable
.
If throwable
is already a checked exception of the desired type,
it will simply be returned. If not and if unwind
is true, a
checked exception of the desired type is tried unwound
from the cause chain acquired from throwable
and if found
returned. Otherwise a new checked exception of the desired type is created,
throwable
is set as the cause, and the created checked
exception is returned.
T
- The type of throwable.throwable
- The throwable; cannot be null.type
- The specific runtime exception type as a class token;
cannot be null.unwind
- True to try and unwind a checked exception of the desired
type from cause chain, false to create such a new checked exception
if needed (a new instance will also be created if no such checked
exception can be found in the cause chain).
NullPointerException
- If either parameter is null.
RuntimeException
- If the current context to does have
access to create a checked exception of the desired type;
or if the desired type does not have a default constructor,
or the construction fails.public static final RuntimeException toRuntimeException(Throwable throwable)
throwable
.
If throwable
is already a runtime exception, it will
simply be returned. Otherwise a new runtime exception is
created, throwable
is set as the cause, and the
created runtime exception is returned.
throwable
- The throwable; cannot be null.
NullPointerException
- If throwable
is null.public static final RuntimeException toRuntimeException(Throwable throwable, boolean unwind)
throwable
.
If throwable
is already a runtime exception,
it will simply be returned. If not and if unwind
is true, a runtime exception is tried unwound from the cause chain
acquired from throwable
and if found
returned. Otherwise a new runtime exception is created,
throwable
is set as the cause, and the created runtime
exception is returned.
throwable
- The throwable; cannot be null.unwind
- True to try and unwind a runtime exception from cause
chain, false to create a new runtime exception if needed
(a new instance will also be created if no runtime
exception can be found in the cause chain).
NullPointerException
- If throwable
is null.public static final <T extends RuntimeException> T toRuntimeException(Throwable throwable, Class<T> type)
type
based on throwable
.
If throwable
is already a runtime exception of
the desired type, it will simply be returned. Otherwise a new
runtime exception of the desired type is created, throwable
is set as the cause, and the created runtime exception is returned.
T
- The type of throwable.throwable
- The throwable; cannot be null.type
- The specific runtime exception type as a class token;
cannot be null.
NullPointerException
- If either parameter is null.
RuntimeException
- If the current context to does have
access to create a runtime exception of the desired type;
or if the desired type does not have a default constructor,
or the construction fails.public static final <T extends RuntimeException> T toRuntimeException(Throwable throwable, Class<T> type, boolean unwind)
type
based on throwable
.
If throwable
is already a runtime exception of the desired type,
it will simply be returned. If not and if unwind
is true, a
runtime exception of the desired type is tried unwound
from the cause chain acquired from throwable
and if found
returned. Otherwise a new runtime exception of the desired type is created,
throwable
is set as the cause, and the created runtime
exception is returned.
T
- The type of throwable.throwable
- The throwable; cannot be null.type
- The specific runtime exception type as a class token;
cannot be null.unwind
- True to try and unwind a runtime exception of the desired
type from cause chain, false to create such a new runtime exception
if needed (a new instance will also be created if no such runtime
exception can be found in the cause chain).
NullPointerException
- If either parameter is null.
RuntimeException
- If the current context to does have
access to create a runtime exception of the desired type;
or if the desired type does not have a default constructor,
or the construction fails.public static final CharSequence toStackTrace(Throwable throwable)
throwable
into
the returned char sequence.
Instead of writing to, for example, System.err
,
the same output is simply written to the returned char
sequence, including newlines.
throwable
- The throwable; cannot be null.
throwable
; never null.
NullPointerException
- If throwable
is null.public static final <T extends Throwable> T toThrowableType(Throwable throwable, Class<T> type)
type
based on throwable
.
If throwable
is already an instance of
the desired type, it will simply be returned. Otherwise a new
throwable of the desired type is created, throwable
is set as the cause, and the created throwable is returned.
This method can be used as a simple idiom for re-throwing the correct type of exception if the exception cannot be handled, without having to use several different catch-blocks to avoid hiding the actual exception type.
Consider:
public foo() throws FooException { try { .. } catch (FooException fe) { throw fe; } catch (Exception e) { throw new FooException(e); } }Using this method instead:
public foo() throws FooException { try { .. } catch (Exception e) { throw toThrowableType(e, FooException.class); } }
T
- The type of throwable.throwable
- The throwable; cannot be null.type
- The specific type as a class token; cannot be null.
NullPointerException
- If either parameter is null.
RuntimeException
- If the current context does not have
access to create a throwable exception of the desired type;
or if the desired type does not have a no-arg constructor,
or the construction fails.toThrowableType(Throwable, Class, boolean)
public static final <T extends Throwable> T toThrowableType(Throwable throwable, Class<T> type, boolean unwind)
type
based on throwable
.
If throwable
is already an instance of the desired type,
it will simply be returned. If not and if unwind
is true, a throwable of the desired type is tried unwound
from the cause chain acquired from throwable
and if found
returned. Otherwise a new throwable of the desired type is created,
throwable
is set as the cause, and the created throwable
is returned.
This method can be used as a simple idiom for re-throwing the correct type of exception if the exception cannot be handled, without having to use several different catch-blocks to avoid hiding the actual exception type.
Consider:
public foo() throws FooException { try { .. } catch (FooException fe) { throw fe; } catch (Exception e) { throw new FooException(e); } }Using this method instead:
public foo() throws FooException { try { .. } catch (Exception e) { throw toThrowableType(e, FooException.class, true); // true = try and unwind } }
T
- The type of throwable.throwable
- The throwable; cannot be null.type
- The specific type as a class token; cannot be null.unwind
- True to try and unwind a throwable of type T
from cause chain, false to create a new throwable
if needed (a new instance will also be created if
no such type can be found in the cause chain).
NullPointerException
- If throwable
or type
are null.
RuntimeException
- If a throwable must be created and the current context
does not have access to create a throwable exception of the desired type;
or if the desired type does not have a no-arg constructor,
or the construction fails.toThrowableType(Throwable, Class)
public static final <T extends Throwable> T unwind(Throwable throwable, Class<T> type)
type
in the
cause chain acquired from throwable
, if any.
The returned throwable may be throwable
it self.
T
- The type of throwable to find in the cause stack.throwable
- The throwable to supply the cause chain; cannot be null.type
- The type as a class token; cannot be null.
type
, or null
if no such throwable is found.
NullPointerException
- If either argument is null.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |