Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ClassCastExceptions in certain cases for AssertTrueEqualsToAssertEquals and AssertFalseEqualsToAssertNotEquals #287

Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -23,7 +23,6 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;

import java.util.List;
import java.util.function.Supplier;

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

J.MethodInvocation methodInvocation = getMethodInvocation(method);
J.MethodInvocation s = (J.MethodInvocation)methodInvocation.getArguments().get(0);
J.MethodInvocation s = (J.MethodInvocation) method.getArguments().get(0);
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)};
JavaTemplate t;
if (mi.getSelect() == null) {
Expand All @@ -86,22 +84,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
return mi;
}

private J.MethodInvocation getMethodInvocation(Expression expr){
List<J> s = expr.getSideEffects();
return ((J.MethodInvocation) s.get(0));
}

private boolean isEquals(Expression expr) {
List<J> s = expr.getSideEffects();

if (s.isEmpty()){
if (!(expr instanceof J.MethodInvocation)) {
return false;
}

J.MethodInvocation methodInvocation = getMethodInvocation(expr);

return "equals".equals(methodInvocation.getName().getSimpleName());
J.MethodInvocation methodInvocation = (J.MethodInvocation) expr;

return "equals".equals(methodInvocation.getName().getSimpleName())
&& methodInvocation.getArguments().size() == 1;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;

import java.util.List;
import java.util.function.Supplier;

public class AssertTrueEqualsToAssertEquals extends Recipe {
Expand Down Expand Up @@ -64,8 +63,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
} else {
sb.append("Assertions.");
}
J.MethodInvocation methodInvocation = getMethodInvocation(mi);
J.MethodInvocation s = (J.MethodInvocation)methodInvocation.getArguments().get(0);
J.MethodInvocation s = (J.MethodInvocation) mi.getArguments().get(0);
sb.append("assertEquals(#{any(java.lang.Object)},#{any(java.lang.Object)}");
Object[] args;
if (mi.getArguments().size() == 2) {
Expand All @@ -90,23 +88,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
return mi;
}

private J.MethodInvocation getMethodInvocation(Expression expr){
List<J> s = expr.getSideEffects();
return ((J.MethodInvocation) s.get(0));
}

private boolean isEquals(Expression expr) {
List<J> s = expr.getSideEffects();

if (s.isEmpty()){
if (!(expr instanceof J.MethodInvocation)) {
return false;
}

J.MethodInvocation methodInvocation = getMethodInvocation(expr);
J.MethodInvocation methodInvocation = (J.MethodInvocation) expr;

return "equals".equals(methodInvocation.getName().getSimpleName())
&& methodInvocation.getArguments().size() == 1;

}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,26 @@ void test() {
)
);
}

@SuppressWarnings("ConstantConditions")
@Test
void retainEqualsAndedWithSomethingElse() {
//language=java
rewriteRun(
java(
"""
import java.util.Arrays;
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
String a = "a";
String b = "b";
Assertions.assertFalse(a.equals(b) && a.length() > 0);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,26 @@ void test() {
)
);
}

@SuppressWarnings("ConstantConditions")
@Test
void retainEqualsAndedWithSomethingElse() {
//language=java
rewriteRun(
java(
"""
import java.util.Arrays;
import org.junit.jupiter.api.Assertions;

public class Test {
void test() {
String a = "a";
String b = "b";
Assertions.assertTrue(a.equals(b) && a.length() > 0);
}
}
"""
)
);
}
}