Add S2209/S3252 recipe: Static members via class name#836
Merged
Conversation
Implements OpenRewrite recipe to replace static field/method access through instance references with access via the declaring class name. Transforms instance.staticField to ClassName.staticField and instance.staticMethod() to ClassName.staticMethod(). Covers both RSPEC-S2209 (MAJOR) and RSPEC-S3252 rules.
Handle nested class names (e.g. Outer.Inner) by building a J.FieldAccess chain instead of using getClassName() as a single identifier. Add recursive side-effect check for chained field access to avoid dropping method calls in chains like getObj().field.STATIC.
Replace hand-rolled nested class handling with the existing utility, which correctly handles type attribution, parameterized types, and local classes.
Comment on lines
+380
to
+396
| void doNotChangeChainedFieldAccessWithSideEffect() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| class MyClass { | ||
| static int COUNT = 0; | ||
| MyClass field; | ||
| static MyClass getInstance() { return new MyClass(); } | ||
| void foo() { | ||
| int x = getInstance().field.COUNT; | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } |
Contributor
There was a problem hiding this comment.
Isn't the starting state going to cause a null pointer exception? I suppose it doesn't necessarily matter for detection here, but we know we never set field to anything.
Member
Author
There was a problem hiding this comment.
Indeed it's a heavily reduced example, but the compiler allows it, and it's enough for detection. Thanks for the ask though!
steve-aom-elliott
approved these changes
Apr 13, 2026
Contributor
steve-aom-elliott
left a comment
There was a problem hiding this comment.
I think it makes sense. I suppose to further be able understand whether the method invocation chained into field access would have been safe to swap out with just a field access we would need more detailed analysis of the flow present.
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.
Summary
Implements OpenRewrite recipe to fix SonarQube rules S2209 (MAJOR) and S3252 (MINOR). Replaces static field/method access through instance references with access via the declaring class name (e.g.,
instance.staticField→ClassName.staticField,instance.staticMethod()→ClassName.staticMethod()).Includes JavaIsoVisitor implementation handling field access and method invocations, with conservative side-effect preservation (skips method calls and
newexpressions as select targets). Full test coverage with 10 test cases.