From e141e817f747bbb1b98626fd5d4ac2f3f55a56bb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 17 Dec 2023 14:13:44 +0100 Subject: [PATCH] Restore public modifiers with javadoc (#46) * Restore public modifiers for Java 11+ with JavaDoc * Fix indentation * Always print clickable link to file with notes * Consistency in printed notes --- .../processor/RefasterTemplateProcessor.java | 18 +++++++++++------- .../template/processor/TemplateProcessor.java | 6 ++++-- .../ParameterReuseRecipe$1_before.java | 4 ++-- ...athRecipe$FullyQualifiedRecipe$1_after.java | 4 ++-- ...thRecipe$FullyQualifiedRecipe$1_before.java | 4 ++-- ...lasspathRecipe$PrimitiveRecipe$1_after.java | 4 ++-- ...asspathRecipe$PrimitiveRecipe$1_before.java | 4 ++-- ...sspathRecipe$UnqualifiedRecipe$1_after.java | 4 ++-- ...spathRecipe$UnqualifiedRecipe$1_before.java | 4 ++-- 9 files changed, 29 insertions(+), 23 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 6bf96a21..259acc9f 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java @@ -690,7 +690,7 @@ private TemplateDescriptor validate(Context context, JCCompilationUnit cu) { for (JCTree member : classDecl.getMembers()) { if (member instanceof JCTree.JCMethodDecl && !beforeTemplates.contains(member) && member != afterTemplate) { for (JCTree.JCAnnotation annotation : getTemplateAnnotations(((JCTree.JCMethodDecl) member), UNSUPPORTED_ANNOTATIONS::contains)) { - printNoteOnce("The @" + annotation.annotationType + " is currently not supported", ((JCTree.JCMethodDecl) member).sym); + printNoteOnce("@" + annotation.annotationType + " is currently not supported", classDecl.sym); valid = false; } } @@ -711,22 +711,22 @@ private boolean validateTemplateMethod(JCTree.JCMethodDecl template) { boolean valid = true; // TODO: support all Refaster method-level annotations for (JCTree.JCAnnotation annotation : getTemplateAnnotations(template, UNSUPPORTED_ANNOTATIONS::contains)) { - printNoteOnce("The @" + annotation.annotationType + " is currently not supported", template.sym); + printNoteOnce("@" + annotation.annotationType + " is currently not supported", classDecl.sym); valid = false; } // TODO: support all Refaster parameter-level annotations for (JCTree.JCVariableDecl parameter : template.getParameters()) { for (JCTree.JCAnnotation annotation : getTemplateAnnotations(parameter, UNSUPPORTED_ANNOTATIONS::contains)) { - printNoteOnce("The @" + annotation.annotationType + " annotation is currently not supported", template.sym); + printNoteOnce("@" + annotation.annotationType + " is currently not supported", classDecl.sym); valid = false; } if (parameter.vartype instanceof ParameterizedTypeTree || parameter.vartype.type instanceof Type.TypeVar) { - printNoteOnce("Generics are currently not supported", template.sym); + printNoteOnce("Generics are currently not supported", classDecl.sym); valid = false; } } if (template.restype instanceof ParameterizedTypeTree || template.restype.type instanceof Type.TypeVar) { - printNoteOnce("Generics are currently not supported", template.sym); + printNoteOnce("Generics are currently not supported", classDecl.sym); valid = false; } valid &= new TreeScanner() { @@ -741,7 +741,7 @@ boolean validate(JCTree tree) { public void visitIdent(JCTree.JCIdent jcIdent) { if (jcIdent.sym != null && jcIdent.sym.packge().getQualifiedName().contentEquals("com.google.errorprone.refaster")) { - printNoteOnce(jcIdent.type.tsym.getQualifiedName() + " is not supported", template.sym); + printNoteOnce(jcIdent.type.tsym.getQualifiedName() + " is currently not supported", classDecl.sym); valid = false; } } @@ -777,7 +777,11 @@ private boolean resolve(Context context, JCCompilationUnit cu) { private final Set printedMessages = new HashSet<>(); - private void printNoteOnce(String message, Symbol symbol) { + /** + * @param message The message to print + * @param symbol The symbol to attach the message to; printed as clickable link to file + */ + private void printNoteOnce(String message, Symbol.ClassSymbol symbol) { if (printedMessages.add(message)) { processingEnv.getMessager().printMessage(Kind.NOTE, message, symbol); } 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 b9add99a..6319ef02 100644 --- a/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java @@ -215,8 +215,10 @@ public void visitIdent(JCTree.JCIdent ident) { } out.write("\n"); - out.write("class " + templateFqn.substring(templateFqn.lastIndexOf('.') + 1) + " {\n"); - out.write(" static JavaTemplate.Builder getTemplate() {\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(" public static JavaTemplate.Builder getTemplate() {\n"); out.write(" return JavaTemplate\n"); out.write(" .builder(\"" + templateSource + "\")"); diff --git a/src/test/resources/template/ParameterReuseRecipe$1_before.java b/src/test/resources/template/ParameterReuseRecipe$1_before.java index d7b9a17e..f69bce71 100644 --- a/src/test/resources/template/ParameterReuseRecipe$1_before.java +++ b/src/test/resources/template/ParameterReuseRecipe$1_before.java @@ -16,8 +16,8 @@ package foo; import org.openrewrite.java.*; -class ParameterReuseRecipe$1_before { - static JavaTemplate.Builder getTemplate() { +public class 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 c32a317b..07a43c53 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_after.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_after.java @@ -16,8 +16,8 @@ package foo; import org.openrewrite.java.*; -class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_after { - static JavaTemplate.Builder getTemplate() { +public class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_after { + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("org.slf4j.LoggerFactory.getLogger(#{message:any(java.lang.String)})") .javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")); diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java b/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java index 6bd3408c..468150cc 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java @@ -16,8 +16,8 @@ package foo; import org.openrewrite.java.*; -class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_before { - static JavaTemplate.Builder getTemplate() { +public class 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 0bce0c82..67b81af6 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_after.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_after.java @@ -16,8 +16,8 @@ package foo; import org.openrewrite.java.*; -class ShouldAddClasspathRecipes$PrimitiveRecipe$1_after { - static JavaTemplate.Builder getTemplate() { +public class 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 f23b84c8..2aa59b18 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_before.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$PrimitiveRecipe$1_before.java @@ -16,8 +16,8 @@ package foo; import org.openrewrite.java.*; -class ShouldAddClasspathRecipes$PrimitiveRecipe$1_before { - static JavaTemplate.Builder getTemplate() { +public class 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 6440266b..be88c040 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_after.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_after.java @@ -16,8 +16,8 @@ package foo; import org.openrewrite.java.*; -class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_after { - static JavaTemplate.Builder getTemplate() { +public class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_after { + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("org.slf4j.LoggerFactory.getLogger(#{message:any(java.lang.String)})") .javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")); diff --git a/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java b/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java index 7ba95f95..53d69e5b 100644 --- a/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java +++ b/src/test/resources/template/ShouldAddClasspathRecipe$UnqualifiedRecipe$1_before.java @@ -16,8 +16,8 @@ package foo; import org.openrewrite.java.*; -class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_before { - static JavaTemplate.Builder getTemplate() { +public class ShouldAddClasspathRecipes$UnqualifiedRecipe$1_before { + public static JavaTemplate.Builder getTemplate() { return JavaTemplate .builder("System.out.println(#{message:any(java.lang.String)})"); }