Skip to content

Commit

Permalink
Support nested classes with static inner classes
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
timtebeek committed Aug 11, 2023
1 parent 0073350 commit 161fe2a
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 15 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dependencies {

// Needed to run tests
testImplementation("org.junit.jupiter:junit-jupiter-api:latest.release")
testImplementation("org.junit.jupiter:junit-jupiter-params:latest.release")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:latest.release")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
if (outerClassRequired) {
out.write("public final class " + className.substring(className.lastIndexOf('.') + 1) + " {\n");
for (String r : recipes) {
out.write(r.replaceAll("(?m)^(.+)$", " $1"));
out.write(r.replaceAll("(?m)^(.+)$", " $1")
.replace("public final class", "public static final class"));
out.write('\n');
}
out.write("}\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.testing.compile.JavaFileObjects;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.File;
import java.net.URL;
Expand All @@ -32,29 +34,36 @@

class RefasterTemplateProcessorTest {

@Test
void generateRecipe() {
@ParameterizedTest
@ValueSource(strings = {
"UseStringIsEmpty",
"ShouldAddImports",
})
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
Compilation compilation = javac()
.withProcessors(new RefasterTemplateProcessor())
.withClasspath(classpath())
.compile(JavaFileObjects.forResource("recipes/UseStringIsEmpty.java"));
.compile(JavaFileObjects.forResource("recipes/" + recipeName + ".java"));
assertThat(compilation).succeeded();
compilation.generatedSourceFiles().forEach(System.out::println);
assertThat(compilation)
.generatedSourceFile("org/openrewrite/java/migrate/lang/UseStringIsEmptyRecipe")
.hasSourceEquivalentTo(JavaFileObjects.forResource("recipes/UseStringIsEmptyRecipe.java"));
.generatedSourceFile("foo/" + recipeName + "Recipe")
.hasSourceEquivalentTo(JavaFileObjects.forResource("recipes/" + recipeName + "Recipe.java"));
}

@Test
void shouldAddImports(){
void nestedRecipes() {
String recipeName = "ShouldSupportNestedClasses";
Compilation compilation = javac()
.withProcessors(new RefasterTemplateProcessor())
.withClasspath(classpath())
.compile(JavaFileObjects.forResource("recipes/ShouldAddImports.java"));
.compile(JavaFileObjects.forResource("recipes/" + recipeName + ".java"));
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("org/openrewrite/java/migrate/lang/ShouldAddImportsRecipe")
.hasSourceEquivalentTo(JavaFileObjects.forResource("recipes/ShouldAddImportsRecipe.java"));
compilation.generatedSourceFiles().forEach(System.out::println);
assertThat(compilation) // Recipes (plural)
.generatedSourceFile("foo/" + recipeName + "Recipes")
.hasSourceEquivalentTo(JavaFileObjects.forResource("recipes/" + recipeName + "Recipes.java"));
}

@NotNull
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/recipes/ShouldAddImports.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openrewrite.java.migrate.lang;
package foo;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/recipes/ShouldAddImportsRecipe.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openrewrite.java.migrate.lang;
package foo;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
Expand Down
18 changes: 18 additions & 0 deletions src/test/resources/recipes/ShouldSupportNestedClasses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package foo;

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

public class ShouldSupportNestedClasses {
public static class NestedClass {
@BeforeTemplate
boolean before(String s) {
return s.length() > 0;
}

@AfterTemplate
boolean after(String s) {
return !s.isEmpty();
}
}
}
43 changes: 43 additions & 0 deletions src/test/resources/recipes/ShouldSupportNestedClassesRecipes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package foo;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.tree.*;

public final class ShouldSupportNestedClassesRecipes {
public static final class NestedClassRecipe extends Recipe {

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

@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\npublic static class NestedClass {\n \n @BeforeTemplate()\n boolean before(String s) {\n return s.length() > 0;\n }\n \n @AfterTemplate()\n boolean after(String s) {\n return !s.isEmpty();\n }\n}\n```\n.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
final JavaTemplate before = JavaTemplate.compile(this, "before", (String s) -> s.length() > 0).build();
final JavaTemplate after = JavaTemplate.compile(this, "after", (String s) -> !s.isEmpty()).build();

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

};
}
}

}
2 changes: 1 addition & 1 deletion src/test/resources/recipes/UseStringIsEmpty.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openrewrite.java.migrate.lang;
package foo;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/recipes/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openrewrite.java.migrate.lang;
package foo;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
Expand Down

0 comments on commit 161fe2a

Please sign in to comment.