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

It seems like getAnnotatedParameterTypes() returns an array of AnnotatedTypes holding raw, rather than generic, types. For example:

public 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());

}

What's the reason for the disagreement with getGenericParameterTypes()?

解决方案

There's a bug report about this, and it has since been fixed.

There's a difference between Method#getGenericParameterTypes() and Method#getAnnotatedParameterTypes().

The former makes guarantees about the types it returns

If a formal parameter type is a parameterized type, the Type object

returned for it must accurately reflect the actual type parameters

used in the source code.

If a formal parameter type is a type variable or a parameterized type,

it is created. Otherwise, it is resolved.

while the latter doesn't, not clearly at least:

Returns an array of AnnotatedType objects that represent the use of

types to specify formal parameter types of the method/constructor

represented by this Executable.

We have to assume that getAnnotatedParameterTypes() returns the erased types (although it may not have been intended that way). The unbounded type variable T is erased to Object. If you had , it would be erased to Foo.

As for the comments, about getting the annotation from a type argument in a method parameter, there's no way given the above. One would think it works as it does for fields.

public static void main(String[] args) throws Exception {

Field field = Example.class.getField("field");

AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType) field

.getAnnotatedType();

System.out.println(annotatedParameterizedType

.getAnnotatedActualTypeArguments()[0].getType());

System.out.println(Arrays.toString(annotatedParameterizedType

.getAnnotatedActualTypeArguments()[0].getAnnotations()));

}

@Retention(RetentionPolicy.RUNTIME)

@Target(value = { ElementType.TYPE_USE })

@interface Bar {

}

public List field;

which prints

class java.lang.String

[@com.example.Example$Bar()]

I very much think it's a bug that needs fixing and will be following the bug report linked above.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值