Reputation: 173
So, im currently developing a SonarQube Java Plugin with some custom rules.
One of them needs to analyze the Java file for a specific annotation, and if it's missing, it should report an issue UNLESS that annotation is on its superclass.
The problem is that i can't analyze another file (the superclass) DURING the current analysis (on the class).
Is there a way to do this?
Obs: I'm using SonarQube Server 5.6.3 and Sonar Java Plugin 4.9.
Upvotes: 1
Views: 264
Reputation: 4420
You can use semantic model to get information about symbols (classes, methods, ...) defined in the project. When you analyze class, you have access to ClassTree
, so you can test if annotation is present on its superclass like this
ClassTree classTree = (ClassTree) tree;
Symbol.TypeSymbol classSymbol = classTree.symbol();
Type superClass = classSymbol.superClass();
SymbolMetadata superClassMetadata = superClass.symbol().metadata();
if (superClassMetadata.isAnnotatedWith("org.acme.MyAnnotation")) {
//...
}
See for example this check implementation, which uses the API
Upvotes: 3