2

I want to check if any of annotated classes' ancestors is actually android.support.v4.app.Fragment.

Currently I wrote a recursive method to do that.

I wonder if any better way of doing this?

The main purpose is to restrain my annotation to be added to any classes.

I also use squareup.javapoet.

private boolean isSubClass(TypeElement typeElement, ClassName superClassName) {
    TypeElement superTypeElement = (TypeElement) ((DeclaredType) typeElement.getSuperclass()).asElement();

    if (superTypeElement.toString().equals(Object.class.getName())) {
        return false;
    } else if (superTypeElement.toString().equals(superClassName.reflectionName())) {
        return true;
    }

    return isSubClass(superTypeElement, superClassName);
}
Arst
  • 3,098
  • 1
  • 35
  • 42
  • Doing it this way is fine, there is a helper method which already does this but it doesn't behave in a way you would expect - hence it is basically useless. Just for the future: remember that you have to account for classes and interfaces differently in your recursion. – Xaver Kapeller Jun 22 '17 at 05:53

0 Answers0