Class ThrowableAnalyzer
- java.lang.Object
-
- org.springframework.security.web.util.ThrowableAnalyzer
-
public class ThrowableAnalyzer extends java.lang.ObjectHandler for analyzingThrowableinstances. Can be subclassed to customize its behavior.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description static ThrowableCauseExtractorDEFAULT_EXTRACTORDefault extractor forThrowableinstances.static ThrowableCauseExtractorINVOCATIONTARGET_EXTRACTORDefault extractor forInvocationTargetExceptioninstances.
-
Constructor Summary
Constructors Constructor Description ThrowableAnalyzer()Creates a newThrowableAnalyzerinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Throwable[]determineCauseChain(java.lang.Throwable throwable)Determines the cause chain of the providedThrowable.java.lang.ThrowablegetFirstThrowableOfType(java.lang.Class<? extends java.lang.Throwable> throwableType, java.lang.Throwable[] chain)Returns the first throwable from the passed in array that is assignable to the provided type.protected voidinitExtractorMap()Initializes associations betweenThrowables andThrowableCauseExtractors.protected voidregisterExtractor(java.lang.Class<? extends java.lang.Throwable> throwableType, ThrowableCauseExtractor extractor)Registers aThrowableCauseExtractorfor the specified type.static voidverifyThrowableHierarchy(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> expectedBaseType)Verifies that the provided throwable is a valid subclass of the provided type (or of the type itself).
-
-
-
Field Detail
-
DEFAULT_EXTRACTOR
public static final ThrowableCauseExtractor DEFAULT_EXTRACTOR
Default extractor forThrowableinstances.- See Also:
Throwable.getCause()
-
INVOCATIONTARGET_EXTRACTOR
public static final ThrowableCauseExtractor INVOCATIONTARGET_EXTRACTOR
Default extractor forInvocationTargetExceptioninstances.- See Also:
InvocationTargetException.getTargetException()
-
-
Method Detail
-
registerExtractor
protected final void registerExtractor(java.lang.Class<? extends java.lang.Throwable> throwableType, ThrowableCauseExtractor extractor)Registers aThrowableCauseExtractorfor the specified type. Can be used in subclasses overridinginitExtractorMap().- Parameters:
throwableType- the type (has to be a subclass ofThrowable)extractor- the associatedThrowableCauseExtractor(notnull)- Throws:
java.lang.IllegalArgumentException- if one of the arguments is invalid
-
initExtractorMap
protected void initExtractorMap()
Initializes associations betweenThrowables andThrowableCauseExtractors. The default implementation performs the following registrations:DEFAULT_EXTRACTORforThrowableINVOCATIONTARGET_EXTRACTORforInvocationTargetException
Subclasses overriding this method are encouraged to invoke the super method to perform the default registrations. They can register additional extractors as required.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_EXTRACTORwhile all other throwables are handled byDEFAULT_EXTRACTOR.
-
determineCauseChain
public final java.lang.Throwable[] determineCauseChain(java.lang.Throwable throwable)
Determines the cause chain of the providedThrowable. The returned array contains all throwables extracted from the stacktrace, using the registeredextractors. 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
ThrowableCauseExtractoris registered for this instance then the returned array will always only contain the passed in throwable.- Parameters:
throwable- theThrowableto analyze- Returns:
- an array of all determined throwables from the stacktrace
- Throws:
java.lang.IllegalArgumentException- if the throwable isnull- See Also:
initExtractorMap()
-
getFirstThrowableOfType
public final java.lang.Throwable getFirstThrowableOfType(java.lang.Class<? extends java.lang.Throwable> throwableType, java.lang.Throwable[] chain)Returns the first throwable from the passed in array that is assignable to the provided type. A returned instance is safe to be cast to the specified type.If the passed in array is null or empty this method returns
null.- Parameters:
throwableType- the type to look forchain- the array (will be processed in element order)- Returns:
- the found
Throwable,nullif not found - Throws:
java.lang.IllegalArgumentException- if the provided type isnullor no subclass ofThrowable
-
verifyThrowableHierarchy
public static void verifyThrowableHierarchy(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> expectedBaseType)Verifies that the provided throwable is a valid subclass of the provided type (or of the type itself). IfexpectdBaseTypeisnull, no check will be performed.Can be used for verification purposes in implementations of
extractors.- Parameters:
throwable- theThrowableto checkexpectedBaseType- the type to check against- Throws:
java.lang.IllegalArgumentException- ifthrowableis eithernullor its type is not assignable toexpectedBaseType
-
-