Home

Awesome

CK

Build Status Code Coverage Maven Central

CK calculates class-level and method-level code metrics in Java projects by means of static analysis (i.e. no need for compiled code). Currently, it contains a large set of metrics, including the famous CK:

Note: CK separates classes, inner classes, and anonymous classes. LOC is the only metric that is not completely isolated from the others, e.g., if A has a declaration of an inner class B, then LOC(A) = LOC(class A) + LOC(inner class B).

Project Structure

CK is a Java code metrics collection tool, streamlined into a simple structure that revolves around three primary packages:

For brevity, within this documentation, package prefixes such as com.github.mauricioaniche.ck are omitted.

Architecture

Core Components

Metrics Identification

Metrics Collection

Results Notification and Storage

Design Patterns

CK framework incorporates a number of well-established design patterns to enhance modularity, extendibility, and maintainability of its codebase. These patterns enable the framework to efficiently handle complex operations such as traversing abstract syntax trees (AST), collecting metrics, and notifying results. Below are the key design patterns utilized:

By leveraging these design patterns, CK efficiently manages complexity and ensures that the framework remains robust, adaptable, and easy to extend as new requirements and metric types emerge.

How to use the standalone version

You need at least Java 8 to be able to compile and run this tool.

To use the latest version (which you should), clone the project and generate a JAR. A simple mvn clean compile package generates the single JAR file for you (see your target folder).

Then, just run:

java -jar ck-x.x.x-SNAPSHOT-jar-with-dependencies.jar \
	<project dir> \
	<use jars:true|false> \
	<max files per partition:0=automatic selection> \
	<variables and fields metrics?:true|false> \
	<output dir> \
	[ignored directories...]

Project dir refers to the directory where CK can find all the source code to be parsed. Ck will recursively look for .java files. CK can use the dependencies of the project as to improve its precision. The use jars parameters tells CK to look for any .jar files in the directory and use them to better resolve types. Max files per partition tells JDT the size of the batch to process. Let us decide that for you and start with 0; if problems happen (i.e., out of memory) you think of tuning it. Variables and field metrics indicates to CK whether you want metrics at variable- and field-levels too. They are highly fine-grained and produce a lot of output; you should skip it if you only need metrics at class or method level. Finally, output dir refer to the directory where CK will export the csv file with metrics from the analyzed project. Optionally, you can specify any number ignored directories, separated by spaces (for example, build/). By default, .git and all other hidden folders are ignored.

The tool will generate three csv files: class, method, and variable levels.

How to integrate it in my Java app

Learn by example. See Runner.java class.

Maven

See the most recent version of the library in the badge at the beginning of this README, or at https://mvnrepository.com/artifact/com.github.mauricioaniche/ck.

Use the following snippet in your pom.xml. Update X.Y.Z with the most recent version of the tool (check mvnrepository.com or the badge at the beginning of this README file):

<!-- https://mvnrepository.com/artifact/com.github.mauricioaniche/ck -->
<dependency>
    <groupId>com.github.mauricioaniche</groupId>
    <artifactId>ck</artifactId>
    <version>X.Y.Z</version>
</dependency>

You also may use the CK maven plugin, developed by @jazzmuesli, which automatically runs CK in your project. Very useful to developers: https://github.com/jazzmuesli/ck-mvn-plugin.

Supporting a new version of Java

This tool uses Eclipse's JDT Core library under the hood for AST construction. Currently the compliance version is set to Java 11.

Need support for a newer language version? The process of adding it is very straightforward, considering contributing a PR:

  1. Add a failing unit test case showcasing at least one of the syntax features present in the new version you want to provide support.
  2. Update the Eclipse JDT Core dependency in the pom.xml file. You may use a repository browser like MVN Repository to ease this process.
  3. Also in the pom.xml file, update the source and target properties of the Maven Compiler plugin accordingly.
  4. Adjust the following lines in CK.java:
    [...]
    ASTParser parser = ASTParser.newParser(AST.JLS11);
    [...]
    JavaCore.setComplianceOptions(JavaCore.VERSION_11, options);
    [...]
    
  5. Check if the failing unit test case you added in the first step is now green. Then submit a PR.

Why is it called CK?

Because the tool was born to just calculate the CK classLevelMetrics, but it grew beyond my expectations... Life is funny!

How to cite?

Please, use the following bibtex entry:

@manual{aniche-ck,
  title={Java code metrics calculator (CK)},
  author={Maurício Aniche},
  year={2015},
  note={Available in https://github.com/mauricioaniche/ck/}
}

How to Contribute

Just submit a PR! :)

License

This software is licensed under the Apache 2.0 License.