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.
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.