Package com.ultikits.ultitools.aop
Class ExceptionInterceptor
java.lang.Object
com.ultikits.ultitools.aop.ExceptionInterceptor
- All Implemented Interfaces:
MethodInterceptor
AOP interceptor that handles @ExceptionCatch annotated methods.
When a method throws an exception that matches the @ExceptionCatch configuration, this interceptor catches it and returns a default value instead of propagating.
- Since:
- 6.2.0
- Author:
- wisdomme
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<ExceptionHandler> private static final Logger -
Constructor Summary
ConstructorsConstructorDescriptionCreates an exception interceptor with no global handlers.ExceptionInterceptor(List<ExceptionHandler> globalHandlers) Creates an exception interceptor with the given global handlers. -
Method Summary
Modifier and TypeMethodDescriptionprivate ObjectgetDefaultValue(Class<?> type) Gets the default value for a type.private ObjectgetEmptyValue(Class<?> type) Gets an empty value for collection/array/string types.private ObjecthandleCaughtException(Throwable e, MethodInvocation invocation, ExceptionCatch annotation) Handles a caught exception according to the annotation configuration.invoke(MethodInvocation invocation) Intercept the given method invocation.private ObjectparseDefaultValue(String expression, Class<?> returnType) Parses a default value expression into an actual value.private booleanshouldCatch(Throwable e, Class<? extends Throwable>[] types) Checks if the given exception should be caught based on the configured types.
-
Field Details
-
LOGGER
-
globalHandlers
-
-
Constructor Details
-
ExceptionInterceptor
public ExceptionInterceptor()Creates an exception interceptor with no global handlers. -
ExceptionInterceptor
Creates an exception interceptor with the given global handlers.- Parameters:
globalHandlers- handlers to try for any exception
-
-
Method Details
-
invoke
Description copied from interface:MethodInterceptorIntercept the given method invocation.Implementations should call
MethodInvocation.proceed()to continue the interceptor chain or invoke the target method.- Specified by:
invokein interfaceMethodInterceptor- Parameters:
invocation- the method invocation context- Returns:
- the result of the method invocation
- Throws:
Throwable- if the interceptor or target method throws an exception
-
shouldCatch
Checks if the given exception should be caught based on the configured types. -
handleCaughtException
private Object handleCaughtException(Throwable e, MethodInvocation invocation, ExceptionCatch annotation) throws Throwable Handles a caught exception according to the annotation configuration.- Throws:
Throwable
-
parseDefaultValue
Parses a default value expression into an actual value. -
getDefaultValue
Gets the default value for a type. -
getEmptyValue
Gets an empty value for collection/array/string types.
-