Skip to content

Commit

Permalink
Support void-typed templates
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 14, 2023
1 parent 8eba3fc commit b472f3a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ private String joinPreconditions(Collection<String> preconditions, String op, in
}

private static String lambdaCastType(Class<? extends JCTree> type, JCTree.JCMethodDecl method) {
if (type == JCTree.JCMethodInvocation.class && method.getBody().getStatements().last() instanceof JCTree.JCExpressionStatement) {
if (type == JCTree.JCMethodInvocation.class
&& method.getBody().getStatements().last() instanceof JCTree.JCExpressionStatement
&& !(method.getReturnType().type instanceof Type.JCVoidType)) {
return "";
}
int paramCount = method.params.size();
Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/recipes/MultipleDereferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MultipleDereferences {

public static class VoidType {
@BeforeTemplate
void before(Path p) throws IOException {
Files.delete(p);
}

@AfterTemplate
void after(Path p)throws IOException {
Files.delete(p);
}
}

public static class StringIsEmpty {
@BeforeTemplate
boolean before(String s) {
Expand Down
53 changes: 52 additions & 1 deletion src/test/resources/recipes/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.util.Arrays;
import java.util.List;

import java.nio.file.Files;
import java.io.IOException;
import java.nio.file.Path;

public final class MultipleDereferencesRecipes extends Recipe {

Expand All @@ -33,10 +36,57 @@ public String getDescription() {
@Override
public List<Recipe> getRecipeList() {
return Arrays.asList(
new VoidTypeRecipe(),
new StringIsEmptyRecipe(),
new EqualsItselfRecipe()
);
}
public static class VoidTypeRecipe extends Recipe {

@Override
public String getDisplayName() {
return "Refaster template `MultipleDereferences.VoidType`";
}

@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\npublic static class VoidType {\n \n @BeforeTemplate()\n void before(Path p) throws IOException {\n Files.delete(p);\n }\n \n @AfterTemplate()\n void after(Path p) throws IOException {\n Files.delete(p);\n }\n}\n```\n.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> javaVisitor = new AbstractRefasterJavaVisitor() {

Supplier<JavaTemplate> before = memoize(() -> JavaTemplate.compile(this, "before", (JavaTemplate.P1<?>) (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build());

Supplier<JavaTemplate> after = memoize(() -> JavaTemplate.compile(this, "after", (JavaTemplate.P1<?>) (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build());

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

};
return Preconditions.check(
Preconditions.and(
new UsesType<>("java.io.IOException", true),
new UsesType<>("java.nio.file.Files", true),
new UsesType<>("java.nio.file.Path", true),
new UsesMethod<>("java.nio.file.Files delete(..)")
),
javaVisitor
);
}
}

public static class StringIsEmptyRecipe extends Recipe {

@Override
Expand Down Expand Up @@ -73,7 +123,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
new UsesMethod<>("java.lang.String isEmpty(..)"),
javaVisitor);
javaVisitor
);
}
}

Expand Down

0 comments on commit b472f3a

Please sign in to comment.