Skip to content

Commit 64bd7e1

Browse files
authored
Fixed ClassCastExceptions in certain cases for AssertTrueEqualsToAssertEquals and AssertFalseEqualsToAssertNotEquals (#287)
1 parent cfb9cbe commit 64bd7e1

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseEqualsToAssertNotEquals.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.openrewrite.java.tree.Expression;
2424
import org.openrewrite.java.tree.J;
2525

26-
import java.util.List;
2726
import java.util.function.Supplier;
2827

2928
public class AssertFalseEqualsToAssertNotEquals extends Recipe {
@@ -70,8 +69,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
7069
}
7170
sb.append(")");
7271

73-
J.MethodInvocation methodInvocation = getMethodInvocation(method);
74-
J.MethodInvocation s = (J.MethodInvocation)methodInvocation.getArguments().get(0);
72+
J.MethodInvocation s = (J.MethodInvocation) method.getArguments().get(0);
7573
args = method.getArguments().size() == 2 ? new Object[]{s.getSelect(), s.getArguments().get(0), mi.getArguments().get(1)} : new Object[]{s.getSelect(), s.getArguments().get(0)};
7674
JavaTemplate t;
7775
if (mi.getSelect() == null) {
@@ -86,22 +84,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
8684
return mi;
8785
}
8886

89-
private J.MethodInvocation getMethodInvocation(Expression expr){
90-
List<J> s = expr.getSideEffects();
91-
return ((J.MethodInvocation) s.get(0));
92-
}
93-
9487
private boolean isEquals(Expression expr) {
95-
List<J> s = expr.getSideEffects();
96-
97-
if (s.isEmpty()){
88+
if (!(expr instanceof J.MethodInvocation)) {
9889
return false;
9990
}
10091

101-
J.MethodInvocation methodInvocation = getMethodInvocation(expr);
102-
103-
return "equals".equals(methodInvocation.getName().getSimpleName());
92+
J.MethodInvocation methodInvocation = (J.MethodInvocation) expr;
10493

94+
return "equals".equals(methodInvocation.getName().getSimpleName())
95+
&& methodInvocation.getArguments().size() == 1;
10596
}
10697
};
10798
}

src/main/java/org/openrewrite/java/testing/cleanup/AssertTrueEqualsToAssertEquals.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.openrewrite.java.tree.Expression;
2424
import org.openrewrite.java.tree.J;
2525

26-
import java.util.List;
2726
import java.util.function.Supplier;
2827

2928
public class AssertTrueEqualsToAssertEquals extends Recipe {
@@ -64,8 +63,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6463
} else {
6564
sb.append("Assertions.");
6665
}
67-
J.MethodInvocation methodInvocation = getMethodInvocation(mi);
68-
J.MethodInvocation s = (J.MethodInvocation)methodInvocation.getArguments().get(0);
66+
J.MethodInvocation s = (J.MethodInvocation) mi.getArguments().get(0);
6967
sb.append("assertEquals(#{any(java.lang.Object)},#{any(java.lang.Object)}");
7068
Object[] args;
7169
if (mi.getArguments().size() == 2) {
@@ -90,23 +88,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
9088
return mi;
9189
}
9290

93-
private J.MethodInvocation getMethodInvocation(Expression expr){
94-
List<J> s = expr.getSideEffects();
95-
return ((J.MethodInvocation) s.get(0));
96-
}
97-
9891
private boolean isEquals(Expression expr) {
99-
List<J> s = expr.getSideEffects();
100-
101-
if (s.isEmpty()){
92+
if (!(expr instanceof J.MethodInvocation)) {
10293
return false;
10394
}
10495

105-
J.MethodInvocation methodInvocation = getMethodInvocation(expr);
96+
J.MethodInvocation methodInvocation = (J.MethodInvocation) expr;
10697

10798
return "equals".equals(methodInvocation.getName().getSimpleName())
10899
&& methodInvocation.getArguments().size() == 1;
109-
110100
}
111101
};
112102
}

src/test/java/org/openrewrite/java/testing/cleanup/AssertFalseEqualToAssertNotEqualsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,26 @@ void test() {
9999
)
100100
);
101101
}
102+
103+
@SuppressWarnings("ConstantConditions")
104+
@Test
105+
void retainEqualsAndedWithSomethingElse() {
106+
//language=java
107+
rewriteRun(
108+
java(
109+
"""
110+
import java.util.Arrays;
111+
import org.junit.jupiter.api.Assertions;
112+
113+
public class Test {
114+
void test() {
115+
String a = "a";
116+
String b = "b";
117+
Assertions.assertFalse(a.equals(b) && a.length() > 0);
118+
}
119+
}
120+
"""
121+
)
122+
);
123+
}
102124
}

src/test/java/org/openrewrite/java/testing/cleanup/AssertTrueEqualsToAssertEqualsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,26 @@ void test() {
124124
)
125125
);
126126
}
127+
128+
@SuppressWarnings("ConstantConditions")
129+
@Test
130+
void retainEqualsAndedWithSomethingElse() {
131+
//language=java
132+
rewriteRun(
133+
java(
134+
"""
135+
import java.util.Arrays;
136+
import org.junit.jupiter.api.Assertions;
137+
138+
public class Test {
139+
void test() {
140+
String a = "a";
141+
String b = "b";
142+
Assertions.assertTrue(a.equals(b) && a.length() > 0);
143+
}
144+
}
145+
"""
146+
)
147+
);
148+
}
127149
}

0 commit comments

Comments
 (0)