Michael
Michael

Reputation: 33297

Get Super Class from TypeElement without Generic?

I have a class:

public class StartPagePresenter extends AbstractPresenter<String> {
...
}

Using Java Annotation Processing I got the TypeElement of the class:

TypeElement startPagePresenterType = // get the TypeElement of StartPagePresenter

Now I need to get the super class which is done with:

startPagePresenterType.getSuperclass();

Then I tried to check if the super class has the correct type with:

if ( !startPagePresenterType.getSuperclass().toString().equals(
     AbstractPresenter.class.getCanonicalName()) ) {
 ...
}

Here is the problem: AbstractPresenter.class.getCanonicalName() leads to:

core.mvp.AbstractPresenter

and startPagePresenterType.getSuperclass().toString() leads to:

core.mvp.AbstractPresenter<java.lang.String>

When you compare these Strings they are never equal although the super classes are the same.

How can I get the super class from startPagePresenterType.getSuperclass() without the generic block?

Upvotes: 4

Views: 1322

Answers (1)

Michael
Michael

Reputation: 33297

I found the answer:

TypeMirror superClassTypeMirror = startPagePresenterType.getSuperclass();
TypeElement superClassTypeElement = 
            (TypeElement)((DeclaredType)superClassTypeMirror).asElement();

That's it! superClassTypeElementthen is the TypeElement of the super class.

Upvotes: 8

Related Questions