Are annotations on a type parameter accessible in runtime?(类型参数上的注释是否可以在运行时访问?)
问题描述
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 instanceof
s 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.
这篇关于类型参数上的注释是否可以在运行时访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:类型参数上的注释是否可以在运行时访问?


基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01