diff --git a/src/main/java/org/openrewrite/staticanalysis/NullableOnMethodReturnType.java b/src/main/java/org/openrewrite/staticanalysis/NullableOnMethodReturnType.java new file mode 100644 index 0000000000..c6d415606d --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/NullableOnMethodReturnType.java @@ -0,0 +1,78 @@ +/* + * 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.staticanalysis;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.Tree;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.Space;
+import org.openrewrite.marker.Markers;
+
+import java.util.Collections;
+
+import static org.openrewrite.java.trait.Traits.annotated;
+
+public class NullableOnMethodReturnType extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Move `@Nullable` method annotations to the return type";
+ }
+
+ @Override
+ public String getDescription() {
+ return "This is the way the cool kids do it.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new JavaIsoVisitor
+ * 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.staticanalysis;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class NullableOnMethodReturnTypeTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new NullableOnMethodReturnType());
+ }
+
+ @Test
+ void nullableOnMethodReturnType() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.openrewrite.internal.lang.Nullable;
+ class Test {
+ @Nullable
+ public String test() {
+ }
+ }
+ """,
+ """
+ import org.openrewrite.internal.lang.Nullable;
+ class Test {
+ public @Nullable String test() {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void dontTouchArguments() {
+ rewriteRun(
+ java(
+ //language=java
+ """
+ import org.openrewrite.internal.lang.Nullable;
+ class Test {
+ void test(@Nullable String s) {
+ }
+ }
+ """
+ )
+ );
+ }
+}