Skip to content

Instantly share code, notes, and snippets.

@raphw
Created April 30, 2018 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphw/fd257af481a9258fc6cd24cb54504c1e to your computer and use it in GitHub Desktop.
Save raphw/fd257af481a9258fc6cd24cb54504c1e to your computer and use it in GitHub Desktop.
public class GenericNestedType {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface SampleTypeAnnotation { }
static class GenericNested<T> {
class Inner { }
}
@SampleTypeAnnotation GenericNested<?>. Inner foo;
GenericNested<?>.@SampleTypeAnnotation Inner bar;
public static void main(String[] args) throws Exception {
Field foo = GenericNestedTypeDemo.class.getDeclaredField("foo");
System.out.println(foo.getAnnotatedType().getType().toString());
System.out.println(foo.getAnnotatedType().getAnnotations().length);
System.out.println(foo.getAnnotatedType().getAnnotatedOwnerType().getType().toString());
System.out.println(foo.getAnnotatedType().getAnnotatedOwnerType().getAnnotations().length);
Field bar = Foo.class.getDeclaredField("bar");
System.out.println(bar.getAnnotatedType().getType().toString());
System.out.println(bar.getAnnotatedType().getAnnotations().length);
System.out.println(bar.getAnnotatedType().getAnnotatedOwnerType().getType().toString());
System.out.println(bar.getAnnotatedType().getAnnotatedOwnerType().getAnnotations().length);
}
}
public class GenericReceiverDemo<T> {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface SampleTypeVariable { }
void foo(Foo<@SampleTypeVariable T> this) { }
public static void main(String[] args) throws Exception {
Method foo = GenericReceiverDemo.class.getDeclaredMethod("foo");
AnnotatedType receiverType = foo.getAnnotatedReceiverType();
// No chance to find out that the parameter type T was annotated, despite the annotation being present in the class file.
System.out.println(receiverType.getType() instanceof ParameterizedType);
}
}
public class TypeVariableBoundDemo {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface SampleTypeVariable { }
<T, S extends @SampleTypeVariable T, U extends @SampleTypeVariable Object> void foo() { }
public static void main(String[] args) throws Exception {
Method foo = TypeVariableBoundDemo.class.getDeclaredMethod("foo");
TypeVariable<Method>[] typeParameters = foo.getTypeParameters();
TypeVariable<?> s = typeParameters[1];
AnnotatedType[] annotatedBoundsOfS = s.getAnnotatedBounds();
System.out.println(annotatedBoundsOfS[0].getDeclaredAnnotations().length);
TypeVariable<?> u = typeParameters[2];
AnnotatedType[] annotatedBoundsOfU = u.getAnnotatedBounds();
System.out.println(annotatedBoundsOfU[0].getDeclaredAnnotations().length);
}
}
public class TypeVariableBoundsDemo<T extends Callable<@Foo.SampleTypeAnnotation ?> & List<@Foo.SampleTypeAnnotation ?>> {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface SampleTypeAnnotation { }
public static void main(String[] args) throws Exception {
TypeVariable<Class<Foo>>[] variables = TypeVariableBoundsDemo.class.getTypeParameters();
AnnotatedType[] bounds = variables[0].getAnnotatedBounds();
System.out.println(bounds[0] instanceof AnnotatedParameterizedType);
AnnotatedParameterizedType callable = (AnnotatedParameterizedType) bounds[0];
callable.getAnnotatedActualTypeArguments(); // throws exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment