diff --git a/src/main/java/org/openrewrite/java/migrate/jspecify/RemoveAnnotationFromVoidMethod.java b/src/main/java/org/openrewrite/java/migrate/jspecify/RemoveAnnotationFromVoidMethod.java new file mode 100644 index 0000000000..98b9df678f --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/jspecify/RemoveAnnotationFromVoidMethod.java @@ -0,0 +1,129 @@ +/* + * Copyright 2026 the original author or authors. + *
+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://docs.moderne.io/licensing/moderne-source-available-license + *
+ * 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.java.migrate.jspecify;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.openrewrite.*;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.TypeMatcher;
+import org.openrewrite.java.search.UsesType;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+import org.openrewrite.java.tree.Space;
+import org.openrewrite.java.tree.TypeUtils;
+
+import java.util.List;
+
+@EqualsAndHashCode(callSuper = false)
+@Value
+public class RemoveAnnotationFromVoidMethod extends Recipe {
+
+ @Option(displayName = "Annotation type",
+ description = "The type of annotation to remove from methods returning `void`.",
+ example = "org.jetbrains.annotations.*ull*")
+ String annotationType;
+
+ String displayName = "Remove annotation from `void` method";
+
+ String description = "Nullability annotations are meaningless on methods returning `void`, " +
+ "and type-use annotations such as JSpecify's `@Nullable` are not permitted there. " +
+ "This recipe removes such annotations so they are not carried over by a later `ChangeType`.";
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return Preconditions.check(new UsesType<>(annotationType, null), new JavaIsoVisitor
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
@@ -261,6 +261,108 @@ public void bar(@Nullable String baz) {
);
}
+ @Test
+ void jetbrainsAnnotationMethodWithVoidReturnType() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Foo {
+ @org.jetbrains.annotations.Nullable
+ public void bar(int i) {
+ }
+ }
+ """,
+ """
+ class Foo {
+ public void bar(int i) {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void jakartaAnnotationMethodWithVoidReturnType() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Foo {
+ @jakarta.annotation.Nullable
+ public void bar(int i) {
+ }
+ }
+ """,
+ """
+ class Foo {
+ public void bar(int i) {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void javaxParametersAreNonnullByDefaultOnVoidMethodMigratedToNullMarked() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import javax.annotation.Nullable;
+ import javax.annotation.ParametersAreNonnullByDefault;
+
+ class Foo {
+ @Nullable
+ @ParametersAreNonnullByDefault
+ public void bar(String s) {
+ }
+ }
+ """,
+ """
+ import org.jspecify.annotations.NullMarked;
+
+ class Foo {
+ @NullMarked
+ public void bar(String s) {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void spotBugsReturnValuesAreNonnullByDefaultOnVoidMethodRetained() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import edu.umd.cs.findbugs.annotations.Nullable;
+ import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
+
+ class Foo {
+ @Nullable
+ @ReturnValuesAreNonnullByDefault
+ public void bar(String s) {
+ }
+ }
+ """,
+ """
+ import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
+
+ class Foo {
+ @ReturnValuesAreNonnullByDefault
+ public void bar(String s) {
+ }
+ }
+ """
+ )
+ );
+ }
+
@Test
void migrateFromJakartaAnnotationApiToJspecify() {
rewriteRun(
diff --git a/src/test/java/org/openrewrite/java/migrate/jspecify/RemoveAnnotationFromVoidMethodTest.java b/src/test/java/org/openrewrite/java/migrate/jspecify/RemoveAnnotationFromVoidMethodTest.java
new file mode 100644
index 0000000000..748781646c
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/migrate/jspecify/RemoveAnnotationFromVoidMethodTest.java
@@ -0,0 +1,251 @@
+/*
+ * Copyright 2026 the original author or authors.
+ *
+ * Licensed under the Moderne Source Available License (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://docs.moderne.io/licensing/moderne-source-available-license
+ *
+ * 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.java.migrate.jspecify;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class RemoveAnnotationFromVoidMethodTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .recipe(new RemoveAnnotationFromVoidMethod("org.jetbrains.annotations.*ull*"))
+ .parser(JavaParser.fromJavaVersion().classpath("annotations"));
+ }
+
+ @DocumentExample
+ @Test
+ void removeNullableFromVoidMethod() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.jetbrains.annotations.Nullable;
+
+ class Foo {
+ @Nullable
+ public void bar() {
+ }
+ }
+ """,
+ """
+ class Foo {
+ public void bar() {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void removeAllMatchingAnnotations() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.jetbrains.annotations.NotNull;
+ import org.jetbrains.annotations.Nullable;
+
+ class Foo {
+ @NotNull
+ @Nullable
+ public void bar() {
+ }
+ }
+ """,
+ """
+ class Foo {
+ public void bar() {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void keepOtherAnnotations() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.jetbrains.annotations.Nullable;
+
+ class Foo {
+ @Nullable
+ @Deprecated
+ public void bar() {
+ }
+
+ @Deprecated
+ @Nullable
+ public void baz() {
+ }
+ }
+ """,
+ """
+ class Foo {
+ @Deprecated
+ public void bar() {
+ }
+
+ @Deprecated
+ public void baz() {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void voidMethodWithoutModifiers() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ class Foo {
+ @org.jetbrains.annotations.Nullable
+ void bar() {
+ }
+
+ @org.jetbrains.annotations.Nullable
+