@Test public void shouldFailIf2FallBackMethodsHandleSameException() throws Throwable { FallbackMethodTest target = new FallbackMethodTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); assertThatThrownBy(() -> FallbackMethod .create("returnMismatchFallback", testMethod, new Object[]{"test"}, target)) .isInstanceOf(NoSuchMethodException.class) .hasMessage( "class java.lang.String class io.github.resilience4j.fallback.FallbackMethodTest.returnMismatchFallback(class java.lang.String,class java.lang.Throwable)"); }
public static FallbackMethod create(String fallbackMethodName, Method originalMethod, Object[] args, Object target) throws NoSuchMethodException { MethodMeta methodMeta = new MethodMeta( fallbackMethodName, originalMethod.getParameterTypes(), originalMethod.getReturnType(), target.getClass()); Map<Class<?>, Method> methods = FALLBACK_METHODS_CACHE .computeIfAbsent(methodMeta, FallbackMethod::extractMethods); if (!methods.isEmpty()) { return new FallbackMethod(methods, originalMethod.getReturnType(), args, target); } else { throw new NoSuchMethodException(String.format("%s %s.%s(%s,%s)", methodMeta.returnType, methodMeta.targetClass, methodMeta.fallbackMethodName, StringUtils.arrayToDelimitedString(methodMeta.params, ","), Throwable.class)); } }
FallbackMethod { public static FallbackMethod create(String fallbackMethodName, Method originalMethod, Object[] args, Object target) throws NoSuchMethodException { MethodMeta methodMeta = new MethodMeta( fallbackMethodName, originalMethod.getParameterTypes(), originalMethod.getReturnType(), target.getClass()); Map<Class<?>, Method> methods = FALLBACK_METHODS_CACHE .computeIfAbsent(methodMeta, FallbackMethod::extractMethods); if (!methods.isEmpty()) { return new FallbackMethod(methods, originalMethod.getReturnType(), args, target); } else { throw new NoSuchMethodException(String.format("%s %s.%s(%s,%s)", methodMeta.returnType, methodMeta.targetClass, methodMeta.fallbackMethodName, StringUtils.arrayToDelimitedString(methodMeta.params, ","), Throwable.class)); } } }
FallbackMethod { public static FallbackMethod create(String fallbackMethodName, Method originalMethod, Object[] args, Object target) throws NoSuchMethodException { MethodMeta methodMeta = new MethodMeta( fallbackMethodName, originalMethod.getParameterTypes(), originalMethod.getReturnType(), target.getClass()); Map<Class<?>, Method> methods = FALLBACK_METHODS_CACHE .computeIfAbsent(methodMeta, FallbackMethod::extractMethods); if (!methods.isEmpty()) { return new FallbackMethod(methods, originalMethod.getReturnType(), args, target); } else { throw new NoSuchMethodException(String.format("%s %s.%s(%s,%s)", methodMeta.returnType, methodMeta.targetClass, methodMeta.fallbackMethodName, StringUtils.arrayToDelimitedString(methodMeta.params, ","), Throwable.class)); } } private  FallbackMethod(Map<Class<?>, Method> fallbackMethods, Class<?> originalMethodReturnType,
        Object[] args, Object target); }
FallbackMethod { public static FallbackMethod create(String fallbackMethodName, Method originalMethod, Object[] args, Object target) throws NoSuchMethodException { MethodMeta methodMeta = new MethodMeta( fallbackMethodName, originalMethod.getParameterTypes(), originalMethod.getReturnType(), target.getClass()); Map<Class<?>, Method> methods = FALLBACK_METHODS_CACHE .computeIfAbsent(methodMeta, FallbackMethod::extractMethods); if (!methods.isEmpty()) { return new FallbackMethod(methods, originalMethod.getReturnType(), args, target); } else { throw new NoSuchMethodException(String.format("%s %s.%s(%s,%s)", methodMeta.returnType, methodMeta.targetClass, methodMeta.fallbackMethodName, StringUtils.arrayToDelimitedString(methodMeta.params, ","), Throwable.class)); } } private  FallbackMethod(Map<Class<?>, Method> fallbackMethods, Class<?> originalMethodReturnType,
        Object[] args, Object target); static FallbackMethod create(String fallbackMethodName, Method originalMethod,
        Object[] args, Object target); @Nullable Object fallback(Throwable thrown); Class<?> getReturnType(); }
FallbackMethod { public static FallbackMethod create(String fallbackMethodName, Method originalMethod, Object[] args, Object target) throws NoSuchMethodException { MethodMeta methodMeta = new MethodMeta( fallbackMethodName, originalMethod.getParameterTypes(), originalMethod.getReturnType(), target.getClass()); Map<Class<?>, Method> methods = FALLBACK_METHODS_CACHE .computeIfAbsent(methodMeta, FallbackMethod::extractMethods); if (!methods.isEmpty()) { return new FallbackMethod(methods, originalMethod.getReturnType(), args, target); } else { throw new NoSuchMethodException(String.format("%s %s.%s(%s,%s)", methodMeta.returnType, methodMeta.targetClass, methodMeta.fallbackMethodName, StringUtils.arrayToDelimitedString(methodMeta.params, ","), Throwable.class)); } } private  FallbackMethod(Map<Class<?>, Method> fallbackMethods, Class<?> originalMethodReturnType,
        Object[] args, Object target); static FallbackMethod create(String fallbackMethodName, Method originalMethod,
        Object[] args, Object target); @Nullable Object fallback(Throwable thrown); Class<?> getReturnType();  }