Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 978
confile Avatar asked Feb 11 '23 07:02

confile


1 Answers

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.

like image 149
confile Avatar answered Feb 27 '23 23:02

confile