Skip to content

Commit

Permalink
Merge pull request Netflix#1258 from dmgcodevil/iss1177
Browse files Browse the repository at this point in the history
iss1177: change getMethod to recursively search in parent types
  • Loading branch information
mattrjacobs authored Jun 27, 2016
2 parents a3dc47c + 2ea954c commit cf16c01
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Method> 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();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}

}
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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> 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{ }

}

0 comments on commit cf16c01

Please sign in to comment.