Class ExceptionInterceptor

java.lang.Object
com.ultikits.ultitools.aop.ExceptionInterceptor
All Implemented Interfaces:
MethodInterceptor

public class ExceptionInterceptor extends Object implements 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 Details

  • Constructor Details

    • ExceptionInterceptor

      public ExceptionInterceptor()
      Creates an exception interceptor with no global handlers.
    • ExceptionInterceptor

      public ExceptionInterceptor(List<ExceptionHandler> globalHandlers)
      Creates an exception interceptor with the given global handlers.
      Parameters:
      globalHandlers - handlers to try for any exception
  • Method Details

    • invoke

      public Object invoke(MethodInvocation invocation) throws Throwable
      Description copied from interface: MethodInterceptor
      Intercept the given method invocation.

      Implementations should call MethodInvocation.proceed() to continue the interceptor chain or invoke the target method.

      Specified by:
      invoke in interface MethodInterceptor
      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

      private boolean shouldCatch(Throwable e, Class<? extends Throwable>[] types)
      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

      private Object parseDefaultValue(String expression, Class<?> returnType)
      Parses a default value expression into an actual value.
    • getDefaultValue

      private Object getDefaultValue(Class<?> type)
      Gets the default value for a type.
    • getEmptyValue

      private Object getEmptyValue(Class<?> type)
      Gets an empty value for collection/array/string types.