Skip to content

Commit

Permalink
polish and fast return
Browse files Browse the repository at this point in the history
  • Loading branch information
MBoegers committed Jan 29, 2025
1 parent 19d9980 commit 3908319
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import java.util.List;
import java.util.Set;

import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.Collections.*;
import static org.openrewrite.staticanalysis.csharp.CSharpFileChecker.isInstanceOfCs;

public class CatchClauseOnlyRethrows extends Recipe {
Expand Down Expand Up @@ -98,10 +97,13 @@ private List<JavaType> getJavaTypes(J.Try.Catch next) {
JavaType.MultiCatch multiCatch = (JavaType.MultiCatch) next.getParameter().getType();
return multiCatch.getThrowableTypes();
}
return singletonList(next.getParameter().getType());
return next.getParameter().getType() != null ? singletonList(next.getParameter().getType()) : emptyList();
}

private boolean isAnyAssignableTo(List<JavaType> nextTypes, List<JavaType> aCatchTypes) {
if (nextTypes.isEmpty()) {
return false;
}
for (JavaType aCatchType : aCatchTypes) {
for (JavaType nextType : nextTypes) {
if (TypeUtils.isAssignableTo(nextType, aCatchType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ public Duration getEstimatedEffortPerOccurrence() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {

return new JavaVisitor<ExecutionContext>() {
@Override
public J visitForControl(J.ForLoop.Control control, ExecutionContext ctx) {
if (control.getCondition() instanceof J.Binary) {
J.Binary condition = (J.Binary) control.getCondition();

if (isNumericalType(condition) &&
control.getUpdate().size() == 1 &&
control.getUpdate().get(0) instanceof J.Unary) {
control.getUpdate().size() == 1 &&
control.getUpdate().get(0) instanceof J.Unary) {
J.Unary update = (J.Unary) control.getUpdate().get(0);
if (updatedExpressionInConditional(update.getExpression(), condition)) {
if (condition.getOperator() == J.Binary.Type.NotEqual) {
Expand Down Expand Up @@ -104,9 +105,9 @@ private boolean updatedExpressionInConditional(Expression updatedExpression, J.B
private boolean isNumericalType(J.Binary condition) {
JavaType type = condition.getRight().getType();
return type == JavaType.Primitive.Short ||
type == JavaType.Primitive.Byte ||
type == JavaType.Primitive.Int ||
type == JavaType.Primitive.Long;
type == JavaType.Primitive.Byte ||
type == JavaType.Primitive.Int ||
type == JavaType.Primitive.Long;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static java.util.Collections.singletonList;
import static org.openrewrite.java.Assertions.java;

@SuppressWarnings("ALL")
Expand Down

0 comments on commit 3908319

Please sign in to comment.