3

我有一个方法对象。

我想Type用泛型提取 return 并将其转换为 aClass以便将此类信息传递给 Spring PropertyResolver

Type type = myMethod.getGenericReturnType();
Class<?> returnType = /* ??? */;
environment.getProperty(key, returnType);
4

2 回答 2

5

在实践中,返回Type实例必须是以下之一:Class(Eg String)、GenericArrayType(Eg String[]or T[]or List<T>[])、TypeVariable(Eg T) 或ParametrizedType(Eg List<String>or List<T>)。另外Type也可以是WildcardType(例如?in List<?>),但这些不能直接用作返回类型。

下面的代码尝试根据这 5 个中的子接口解析给定一个实例的类。很少 aType不会扩展 5 中的任何一个,在这种情况下,我们只是说我们不能继续使用 a UnsupportedOperationException。例如,您可以创建自己的合成Type扩展类,但为什么要这样做呢?

public static Class<?> type2Class(Type type) {
    if (type instanceof Class) {
       return (Class<?>) type;
    } else if (type instanceof GenericArrayType) {
       // having to create an array instance to get the class is kinda nasty 
       // but apparently this is a current limitation of java-reflection concerning array classes.
       return Array.newInstance(type2Class(((GenericArrayType)type).getGenericComponentType()), 0).getClass(); // E.g. T[] -> T -> Object.class if <T> or Number.class if <T extends Number & Comparable>
    } else if (type instanceof ParameterizedType) {
       return type2Class(((ParameterizedType) type).getRawType()); // Eg. List<T> would return List.class
    } else if (type instanceof TypeVariable) {
       Type[] bounds = ((TypeVariable<?>) type).getBounds();
       return bounds.length == 0 ? Object.class : type2Class(bounds[0]); // erasure is to the left-most bound.
    } else if (type instanceof WildcardType) {
       Type[] bounds = ((WildcardType) type).getUpperBounds();
       return bounds.length == 0 ? Object.class : type2Class(bounds[0]); // erasure is to the left-most upper bound.
    } else { 
       throw new UnsupportedOperationException("cannot handle type class: " + type.getClass());
    }
} 

请注意,代码未经测试,因此可能包含编译错误。另外我不确定GenericArrayType多维数组类型的行为如何T[][](也许它会返回Object[]而不是Object[][]如果<T>我们需要在这里做额外的工作)。请让我知道是否需要任何更正。

最后,我们在这里尝试做的是计算 Erasure 类,Type我想知道是否有一些“标准”代码可以做到这一点,也许是 Sun/Oracle 编译器或代码分析器工具的一部分,你可以使用他们的实用程序并为自己省去编码和维护它的麻烦......我没有通过快速浏览找到任何东西。

于 2018-01-02T18:31:17.067 回答
1

您可以使用解决方法将java.lang.reflect.Type返回的Method.getGenericReturnType()转换Class为泛型的。

字符串解析:

final String typeName = method.getGenericReturnType().getTypeName();
Pattern pattern = Pattern.compile("<(.*)>");
final Matcher matcher = pattern.matcher(typeName);
if (matcher.find()) {
    String className = matcher.group(1);
    Class<?> clazz = Class.forName(className);        
}

您也可以向下转换java.lang.reflect.Type为运行时使用的具体类:sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl但我想它可能会根据 JVM 发生变化。

final Type genericReturnType = method.getGenericReturnType();
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl typeImpl = (ParameterizedTypeImpl) genericReturnType;
String className = typeImpl.getActualTypeArguments()[0].getTypeName();
Class<?> clazz = Class.forName(className);
于 2018-01-02T10:11:56.683 回答