Skip to content

Commit

Permalink
Correctly preserve prefix in StringLiteralEquality
Browse files Browse the repository at this point in the history
Fixes: #130
  • Loading branch information
knutwannheden committed Jun 28, 2023
1 parent 2127a09 commit 0f13b29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private boolean isStringLiteral(Expression expression) {
private J.MethodInvocation asEqualsMethodInvocation(J.Binary binary) {
return new J.MethodInvocation(
Tree.randomId(),
binary.getPrefix(),
Space.EMPTY,
Markers.EMPTY,
new JRightPadded<>(binary.getLeft().withPrefix(Space.EMPTY), Space.EMPTY, Markers.EMPTY),
null,
Expand Down Expand Up @@ -109,10 +109,9 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
if (isStringLiteral(binary.getLeft()) || isStringLiteral(binary.getRight())) {
J after = null;
if (binary.getOperator() == J.Binary.Type.Equal) {
after = asEqualsMethodInvocation(binary);
after = asEqualsMethodInvocation(binary).withPrefix(binary.getPrefix());
} else if (binary.getOperator() == J.Binary.Type.NotEqual) {
J.MethodInvocation mi = asEqualsMethodInvocation(binary);
after = asNegatedUnary(mi);
after = asNegatedUnary(asEqualsMethodInvocation(binary)).withPrefix(binary.getPrefix());
}
if (after != null) {
doAfterVisit(new EqualsAvoidsNull().getVisitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String getString() {
}
public void method(String str) {
if (str == "test") ;
if (str.length() > 1 && str == "test") ;
if ("test" == str) ;
if ("test" == "test") ;
if ("test" == new String("test")) ;
Expand All @@ -78,7 +78,7 @@ public String getString() {
}
public void method(String str) {
if ("test".equals(str)) ;
if (str.length() > 1 && "test".equals(str)) ;
if ("test".equals(str)) ;
if ("test".equals("test")) ;
if ("test".equals(new String("test"))) ;
Expand Down Expand Up @@ -113,7 +113,7 @@ public String getString() {
}
public void method(String str) {
if (str != "test") ;
if (str.length() > 1 && str != "test") ;
if ("test" != str) ;
if ("test" != "test") ;
if ("test" != new String("test")) ;
Expand All @@ -131,7 +131,7 @@ public String getString() {
}
public void method(String str) {
if (!"test".equals(str)) ;
if (str.length() > 1 && !"test".equals(str)) ;
if (!"test".equals(str)) ;
if (!"test".equals("test")) ;
if (!"test".equals(new String("test"))) ;
Expand Down

0 comments on commit 0f13b29

Please sign in to comment.