openrewrite / openrewrite/rewrite
`ReplaceConstant` may require adding explicit type cast
Open
Nobody has claimed this yet.
enhancement
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 571
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
This is a corner case and also rather unlikely to be seen in the wild, but when a constant is replaced with null the value may require an explicit type cast. See this test case where a type cast is required due to method overloads:
@Test
void addsExplicitCastWhenReplacingWithNullAndOverloadRequiresIt() {
assertThrows(AssertionError.class, () ->
rewriteRun(
spec -> spec.recipe(new ReplaceConstant("com.abc.A", "VAR", "null")),
java(
"""
package com.abc;
class A {
public static final String VAR = "default";
private String m1() {
return m2(VAR);
}
private String m2(String s) {
return s;
}
private String m2(Integer i) {
return i.toString();
}
}
""",
"""
package com.abc;
class A {
public static final String VAR = "default";
private String m1() {
return m2((String) null);
}
private String m2(String s) {
return s;
}
private String m2(Integer i) {
return i.toString();
}
}
"""
)
));
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the ReplaceConstant recipe and use the supplied addsExplicitCastWhenReplacingWithNullAndOverloadRequiresIt test case as the entry point. Reproduce the overload scenario, then verify that replacing the constant with null produces an explicit String cast and that the test passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100