Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (VALUE_OF.matches(mi) && mi.getArguments().size() == 1) {
Expression argument = mi.getArguments().get(0);
if (TypeUtils.isString(argument.getType()) || removeValueOfForStringConcatenation(argument)) {
if ((TypeUtils.isString(argument.getType()) && !(argument instanceof J.MethodInvocation)) || removeValueOfForStringConcatenation(argument)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't look into it too much but if we could determine here whether the result of the method invocation is actually nullable that would allow us to make more transformations

return maybeParenthesize(argument.withPrefix(mi.getPrefix()), updateCursor(mi));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,21 @@ static void method(int i) {
)
);
}

@Test
void doNotRemoveValueOfForNullableStrings() {
rewriteRun(
//language=java
java(
"""
class Test {

String method(Object some) {
return String.valueOf(some.toString());
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,28 @@ boolean notOne(A a) {
)
);
}

@Test
void correctlySimplifyNegatedTernaryEqualsNull() {
rewriteRun(
java(
"""
class A {
void doSome(String o1, String o2) {
if (!(o1 == null ? o2 == null : o1.equals(o2))) {
}
}
}
""",
"""
class A {
void doSome(String o1, String o2) {
if (o1 == null ? o2 != null : !o1.equals(o2)) {
}
}
}
"""
)
);
}
}
Loading