问题
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 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.
回答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