相关疑难解决方法(0)

如何从Java 8中的getAnnotatedParameterTypes()获取泛型类型信息?

它似乎getAnnotatedParameterTypes()返回一个包含AnnotatedType原始类型而不是泛型类型的s 数组.例如:

public <T> void genericMethod(T t) {
}

@Test
public void testAnnotatedTypes() throws ReflectiveOperationException {
    Method method = getClass().getMethod("genericMethod", Object.class);

    Type type = method.getGenericParameterTypes()[0];
    assertTrue(type instanceof TypeVariable);

    AnnotatedType annotatedType = method.getAnnotatedParameterTypes()[0];

    // This fails; annotatedType implements only AnnotatedType
    assertTrue(annotatedType instanceof AnnotatedTypeVariable);

    // This fails too; type is a TypeVariable while annotatedType.getType() is
    // Object.class
    assertEquals(type, annotatedType.getType());
}
Run Code Online (Sandbox Code Playgroud)

分歧的原因是什么getGenericParameterTypes()?

java generics reflection java-8 type-annotation

8
推荐指数
1
解决办法
949
查看次数

标签 统计

generics ×1

java ×1

java-8 ×1

reflection ×1

type-annotation ×1