Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iss1177: change getMethod to recursively search in parent types #1258

Merged
merged 1 commit into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{ }

}