Skip to content

Commit

Permalink
Remove deprecated embed() method
Browse files Browse the repository at this point in the history
The annotation processor now detects and generates the embedding options to be applied. Currently, the shortening of qualified names visitor is always registered, but this can also be optimized in the future.
  • Loading branch information
knutwannheden committed Oct 1, 2023
1 parent 72383e2 commit d597ce2
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,14 @@
import org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor;
import org.openrewrite.java.tree.J;

import java.util.Arrays;
import java.util.EnumSet;

@SuppressWarnings("unused")
public abstract class AbstractRefasterJavaVisitor extends JavaVisitor<ExecutionContext> {

@Deprecated
// to be removed as soon as annotation processor generates required options
protected J embed(J j, Cursor cursor, ExecutionContext ctx) {
return embed(j, cursor, ctx, EmbeddingOption.values());
}

@SuppressWarnings({"DataFlowIssue", "SameParameterValue"})
@SuppressWarnings("SameParameterValue")
protected J embed(J j, Cursor cursor, ExecutionContext ctx, EmbeddingOption... options) {
EnumSet<EmbeddingOption> optionsSet = options.length > 0 ? EnumSet.copyOf(Arrays.asList(options)) :
EnumSet<EmbeddingOption> optionsSet = options.length > 0 ? EnumSet.of(options[0], options) :
EnumSet.noneOf(EmbeddingOption.class);

TreeVisitor<?, ExecutionContext> visitor;
Expand All @@ -54,7 +47,7 @@ protected J embed(J j, Cursor cursor, ExecutionContext ctx, EmbeddingOption... o
return j;
}

protected enum EmbeddingOption {
public enum EmbeddingOption {
SHORTEN_NAMES, SIMPLIFY_BOOLEANS, REMOVE_PARENS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.sun.source.tree.*;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.TreeMaker;
Expand All @@ -31,7 +32,10 @@

import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.*;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -253,13 +257,24 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
maybeRemoveImports(imports, entry, descriptor, recipe);
maybeRemoveImports(staticImports, entry, descriptor, recipe);

List<String> embedOptions = new ArrayList<>();
if (getType(descriptor.afterTemplate) == JCTree.JCParens.class) {
embedOptions.add("REMOVE_PARENS");
}
// TODO check if after template contains type or member references
embedOptions.add("SHORTEN_NAMES");
if (descriptor.afterTemplate.getReturnType().type.getTag() == TypeTag.BOOLEAN) {
embedOptions.add("SIMPLIFY_BOOLEANS");
}

if (parameters.isEmpty()) {
recipe.append(" return embed(").append(after).append(".apply(getCursor(), elem.getCoordinates().replace()), getCursor(), ctx);\n");
} else {
recipe.append(" return embed(\n");
recipe.append(" ").append(after).append(".apply(getCursor(), elem.getCoordinates().replace(), ").append(parameters).append("),\n");
recipe.append(" getCursor(),\n");
recipe.append(" ctx\n");
recipe.append(" ctx,\n");
recipe.append(" ").append(String.join(", ", embedOptions)).append("\n");
recipe.append(" );\n");
}
recipe.append(" }\n");
Expand Down Expand Up @@ -308,6 +323,8 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
out.write("import org.openrewrite.java.tree.*;\n");
out.write("\n");
out.write("import java.util.*;\n");
out.write("\n");
out.write("import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;\n");

out.write("\n");

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/Matching.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ boolean before2(int i, @Matches(MethodInvocationMatcher.class) String s) {

@AfterTemplate
boolean after(String s) {
return s != null && s.length() == 0;
return (s != null && s.length() == 0);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/test/resources/refaster/MatchingRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;


public final class MatchingRecipes extends Recipe {
@Override
Expand Down Expand Up @@ -63,7 +65,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> javaVisitor = new AbstractRefasterJavaVisitor() {
final JavaTemplate before = Semantics.expression(this, "before", (@Primitive Integer i, String s) -> s.substring(i).isEmpty()).build();
final JavaTemplate before2 = Semantics.expression(this, "before2", (@Primitive Integer i, String s) -> s.substring(i).isEmpty()).build();
final JavaTemplate after = Semantics.expression(this, "after", (String s) -> s != null && s.length() == 0).build();
final JavaTemplate after = Semantics.expression(this, "after", (String s) -> (s != null && s.length() == 0)).build();

@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
Expand All @@ -75,7 +77,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
REMOVE_PARENS, SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
if ((matcher = before2.matcher(getCursor())).find()) {
Expand All @@ -85,7 +88,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
REMOVE_PARENS, SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitMethodInvocation(elem, ctx);
Expand Down
8 changes: 6 additions & 2 deletions src/test/resources/refaster/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;

import java.nio.file.Files;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -66,7 +68,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES
);
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -111,7 +114,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitMethodInvocation(elem, ctx);
Expand Down
8 changes: 6 additions & 2 deletions src/test/resources/refaster/NestedPreconditionsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.HashMap;
Expand Down Expand Up @@ -49,15 +51,17 @@ public J visitExpression(Expression elem, ExecutionContext ctx) {
return embed(
hashtable.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES
);
}
if ((matcher = linkedHashMap.matcher(getCursor())).find()) {
maybeRemoveImport("java.util.LinkedHashMap");
return embed(
hashtable.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES
);
}
return super.visitExpression(elem, ctx);
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/refaster/ParameterReuseRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;


@NonNullApi
public class ParameterReuseRecipe extends Recipe {
Expand Down Expand Up @@ -43,7 +45,8 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitBinary(elem, ctx);
Expand Down
14 changes: 10 additions & 4 deletions src/test/resources/refaster/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;

import java.util.Objects;

import static java.util.Objects.hash;
Expand Down Expand Up @@ -66,7 +68,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES
);
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -108,14 +111,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return embed(
isis.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
if ((matcher = compareZero.matcher(getCursor())).find()) {
return embed(
isis.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -162,7 +167,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES
);
}
return super.visitMethodInvocation(elem, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;


public final class ShouldSupportNestedClassesRecipes extends Recipe {
@Override
Expand Down Expand Up @@ -62,7 +64,8 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitBinary(elem, ctx);
Expand Down Expand Up @@ -102,7 +105,8 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitBinary(elem, ctx);
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/refaster/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.*;

import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;


@NonNullApi
public class UseStringIsEmptyRecipe extends Recipe {
Expand Down Expand Up @@ -43,7 +45,8 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitBinary(elem, ctx);
Expand Down

0 comments on commit d597ce2

Please sign in to comment.