Skip to content

Commit

Permalink
refactor: OpenRewrite recipe best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
2 people authored and app committed Dec 8, 2023
1 parent e179d01 commit e57dac0
Show file tree
Hide file tree
Showing 61 changed files with 224 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final JavaTemplate template = JavaTemplate.builder("private static final long serialVersionUID = 1;").build();

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
// Anonymous classes are not of interest
return method;
}

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext executionContext) {
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
// Anonymous classes are not of interest
return multiVariable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private final MethodMatcher aiMethodMatcher = new MethodMatcher("java.lang.Object equals(java.lang.Object)");

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, executionContext);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (mi.getSelect() != null && isAtomicEqualsType(mi.getSelect().getType()) && aiMethodMatcher.matches(mi)
&& TypeUtils.isOfType(mi.getSelect().getType(), mi.getArguments().get(0).getType())) {
JavaType.FullyQualified fqt = TypeUtils.asFullyQualified(mi.getSelect().getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public Expression visitExpression(Expression expression, ExecutionContext ctx) {
}

@Override
public J visitUnary(J.Unary unary, ExecutionContext executionContext) {
J.Unary un = (J.Unary) super.visitUnary(unary, executionContext);
public J visitUnary(J.Unary unary, ExecutionContext ctx) {
J.Unary un = (J.Unary) super.visitUnary(unary, ctx);
if (J.Unary.Type.Not == un.getOperator() && TypeUtils.isOfClassType(un.getExpression().getType(), "java.lang.Boolean")) {
return JavaTemplate.apply("Boolean.FALSE.equals(#{any(java.lang.Boolean)})",
updateCursor(un), un.getCoordinates().replace(), un.getExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
.imports("java.math.RoundingMode").build();

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation m = super.visitMethodInvocation(method, executionContext);
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (BIG_DECIMAL_DIVIDE.matches(m) &&
isConvertibleBigDecimalConstant(m.getArguments().get(1))) {
String roundingModeEnum = getTemplateText(m.getArguments().get(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
}

@Override
public J.Try visitTry(J.Try tryable, ExecutionContext executionContext) {
J.Try t = super.visitTry(tryable, executionContext);
public J.Try visitTry(J.Try tryable, ExecutionContext ctx) {
J.Try t = super.visitTry(tryable, ctx);
return t.withCatches(ListUtils.map(t.getCatches(), (i, aCatch) -> {
if (onlyRethrows(aCatch)) {
// if a subsequent catch is a wider exception type and doesn't rethrow, we should
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private static class CombineSemanticallyEqualCatchBlocksVisitor extends JavaVisitor<ExecutionContext> {

@Override
public J visitTry(J.Try tryable, ExecutionContext executionContext) {
J.Try t = (J.Try) super.visitTry(tryable, executionContext);
public J visitTry(J.Try tryable, ExecutionContext ctx) {
J.Try t = (J.Try) super.visitTry(tryable, ctx);
Map<J.Try.Catch, List<J.Try.Catch>> semanticallyEqualCatchesMap = new LinkedHashMap<>();
List<J.Try.Catch> catches = t.getCatches();
// Check if the try contains semantically equal catch blocks.
Expand Down Expand Up @@ -149,24 +149,24 @@ static class RemoveCatches extends JavaVisitor<ExecutionContext> {
}

@Override
public J visitMultiCatch(J.MultiCatch multiCatch, ExecutionContext executionContext) {
public J visitMultiCatch(J.MultiCatch multiCatch, ExecutionContext ctx) {
Cursor parentCursor = getCursor().dropParentUntil(is -> is instanceof J.Try.Catch || is instanceof J.Try);
if (removeCatches != null && parentCursor.getValue() instanceof J.Try.Catch) {
if (removeCatches.contains((J.Try.Catch) parentCursor.getValue())) {
return null;
}
}
return super.visitMultiCatch(multiCatch, executionContext);
return super.visitMultiCatch(multiCatch, ctx);
}

@Override
public J visitCatch(J.Try.Catch _catch, ExecutionContext executionContext) {
public J visitCatch(J.Try.Catch _catch, ExecutionContext ctx) {
if (removeCatches != null) {
if (removeCatches.contains(_catch)) {
return null;
}
}
return super.visitCatch(_catch, executionContext);
return super.visitCatch(_catch, ctx);
}
}

Expand All @@ -184,29 +184,29 @@ private static class CombineCatches extends JavaVisitor<ExecutionContext> {
}

@Override
public J visitMultiCatch(J.MultiCatch multiCatch, ExecutionContext executionContext) {
J.MultiCatch m = (J.MultiCatch) super.visitMultiCatch(multiCatch, executionContext);
public J visitMultiCatch(J.MultiCatch multiCatch, ExecutionContext ctx) {
J.MultiCatch m = (J.MultiCatch) super.visitMultiCatch(multiCatch, ctx);
Cursor parentCursor = getCursor().dropParentUntil(is -> is instanceof J.Try.Catch || is instanceof J.Try);
if (parentCursor.getValue() instanceof J.Try.Catch) {
J.Try.Catch parent = parentCursor.getValue();
if (parent == scope) {
List<JRightPadded<NameTree>> combinedCatches = combineEquivalentCatches();
m = maybeAutoFormat(m, m.getPadding().withAlternatives(combinedCatches), executionContext);
m = maybeAutoFormat(m, m.getPadding().withAlternatives(combinedCatches), ctx);
}
}
return m;
}

@Override
public J visitCatch(J.Try.Catch _catch, ExecutionContext executionContext) {
J.Try.Catch c = (J.Try.Catch) super.visitCatch(_catch, executionContext);
public J visitCatch(J.Try.Catch _catch, ExecutionContext ctx) {
J.Try.Catch c = (J.Try.Catch) super.visitCatch(_catch, ctx);
if (c == scope && !isMultiCatch(c)) {
if (c.getParameter().getTree().getTypeExpression() != null) {
List<JRightPadded<NameTree>> combinedCatches = combineEquivalentCatches();
c = maybeAutoFormat(c, c.withParameter(c.getParameter()
.withTree(c.getParameter().getTree()
.withTypeExpression(new J.MultiCatch(Tree.randomId(), Space.EMPTY, Markers.EMPTY, combinedCatches)))),
executionContext);
ctx);
}
}
return c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) {
}

@Override
public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
J.Block b = super.visitBlock(block, executionContext);
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
J.Block b = super.visitBlock(block, ctx);
AtomicBoolean foundControlFlowRequiringReformatting = new AtomicBoolean(false);
return b.withStatements(ListUtils.map(b.getStatements(), (i, statement) -> {
if (foundControlFlowRequiringReformatting.get() || shouldReformat(statement)) {
foundControlFlowRequiringReformatting.set(true);
return (Statement) new TabsAndIndentsVisitor<>(tabsAndIndentsStyle).visit(statement, executionContext, getCursor());
return (Statement) new TabsAndIndentsVisitor<>(tabsAndIndentsStyle).visit(statement, ctx, getCursor());
}
return statement;
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private static class ExternalizableHasNoArgsConstructorVisitor extends JavaIsoVi
private static final JavaType externalizableType = JavaType.buildType("java.io.Externalizable");

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (TypeUtils.isAssignableTo(externalizableType, cd.getType())) {
boolean hasFinalUninitializedFieldVar = false;
Integer firstMethodDeclarationIndex = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext p) {
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, p);
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, ctx);

// if this already has "final", we don't need to bother going any further; we're done
if (mv.hasModifier(J.Modifier.Type.Final)) {
Expand Down Expand Up @@ -80,7 +80,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
mv = autoFormat(
mv.withModifiers(
ListUtils.concat(mv.getModifiers(), new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, null, J.Modifier.Type.Final, Collections.emptyList()))
), p);
), ctx);
}

return mv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public MethodDeclaration visitMethodDeclaration(MethodDeclaration methodDeclaration, ExecutionContext executionContext) {
MethodDeclaration declarations = super.visitMethodDeclaration(methodDeclaration, executionContext);
public MethodDeclaration visitMethodDeclaration(MethodDeclaration methodDeclaration, ExecutionContext ctx) {
MethodDeclaration declarations = super.visitMethodDeclaration(methodDeclaration, ctx);

if (isWrongKind(methodDeclaration) ||
isEmpty(declarations.getParameters()) ||
Expand Down Expand Up @@ -92,7 +92,7 @@ private void checkIfAssigned(final AtomicBoolean assigned, final Statement p) {
}

@Override
public boolean isAcceptable(final SourceFile sourceFile, final ExecutionContext executionContext) {
public boolean isAcceptable(final SourceFile sourceFile, final ExecutionContext ctx) {
return sourceFile instanceof JavaSourceFile;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ private static class FixPrintfExpressionsVisitor extends JavaIsoVisitor<Executio
MethodMatcher sFormattedMatcher = new MethodMatcher("java.lang.String formatted(..)");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation mi = super.visitMethodInvocation(method, executionContext);
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (sFormatMatcher.matches(mi) || sFormattedMatcher.matches(mi)) {
boolean isStringFormattedExpression = false;
J.Literal fmtArg = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public J visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
f = f.withBody((Statement) new JavaVisitor<ExecutionContext>() {
@Nullable
@Override
public J visit(@Nullable Tree tree, ExecutionContext executionContext) {
return tree == unary ? null : super.visit(tree, executionContext);
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
return tree == unary ? null : super.visit(tree, ctx);
}
}.visit(f.getBody(), ctx));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public Duration getEstimatedEffortPerOccurrence() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
J.Block bl = super.visitBlock(block, executionContext);
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
J.Block bl = super.visitBlock(block, ctx);
List<Statement> statements = bl.getStatements();
if (statements.size() > 1) {
String identReturned = identReturned(statements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

return Preconditions.check(preconditions, new JavaVisitor<ExecutionContext>() {
@Override
public @Nullable J postVisit(J tree, ExecutionContext executionContext) {
J result = super.postVisit(tree, executionContext);
public @Nullable J postVisit(J tree, ExecutionContext ctx) {
J result = super.postVisit(tree, ctx);
InstanceOfPatternReplacements original = getCursor().getMessage("flowTypeScope");
if (original != null && !original.isEmpty()) {
return UseInstanceOfPatternMatching.refactor(result, original, getCursor().getParentOrThrow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new JavaFileChecker<>(),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext executionContext) {
J.Lambda l = super.visitLambda(lambda, executionContext);
public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) {
J.Lambda l = super.visitLambda(lambda, ctx);
if (lambda.getBody() instanceof J.Block) {
List<Statement> statements = ((J.Block) lambda.getBody()).getStatements();
if (statements.size() == 1 && statements.get(0) instanceof J.Return) {
Expand All @@ -59,11 +59,11 @@ public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext executionContext)
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (hasLambdaArgument(method) && hasMethodOverloading(method)) {
return method;
}
return super.visitMethodInvocation(method, executionContext);
return super.visitMethodInvocation(method, ctx);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Map<String, String> getInitialValue(ExecutionContext ctx) {
public TreeVisitor<?, ExecutionContext> getScanner(Map<String, String> acc) {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public @Nullable J preVisit(J tree, ExecutionContext executionContext) {
public @Nullable J preVisit(J tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
J.Package pkg = cu.getPackageDeclaration();
Expand All @@ -69,7 +69,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(Map<String, String> acc) {
}
stopAfterPreVisit();
}
return super.preVisit(tree, executionContext);
return super.preVisit(tree, ctx);
}
};
}
Expand Down
Loading

0 comments on commit e57dac0

Please sign in to comment.