简体   繁体   中英

sonarqube java rule not extended classes should be final

I´ve programmed a rule which checks if a not extended class is final. I have used ClassTree.symbol().usages() to check if it is being used:

public List<Kind> nodesToVisit() {
    // TODO Auto-generated method stub
    return ImmutableList.of(Tree.Kind.CLASS);
}

public void visitNode(Tree ttree) {
    ClassTree tree = (ClassTree) ttree;
    List<IdentifierTree> itl = tree.symbol().usages();
    System.out.println(itl.size());
}

When i test the rule here:

public class Class1 {

}

public class Class2 {

}

public class Class3 extends Class1 {

}

Class1 usages = 1 Class2 and Class 3 usages = 0;

This works only if i define the three classes in one single file, in one single class. When I test this in sonar Class1, Class2 and Class3 are separated classes of the project and the result is: usages = 0 for Class1, Class2 and Class3 even though Class1 is being extended by Class3.

How can i fix it?

Thanks in advance

You won't be able to write such rules based on the SonarJava semantic engine. The usages you are accessing though tree.symbol().usages() only refers to usages within the current file being analyzed.

Other usages (like type extensions) in other files are not recognized, nor collected, so there is currently no way, using the SonarJava API, to easily implement the rule you are trying to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM