java-complexity

0.8.0 • Public • Published

java-complexity

This tool approximates the cyclomatic complexity of a snippet of Java code, based loosely on these rules.

const javaComplexity = require('java-complexity');
 
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 = javaComplexity(input) // => { 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.

Readme

Keywords

none

Package Sidebar

Install

npm i java-complexity

Weekly Downloads

30

Version

0.8.0

License

MIT

Unpacked Size

2.88 MB

Total Files

23

Last publish

Collaborators

  • ajaymt