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...
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.
There is indeed a method getAnnotatedActualTypeArguments
but in AnnotatedParameterizedType
, not in ParameterizedType
where I was looking for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With