Skip to content

Commit

Permalink
Add doAfterVisit(UnnecessaryParentheses) to generated recipes (#22)
Browse files Browse the repository at this point in the history
* Add doAfterVisit(UnnecessaryParentheses) to generated recipes

* Restore missing imports

* Adopt org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor
  • Loading branch information
timtebeek authored Aug 18, 2023
1 parent 16edf05 commit ad66be3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public class RefasterTemplateProcessor extends AbstractProcessor {
PRIMITIVE_TYPE_MAP.put(void.class.getName(), Void.class.getSimpleName());
}

static Set<String> DO_AFTER_VISIT = Stream.of(
"new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor()",
"new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor()"
).collect(Collectors.toCollection(LinkedHashSet::new));

static ClassValue<List<String>> LST_TYPE_MAP = new ClassValue<List<String>>() {
@Override
protected List<String> computeValue(Class<?> type) {
Expand Down Expand Up @@ -313,7 +318,9 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
recipe.append(" maybeAddImport(\"" + import_.substring(0, dot) + "\", \"" + import_.substring(dot + 1) + "\");\n");
}
}
recipe.append(" doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());\n");
for (String doAfterVisit : DO_AFTER_VISIT) {
recipe.append(" doAfterVisit(" + doAfterVisit + ");\n");
}
if (parameters.isEmpty()) {
recipe.append(" return " + after + ".apply(getCursor(), elem.getCoordinates().replace());\n");
} else {
Expand Down Expand Up @@ -344,7 +351,6 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
out.write("import org.openrewrite.TreeVisitor;\n");
out.write("import org.openrewrite.java.JavaTemplate;\n");
out.write("import org.openrewrite.java.JavaVisitor;\n");
out.write("import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;\n");
out.write("import org.openrewrite.java.template.Primitive;\n");
out.write("import org.openrewrite.java.tree.*;\n");
if (outerClassRequired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,17 @@ void nestedRecipes(String recipeName) {
@NotNull
private static Collection<File> classpath() {
return Arrays.asList(
fileForClass(BeforeTemplate.class.getName()),
fileForClass(AfterTemplate.class.getName()),
fileForClass(com.sun.tools.javac.tree.JCTree.class.getName()),
fileForClass(org.openrewrite.Recipe.class.getName()),
fileForClass(org.openrewrite.java.JavaTemplate.class.getName()),
fileForClass(org.slf4j.Logger.class.getName())
fileForClass(BeforeTemplate.class),
fileForClass(AfterTemplate.class),
fileForClass(com.sun.tools.javac.tree.JCTree.class),
fileForClass(org.openrewrite.Recipe.class),
fileForClass(org.openrewrite.java.JavaTemplate.class),
fileForClass(org.slf4j.Logger.class)
);
}

// As per https://github.com/google/auto/blob/auto-value-1.10.2/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java#L99
static File fileForClass(String className) {
Class<?> c;
try {
c = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
static File fileForClass(Class<?> c) {
URL url = c.getProtectionDomain().getCodeSource().getLocation();
assert url.getProtocol().equals("file");
return new File(url.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,17 @@ void generateRecipeTemplates(String qualifier) {
@NotNull
private static Collection<File> classpath() {
return Arrays.asList(
fileForClass(BeforeTemplate.class.getName()),
fileForClass(AfterTemplate.class.getName()),
fileForClass(com.sun.tools.javac.tree.JCTree.class.getName()),
fileForClass(org.openrewrite.Recipe.class.getName()),
fileForClass(org.openrewrite.java.JavaTemplate.class.getName()),
fileForClass(org.slf4j.Logger.class.getName())
fileForClass(BeforeTemplate.class),
fileForClass(AfterTemplate.class),
fileForClass(com.sun.tools.javac.tree.JCTree.class),
fileForClass(org.openrewrite.Recipe.class),
fileForClass(org.openrewrite.java.JavaTemplate.class),
fileForClass(org.slf4j.Logger.class)
);
}

// As per https://github.com/google/auto/blob/auto-value-1.10.2/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java#L99
static File fileForClass(String className) {
Class<?> c;
try {
c = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
static File fileForClass(Class<?> c) {
URL url = c.getProtectionDomain().getCodeSource().getLocation();
assert url.getProtocol().equals("file");
return new File(url.getPath());
Expand Down
7 changes: 4 additions & 3 deletions src/test/resources/recipes/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand Down Expand Up @@ -54,7 +53,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(0));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -86,7 +86,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace());
}
return super.visitBinary(elem, ctx);
Expand Down
7 changes: 4 additions & 3 deletions src/test/resources/recipes/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand Down Expand Up @@ -58,7 +57,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
maybeAddImport("java.util.Objects");
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -92,7 +92,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = equals.matcher(getCursor())).find() || (matcher = compareZero.matcher(getCursor())).find()) {
maybeRemoveImport("java.util.Objects");
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
return isis.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand Down Expand Up @@ -54,7 +53,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down Expand Up @@ -86,7 +86,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down
5 changes: 2 additions & 3 deletions src/test/resources/recipes/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

Expand All @@ -31,7 +30,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitBinary(J.Binary elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand All @@ -40,4 +40,3 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
};
}
}

0 comments on commit ad66be3

Please sign in to comment.