Extracting a specific type argument from a type that extends a generic type in java annotation processors

I have a type:

interface Foo<T> {
  boolean foo();
}

And I create BarFoo like this:

class BarFoo implements Foo<Bar> {
  boolean foo();
}

In the Java6 annotation processor, with access to the TypeElement BarFoo element, how can I find out the specific type with which this non-general subclass was parameterized in the context of the Foo implementation (in particular, Bar).

And how, further, when I have a more complex hierarchy, like this:

interface Foo<T> {
  boolean foo();
}

class BarFoo implements Foo<Bar> {
  boolean foo();
}

class Grandchild extends BarFoo {}

How, given the value of the Grandchild TypeElement parameter, can I find out the type of the parameter of type Foo?

Note: I do not need to know the reflection APIs that will do this, but especially the javax.lang.model and javax.annotation.processing APIs that will lead me to Barthis scenario.

: T boolean, , T, . , , .

+4
1

:

  • TypeElement.getInterfaces()List<? extends TypeMirror>
  • TypeMirror.accept(...) , DeclaredType
  • DeclaredType.getTypeArguments()List<? extends TypeMirror>, TypeElement Bar

Grandchild, , , getSuperclass().

- Foo, getTypeParameters(), T, , T getReturnType(), (, , , ) , , , . .

+4

All Articles