I am looking at a rather trivial class with a single method that defines an annotated receiver type:
class Foo<T> {
void foo(Foo<@Bar T> this) {}
}
I would now like to access the type annotation on the receiver type's parameter @Bar
but the Java reflection API returns an annotated raw type when accessing the receiver:
assert Foo.class.getDeclaredMethod("foo")
.getAnnotatedReceiverType()
instanceof AnnotatedParameterizedType;
The assertion fails as the annotated type that is returned is returned as a raw type Foo
. Is this intentional? I can still find the @Bar
annotation when accessing private properties of the implementation of AnnotatedType
that is returned.
I am running a recent version of Java 8.
Turns out this is a simple bug. I first thought, this is an implicitation of the linked issues but type annotations have nothing to do with that. Type annotations are poorly tested in the current implementation of the Java runtime. I found a sheer number of issues when deep diving into the matter: