Jansi Codebase Refactoring: Static Analysis and Code Smell Remediation#321
Open
NaziaHasan12007 wants to merge 6 commits into
Open
Jansi Codebase Refactoring: Static Analysis and Code Smell Remediation#321NaziaHasan12007 wants to merge 6 commits into
NaziaHasan12007 wants to merge 6 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.