CodeStyleDetector (CSD) is a Java command-line tool that analyzes an existing Java codebase, infers commonly used style conventions, and generates an XML configuration for Checkstyle.
Instead of starting with a predefined style guide, CSD inspects the source code and derives rule settings from the conventions it finds. The generated configuration can then be used to check a project with Checkstyle.
- Recursively discovers Java source files in a directory, or analyzes a single
.javafile. - Parses Java source with the Eclipse JDT Abstract Syntax Tree (AST).
- Infers rule values across the codebase using rule-specific builders.
- Generates a Checkstyle XML configuration.
- Lets you select individual rules and print analysis or builder diagnostics.
- Includes unit, integration, and command-line system tests.
The current implementation analyzes these rules:
| Rule ID | Checkstyle rule |
|---|---|
AVOID_STAR_IMPORT |
AvoidStarImport |
NESTED_FOR_DEPTH |
NestedForDepth |
MAGIC_NUMBER |
MagicNumber |
CONSTANT_NAME |
ConstantName |
EMPTY_STATEMENT |
EmptyStatement |
EMPTY_CATCH_BLOCK |
EmptyCatchBlock |
ABSTRACT_CLASS_NAME |
AbstractClassName |
When multiple source files are analyzed, rule builders aggregate the values found in the ASTs and build the resulting rules from those observations.
- JDK 17
- Gradle (the included Gradle wrapper can be used instead of installing Gradle separately)
From the repository root:
cd CodeStyleDetector
./gradlew buildRun the tests independently with:
./gradlew testGradle is configured with JaCoCo reporting and the PIT mutation-testing plugin. Test and coverage reports are written under build/reports/. The GitHub Actions workflow builds the project and checks for at least 70% JaCoCo coverage; it does not currently run PIT.
Analyze a Java source directory and write the generated configuration:
./gradlew run --args='--path /path/to/java/project --output-path ./checkstyleConfiguration.xml'Analyze one Java file:
./gradlew run --args='--path /path/to/Example.java --output-path ./checkstyleConfiguration.xml'By default, CSD analyzes the current directory and writes ./style-conf.xml.
To analyze only selected rules, create a text file containing one rule ID per line, for example:
MAGIC_NUMBER
AVOID_STAR_IMPORT
Pass the file with --rule-list:
./gradlew run --args='--path /path/to/java/project --rule-list ./rules.txt --output-path ./checkstyleConfiguration.xml'| Option | Description |
|---|---|
-p, --path |
Java source file or codebase directory to analyze (default: ./). |
-o, --output-path, --output-filename |
Path and filename for the generated XML (default: ./style-conf.xml). |
-rl, --rule-list |
File containing the rule IDs to analyze; omit it to analyze all supported rules. |
-v1, --verbose-1 |
Print the rule analysis results. |
-v2, --verbose-2 |
Print rule-builder diagnostics. |
--help |
Show command-line help. |
--man |
Show the bundled user manual. |
CodeStyleDetector/
├── src/main/java/ Application, codebase analysis, rules, and XML generation
├── src/main/resources/ Logging configuration and CLI manual/help text
├── src/test/java/ Unit, integration, and system tests
├── src/test/resources/ Java fixtures and CLI test inputs
├── config/checkstyle/ Checkstyle configuration files
└── config/pmd/ PMD ruleset
CodeStyleDetector is distributed under the BSD 2-Clause License.