Modifier and Type | Field and Description |
---|---|
static ThrowableCauseExtractor |
DEFAULT_EXTRACTOR
Default extractor for
Throwable instances. |
static ThrowableCauseExtractor |
INVOCATIONTARGET_EXTRACTOR
Default extractor for
InvocationTargetException instances. |
Constructor and Description |
---|
ThrowableAnalyzer()
Creates a new
ThrowableAnalyzer instance. |
Modifier and Type | Method and Description |
---|---|
Throwable[] |
determineCauseChain(Throwable throwable)
Determines the cause chain of the provided
Throwable . |
Throwable |
getFirstThrowableOfType(Class<? extends Throwable> throwableType,
Throwable[] chain)
Returns the first throwable from the passed in array that is assignable to the provided type.
|
protected void |
initExtractorMap()
Initializes associations between
Throwable s and ThrowableCauseExtractor s. |
protected void |
registerExtractor(Class<? extends Throwable> throwableType,
ThrowableCauseExtractor extractor)
Registers a
ThrowableCauseExtractor for the specified type. |
static void |
verifyThrowableHierarchy(Throwable throwable,
Class<? extends Throwable> expectedBaseType)
Verifies that the provided throwable is a valid subclass of the provided type (or of the type itself).
|
public static final ThrowableCauseExtractor DEFAULT_EXTRACTOR
Throwable
instances.Throwable.getCause()
public static final ThrowableCauseExtractor INVOCATIONTARGET_EXTRACTOR
InvocationTargetException
instances.public ThrowableAnalyzer()
ThrowableAnalyzer
instance.protected final void registerExtractor(Class<? extends Throwable> throwableType, ThrowableCauseExtractor extractor)
ThrowableCauseExtractor
for the specified type.
Can be used in subclasses overriding initExtractorMap()
.throwableType
- the type (has to be a subclass of Throwable
)extractor
- the associated ThrowableCauseExtractor
(not null
)IllegalArgumentException
- if one of the arguments is invalidprotected void initExtractorMap()
Throwable
s and ThrowableCauseExtractor
s.
The default implementation performs the following registrations:
DEFAULT_EXTRACTOR
for Throwable
INVOCATIONTARGET_EXTRACTOR
for InvocationTargetException
Note: An extractor registered for a specific type is applicable for that type and all subtypes thereof.
However, extractors registered to more specific types are guaranteed to be resolved first.
So in the default case InvocationTargetExceptions will be handled by INVOCATIONTARGET_EXTRACTOR
while all other throwables are handled by DEFAULT_EXTRACTOR
.
public final Throwable[] determineCauseChain(Throwable throwable)
Throwable
.
The returned array contains all throwables extracted from the stacktrace, using the registered
extractors
. The elements of the array are ordered:
The first element is the passed in throwable itself. The following elements
appear in their order downward the stacktrace.
Note: If no ThrowableCauseExtractor
is registered for this instance
then the returned array will always only contain the passed in throwable.
throwable
- the Throwable
to analyzeIllegalArgumentException
- if the throwable is null
initExtractorMap()
public final Throwable getFirstThrowableOfType(Class<? extends Throwable> throwableType, Throwable[] chain)
If the passed in array is null or empty this method returns null
.
throwableType
- the type to look forchain
- the array (will be processed in element order)Throwable
, null
if not foundIllegalArgumentException
- if the provided type is null
or no subclass of Throwable
public static void verifyThrowableHierarchy(Throwable throwable, Class<? extends Throwable> expectedBaseType)
expectdBaseType
is null
, no check will be performed.
Can be used for verification purposes in implementations
of extractors
.
throwable
- the Throwable
to checkexpectedBaseType
- the type to check againstIllegalArgumentException
- if throwable
is either null
or its type is not assignable to expectedBaseType