diff --git a/src/main/java/org/openrewrite/staticanalysis/AnnotateNullableMethods.java b/src/main/java/org/openrewrite/staticanalysis/AnnotateNullableMethods.java new file mode 100644 index 0000000000..94763084af --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/AnnotateNullableMethods.java @@ -0,0 +1,162 @@ +/* + * 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.jspecify.annotations.Nullable;
+import org.openrewrite.Cursor;
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.*;
+import org.openrewrite.java.service.AnnotationService;
+import org.openrewrite.java.tree.Expression;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class AnnotateNullableMethods extends Recipe {
+
+ private static final String NULLABLE_ANN_CLASS = "org.jspecify.annotations.Nullable";
+ private static final AnnotationMatcher NULLABLE_ANNOTATION_MATCHER = new AnnotationMatcher("@" + NULLABLE_ANN_CLASS);
+
+ @Override
+ public String getDisplayName() {
+ return "Annotate methods which may return `null` with `@Nullable`";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Add the `@org.jspecify.annotation.Nullable` to non-private methods that may return `null`. " +
+ "This recipe scans for methods that do not already have a `@Nullable` annotation and checks their return " +
+ "statements for potential null values. It also identifies known methods from standard libraries that may " +
+ "return null, such as methods from `Map`, `Queue`, `Deque`, `NavigableSet`, and `Spliterator`. " +
+ "The return of streams, or lambdas are not taken into account.";
+ }
+
+ @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.DocumentExample;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class AnnotateNullableMethodsTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .recipe(new AnnotateNullableMethods())
+ .parser(JavaParser.fromJavaVersion().classpath("jspecify"));
+ }
+
+ @DocumentExample
+ @Test
+ void methodReturnsNullLiteral() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ public class Test {
+
+ public String getString() {
+ return null;
+ }
+
+ public String getStringWithMultipleReturn() {
+ if (System.currentTimeMillis() % 2 == 0) {
+ return "Not null";
+ }
+ return null;
+ }
+ }
+ """,
+ """
+ import org.jspecify.annotations.Nullable;
+
+ public class Test {
+
+ public @Nullable String getString() {
+ return null;
+ }
+
+ public @Nullable String getStringWithMultipleReturn() {
+ if (System.currentTimeMillis() % 2 == 0) {
+ return "Not null";
+ }
+ return null;
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void methodReturnNullButIsAlreadyAnnotated() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.jspecify.annotations.Nullable;
+
+ public class Test {
+ public @Nullable String getString() {
+ return null;
+ }
+
+ public @Nullable String getStringWithMultipleReturn() {
+ if (System.currentTimeMillis() % 2 == 0) {
+ return "Not null";
+ }
+ return null;
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void methodDoesNotReturnNull() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ package org.example;
+
+ public class Test {
+ public String getString() {
+ return "Hello";
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void methodReturnsDelegateKnowNullableMethod() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import java.util.Map;
+
+ public class Test {
+
+ public String getString(Map