假设我们有以下接口和实现:
interface Matcher<T>{
boolean matches(T arg);
}
class NumberMatcher<T extends Number> implements Matcher<T>{
@Override
public boolean matches(T arg){...}
}
class StringMatcher extends Matcher<String>{
@Override
public boolean matches(String arg){ ...}
}
class CustomMatcher extends NumberMatcher<Integer> {
public boolean matches(String arg){...}
@Override
public boolean matches(Integer arg){...}
}
我需要的是给定 Matcher 实现的 matches(T) 方法的参数类型。
NumberMatcher numberMatcher = new NumberMatcher<Long>();
StringMatcher stringMatcher = new StringMatcher();
CustomMatcher customMatcher = new CustomMatcher();
Matcher<Date> dateMatcher = new Matcher<Date>(){...};
getArgumentType(numberMatcher) // should return Number.class
getArgumentType(stringMatcher) // should return String.class
getArgumentType(customMatcher) // should return Integer.class
getArgumentType(dateMatcher ) // should return Object.class
这是一个除了 CustomMatcher 情况外有效的实现,因为它无法检测到覆盖的 matches(..) 方法并返回String.class
而不是Integer.class
.
Class<?> getArgumentType(Matcher<?> matcher) {
Method[] methods = matcher.getClass().getMethods();
for (Method method : methods) {
if (isMatchesMethod(method)) {
return method.getParameterTypes()[0];
}
}
throw new NoSuchMethodError("Method 'matches(T)' not found!");
}
private boolean isMatchesMethod(Method method) {
if (!isPublic(method.getModifiers()))
return false;
if (method.getParameterCount() != 1)
return false;
return method.getName().equals("matches");
}
编辑:
我正在寻找一种不需要像这样指定参数类型的解决方案:
interface Matcher<T>{
boolean matches(T arg);
Class<T> argumentType();
}