0

我检查了 Sonar-Custom-Plugin-Example 项目,复制了传感器并开始编写我的规则。我很困惑,因为此示例项目中缺少树结构。如果字段以 DTO 结尾,我将如何检查它?我已经找到了匹配的正则表达式,只需要找到一种方法来遍历源树。

public class NotEndOnDTOJavaFilesSensor implements Sensor {

  protected static final String DEFAULT_FORMAT_VALUE = ".*(?<!(dto|DTO))$";

  @Override
  public void describe(SensorDescriptor descriptor) {
    descriptor.name("Field Name is not allowed to contain DTO");

    // optimisation to disable execution of sensor if project does
    // not contain Java files or if the example rule is not activated
    // in the Quality profile
    descriptor.onlyOnLanguage("java");
    descriptor.createIssuesForRuleRepositories(JavaRulesDefinition.REPOSITORY);
  }

  @Override
  public void execute(SensorContext context) {
    FileSystem fs = context.fileSystem();
    Iterable<InputFile> javaFiles = fs.inputFiles(fs.predicates().hasLanguage("java"));
    for (InputFile javaFile : javaFiles) {
      // no need to define the severity as it is automatically set according
      // to the configured Quality profile
      NewIssue newIssue = context.newIssue()
        .forRule(JavaRulesDefinition.MY_DTO_RULE);

      NewIssueLocation primaryLocation = newIssue.newLocation()
        .on(javaFile)
        // here
        .message("Field Name is not allowed to contain DTO!");
      newIssue.at(primaryLocation);
      newIssue.save();
    }
  }
}
4

0 回答 0