diff --git a/rewrite-core/src/main/java/org/openrewrite/PackageRecipeTags.java b/rewrite-core/src/main/java/org/openrewrite/PackageRecipeTags.java new file mode 100644 index 00000000000..aefcaef91ec --- /dev/null +++ b/rewrite-core/src/main/java/org/openrewrite/PackageRecipeTags.java @@ -0,0 +1,39 @@ +/* + * Copyright 2024 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 org.openrewrite; + +import org.openrewrite.config.RecipeDescriptor; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * A package-level annotation that can be used to tag all {@link Recipe} within a given package and children + * packages with a given tag. + *
+ * This annotation is useful for tagging all recipes within a package with a given tag, without having to + * add it manually to each overridden {@link Recipe#getTags()} method. + *
+ * The tag will not be accessible via {@link Recipe#getTags()}, but will be accessible via {@link RecipeDescriptor#getTags()}
+ * when the {@link RecipeDescriptor} is created via {@link Recipe#getDescriptor()}.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.PACKAGE})
+public @interface PackageRecipeTags {
+ String[] value() default {};
+}
diff --git a/rewrite-core/src/main/java/org/openrewrite/Recipe.java b/rewrite-core/src/main/java/org/openrewrite/Recipe.java
index fc7603d8c98..1d63ec89a26 100644
--- a/rewrite-core/src/main/java/org/openrewrite/Recipe.java
+++ b/rewrite-core/src/main/java/org/openrewrite/Recipe.java
@@ -223,10 +223,45 @@ protected RecipeDescriptor createRecipeDescriptor() {
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
+ Set
+ * 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 org.openrewrite.tags;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.Recipe;
+import org.openrewrite.config.RecipeDescriptor;
+import org.openrewrite.tags.child.ChildRecipeWithNoTags;
+
+import java.util.Collections;
+import java.util.Set;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PackageRecipeTagsAnnotationTest {
+
+ private static class RecipeWithNoTags extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Recipe with no tags";
+ }
+
+ @Override
+ public String getDescription() {
+ return "It's tag-less!";
+ }
+
+ @Override
+ public Set
+ * 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 org.openrewrite.tags.child;
+
+import org.openrewrite.Recipe;
+
+public class ChildRecipeWithNoTags extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Recipe with no tags";
+ }
+
+ @Override
+ public String getDescription() {
+ return "It's tag-less!";
+ }
+
+}
diff --git a/rewrite-core/src/test/java/org/openrewrite/tags/child/package-info.java b/rewrite-core/src/test/java/org/openrewrite/tags/child/package-info.java
new file mode 100644
index 00000000000..587c6d5a95d
--- /dev/null
+++ b/rewrite-core/src/test/java/org/openrewrite/tags/child/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2024 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.
+ */
+@NonNullApi
+package org.openrewrite.tags.child;
+
+import io.micrometer.core.lang.NonNullApi;
diff --git a/rewrite-core/src/test/java/org/openrewrite/tags/package-info.java b/rewrite-core/src/test/java/org/openrewrite/tags/package-info.java
new file mode 100644
index 00000000000..a7f6d95cee9
--- /dev/null
+++ b/rewrite-core/src/test/java/org/openrewrite/tags/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2024 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.
+ */
+@PackageRecipeTags({"What a fun tag!"})
+@NonNullApi
+package org.openrewrite.tags;
+
+import org.openrewrite.PackageRecipeTags;
+import org.openrewrite.internal.lang.NonNullApi;