diff --git a/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/utils/MethodProvider.java b/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/utils/MethodProvider.java index 8ba2c34b4..0295793ca 100644 --- a/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/utils/MethodProvider.java +++ b/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/utils/MethodProvider.java @@ -87,10 +87,26 @@ public FallbackMethod getFallbackMethod(Class type, Method commandMethod, boo return FallbackMethod.ABSENT; } + /** + * Gets method by name and parameters types using reflection, + * if the given type doesn't contain required method then continue applying this method for all super classes up to Object class. + * + * @param type the type to search method + * @param name the method name + * @param parameterTypes the parameters types + * @return Some if method exists otherwise None + */ public Optional getMethod(Class type, String name, Class... parameterTypes) { - try { - return Optional.of(type.getDeclaredMethod(name, parameterTypes)); - } catch (NoSuchMethodException e) { + Method[] methods = type.getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().equals(name) && Arrays.equals(method.getParameterTypes(), parameterTypes)) { + return Optional.of(method); + } + } + Class superClass = type.getSuperclass(); + if (superClass != null && !superClass.equals(Object.class)) { + return getMethod(superClass, name, parameterTypes); + } else { return Optional.absent(); } } diff --git a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/fallback/InheritedFallbackTest.java b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/fallback/InheritedFallbackTest.java new file mode 100644 index 000000000..62b05e57f --- /dev/null +++ b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/fallback/InheritedFallbackTest.java @@ -0,0 +1,38 @@ +package com.netflix.hystrix.contrib.javanica.test.spring.fallback; + +import com.netflix.hystrix.contrib.javanica.test.common.fallback.BasicCommandFallbackTest; +import com.netflix.hystrix.contrib.javanica.test.spring.conf.AopCglibConfig; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Configurable; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Created by dmgcodevil. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {AopCglibConfig.class, InheritedFallbackTest.CommandTestConfig.class}) +public class InheritedFallbackTest extends BasicCommandFallbackTest { + + @Autowired + private UserService userService; + + @Override + protected BasicCommandFallbackTest.UserService createUserService() { + return userService; + } + + @Configurable + public static class CommandTestConfig { + @Bean + public UserService userService() { + return new SubClass(); + } + } + + public static class SubClass extends BasicCommandFallbackTest.UserService { + } + +} \ No newline at end of file diff --git a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/util/GetMethodTest.java b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/util/GetMethodTest.java new file mode 100644 index 000000000..56dc8d3e0 --- /dev/null +++ b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/util/GetMethodTest.java @@ -0,0 +1,51 @@ +package com.netflix.hystrix.contrib.javanica.util; + +import com.google.common.base.Optional; +import com.netflix.hystrix.contrib.javanica.utils.MethodProvider; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by dmgcodevil. + */ +@RunWith(Parameterized.class) +public class GetMethodTest { + + private String methodName; + private Class[] parametersTypes; + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][] { + { "foo", new Class[]{ String.class } }, + { "bar", new Class[]{ Integer.class } } + }); + } + + public GetMethodTest(String methodName, Class[] parametersTypes) { + this.methodName = methodName; + this.parametersTypes = parametersTypes; + } + + @Test + public void testGetMethodFoo(){ + Optional method = MethodProvider.getInstance().getMethod(C.class, methodName, parametersTypes); + + assertTrue(method.isPresent()); + assertEquals(methodName, method.get().getName()); + } + + + public static class A { void foo(String in) {} } + public static class B extends A { void bar(Integer in) {} } + public static class C extends B{ } + +} \ No newline at end of file