Skip to content

Commit

Permalink
Clean up nullable and empty string comparisons after visit (#31)
Browse files Browse the repository at this point in the history
* Clean up nullable and empty string comparisons after visit

* Use SimplifyBooleanExpressionVisitor from rewrite-java
  • Loading branch information
timtebeek authored Aug 19, 2023
1 parent 4f89462 commit fbd48f3
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public class RefasterTemplateProcessor extends AbstractProcessor {

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

static ClassValue<List<String>> LST_TYPE_MAP = new ClassValue<List<String>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void nestedRecipes(String recipeName) {
}

@NotNull
private static Collection<File> classpath() {
static Collection<File> classpath() {
return Arrays.asList(
fileForClass(BeforeTemplate.class),
fileForClass(AfterTemplate.class),
Expand All @@ -87,4 +87,4 @@ static File fileForClass(Class<?> c) {
assert url.getProtocol().equals("file");
return new File(url.getPath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,14 @@
*/
package org.openrewrite.java.template;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
import static org.openrewrite.java.template.RefasterTemplateProcessorTest.classpath;

class TemplateProcessorTest {

Expand All @@ -54,22 +47,4 @@ void generateRecipeTemplates(String qualifier) {
.hasSourceEquivalentTo(JavaFileObjects.forResource("recipes/ShouldAddClasspathRecipe$" + qualifier + "Recipe$1_after.java"));
}

@NotNull
private static Collection<File> classpath() {
return Arrays.asList(
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(Class<?> c) {
URL url = c.getProtectionDomain().getCodeSource().getLocation();
assert url.getProtocol().equals("file");
return new File(url.getPath());
}
}
}
2 changes: 2 additions & 0 deletions src/test/resources/recipes/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(0));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -93,6 +94,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace());
}
return super.visitBinary(elem, ctx);
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/recipes/NestedPreconditionsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public J visitExpression(Expression elem, ExecutionContext ctx) {
maybeAddImport("java.util.Hashtable");
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return hashtable.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitExpression(elem, ctx);
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/recipes/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
maybeAddImport("java.util.Objects");
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -100,6 +101,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
maybeRemoveImport("java.util.Objects");
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return isis.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down Expand Up @@ -137,6 +139,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
maybeRemoveImport("java.util.Objects.hash");
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitMethodInvocation(elem, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down Expand Up @@ -93,6 +94,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/recipes/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
if ((matcher = before.matcher(getCursor())).find()) {
doAfterVisit(new org.openrewrite.java.ShortenFullyQualifiedTypeReferences().getVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor());
doAfterVisit(new org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor());
return after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
}
return super.visitBinary(elem, ctx);
Expand Down

0 comments on commit fbd48f3

Please sign in to comment.