Skip to content

Instantly share code, notes, and snippets.

@raphw
Last active January 12, 2016 00:08
Show Gist options
  • Save raphw/8e1c805ccda86f422132 to your computer and use it in GitHub Desktop.
Save raphw/8e1c805ccda86f422132 to your computer and use it in GitHub Desktop.
Impossible to read owner type of an annotated parameterized type.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedParameterizedType;
public class Foo<T> {
@Sample(1) Foo<@Sample(2) Void>.@Sample(3) Bar<@Sample(4) Void> field;
public static void main(String[] args) throws Exception{
System.out.println(Foo.class.getDeclaredField("field").getAnnotatedType().isAnnotationPresent(Sample.class));
System.out.println(Foo.class.getDeclaredField("field").getAnnotatedType() instanceof AnnotatedParameterizedType);
AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType) Foo.class.getDeclaredField("field").getAnnotatedType();
AnnotatedType ownerType = annotatedParameterizedType.getAnnotatedOwnerType(); // No such method exists.
}
class Bar<S> { }
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface Sample {
int value();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment