kotcrab / callsuper-annotation

Java annotation that forces you to call super method if it was annotated with CallSuper
Apache License 2.0
12 stars 5 forks source link

Checking super type is broken for generic types #7

Open vovcacik opened 8 years ago

vovcacik commented 8 years ago

If the type parameter in checkSuperTypes is generic it will have erased supertype and thus type.supertype_field == null. Luckily we can still access the non-erased type thru tsym.

public boolean checkSuperTypes (ClassType type) {
    // generic types have the goodies elsewhere
    if (type.hasErasedSupertypes())
        type=(ClassType)type.tsym.type

    if (type.supertype_field != null && type.supertype_field.tsym != null) {
        if (checkScope(type.supertype_field.tsym.members()))
            return true;
        else
            return checkSuperTypes((ClassType) type.supertype_field);
    }

    return false;
}

Note that this problem affect generic types only if there is at least three or more in the inheritance chain. If there are only two generic classes, then only checkScopes is called and its not affected. Only checkSuperTypes is affected. I believe it boils down to that checkScopes is taking members from JCIdent and the checkSuperTypes from ClassType object.

vovcacik commented 8 years ago

One more comment. It might be better to replace

    if (type.hasErasedSupertypes())
        type=(ClassType)type.tsym.type

with

    if (type.supertype_field == null)
        type=(ClassType)type.tsym.type

It should be true that type.hasErasedSupertypes() == (type.supertype_field == null) but I am not absolutely sure about this.