Skip to content

Jansi Codebase Refactoring: Static Analysis and Code Smell Remediation#321

Open
NaziaHasan12007 wants to merge 6 commits into
fusesource:masterfrom
NaziaHasan12007:feature/refactor-architecture
Open

Jansi Codebase Refactoring: Static Analysis and Code Smell Remediation#321
NaziaHasan12007 wants to merge 6 commits into
fusesource:masterfrom
NaziaHasan12007:feature/refactor-architecture

Conversation

@NaziaHasan12007

@NaziaHasan12007 NaziaHasan12007 commented Jun 22, 2026

Copy link
Copy Markdown

Overview
This Pull Request addresses technical debt and code smells across several core classes in the Jansi project. The primary goal of these changes is to improve readability, modularity, and maintainability without altering the existing public API, JNI bindings, or external functional behavior.

Key Changes
AnsiOutputStream.java:

Addressed the "Long Method" code smell in the write() method by extracting deeply nested switch-case logic into dedicated private helper methods (e.g., handleNextArg, handleOscCommand).

Removed "Duplicate Code" by introducing a @FunctionalInterface (ProcessorAction) to centralize error-handling and state-resetting logic, eliminating redundant try-catch blocks.

Centralized buffer-to-string/integer parsing to prevent magic number and offset bugs.

AnsiConsole.java & JansiLoader.java:

Mitigated "God Class" symptoms by strictly separating responsibilities and cleaning up resource management, carefully maintaining the required step-by-step native OS setup.

Kernel32.java & CLibrary.java:

Refactored structural inefficiencies to drastically lower their static analysis issue counts while ensuring the JNI bindings and native C-library calls remain perfectly intact.

Ansi.java:

Eliminated "Primitive Obsession" by replacing hardcoded magic characters and values with self-documenting static constants.

Why These Changes Were Made
As the codebase has grown, classes like AnsiOutputStream and Kernel32 accumulated high cyclomatic complexity and repetitive logic. This made the code harder to read and increased the risk of bugs when making future updates. By applying standard object-oriented refactoring patterns (like Extract Method), the architecture is now much closer to DRY (Don't Repeat Yourself) principles and is significantly easier for future contributors to navigate.

Validation and Metrics
To guarantee zero regressions, these structural changes were rigorously tested against the existing automated test suite:

Functional Parity: Ran targeted Maven tests (mvn clean test) specifically on AnsiTest, AnsiConsoleTest, Kernel32Test, CLibraryTest, and JansiLoaderTest. All tests passed (BUILD SUCCESS), confirming a 1:1 behavioral match with the original code.

Static Analysis: Verified via SonarQube. These changes successfully reduced both reliability and maintainability open issues, while achieving a 0.0% code duplication rate.

Checklist
[x] Code compiles cleanly.

[x] All targeted unit tests pass successfully.

[x] No changes were made to the external public API.

[x] Native JNI boundaries and behaviors remain unaffected.

@NaziaHasan12007 NaziaHasan12007 changed the title Refactored code removing main code smells Jansi Codebase Refactoring: Static Analysis and Code Smell Remediation Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant