Skip to content

Commit

Permalink
Support tags in
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Sep 30, 2023
1 parent 1d80be3 commit 2825a6c
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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<String> 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()) {
Expand All @@ -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<String> getTags() {\n" +
" return Collections.singleton(\"" + String.join("\", \"", tags) + "\");\n" +
" }\n" +
"\n";
} else if(tags.size() > 1) {
recipeDescriptor += " @Override\n" +
" public Set<String> getTags() {\n" +
" return new HashSet<>(Arrays.asList(\"" + String.join("\", \"", tags) + "\"));\n" +
" }\n" +
"\n";
}

return recipeDescriptor;
}

private void maybeRemoveImport(Map<JCTree.JCMethodDecl, Set<String>> imports, Set<String> beforeImports, Set<String> afterImports, StringBuilder recipe) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/Matching.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 11 additions & 18 deletions src/test/resources/refaster/MatchingRecipes.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
Expand All @@ -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 {
Expand All @@ -47,6 +30,11 @@ public String getDescription() {
return "A set of static analysis recipes.";
}

@Override
public Set<String> getTags() {
return Collections.singleton("sast");
}

@Override
public List<Recipe> getRecipeList() {
return Arrays.asList(
Expand All @@ -68,6 +56,11 @@ public String getDescription() {
return "Use String#length() == 0 instead of String#isEmpty().";
}

@Override
public Set<String> getTags() {
return new HashSet<>(Arrays.asList("sast", "strings"));
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> javaVisitor = new AbstractRefasterJavaVisitor() {
Expand Down
4 changes: 1 addition & 3 deletions src/test/resources/refaster/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/refaster/NestedPreconditionsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/refaster/ParameterReuseRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openrewrite.java.tree.*;

import java.util.function.Supplier;
import java.util.*;


@NonNullApi
Expand Down
4 changes: 1 addition & 3 deletions src/test/resources/refaster/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/refaster/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openrewrite.java.tree.*;

import java.util.function.Supplier;
import java.util.*;


@NonNullApi
Expand Down

0 comments on commit 2825a6c

Please sign in to comment.