Feature Request: Recipe to Extract Magic Numbers to Type-and-Value-Named Constants per Class
Introduce ReplaceMagicNumbersWithConstants recipe implementing Sonar rule java:S109. This recipe extracts numeric literals from method bodies into private static final constants to improve code readability and maintainability.
Also add ReplaceMagicNumbersWithConstantsTest with test cases verifying:
- constants are generated for numeric literals
- literals already assigned to variables or fields are ignored
- replacements preserve correctness and type consistency
What's changed?
Added new recipe: ReplaceMagicNumbersWithConstants Added new test class: ReplaceMagicNumbersWithConstantsTest
What's your motivation?
To enhance code quality and readability by enforcing Sonar rule java:S109, reducing the use of magic numbers in Java code.
Anything in particular you'd like reviewers to focus on?
Verify correct handling of numeric literals and naming of generated constants Confirm no false positives for literals already assigned to variables or fields Ensure the recipe integrates cleanly with existing static analysis infrastructure
Have you considered any alternatives or workarounds?
Manual refactoring or partial Sonar-based analysis, but these approaches lack the automation and reproducibility provided by this recipe.