From 687f7df50d0e909bfb90147cdd44e7f866097c4a Mon Sep 17 00:00:00 2001
From: Tim te Beek <tim@moderne.io>
Date: Wed, 28 Feb 2024 11:55:06 +0100
Subject: [PATCH] refactor: Recipe nullability best practices (#487)

* refactor: Recipe nullability best practices

Use this link to re-run the recipe: https://app.moderne.io/recipes/builder/XkVt46WER?organizationId=T3BlblJld3JpdGU%3D

Co-authored-by: Moderne <team@moderne.io>

* Add package-info.java to Hamcrest package

---------

Co-authored-by: Moderne <team@moderne.io>
---
 .../hamcrest/HamcrestMatcherToAssertJ.java    |  2 --
 .../hamcrest/HamcrestOfMatchersToAssertJ.java |  1 -
 .../testing/hamcrest/RemoveIsMatcher.java     |  1 -
 .../java/testing/hamcrest/package-info.java   | 21 +++++++++++++++++++
 .../junit5/RemoveTryCatchFailBlocks.java      |  3 ---
 .../ExplicitContainerImage.java               |  2 --
 6 files changed, 21 insertions(+), 9 deletions(-)
 create mode 100644 src/main/java/org/openrewrite/java/testing/hamcrest/package-info.java

diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java
index f627231d9..2003876fb 100644
--- a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java
+++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java
@@ -17,7 +17,6 @@
 
 import lombok.AllArgsConstructor;
 import lombok.NoArgsConstructor;
-import org.jetbrains.annotations.NotNull;
 import org.openrewrite.*;
 import org.openrewrite.internal.lang.Nullable;
 import org.openrewrite.java.JavaIsoVisitor;
@@ -134,7 +133,6 @@ actual, assertion, getArgumentsTemplate(matcherArgumentMethod)))
 
         private final MethodMatcher CLOSE_TO_MATCHER = new MethodMatcher("org.hamcrest.Matchers closeTo(..)");
 
-        @NotNull
         private String getArgumentsTemplate(J.MethodInvocation matcherArgument) {
             List<Expression> methodArguments = matcherArgument.getArguments();
             if (CLOSE_TO_MATCHER.matches(matcherArgument)) {
diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestOfMatchersToAssertJ.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestOfMatchersToAssertJ.java
index 990645e99..210172b14 100644
--- a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestOfMatchersToAssertJ.java
+++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestOfMatchersToAssertJ.java
@@ -32,7 +32,6 @@
 import java.util.List;
 import java.util.stream.Collectors;
 
-@SuppressWarnings("NullableProblems")
 public class HamcrestOfMatchersToAssertJ extends Recipe {
     @Override
     public String getDisplayName() {
diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcher.java b/src/main/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcher.java
index 8e21e0b1f..7d0bd1237 100644
--- a/src/main/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcher.java
+++ b/src/main/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcher.java
@@ -24,7 +24,6 @@
 import org.openrewrite.java.search.UsesMethod;
 import org.openrewrite.java.tree.J;
 
-@SuppressWarnings("NullableProblems")
 public class RemoveIsMatcher extends Recipe {
     @Override
     public String getDisplayName() {
diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/package-info.java b/src/main/java/org/openrewrite/java/testing/hamcrest/package-info.java
new file mode 100644
index 000000000..7f9f59ac6
--- /dev/null
+++ b/src/main/java/org/openrewrite/java/testing/hamcrest/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2024 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.
+ */
+@NonNullApi
+@NonNullFields
+package org.openrewrite.java.testing.hamcrest;
+
+import org.openrewrite.internal.lang.NonNullApi;
+import org.openrewrite.internal.lang.NonNullFields;
diff --git a/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java b/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java
index ff1c9cd26..9f0db001a 100644
--- a/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java
+++ b/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java
@@ -15,7 +15,6 @@
  */
 package org.openrewrite.java.testing.junit5;
 
-import org.jetbrains.annotations.NotNull;
 import org.openrewrite.ExecutionContext;
 import org.openrewrite.Preconditions;
 import org.openrewrite.Recipe;
@@ -116,7 +115,6 @@ private static boolean isException(Expression expression) {
             return expression instanceof J.Identifier && TypeUtils.isAssignableTo("java.lang.Throwable", expression.getType());
         }
 
-        @NotNull
         private J.MethodInvocation replaceWithAssertDoesNotThrowWithoutStringExpression(ExecutionContext ctx, J.Try try_) {
             maybeAddImport("org.junit.jupiter.api.Assertions");
             maybeRemoveCatchTypes(try_);
@@ -139,7 +137,6 @@ private void maybeRemoveCatchTypes(J.Try try_) {
             }
         }
 
-        @NotNull
         private J.MethodInvocation replaceWithAssertDoesNotThrowWithStringExpression(ExecutionContext ctx, J.Try try_, Expression failCallArgument) {
             // Retain the fail(String) call argument
             maybeAddImport("org.junit.jupiter.api.Assertions");
diff --git a/src/main/java/org/openrewrite/java/testing/testcontainers/ExplicitContainerImage.java b/src/main/java/org/openrewrite/java/testing/testcontainers/ExplicitContainerImage.java
index f166ebd17..9599ae40a 100644
--- a/src/main/java/org/openrewrite/java/testing/testcontainers/ExplicitContainerImage.java
+++ b/src/main/java/org/openrewrite/java/testing/testcontainers/ExplicitContainerImage.java
@@ -16,7 +16,6 @@
 package org.openrewrite.java.testing.testcontainers;
 
 import lombok.RequiredArgsConstructor;
-import org.jetbrains.annotations.NotNull;
 import org.openrewrite.*;
 import org.openrewrite.java.JavaIsoVisitor;
 import org.openrewrite.java.JavaParser;
@@ -75,7 +74,6 @@ public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
                 return nc;
             }
 
-            @NotNull
             private Expression getConstructorArgument(J.NewClass newClass) {
                 if (parseImage != null && parseImage) {
                     maybeAddImport("org.testcontainers.utility.DockerImageName");