Skip to content

Instantly share code, notes, and snippets.

@tomaszalusky
Created October 10, 2016 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaszalusky/3e3777b4fd0c6096f3f707bb19b50b52 to your computer and use it in GitHub Desktop.
Save tomaszalusky/3e3777b4fd0c6096f3f707bb19b50b52 to your computer and use it in GitHub Desktop.
import java.lang.reflect.*;
import java.util.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class AnnotationOnTypeArgument {
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD,ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Anno {
}
interface Nested<T> {
}
Toplevel<@Anno Integer> toplevel;
Nested<@Anno Integer> nested;
public static void main(String[] args) throws Exception {
print(AnnotationOnTypeArgument.class.getDeclaredField("toplevel"));
print(AnnotationOnTypeArgument.class.getDeclaredField("nested"));
}
private static void print(Field field) {
AnnotatedType annotatedType = field.getAnnotatedType();
AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType)annotatedType;
ParameterizedType parameterizedType = (ParameterizedType)annotatedParameterizedType.getType();
AnnotatedType argType = annotatedParameterizedType.getAnnotatedActualTypeArguments()[0];
System.out.printf("field %s%ntype=%s%nannotatedType=%s%nannotations=%s%ntype=%s%n%n",
field.getName(), parameterizedType, argType, Arrays.asList(argType.getDeclaredAnnotations()), argType.getType());
}
}
interface Toplevel<T> {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment