简体   繁体   English

sonarqube java规则不扩展类应该是最终的

[英]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: 我已经使用ClassTree.symbol()。usages()来检查是否正在使用它:

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; Class1用法= 1 Class2和Class 3用法= 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. 当我在声纳中对Class1进行测试时,Class2和Class3是项目的分离类,结果是:Class1,Class2和Class3的用法= 0,即使Class1被Class3扩展了。

How can i fix it? 我该如何解决?

Thanks in advance 提前致谢

You won't be able to write such rules based on the SonarJava semantic engine. 您将无法基于SonarJava语义引擎编写此类规则。 The usages you are accessing though tree.symbol().usages() only refers to usages within the current file being analyzed. 通过tree.symbol().usages()访问的tree.symbol().usages()仅指正在分析的当前文件中的用法。

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. 无法识别或收集其他文件中的其他用法(例如类型扩展名),因此当前无法使用SonarJava API轻松实现您要实施的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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