java-complexity
This tool approximates the cyclomatic complexity of a snippet of Java code, based loosely on these rules.
const javaComplexity = ; const input = `void main() { int number = 5; double pi = 22.0 / 7.0; if (number < 0) return; switch (number) { case 'a': break; case 2: break; case 5: System.out.println("pi is " + (pi > 3 ? "higher" : "lower") + " than 3."); break; default: break; }}` const complexity = // => { complexityValues: { main: 6 }, total: 6 }
When invoked from the command line, java-complexity
reads input from stdin.
API
javaComplexity(input, [suppressErrors], [root])
input
is a string of Java code to process. suppressErrors
is an optional flag that will suppress error logging. root
is an optional (string) root grammar rule.
This function returns a map of method names to cyclomatic complexity values.
Unless root
is specified, it attempts to parse the input as
- an entire compilation unit (i.e package declaration, imports and class definitions)
- the inside of a class body
- the inside of a method
If all of these fail, the function returns null
.
Invoking from the command-line
java-complexity [-s] [input]
The -s
flag suppresses error logging. If input
is not provided as an argument, it is read from stdin. This program exits with code 1
if it fails to parse the input string.