Are annotations on a type parameter accessible in runtime?

时光毁灭记忆、已成空白 提交于 2021-02-04 14:10:42

问题


Java 8 allows things like:

public List<@NonNull String> names;

But is there a way to access this annotation in runtime or is it available only to compiler plugins?

There's new Method#getAnnotatedReturnType that provides access to annotations on the return type, so I was hoping ParameterizedType would now have something like getActualAnnotatedTypeArguments that would do the same for generic type arguments, but it doesn't exist...


回答1:


The new API continues the tradition of requiring lots of instanceofs and type casts:

import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.stream.*;

public class AnnoTest {
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE_USE)
    @interface NonNull {}

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE_USE)
    @interface NonEmpty {}

    List<@NonNull String> list;
    Map<@NonNull Integer, @NonNull @NonEmpty Set<String>> map;
    Object plain;

    public static void main(String[] args) throws ReflectiveOperationException {
        for(Field field: AnnoTest.class.getDeclaredFields()) {
            AnnotatedType at = field.getAnnotatedType();
            System.out.println(formatType(at)+" "+field.getName());
        }
    }
    static CharSequence formatType(AnnotatedType type) {
        StringBuilder sb=new StringBuilder();
        for(Annotation a: type.getAnnotations()) sb.append(a).append(' ');
        if(type instanceof AnnotatedParameterizedType) {
            AnnotatedParameterizedType apt=(AnnotatedParameterizedType)type;
            sb.append(((ParameterizedType)type.getType()).getRawType().getTypeName());
            sb.append(Stream.of(apt.getAnnotatedActualTypeArguments())
                .map(AnnoTest::formatType).collect(Collectors.joining(",", "<", ">")));
        }
        else sb.append(type.getType().getTypeName());
        return sb;
    }
}

See also the end of this answer for an example handling the other scenarios like type variables, wild card types and arrays.




回答2:


There is indeed a method getAnnotatedActualTypeArguments but in AnnotatedParameterizedType, not in ParameterizedType where I was looking for it.



来源:https://stackoverflow.com/questions/38486360/are-annotations-on-a-type-parameter-accessible-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!