为什么对于泛型类型参数的注释对于嵌套类型不可见? [英] Why annotation on generic type argument is not visible for nested type?
问题描述
我没有得到以下代码的行为:
https://gist.github .com / tomaszalusky / 3e3777b4fd0c6096f3f707bb19b50b52 - 查看嵌入:
import java.lang.reflect。*;
import java.util。*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class AnnotationOnTypeArgument {
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD,ElementType.TYPE_USE})
@保留(RetentionPolicy.RUNTIME)
public @interface Anno {
}
接口嵌套< T> {
}
Toplevel< @Anno Integer>顶层;
嵌套< @Anno Integer>嵌套;
$ b $ public static void main(String [] args)throws Exception {
print(AnnotationOnTypeArgument.class.getDeclaredField(toplevel));
print(AnnotationOnTypeArgument.class.getDeclaredField(nested));
}
private static void print(Field field){
AnnotatedType annotatedType = field.getAnnotatedType();
AnnotatedParameterizedType annotatedParameterizedType =(AnnotatedParameterizedType)annotatedType;
ParameterizedType parameterizedType =(ParameterizedType)annotatedParameterizedType.getType();
AnnotatedType argType = annotatedParameterizedType.getAnnotatedActualTypeArguments()[0];
System.out.printf(field%s%ntype =%s%nannotatedType =%s%nannotations =%s%ntype =%s%n%n,
field.getName(),参数化类型,argType,Arrays.asList(argType.getDeclaredAnnotations()),argType.getType());
}
}
interface Toplevel< T> {b
$ b}
编辑:实际结果是:
字段toplevel
type = Toplevel< java.lang.Integer>
annotatedType=sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1540e19d
annotations = [@ AnnotationOnTypeArgument $ Anno()]
type = class java.lang.Integer
字段嵌套
type = AnnotationOnTypeArgument.AnnotationOnTypeArgument $嵌套< java.lang.Integer>
annotatedType=sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@677327b6
annotations = []
type = class java.lang.Integer
为什么当周围类型嵌套时,类型参数上的声明注释数组是空的?我希望有一个元素,就像顶级类型一样。我会很感激任何基于JLS的解释。
在JDK8u101( http:
谢谢!
我给了一些时间去调试,并且annotatedParameterizedType变量似乎仍然在其allOnSameTargetTypeAnnotations字段中也包含对Anno注释的引用,以用于嵌套的情况。
annotatedParameterizedType = {AnnotatedTypeFactory $ AnnotatedParameterizedTypeImpl @ 539}
type = {ParameterizedTypeImpl @ 540}com.sample.Toplevel< java.lang.Integer>
decl = {Field @ 538}com.sample.Toplevel com.sample.AnnotationOnTypeArgument.toplevel
location = {TypeAnnotation $ LocationInfo @ 543}
depth = 0
locations = {TypeAnnotation $ LocationInfo $ Location [0] @ 549}
allOnSameTargetTypeAnnotations = {TypeAnnotation [1] @ 544}
0 = {TypeAnnotation @ 551}@ com.sample.AnnotationOnTypeArgument $ Anno()with Targetnfo:FIELD:-2,-2表示基本声明:com.sample.Toplevel com.sample.AnnotationOnTypeArgument.toplevel
annotations = {LinkedHashMap @ 546} size = 0
vs
annotatedParameterizedType = {AnnotatedTypeFactory $ AnnotatedParameterizedTypeImpl @ 602}
type = {ParameterizedTypeImpl @ 603}com.sample.AnnotationOnTypeArgument.com.sample.AnnotationOnTypeArgument $嵌套< java.lang.Integer>
decl = {Field @ 601}com.sample.AnnotationOnTypeArgument $ Nested com.sample.AnnotationOnTypeArgument.nested
location = {TypeAnnotation $ LocationInfo @ 606}
depth = 1
位置= {TypeAnnotation $ LocationInfo $ Location [1] @ 611}
allOnSameTargetTypeAnnotations = {TypeAnnotation [1] @ 607}
0 = {TypeAnnotation @ 612}@ com.sample.AnnotationOnTypeArgument $ Anno )与Targetnfo:FIELD:-2,-2在基本声明:com.sample.AnnotationOnTypeArgument $嵌套com.sample.AnnotationOnTypeArgument.nested
annotations = {LinkedHashMap @ 608} size = 0
code>
然而,位置深度有所不同,即将到来的AnnotatedTypeFactory的getAnnotatedActualTypeArguments()方法包含TypeAnnotation.isSameLocationInfo()一个将元素添加到注释映射的前提条件,它将在嵌套的情况下变为false,因此最终不会添加元素。
我没有找到任何文档它也是。也许您在这里发现了一个问题
I don't get the behaviour of following code: https://gist.github.com/tomaszalusky/3e3777b4fd0c6096f3f707bb19b50b52 - see embedded:
import java.lang.reflect.*;
import java.util.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class AnnotationOnTypeArgument {
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD,ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Anno {
}
interface Nested<T> {
}
Toplevel<@Anno Integer> toplevel;
Nested<@Anno Integer> nested;
public static void main(String[] args) throws Exception {
print(AnnotationOnTypeArgument.class.getDeclaredField("toplevel"));
print(AnnotationOnTypeArgument.class.getDeclaredField("nested"));
}
private static void print(Field field) {
AnnotatedType annotatedType = field.getAnnotatedType();
AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType)annotatedType;
ParameterizedType parameterizedType = (ParameterizedType)annotatedParameterizedType.getType();
AnnotatedType argType = annotatedParameterizedType.getAnnotatedActualTypeArguments()[0];
System.out.printf("field %s%ntype=%s%nannotatedType=%s%nannotations=%s%ntype=%s%n%n",
field.getName(), parameterizedType, argType, Arrays.asList(argType.getDeclaredAnnotations()), argType.getType());
}
}
interface Toplevel<T> {
}
EDIT: the actual result is:
field toplevel
type=Toplevel<java.lang.Integer>
annotatedType=sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1540e19d
annotations=[@AnnotationOnTypeArgument$Anno()]
type=class java.lang.Integer
field nested
type=AnnotationOnTypeArgument.AnnotationOnTypeArgument$Nested<java.lang.Integer>
annotatedType=sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@677327b6
annotations=[]
type=class java.lang.Integer
Why the array of declared annotations on type argument is empty when surrounding type is nested? I expect to have one element just as for top level type. I would appreciate any explanation based on JLS.
Happens consistently on JDK8u101 (http://compilejava.net), older JDK8 and Eclipse.
Thanks!
I gave some time to debugging and the annotatedParameterizedType variable seems to still contain the reference to the Anno annotation in its allOnSameTargetTypeAnnotations field for the nested case too
annotatedParameterizedType = {AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@539}
type = {ParameterizedTypeImpl@540} "com.sample.Toplevel<java.lang.Integer>"
decl = {Field@538} "com.sample.Toplevel com.sample.AnnotationOnTypeArgument.toplevel"
location = {TypeAnnotation$LocationInfo@543}
depth = 0
locations = {TypeAnnotation$LocationInfo$Location[0]@549}
allOnSameTargetTypeAnnotations = {TypeAnnotation[1]@544}
0 = {TypeAnnotation@551} "@com.sample.AnnotationOnTypeArgument$Anno() with Targetnfo: FIELD: -2, -2 on base declaration: com.sample.Toplevel com.sample.AnnotationOnTypeArgument.toplevel"
annotations = {LinkedHashMap@546} size = 0
vs
annotatedParameterizedType = {AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@602}
type = {ParameterizedTypeImpl@603} "com.sample.AnnotationOnTypeArgument.com.sample.AnnotationOnTypeArgument$Nested<java.lang.Integer>"
decl = {Field@601} "com.sample.AnnotationOnTypeArgument$Nested com.sample.AnnotationOnTypeArgument.nested"
location = {TypeAnnotation$LocationInfo@606}
depth = 1
locations = {TypeAnnotation$LocationInfo$Location[1]@611}
allOnSameTargetTypeAnnotations = {TypeAnnotation[1]@607}
0 = {TypeAnnotation@612} "@com.sample.AnnotationOnTypeArgument$Anno() with Targetnfo: FIELD: -2, -2 on base declaration: com.sample.AnnotationOnTypeArgument$Nested com.sample.AnnotationOnTypeArgument.nested"
annotations = {LinkedHashMap@608} size = 0
However there is a difference in the location depth and the upcoming getAnnotatedActualTypeArguments() method of AnnotatedTypeFactory contains a TypeAnnotation.isSameLocationInfo() comparison as a precondition to add elements to the annotations map, which will become false in the nested case, thus eventually no element is being added
I did not find no documentation on it either. Perhaps you have found an issue here
这篇关于为什么对于泛型类型参数的注释对于嵌套类型不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!