Sonar建议汇总

Sonar建议汇总

标签(空格分隔): 工具 编码规范


The Cyclomatic Complexity of this method “deleteMission” is 14 which is greater than 10 authorized

嵌套复杂度为14,高于限定值10

Refactor this code to not nest more than 3 if/for/while/switch/try statements.

重构此代码,不得超过3行

Move this variable to comply with Java Code Conventions

构造函数应遵守Java代码约定,请移动变量的代码。参考:Java Code Conventions
下表描述了类和接口声明的各个部分以及它们出现的先后次序。

次序类/接口声明的各部分注解
1类/接口文档注释(/*……/)该注释中所需包含的信息,参见”文档注释”
2类或接口的声明
3类/接口实现的注释(/……/)如果有必要的话该注释应包含任何有关整个类或接口的信息,而这些信息又不适合作为类/接口文档注释。
4类的(静态)变量首先是类的公共变量,随后是保护变量,再后是包一级别的变量(没有访问修饰符,access modifier),最后是私有变量。
5实例变量首先是公共级别的,随后是保护级别的,再后是包一级别的(没有访问修饰符),最后是私有级别的。
6构造器
7方法这些方法应该按功能,而非作用域或访问权限,分组。例如,一个私有的类方法可以置于两个公有的实例方法之间。其目的是为了更便于阅读和理解代码。

Reorder the modifiers to comply with the Java Language Specification.

调整修饰符次序,反例:public final static PropertyMap<Event, EventVO> voMap = new PropertyMap<Event, EventVO>()

Rename this constant name to match the regular expression ‘^[A-Z][A-Z0-9](_[A-Z0-9]+)$’

常量要求大写

Add a private constructor to hide the implicit public one.

工具类不应该有默认或者公共的构造函数,也就是说这个类里可能方法都是static,那就不需要构造它的实例,因此应该给加一个private的构造函数,就不会报这个错了。
a class which only has private constructors should be final
例如上一个,加了private构造函数,又会出这个,把class设置成final即可。例:

  public class Shape {
    private Shape() { 
       /* set something here */
    }

    public static Shape makeShape(/* arglist */) {
       System.out.println("here is the shape you ordered");
       return (new Shape());
    }
 }

Invoke method(s) only conditionally

以下代码会报错:

logger.log(Level.DEBUG, "Something went wrong: " + message);  // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages

LOG.error("Unable to open file " + csvPath, e);  // Noncompliant

Preconditions.checkState(a > 0, "Arg must be positive, but got " + a);  // Noncompliant. String concatenation performed even when a > 0

Preconditions.checkState(condition, formatMessage());  // Noncompliant. formatMessage() invoked regardless of condition

Preconditions.checkState(condition, "message: %s", formatMessage());  // Noncompliant

推荐如下:

logger.log(Level.SEVERE, "Something went wrong: %s ", message);  // String formatting only applied if needed

logger.log(Level.SEVERE, () -> "Something went wrong: " + message); // since Java 8, we can use Supplier , which will be evaluated lazily

LOG.error("Unable to open file {}", csvPath, e);

if (LOG.isDebugEnabled() {
  LOG.debug("Unable to open file " + csvPath, e);  // this is compliant, because it will not evaluate if log level is above debug.
}

Preconditions.checkState(arg > 0, "Arg must be positive, but got %d", a);  // String formatting only applied if needed

官方解释:

“Preconditions” and logging arguments should not require evaluation

Passing message arguments that require further evaluation into a Guava com.google.common.base.Preconditions check can result in a performance penalty. That’s because whether or not they’re needed, each argument must be resolved before the method is actually called.

Only the sign of the result should be examined

While most compareTo methods return -1, 0, or 1, some do not, and testing the result of a compareTo against a specific value other than 0 could result in false negatives.

大部分compareTo()比较方法,返回结果都是-1, 0, or 1,但并非全部,建议如下:

Noncompliant Code Example

if (myClass.compareTo(arg) == -1) {  // Noncompliant
  // ...
}
Compliant Solution

if (myClass.compareTo(arg) < 0) {
  // ...
}
### 多模块工程中 Jacoco 覆盖率汇总Sonar 集成方法 在多模块 Maven 工程中,Jacoco 的覆盖率数据通常以 `.exec` 文件形式存储。然而,为了更好地与 SonarQube 集成并提供更直观的 XML 报告展示,需要将多个子模块的覆盖率数据汇总为单一的 `jacoco.xml` 文件。 --- #### 1. 使用 Aggregator 模块汇总覆盖率 创建一个新的聚合模块(Aggregator),专门负责收集所有子模块的覆盖率数据并将它们合并为单个报告。以下是实现步骤: ##### (a) 修改父级 `pom.xml` 在根项目的 `pom.xml` 中定义全局配置,确保每个子模块都能生成自己的覆盖率数据: ```xml <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.9</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report-aggregate</id> <phase>verify</phase> <goals> <goal>report-aggregate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 此处的关键在于 `<goal>report-aggregate</goal>`,它会遍历所有子模块并生成一份综合的覆盖率报告[^2]。 --- ##### (b) 创建 Aggregator 模块 新增一个名为 `aggregator-module` 的模块,并在其 `pom.xml` 中声明依赖其他子模块。例如: ```xml <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>module-a</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>module-b</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <executions> <execution> <id>aggregate-report</id> <phase>verify</phase> <goals> <goal>report-aggregate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 运行以下命令可生成汇总后的 `jacoco.xml` 文件: ```bash mvn verify ``` 此时,`target/site/jacoco-aggregate/` 目录下将会包含完整的覆盖率报告文件 `jacoco.xml`[^2]。 --- #### 2.汇总后的覆盖率数据上传至 SonarQube 为了让 SonarQube 正确解析覆盖率数据,需将其路径传递给 Sonar 扫描器。以下是具体的配置方式: ##### (a) 设置 Sonar 属性 编辑根项目的 `pom.xml` 或者通过 CLI 命令传入必要的属性值: ```xml <properties> <sonar.coverage.jacoco.xmlReportPaths>aggregator-module/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths> </properties> ``` 或者直接在命令行中指定: ```bash mvn sonar:sonar \ -Dsonar.host.url=http://localhost:9000 \ -Dsonar.login=<your-token> \ -Dsonar.coverage.jacoco.xmlReportPaths=aggregator-module/target/site/jacoco-aggregate/jacoco.xml ``` 这里需要注意的是,`sonar.coverage.jacoco.xmlReportPaths` 应指向前面生成的汇总报告文件路径[^4]。 --- ##### (b) 确保 Sonar 插件可用 虽然部分项目可能无需显式声明 `sonar-maven-plugin` 即可成功运行扫描,但在复杂的多模块环境中建议手动添加插件以增强稳定性: ```xml <plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.9.1.2184</version> </plugin> ``` 随后再次执行扫描命令即可完成整个流程[^4]。 --- ### 总结 通过上述方法,不仅可以有效解决多模块工程中覆盖率数据分散的问题,还能借助 SonarQube 实现统一的质量监控和可视化管理。这种方法充分利用了 Jacoco 的 `report-aggregate` 功能以及 Sonar 对外部报告的支持特性[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值