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 31351b1a..a300f0f5 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java @@ -327,11 +327,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { out.write("import org.openrewrite.java.tree.*;\n"); out.write("\n"); out.write("import java.util.function.Supplier;\n"); - if (outerClassRequired) { - out.write("\n"); - out.write("import java.util.Arrays;\n"); - out.write("import java.util.List;\n"); - } + out.write("import java.util.*;\n"); out.write("\n"); @@ -386,6 +382,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { private String recipeDescriptor(JCTree.JCClassDecl classDecl, String defaultDisplayName, String defaultDescription) { String displayName = defaultDisplayName; String description = defaultDescription; + Set tags = new LinkedHashSet<>(); for (JCTree.JCAnnotation annotation : classDecl.getModifiers().getAnnotations()) { if (annotation.type.toString().equals("org.openrewrite.java.template.RecipeDescriptor")) { for (JCTree.JCExpression argExpr : annotation.getArguments()) { @@ -397,23 +394,47 @@ private String recipeDescriptor(JCTree.JCClassDecl classDecl, String defaultDisp case "description": description = ((JCTree.JCLiteral) arg.rhs).getValue().toString(); break; + case "tags": + if (arg.rhs instanceof JCTree.JCLiteral) { + tags.add(((JCTree.JCLiteral) arg.rhs).getValue().toString()); + } else if (arg.rhs instanceof JCTree.JCNewArray) { + for (JCTree.JCExpression e : ((JCTree.JCNewArray) arg.rhs).elems) { + tags.add(((JCTree.JCLiteral) e).getValue().toString()); + } + } + break; } } break; } } - return "\n" + - " @Override\n" + - " public String getDisplayName() {\n" + - " return \"" + displayName + "\";\n" + - " }\n" + - "\n" + - " @Override\n" + - " public String getDescription() {\n" + - " return \"" + description + "\";\n" + - " }\n" + - "\n"; + String recipeDescriptor = " @Override\n" + + " public String getDisplayName() {\n" + + " return \"" + displayName + "\";\n" + + " }\n" + + "\n" + + " @Override\n" + + " public String getDescription() {\n" + + " return \"" + description + "\";\n" + + " }\n" + + "\n"; + + if (tags.size() == 1) { + recipeDescriptor += " @Override\n" + + " public Set getTags() {\n" + + " return Collections.singleton(\"" + String.join("\", \"", tags) + "\");\n" + + " }\n" + + "\n"; + } else if(tags.size() > 1) { + recipeDescriptor += " @Override\n" + + " public Set getTags() {\n" + + " return new HashSet<>(Arrays.asList(\"" + String.join("\", \"", tags) + "\"));\n" + + " }\n" + + "\n"; + } + + return recipeDescriptor; } private void maybeRemoveImport(Map> imports, Set beforeImports, Set afterImports, StringBuilder recipe) { diff --git a/src/test/resources/refaster/Matching.java b/src/test/resources/refaster/Matching.java index 3db964f3..ff25622d 100644 --- a/src/test/resources/refaster/Matching.java +++ b/src/test/resources/refaster/Matching.java @@ -32,7 +32,7 @@ public class Matching { @RecipeDescriptor( name = "Use String length comparison", description = "Use String#length() == 0 instead of String#isEmpty().", - tags = "sast" + tags = {"sast", "strings"} ) public static class StringIsEmpty { @BeforeTemplate diff --git a/src/test/resources/refaster/MatchingRecipes.java b/src/test/resources/refaster/MatchingRecipes.java index cf0858e7..7589a836 100644 --- a/src/test/resources/refaster/MatchingRecipes.java +++ b/src/test/resources/refaster/MatchingRecipes.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 the original author or authors. - *

- * 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 - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * 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 org.openrewrite.ExecutionContext; @@ -30,9 +15,7 @@ import org.openrewrite.java.tree.*; import java.util.function.Supplier; - -import java.util.Arrays; -import java.util.List; +import java.util.*; public final class MatchingRecipes extends Recipe { @@ -47,6 +30,11 @@ public String getDescription() { return "A set of static analysis recipes."; } + @Override + public Set getTags() { + return Collections.singleton("sast"); + } + @Override public List getRecipeList() { return Arrays.asList( @@ -68,6 +56,11 @@ public String getDescription() { return "Use String#length() == 0 instead of String#isEmpty()."; } + @Override + public Set getTags() { + return new HashSet<>(Arrays.asList("sast", "strings")); + } + @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { diff --git a/src/test/resources/refaster/MultipleDereferencesRecipes.java b/src/test/resources/refaster/MultipleDereferencesRecipes.java index a1627f5b..6e94390f 100644 --- a/src/test/resources/refaster/MultipleDereferencesRecipes.java +++ b/src/test/resources/refaster/MultipleDereferencesRecipes.java @@ -30,9 +30,7 @@ import org.openrewrite.java.tree.*; import java.util.function.Supplier; - -import java.util.Arrays; -import java.util.List; +import java.util.*; import java.nio.file.Files; import java.io.IOException; diff --git a/src/test/resources/refaster/NestedPreconditionsRecipe.java b/src/test/resources/refaster/NestedPreconditionsRecipe.java index 8b452b82..ec96ca88 100644 --- a/src/test/resources/refaster/NestedPreconditionsRecipe.java +++ b/src/test/resources/refaster/NestedPreconditionsRecipe.java @@ -30,6 +30,7 @@ import org.openrewrite.java.tree.*; import java.util.function.Supplier; +import java.util.*; import java.util.LinkedHashMap; import java.util.Map; diff --git a/src/test/resources/refaster/ParameterReuseRecipe.java b/src/test/resources/refaster/ParameterReuseRecipe.java index 94dcdcfd..d77855ce 100644 --- a/src/test/resources/refaster/ParameterReuseRecipe.java +++ b/src/test/resources/refaster/ParameterReuseRecipe.java @@ -30,6 +30,7 @@ import org.openrewrite.java.tree.*; import java.util.function.Supplier; +import java.util.*; @NonNullApi diff --git a/src/test/resources/refaster/ShouldAddImportsRecipes.java b/src/test/resources/refaster/ShouldAddImportsRecipes.java index 112baaa8..b1973d96 100644 --- a/src/test/resources/refaster/ShouldAddImportsRecipes.java +++ b/src/test/resources/refaster/ShouldAddImportsRecipes.java @@ -30,9 +30,7 @@ import org.openrewrite.java.tree.*; import java.util.function.Supplier; - -import java.util.Arrays; -import java.util.List; +import java.util.*; import java.util.Objects; diff --git a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java index 4e1031d4..fb08a033 100644 --- a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java +++ b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java @@ -30,9 +30,7 @@ import org.openrewrite.java.tree.*; import java.util.function.Supplier; - -import java.util.Arrays; -import java.util.List; +import java.util.*; public final class ShouldSupportNestedClassesRecipes extends Recipe { diff --git a/src/test/resources/refaster/UseStringIsEmptyRecipe.java b/src/test/resources/refaster/UseStringIsEmptyRecipe.java index 22e8f96b..e97ffe14 100644 --- a/src/test/resources/refaster/UseStringIsEmptyRecipe.java +++ b/src/test/resources/refaster/UseStringIsEmptyRecipe.java @@ -30,6 +30,7 @@ import org.openrewrite.java.tree.*; import java.util.function.Supplier; +import java.util.*; @NonNullApi