Check existence of special method in complete inheritance-tree

java

(Stefan) #1

Hi together,

I’m trying to write a sonar-plugin, which checks if a class provides a special method.

Example:

class MyChildClass extends MyParentClass { 
    MyClass() {
    }
}

class MyParentClass {
    MyParentClass() {
    }

    public static void getValue() {
        return "text";
    }
}

How can I check, if MyChildClass - or a superclass of it - provides the getValue() method?
Currently I use an IssuableSubscriptionVisitor and visit the Kind.CLASS nodes.
So the visitNode(Tree tree) method delivers me a ClassTree that represents MyChildClass … but this ClassTree does not contain the methods of superclasses.

Thanks for every help in advance!
Stefan


(Michael Gumowski) #2

Hello,

The ClassTree (and in fact the whole syntax tree API) is only going to give you what is present in the file currently being analyzed. In order to explore parent classes, you need to use the semantic API.

From MyChildClass corresponding ClassTree, check for the symbol(), then its corresponding type(), which will allow you to start digging into the type hierarchy. From each parent type, you will be able to explore the associated type symbol, and therefore its members.

Hope this helps,
Michael


(Stefan) #3

Hello Michael,

Thank you very much for your advice!
Now I got the rule running.

Regards
Stefan