Skip to content

Commit

Permalink
Guard against ClassCastException, convert tabs to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Jan 22, 2023
1 parent b08bed5 commit 60a1851
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation m, ExecutionC
if (!isTrue && !IS_FALSE.matches(method)) {
return method;
}

if (!(method.getSelect() instanceof J.MethodInvocation)) {
return method;
return method;
}

if (!ASSERT_THAT.matches((J.MethodInvocation)method.getSelect())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,37 @@ public static class UseExplicitSizeVisitor extends JavaIsoVisitor<ExecutionConte

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation m, ExecutionContext ctx) {
J.MethodInvocation method = super.visitMethodInvocation(m, ctx);
if (!IS_EQUAL_TO.matches(method)) {
J.MethodInvocation method = super.visitMethodInvocation(m, ctx);
if (!IS_EQUAL_TO.matches(method)) {
return method;
}


if (!(method.getSelect() instanceof J.MethodInvocation)) {
return method;
}

if (!ASSERT_THAT.matches((J.MethodInvocation)method.getSelect())) {
return method;
}

J.MethodInvocation assertThat = (MethodInvocation) method.getSelect();

if (!(assertThat.getArguments().get(0) instanceof J.MethodInvocation)) {
return method;
return method;
}

J.MethodInvocation size = (J.MethodInvocation) assertThat.getArguments().get(0);

if (!SIZE.matches(size)) {
return method;
return method;
}

Expression list = size.getSelect();
Expression expectedSize = method.getArguments().get(0);

String template = "assertThat(#{any(java.util.List)}).hasSize(#{any()});";
return method.withTemplate(
JavaTemplate.builder(this::getCursor, template)
JavaTemplate.builder(this::getCursor, template)
.javaParser(ASSERTJ_JAVA_PARSER)
.build(),
method.getCoordinates().replace(),
Expand Down

0 comments on commit 60a1851

Please sign in to comment.