I have a method in a class:
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();
}
According OpenClover documentation:
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
In report generated by OpenClover (version 4.4.1) the complexity for this method equals 3. Why? How it was counted?
PS. For example in JaCoCo report I see Ctx=1 for this method.
tryCatchBlock [boolean labelled] returns [CloverToken last]
{
last = null;
int complexity = 0;
ContextSet saveContext = getCurrentContext();
}
: tr:"try" (lp:LPAREN {insertAutoCloseableClassDecl((CloverToken)tr);} declaration {complexity++; instrArmDecl(((CloverToken)lp).getNext(), (CloverToken)LT(0), saveContext);} (semi:SEMI declaration {complexity++; instrArmDecl(((CloverToken)semi).getNext(), (CloverToken)LT(0), saveContext);})* (SEMI)? rp:RPAREN )?
{enterContext(ContextStore.CONTEXT_TRY); saveContext = getCurrentContext();}
last=compoundStatement
{exitContext();}
(last=handler {complexity++;})*
( "finally"
{enterContext(ContextStore.CONTEXT_FINALLY); saveContext = getCurrentContext();}
last=compoundStatement
{exitContext();}
)?
{ if (!labelled) {
instrInlineBefore((CloverToken)tr, last, saveContext, complexity);
}
}
;
* 1st comes from the try-with-resources block:
try (FileOutputStream fos = new FileOutputStream(file))
* 2nd comes from the method entry as per docs
I'm not sure about the 3rd one...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.