This Eclipse Plug-in automates the detection and refactoring of Java methods with high Cognitive Complexity. It functions as a pipeline that scans a target project, identifies methods exceeding a configured complexity threshold, suggests "Extract Method" refactorings to improve maintainability, and can automatically apply saved refactoring solutions back to the source code.
The tool is designed to run as a headless Eclipse Application, making it suitable for integration into build systems, CI/CD pipelines, or large-scale batch analysis workflows.
This repository contains the evolving codebase for our refactoring research. If you are looking for the source code associated with specific publications, please refer to the following versions:
- Current Version (v2.0): Features a new pipeline architecture, batch processing capabilities, includes an exact Integer Linear Programming (ILP) solver, and an automated solution applier.
- Legacy Version (v1.0): Features the original approach using an exhaustive search as solver.
- Paper: R. Saborido, J. Ferrer, F. Chicano and E. Alba, "Automatizing Software Cognitive Complexity Reduction," in IEEE Access, vol. 10, pp. 11642-11656, 2022, doi: 10.1109/ACCESS.2022.3144743.
- Source Code: Download the exact v1.0 release from the GitHub Releases page.
- Automated Project Scanning: Recursively parses the specified Eclipse project to locate Java methods.
- Batch Processing Mode: Bypasses full project scanning to process a predefined list of targeted methods from an input CSV, ideal for large empirical studies across multiple projects.
- Complexity Filtering: Automatically filters methods that exceed the defined cognitive complexity threshold.
- Multiple Refactoring Solvers: Offers both exact optimization (ILP) and exhaustive search strategies to generate refactoring solutions.
- Refactoring Pipeline: Orchestrates the refactoring logic via
MethodRefactoringPipeline, connecting the analysis phase with the selected solver. - Saved Solution Application: Applies saved optimal refactoring solutions (
.resultsfiles) directly to workspace source code deterministically using Eclipse JDT. - CSV Reporting: Outputs a detailed CSV report containing the analysis results and refactoring outcomes for every processed method.
- Headless Execution: Implements
IApplicationto run without the Eclipse UI overhead.
- Java Runtime: JDK 11 or higher.
- Eclipse Platform: Eclipse IDE for RCP and RAP Developers (4.20+) or any Eclipse distribution containing the Plugin Development Environment (PDE) and JDT.
- Workspace: The target project(s) to be analyzed or modified must exist in an Eclipse workspace (different from the one where this plugin project is located).
- IBM ILOG CPLEX: The ILP solver requires CPLEX Optimization Studio (specifically the
cplex.jarand its native system libraries).
- Clone the Repository: Download the source code to your local machine.
- Configure CPLEX Dependency: Due to licensing restrictions, the
cplex.jarbinary is not included in this repository. You must install CPLEX locally, then right-click the project in Eclipse -> Build Path -> Configure Build Path... -> Libraries -> Add External JARs... and select your localcplex.jar. - Import into Eclipse: Navigate to File > Import > General > Existing Projects into Workspace, and select the root directory of the cloned repository.
- Resolve Dependencies: Open
plugin.xmlorMANIFEST.MFand ensure all Eclipse dependencies (e.g.,org.eclipse.jdt.core,org.eclipse.equinox.app) are resolved via the Target Platform.
The application separates environment-specific configurations from core execution arguments.
- Locate the
config.template.propertiesfile in the project root. - Copy and rename it to
config.properties. - Edit the file to specify your environment settings. The key properties include:
output.folder: Absolute path to the directory where the output CSV files will be saved (e.g.,./output).complexity.threshold: The cognitive complexity score required to trigger an analysis on a method (e.g.,15).cplex.native.library.path: The absolute path to your local CPLEX native binaries (e.g.,/path/to/cplex/bin/your_os_arch).working.memory: Maximum memory (in Megabytes) solvers as CPLEX are allowed to use. Setting this preventsOutOfMemoryErrorcrashes during executions by forcing the solver to write to disk or abort if a single method's extraction model becomes too complex.
Analyzes methods exceeding the cognitive complexity threshold and generates .results solution files. This mode can be executed in two sub-modes:
-
Single Project Sub-Mode (scans an entire project):
- Project Name (Required): Target project name in the external workspace.
- Solver Type (Optional): Algorithm key (
ILP,ES-LSF,ES-SSF). Default:ILP.
- Program Arguments Example:
MyTargetProject ILP
-
Batch Sub-Mode (processes a target list of methods across projects):
- Batch Flag & Input File (Required):
-batch <path_to_input_csv>pointing to a targeted method list CSV. - Solver Type (Optional): Algorithm key (
ILP,ES-LSF,ES-SSF). Default:ILP.
- Program Arguments Example:
-batch /path/to/methods.csv ILP
- Batch Flag & Input File (Required):
- Results Folder Path (Required): The absolute or workspace-relative path to the directory containing
.resultssolution files to be applied. It just applies refactoring operations which are optimal (solved by theÌLPsolver and with statusOptimal, others are discarded). Note that.resultsfiles are generated by Mode 1.
- Create Run Configuration: Go to Run > Run Configurations..., right-click Eclipse Application, and select New.
- Main Tab Setup: Name the configuration (e.g.,
ReduceComplexity-Headless). Select "Run an application" and chooseneo.reducecognitivecomplexity.app.Application. Set the Workspace Data location to point to the workspace containing the target project(s) to be analyzed. - Arguments Tab Setup:
- Program Arguments:
- Single Project Mode: Enter the target project and the chosen solver key (e.g.,
MyLegacyProject ILP). - Batch Mode: Enter the batch flag/file and the solver key (e.g.,
-batch /path/to/input_methods.csv ILP).
- Single Project Mode: Enter the target project and the chosen solver key (e.g.,
- VM Arguments: You must define the native library path for CPLEX, alongside sufficient JVM heap memory to parse large ASTs. For example:
(Note: Replace
-Djava.library.path="/path/to/cplex/bin/your_os_arch" -Xmx8G/path/to/cplex/bin/your_os_archwith your actual CPLEX bin directory. Ensure your-Xmxheap size is comfortably larger than theworking.memorylimit defined in your properties file).
- Program Arguments:
- Working Directory Setup (Crucial): Still in the Arguments tab, scroll down to the Working directory section.
- Change the selection from "Default" to Other.
- Click the Workspace... button.
- Select this plugin project (the folder containing your
config.propertiesfile) and click OK. This ensures the application finds your configuration file instead of falling back to default Eclipse paths.
- Run: Click Apply and then Run to execute the configuration.
- Create Run Configuration: Go to Run > Run Configurations..., right-click Eclipse Application, and select New.
- Main Tab Setup: Name the configuration (e.g.,
ApplySolutions-Headless). Select "Run an application" and chooseneo.reducecognitivecomplexity.app.ApplySolutions. Set the Workspace Data location to point to the workspace containing the target project(s) to be refactored. - Arguments Tab Setup:
- Program Arguments: Pass the path to the folder containing
.resultssolution files:/path/to/results_folder
- Program Arguments: Pass the path to the folder containing
- Run: Click Apply and then Run to execute the AST transformations and apply refactorings to the source code.
The application generates output files in the configured output or results directory:
- Analysis Mode: Generates a CSV file (
[ProjectName].csvor[BatchFileName]_results.csv) detailing initial Cognitive Complexity, suggested refactoring sequences, solver metrics, and predicted complexity reductions. - Refactoring Application Mode: Modifies target Java source files in place within the target workspace, generating execution logs (
execution.log) and a summary report (execution_results.csv) detailing success and failure metrics for each extraction.
The project follows a pipeline architecture separating the application layer, core logic, and input/output.
Application.java: Entry point for method detection, analysis, and solution generation.ApplySolutions.java: Entry point for applying saved.resultsrefactoring solutions directly to workspace source files using Eclipse JDT.Config.java: Handles parsing and validation of command-line arguments.Constants.java: Centralizes static definitions and dynamically loads environment variables fromconfig.properties.
MethodRefactoringPipeline.java: The central coordinator for analysis and solver selection.SavedSolutionProcessor.java: Executes batch refactoring transformations deterministically with position tracking and error handling.BatchCsvProcessor.java: Handles the iterative processing of pre-defined methods supplied via an input CSV.solvers.SolverType: Defines the supported optimization strategies (ILP,ES-LSF,ES-SSF).
JavaMethodProcessor.java: Uses Eclipse JDT to traverse the project's AST (Abstract Syntax Tree), calculate metrics, and identify methods requiring refactoring.
CsvResultWriter.java: Manages structured output, ensuring thread-safe writing of analysis results to the file system.
This project is distributed under the GPL-3.0 License.