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

2022-01-13 00:00:00 annotations java-8 java bean-validation

Java 8 允许:

public List<@NonNull String> names;

但是有没有办法在运行时访问这个注解,还是只对编译器插件可用?

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

有新的 Method#getAnnotatedReturnType 提供对返回类型注释的访问,所以我希望 ParameterizedType 现在会有类似 getActualAnnotatedTypeArguments 的东西这对泛型类型参数会做同样的事情,但它不存在......

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...

推荐答案

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

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.

相关文章