在我的情况下,我想知道apply
Guava接口的给定实现的Function
方法参数是否带有注释@Nullable
。要实施的方法:
boolean isNullableArgument(Class<? extends Function<?,?>> function);
我不知道如何从apply
类中实现function
方法。
E.g。可能有Function
这样的实现:
new Function<String,Integer>() {
public Integer apply(String input) { … }
public Integer apply(Integer input) { … }
}
答案 0 :(得分:2)
这是一个快速而肮脏的解决方案。不要直接复制和粘贴它 - 它只是一个让你入门的例子。
static boolean applyHasAnnotation(
@SuppressWarnings("rawtypes") final Class<? extends Function> functionType,
final Class<? extends Annotation> annotationType
) throws SecurityException, NoSuchMethodException {
//for each directly implemented interface,
for (final Type interfaceType : functionType.getGenericInterfaces()) {
//if the interface is parameterized,
if (interfaceType instanceof ParameterizedType) {
final ParameterizedType genericInterfaceType = (ParameterizedType)interfaceType;
//if the interface is Function
if (genericInterfaceType.getRawType() == Function.class) {
//get the type argument for T
final Type inputType = genericInterfaceType.getActualTypeArguments()[0];
//get its raw type
final Class<?> rawInputType =
(inputType instanceof ParameterizedType)
? (Class<?>)((ParameterizedType)inputType).getRawType()
: (Class<?>)inputType;
//use it to find the apply implementation
final Method applyMethod = functionType.getDeclaredMethod("apply", rawInputType);
//for each annotation on its first (and only) parameter,
for (final Annotation inputAnnotation : applyMethod.getParameterAnnotations()[0]) {
//if its type is the specified annotation type, return true
if (inputAnnotation.annotationType() == annotationType) {
return true;
}
}
return false;
}
}
}
//a more complicated inheritance hierarchy has defeated us
throw new IllegalArgumentException("Function info not found.");
}
实际上,您希望单独编写各种问题的代码:
F
类型参数apply
实施正如代码中所指出的,这个解决方案很容易打破更复杂的类型层次结构,例如:
abstract class LongFunction<T> implements Function<Long, T> { }
new LongFunction<String> { }
将是Function<Long, String>
,但上述方法不会将通用Function
接口放在其运行时类型上。
答案 1 :(得分:2)
解决方案是:
import com.google.common.base.Function;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.commons.lang3.reflect.TypeUtils;
public class FunkUtils { private FunkUtils() {}
public static boolean isNullableArgument(Class<? extends Function> functionClass) throws Exception {
Map<TypeVariable<?>,Type> typeArgs = TypeUtils.getTypeArguments(functionClass, Function.class);
TypeVariable<?> argTypeParam = Function.class.getTypeParameters()[0];
Type argType = typeArgs.get(argTypeParam);
Class argClass = TypeUtils.getRawType(argType, null);
Method applyMethod = functionClass.getDeclaredMethod("apply", argClass);
Annotation[] argAnnos = applyMethod.getParameterAnnotations()[0];
for (int i = 0; i < argAnnos.length; i++) {
if (argAnnos[i] instanceof Nullable) return true;
}
return false;
}
}
commons-lang3 3.1版中的TypeUtils.getTypeArguments中存在一个错误,但它已在3.2中修复,现在正在开发中。
答案 2 :(得分:1)
如果我对你提出了问题,你想在apply方法中对注释进行一些验证。您需要使用反射this case(特别是this method)。这将是类似的事情(我将只草拟省略例外):
this.getClass().getMethod("apply", parameter.getClass()).getParameterAnnotations()