Skip to content

Commit

Permalink
ReplaceDuplicateStringLiterals throws NPE from 1.19.0+ #384 (#385)
Browse files Browse the repository at this point in the history
* Add test case for issue #384

#384

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Refactor `staticWithObjectArray` test method

* Guard against NPE

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
4 people authored Jan 11, 2025
1 parent 004f102 commit 871554c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
boolean privateStaticFinalVariable = isPrivateStaticFinalVariable(variable);
// `private static final String`(s) are handled separately by `FindExistingPrivateStaticFinalFields`.
if (v.getInitializer() instanceof J.Literal &&
(parentScope.getValue() instanceof J.MethodDeclaration || parentScope.getValue() instanceof J.ClassDeclaration) &&
!(privateStaticFinalVariable && ((J.Literal) v.getInitializer()).getValue() instanceof String)) {
(parentScope.getValue() instanceof J.MethodDeclaration || parentScope.getValue() instanceof J.ClassDeclaration) &&
((J.Literal) v.getInitializer()).getValue() instanceof String &&
!privateStaticFinalVariable) {
String value = (((J.Literal) v.getInitializer()).getValue()).toString();
result.existingFieldValueToFieldName.put(v.getSimpleName(), value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,24 @@ interface A {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/384")
@Test
void staticWithObjectArray() {
rewriteRun(
//language=java
java(
"""
class A {
public void method() {
Object[] args = null;
args = new Object[] {"value"};
}
}
"""
)
);
}


}

0 comments on commit 871554c

Please sign in to comment.