如何在Java中获取通用接口实现的方法对象
在我的例子中,我想知道给定接口实现的方法参数是否被注释。实施方法:如何在Java中获取通用接口实现的方法对象,java,generics,annotations,guava,Java,Generics,Annotations,Guava,在我的例子中,我想知道给定接口实现的方法参数是否被注释。实施方法: boolean isNullableArgument(Class<? extends Function<?,?>> function); 如果我答对了您的问题,您需要在apply方法中对注释进行一些验证。您需要使用反射(特别是)。这将是这样的(我将只做一个草案,略去例外情况): 如果我答对了您的问题,您需要在apply方法中对注释进行一些验证。您需要使用反射(特别是)。这将是这样的(我将只做一个草案,略
boolean isNullableArgument(Class<? extends Function<?,?>> function);
如果我答对了您的问题,您需要在apply方法中对注释进行一些验证。您需要使用反射(特别是)。这将是这样的(我将只做一个草案,略去例外情况):
如果我答对了您的问题,您需要在apply方法中对注释进行一些验证。您需要使用反射(特别是)。这将是这样的(我将只做一个草案,略去例外情况):
这里有一个快速而肮脏的解决方案。不要直接复制和粘贴它——这只是一个让你开始学习的例子
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.");
}
新的LongFunction{}
将是一个函数
,但上述方法不会将泛型函数
接口定位在其运行时类型上。下面是一个快速而肮脏的解决方案。不要直接复制和粘贴它——这只是一个让你开始学习的例子
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.");
}
新长函数{}
将是一个函数
,但上述方法不会将泛型函数
接口定位在其运行时类型上。解决方案是:
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;
}
}
import com.google.common.base.Function;
导入java.lang.annotation.annotation;
导入java.lang.reflect.Method;
导入java.lang.reflect.Type;
导入java.lang.reflect.TypeVariable;
导入java.util.Map;
导入javax.annotation.Nullable;
导入org.apache.commons.lang3.reflect.TypeUtils;
公共类FunkUtils{private FunkUtils(){}
公共静态布尔isNullableArgument(类解决方案是:
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;
}
}
import com.google.common.base.Function;
导入java.lang.annotation.annotation;
导入java.lang.reflect.Method;
导入java.lang.reflect.Type;
导入java.lang.reflect.TypeVariable;
导入java.util.Map;
导入javax.annotation.Nullable;
导入org.apache.commons.lang3.reflect.TypeUtils;
公共类FunkUtils{private FunkUtils(){}
公共静态布尔isNullableArgument(ClassI)我认为OP不知道运行时的参数。getClass()
是什么。看看他们想要实现的方法:布尔isNullableArgument(Class>函数);
。主要问题是如何从“草稿”中获取参数。我认为OP不知道运行时参数.getClass()
是什么。看看他们想要实现的方法:布尔isNullableArgument(Class>函数);
。主要问题是如何从“草稿”中获取参数.我不认为你描述的可能的函数实现实际上是合法的。你试过了吗?@LouisWasserman我找到了一个解决方案。我不认为你描述的可能的函数实现实际上是合法的。你试过了吗?@LouisWasserman我找到了一个解决方案。
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;
}
}