java - OpenClover 如何计算方法的复杂度?

标签 java clover

我在类中有一个方法:

    public boolean tryFile() throws IOException {
        File file = File.createTempFile("test", "dat");
        file.deleteOnExit();
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(20);
        }
        return file.exists();
    }

根据 OpenClover 文档:

Method Complexity

Cyclomatic complexity of a single method. It's calculated as follows:

empty method complexity == 1
simple statement complexity == 0
switch block complexity == number of case statements
try catch block complexity == number of catch statements
ternary expression complexity == 1
boolean expression complexity == number of && or || in expression

在 OpenClover(版本 4.4.1)生成的报告中,该方法的复杂度等于 3。为什么?是怎么算的?

PS。例如,在 JaCoCo 报告中,我看到此方法的 Ctx=1。

最佳答案

Try-with-resources 是有效的语法糖。生成了一些捕获和条件 block 。请参阅JLS section 14.20.3.1 .

Clover 不知道使用了 try-with-resources。它可能正在查看已经将 try-with-resources 转换为等效的 try-catch-finally 的 AST。这意味着没有任何特定语言构造的痕迹,并且没有办法为其提供特殊情况,即使 Clover 开发人员想要有一个。

关于java - OpenClover 如何计算方法的复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59827841/

相关文章:

java - 如何在 ListView android 上方添加固定按钮?

java - Clover:检测后附加不需要的代码

java - 如何在 Maven 和 IntelliJ 之间共享 Clover 覆盖率数据

java - Web 服务中的 selenium 抛出 java.lang.NoClassDefFoundError 异常

java - 如何在Java中仅删除字符串的尾随空格并保留前导空格?

java - 使用带有填充和 block 密码模式的哈希

java - 从 Clover 代码覆盖率中全局排除所有 Spring JavaConfig

junit - 如何指定 Clover 忽略的代码行?

java - Couchbase 错误 : rx. exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value