Class FallbackMethod


  • public class FallbackMethod
    extends java.lang.Object
    Reflection utility for invoking a fallback method. Fallback method should have same return type and parameter types of original method but the last additional parameter. The last additional parameter should be a subclass of Throwable. When fallback(Throwable) is invoked, Throwable will be passed to that last parameter. If there are multiple fallback method, one of the methods that has most closest superclass parameter of thrown object will be invoked.
     For example, there are two fallback methods
     
     String fallbackMethod(String parameter, RuntimeException exception)
     String fallbackMethod(String parameter, IllegalArgumentException exception)
     
     and if try to fallback from NumberFormatException, String fallbackMethod(String parameter, IllegalArgumentException exception) will be invoked.
     
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static FallbackMethod create​(java.lang.String fallbackMethodName, java.lang.reflect.Method originalMethod, java.lang.Object[] args, java.lang.Object target)  
      java.lang.Object fallback​(java.lang.Throwable thrown)
      try to fallback from Throwable
      java.lang.Class<?> getReturnType()
      get return type of fallbackMethod method
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • create

        public static FallbackMethod create​(java.lang.String fallbackMethodName,
                                            java.lang.reflect.Method originalMethod,
                                            java.lang.Object[] args,
                                            java.lang.Object target)
                                     throws java.lang.NoSuchMethodException
        Parameters:
        fallbackMethodName - the configured fallback method name
        originalMethod - the original method which has fallback method configured
        args - the original method arguments
        target - the target class that own the original method and fallback method
        Returns:
        FallbackMethod instance
        Throws:
        java.lang.NoSuchMethodException
      • fallback

        @Nullable
        public java.lang.Object fallback​(java.lang.Throwable thrown)
                                  throws java.lang.Throwable
        try to fallback from Throwable
        Parameters:
        thrown - Throwable that should be fallback
        Returns:
        fallback value
        Throws:
        java.lang.Throwable - if throwable is unrecoverable, throwable will be thrown
      • getReturnType

        public java.lang.Class<?> getReturnType()
        get return type of fallbackMethod method
        Returns:
        return type of fallbackMethod method