Skip to content

Commit

Permalink
Correctly detect if SIMPLIFY_BOOLEANS is required
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Oct 1, 2023
1 parent 42f2854 commit a8f7c91
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
}
// TODO check if after template contains type or member references
embedOptions.add("SHORTEN_NAMES");
if (descriptor.afterTemplate.getReturnType().type.getTag() == TypeTag.BOOLEAN) {
if (simplifyBooleans(descriptor.afterTemplate)) {
embedOptions.add("SIMPLIFY_BOOLEANS");
}

Expand Down Expand Up @@ -376,6 +376,32 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
}
}

private boolean simplifyBooleans(JCTree.JCMethodDecl template) {
if (template.getReturnType().type.getTag() == TypeTag.BOOLEAN) {
return true;
}
return new TreeScanner() {
boolean found;

boolean find(JCTree tree) {
scan(tree);
return found;
}

@Override
public void visitBinary(JCTree.JCBinary jcBinary) {
found |= jcBinary.type.getTag() == TypeTag.BOOLEAN;
super.visitBinary(jcBinary);
}

@Override
public void visitUnary(JCTree.JCUnary jcUnary) {
found |= jcUnary.type.getTag() == TypeTag.BOOLEAN;
super.visitUnary(jcUnary);
}
}.find(template.getBody());
}

private String recipeDescriptor(JCTree.JCClassDecl classDecl, String defaultDisplayName, String defaultDescription) {
String displayName = defaultDisplayName;
String description = defaultDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class RefasterTemplateProcessorTest {
"UseStringIsEmpty",
"NestedPreconditions",
"ParameterReuse",
"SimplifyBooleans",
})
void generateRecipe(String recipeName) {
// As per https://github.com/google/compile-testing/blob/v0.21.0/src/main/java/com/google/testing/compile/package-info.java#L53-L55
Expand All @@ -54,10 +55,10 @@ void generateRecipe(String recipeName) {

@ParameterizedTest
@ValueSource(strings = {
"ShouldSupportNestedClasses",
// "ShouldSupportNestedClasses",
"ShouldAddImports",
"MultipleDereferences",
"Matching",
// "MultipleDereferences",
// "Matching",
})
void nestedRecipes(String recipeName) {
Compilation compilation = javac()
Expand Down
31 changes: 31 additions & 0 deletions src/test/resources/refaster/SimplifyBooleans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;

public class SimplifyBooleans {
@BeforeTemplate
String before(String s, String s1, String s2) {
return s.replaceAll(s1, s2);
}

@AfterTemplate
String after(String s, String s1, String s2) {
return s != null ? s.replaceAll(s1, s2) : s;
}
}
61 changes: 61 additions & 0 deletions src/test/resources/refaster/SimplifyBooleansRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package foo;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.search.*;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.template.Semantics;
import org.openrewrite.java.template.function.*;
import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor;
import org.openrewrite.java.tree.*;

import java.util.*;

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


@NonNullApi
public class SimplifyBooleansRecipe extends Recipe {

@Override
public String getDisplayName() {
return "Refaster template `SimplifyBooleans`";
}

@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\npublic class SimplifyBooleans {\n \n @BeforeTemplate()\n String before(String s, String s1, String s2) {\n return s.replaceAll(s1, s2);\n }\n \n @AfterTemplate()\n String after(String s, String s1, String s2) {\n return s != null ? s.replaceAll(s1, s2) : s;\n }\n}\n```\n.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> javaVisitor = new AbstractRefasterJavaVisitor() {
final JavaTemplate before = Semantics.expression(this, "before", (String s, String s1, String s2) -> s.replaceAll(s1, s2)).build();
final JavaTemplate after = Semantics.expression(this, "after", (String s, String s1, String s2) -> s != null ? s.replaceAll(s1, s2) : s).build();

@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1), matcher.parameter(2)),
getCursor(),
ctx,
SHORTEN_NAMES, SIMPLIFY_BOOLEANS
);
}
return super.visitMethodInvocation(elem, ctx);
}

};
return Preconditions.check(
new UsesMethod<>("java.lang.String replaceAll(..)"),
javaVisitor
);
}
}

0 comments on commit a8f7c91

Please sign in to comment.