我在类中有一个方法:
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/