Last active
January 12, 2016 00:08
-
-
Save raphw/8e1c805ccda86f422132 to your computer and use it in GitHub Desktop.
Impossible to read owner type of an annotated parameterized type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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