类型参数上的注释可以在运行时访问吗?

kaq*_*qao 5 java annotations bean-validation java-8

Java 8 允许执行以下操作:

public List<@NonNull String> names;
Run Code Online (Sandbox Code Playgroud)

但是有没有办法在运行时访问此注释,或者它仅适用于编译器插件?

有一个新Method#getAnnotatedReturnType功能可以访问返回类型上的注释,所以我希望ParameterizedType现在有类似的东西getActualAnnotatedTypeArguments可以对泛型类型参数执行相同的操作,但它不存在......

Hol*_*ger 5

新的 API 延续了需要大量 s 和类型转换的传统instanceof

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

另请参阅本答案末尾的示例,了解处理类型变量、通配符类型和数组等其他场景的示例。