From 25c979f3d2b99a51e19cfed6b0f212037c221772 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 17 Dec 2023 22:23:31 +0100 Subject: [PATCH] Generate default constructor with JavaDoc for Xdoclint (#50) * Generate default constructor with JavaDoc for Xdoclint * Drop link and return; add missing summary line * Code review comments * Also add constructors to RefasterTemplateProcessor * Replace backticks with `{@code ...}` --- .../processor/RefasterTemplateProcessor.java | 15 +++++++++++---- .../template/processor/TemplateProcessor.java | 14 +++++++++++--- src/test/resources/refaster/EscapesRecipes.java | 4 ++++ src/test/resources/refaster/MatchingRecipes.java | 5 +++++ .../resources/refaster/MethodThrowsRecipe.java | 2 ++ .../refaster/MultipleDereferencesRecipes.java | 9 +++++++++ .../refaster/NestedPreconditionsRecipe.java | 2 ++ .../resources/refaster/ParameterReuseRecipe.java | 2 ++ .../refaster/ShouldAddImportsRecipes.java | 9 +++++++++ .../ShouldSupportNestedClassesRecipes.java | 7 +++++++ .../refaster/SimplifyBooleansRecipe.java | 2 ++ .../refaster/UseStringIsEmptyRecipe.java | 2 ++ .../template/ParameterReuseRecipe$1_before.java | 2 ++ ...sspathRecipe$FullyQualifiedRecipe$1_after.java | 2 ++ ...spathRecipe$FullyQualifiedRecipe$1_before.java | 2 ++ ...ddClasspathRecipe$PrimitiveRecipe$1_after.java | 2 ++ ...dClasspathRecipe$PrimitiveRecipe$1_before.java | 2 ++ ...ClasspathRecipe$UnqualifiedRecipe$1_after.java | 2 ++ ...lasspathRecipe$UnqualifiedRecipe$1_before.java | 2 ++ 19 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java index b7857405..f12d4362 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java @@ -177,12 +177,15 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { StringBuilder recipe = new StringBuilder(); String refasterRuleClassName = classDecl.sym.fullname.toString().substring(classDecl.sym.packge().fullname.length() + 1); - recipe.append("/**\n * OpenRewrite recipe created for Refaster template `").append(refasterRuleClassName).append("`.\n */\n"); + recipe.append("/**\n * OpenRewrite recipe created for Refaster template {@code ").append(refasterRuleClassName).append("}.\n */\n"); String recipeName = templateFqn.substring(templateFqn.lastIndexOf('.') + 1); recipe.append("@NonNullApi\n"); recipe.append(descriptor.classDecl.sym.outermostClass() == descriptor.classDecl.sym ? - "public class " : "public static class ").append(recipeName).append(" extends Recipe {\n"); - recipe.append("\n"); + "public class " : "public static class ").append(recipeName).append(" extends Recipe {\n\n"); + recipe.append(" /**\n"); + recipe.append(" * Instantiates a new instance.\n"); + recipe.append(" */\n"); + recipe.append(" public ").append(recipeName).append("() {}\n\n"); recipe.append(recipeDescriptor(classDecl, "Refaster template `" + refasterRuleClassName + '`', "Recipe created for the following Refaster template:\\n```java\\n" + escape(templateCode) + "\\n```\\n." @@ -335,9 +338,13 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { } if (outerClassRequired) { - out.write("/**\n * OpenRewrite recipes created for Refaster template `" + inputOuterFQN + "`.\n */\n"); + out.write("/**\n * OpenRewrite recipes created for Refaster template {@code " + inputOuterFQN + "}.\n */\n"); String outerClassName = className.substring(className.lastIndexOf('.') + 1); out.write("public class " + outerClassName + " extends Recipe {\n"); + out.write(" /**\n"); + out.write(" * Instantiates a new instance.\n"); + out.write(" */\n"); + out.write(" public " + outerClassName + "() {}\n\n"); out.write(recipeDescriptor(classDecl, String.format("`%s` Refaster recipes", inputOuterFQN.substring(inputOuterFQN.lastIndexOf('.') + 1)), String.format("Refaster template recipes for `%s`.", inputOuterFQN))); diff --git a/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java b/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java index 6319ef02..94266003 100644 --- a/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java @@ -215,9 +215,17 @@ public void visitIdent(JCTree.JCIdent ident) { } out.write("\n"); - out.write("/**\n * OpenRewrite `" + templateName.getValue() + "` template created for `" + templateFqn.split("_")[0] + "`.\n */\n"); - out.write("public class " + templateFqn.substring(templateFqn.lastIndexOf('.') + 1) + " {\n"); - out.write(" /**\n * @return `JavaTemplate` to match or replace.\n */\n"); + out.write("/**\n * OpenRewrite `" + templateName.getValue() + "` template created for {@code " + templateFqn.split("_")[0] + "}.\n */\n"); + String templateClassName = templateFqn.substring(templateFqn.lastIndexOf('.') + 1); + out.write("public class " + templateClassName + " {\n"); + out.write(" /**\n"); + out.write(" * Instantiates a new instance.\n"); + out.write(" */\n"); + out.write(" public " + templateClassName + "() {}\n\n"); + out.write(" /**\n"); + out.write(" * Get the {@code JavaTemplate.Builder} to match or replace.\n"); + out.write(" * @return the JavaTemplate builder.\n"); + out.write(" */\n"); out.write(" public static JavaTemplate.Builder getTemplate() {\n"); out.write(" return JavaTemplate\n"); out.write(" .builder(\"" + templateSource + "\")"); diff --git a/src/test/resources/refaster/EscapesRecipes.java b/src/test/resources/refaster/EscapesRecipes.java index 538ebf8c..285ace88 100644 --- a/src/test/resources/refaster/EscapesRecipes.java +++ b/src/test/resources/refaster/EscapesRecipes.java @@ -37,6 +37,8 @@ import com.sun.tools.javac.util.Constants; public class EscapesRecipes extends Recipe { + public EscapesRecipes() {} + @Override public String getDisplayName() { return "`Escapes` Refaster recipes"; @@ -57,6 +59,7 @@ public List getRecipeList() { @NonNullApi public static class ConstantsFormatRecipe extends Recipe { + public ConstantsFormatRecipe() {} @Override public String getDisplayName() { @@ -103,6 +106,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @NonNullApi public static class SplitRecipe extends Recipe { + public SplitRecipe() {} @Override public String getDisplayName() { diff --git a/src/test/resources/refaster/MatchingRecipes.java b/src/test/resources/refaster/MatchingRecipes.java index 0be1e83b..d56d067c 100644 --- a/src/test/resources/refaster/MatchingRecipes.java +++ b/src/test/resources/refaster/MatchingRecipes.java @@ -35,6 +35,9 @@ public class MatchingRecipes extends Recipe { + + public MatchingRecipes() {} + @Override public String getDisplayName() { return "Static analysis"; @@ -60,6 +63,8 @@ public List getRecipeList() { @NonNullApi public static class StringIsEmptyRecipe extends Recipe { + public StringIsEmptyRecipe() {} + @Override public String getDisplayName() { return "Use String length comparison"; diff --git a/src/test/resources/refaster/MethodThrowsRecipe.java b/src/test/resources/refaster/MethodThrowsRecipe.java index 7df1da3d..868b14f9 100644 --- a/src/test/resources/refaster/MethodThrowsRecipe.java +++ b/src/test/resources/refaster/MethodThrowsRecipe.java @@ -40,6 +40,8 @@ @NonNullApi public class MethodThrowsRecipe extends Recipe { + public MethodThrowsRecipe() {} + @Override public String getDisplayName() { return "Refaster template `MethodThrows`"; diff --git a/src/test/resources/refaster/MultipleDereferencesRecipes.java b/src/test/resources/refaster/MultipleDereferencesRecipes.java index 55857594..ea75d504 100644 --- a/src/test/resources/refaster/MultipleDereferencesRecipes.java +++ b/src/test/resources/refaster/MultipleDereferencesRecipes.java @@ -37,6 +37,9 @@ import java.nio.file.Path; public class MultipleDereferencesRecipes extends Recipe { + + public MultipleDereferencesRecipes() {} + @Override public String getDisplayName() { return "`MultipleDereferences` Refaster recipes"; @@ -59,6 +62,8 @@ public List getRecipeList() { @NonNullApi public static class VoidTypeRecipe extends Recipe { + public VoidTypeRecipe() {} + @Override public String getDisplayName() { return "Refaster template `MultipleDereferences.VoidType`"; @@ -104,6 +109,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @NonNullApi public static class StringIsEmptyRecipe extends Recipe { + public StringIsEmptyRecipe() {} + @Override public String getDisplayName() { return "Refaster template `MultipleDereferences.StringIsEmpty`"; @@ -145,6 +152,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @NonNullApi public static class EqualsItselfRecipe extends Recipe { + public EqualsItselfRecipe() {} + @Override public String getDisplayName() { return "Refaster template `MultipleDereferences.EqualsItself`"; diff --git a/src/test/resources/refaster/NestedPreconditionsRecipe.java b/src/test/resources/refaster/NestedPreconditionsRecipe.java index 91a49472..81caad63 100644 --- a/src/test/resources/refaster/NestedPreconditionsRecipe.java +++ b/src/test/resources/refaster/NestedPreconditionsRecipe.java @@ -41,6 +41,8 @@ @NonNullApi public class NestedPreconditionsRecipe extends Recipe { + public NestedPreconditionsRecipe() {} + @Override public String getDisplayName() { return "Refaster template `NestedPreconditions`"; diff --git a/src/test/resources/refaster/ParameterReuseRecipe.java b/src/test/resources/refaster/ParameterReuseRecipe.java index 3a2d1dda..b58f548d 100644 --- a/src/test/resources/refaster/ParameterReuseRecipe.java +++ b/src/test/resources/refaster/ParameterReuseRecipe.java @@ -37,6 +37,8 @@ @NonNullApi public class ParameterReuseRecipe extends Recipe { + public ParameterReuseRecipe() {} + @Override public String getDisplayName() { return "Refaster template `ParameterReuse`"; diff --git a/src/test/resources/refaster/ShouldAddImportsRecipes.java b/src/test/resources/refaster/ShouldAddImportsRecipes.java index 14e920cc..cbaf999c 100644 --- a/src/test/resources/refaster/ShouldAddImportsRecipes.java +++ b/src/test/resources/refaster/ShouldAddImportsRecipes.java @@ -38,6 +38,9 @@ import static java.util.Objects.hash; public class ShouldAddImportsRecipes extends Recipe { + + public ShouldAddImportsRecipes() {} + @Override public String getDisplayName() { return "`ShouldAddImports` Refaster recipes"; @@ -60,6 +63,8 @@ public List getRecipeList() { @NonNullApi public static class StringValueOfRecipe extends Recipe { + public StringValueOfRecipe() {} + @Override public String getDisplayName() { return "Refaster template `ShouldAddImports.StringValueOf`"; @@ -101,6 +106,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @NonNullApi public static class ObjectsEqualsRecipe extends Recipe { + public ObjectsEqualsRecipe() {} + @Override public String getDisplayName() { return "Refaster template `ShouldAddImports.ObjectsEquals`"; @@ -158,6 +165,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @NonNullApi public static class StaticImportObjectsHashRecipe extends Recipe { + public StaticImportObjectsHashRecipe() {} + @Override public String getDisplayName() { return "Refaster template `ShouldAddImports.StaticImportObjectsHash`"; diff --git a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java index 3b204063..1492545f 100644 --- a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java +++ b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java @@ -35,6 +35,9 @@ public class ShouldSupportNestedClassesRecipes extends Recipe { + + public ShouldSupportNestedClassesRecipes() {} + @Override public String getDisplayName() { return "`ShouldSupportNestedClasses` Refaster recipes"; @@ -56,6 +59,8 @@ public List getRecipeList() { @NonNullApi public static class NestedClassRecipe extends Recipe { + public NestedClassRecipe() {} + @Override public String getDisplayName() { return "Refaster template `ShouldSupportNestedClasses.NestedClass`"; @@ -97,6 +102,8 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) { @NonNullApi public static class AnotherClassRecipe extends Recipe { + public AnotherClassRecipe() {} + @Override public String getDisplayName() { return "Refaster template `ShouldSupportNestedClasses.AnotherClass`"; diff --git a/src/test/resources/refaster/SimplifyBooleansRecipe.java b/src/test/resources/refaster/SimplifyBooleansRecipe.java index 239b2ae8..10d1b105 100644 --- a/src/test/resources/refaster/SimplifyBooleansRecipe.java +++ b/src/test/resources/refaster/SimplifyBooleansRecipe.java @@ -37,6 +37,8 @@ @NonNullApi public class SimplifyBooleansRecipe extends Recipe { + public SimplifyBooleansRecipe() {} + @Override public String getDisplayName() { return "Refaster template `SimplifyBooleans`"; diff --git a/src/test/resources/refaster/UseStringIsEmptyRecipe.java b/src/test/resources/refaster/UseStringIsEmptyRecipe.java index 87f46f17..5651e27c 100644 --- a/src/test/resources/refaster/UseStringIsEmptyRecipe.java +++ b/src/test/resources/refaster/UseStringIsEmptyRecipe.java @@ -40,6 +40,8 @@ @NonNullApi public class UseStringIsEmptyRecipe extends Recipe { + public UseStringIsEmptyRecipe() {} + @Override public String getDisplayName() { return "Replace `s.length() > 0` with `!s.isEmpty()`"; diff --git a/src/test/resources/template/ParameterReuseRecipe$1_before.java b/src/test/resources/template/ParameterReuseRecipe$1_before.java index f69bce71..844a83b3 100644 --- a/src/test/resources/template/ParameterReuseRecipe$1_before.java +++ b/src/test/resources/template/ParameterReuseRecipe$1_before.java @@ -17,6 +17,8 @@ import org.openrewrite.java.*; public class ParameterReuseRecipe$1_before { + public ParameterReuseRecipe$1_before() {} + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("#{s:any(java.lang.String)}.equals(#{s})"); diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_after.java b/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_after.java index 07a43c53..b9db1545 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_after.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_after.java @@ -17,6 +17,8 @@ import org.openrewrite.java.*; public class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_after { + public ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_after() {} + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("org.slf4j.LoggerFactory.getLogger(#{message:any(java.lang.String)})") diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java b/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java index 468150cc..6cb01fa2 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java @@ -17,6 +17,8 @@ import org.openrewrite.java.*; public class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_before { + public ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_before() {} + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("System.out.println(#{message:any(java.lang.String)})"); diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_after.java b/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_after.java index 67b81af6..70f612e8 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_after.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_after.java @@ -17,6 +17,8 @@ import org.openrewrite.java.*; public class ShouldAddClasspathRecipes$PrimitiveRecipe$1_after { + public ShouldAddClasspathRecipes$PrimitiveRecipe$1_after() {} + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("System.out.print(#{i:any(int)})"); diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_before.java b/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_before.java index 2aa59b18..299e2656 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_before.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_before.java @@ -17,6 +17,8 @@ import org.openrewrite.java.*; public class ShouldAddClasspathRecipes$PrimitiveRecipe$1_before { + public ShouldAddClasspathRecipes$PrimitiveRecipe$1_before() {} + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("System.out.println(#{i:any(int)})"); diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_after.java b/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_after.java index be88c040..f559ace7 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_after.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_after.java @@ -17,6 +17,8 @@ import org.openrewrite.java.*; public class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_after { + public ShouldAddClasspathRecipes$UnqualifiedRecipe$1_after() {} + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("org.slf4j.LoggerFactory.getLogger(#{message:any(java.lang.String)})") diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java b/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java index 53d69e5b..88795040 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java @@ -17,6 +17,8 @@ import org.openrewrite.java.*; public class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_before { + public ShouldAddClasspathRecipes$UnqualifiedRecipe$1_before() {} + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("System.out.println(#{message:any(java.lang.String)})");