Skip to content

Commit

Permalink
refactor: Recipe nullability best practices (#487)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Add package-info.java to Hamcrest package

---------

Co-authored-by: Moderne <[email protected]>
  • Loading branch information
timtebeek and TeamModerne authored Feb 28, 2024
1 parent edf0608 commit 687f7df
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.List;
import java.util.stream.Collectors;

@SuppressWarnings("NullableProblems")
public class HamcrestOfMatchersToAssertJ extends Recipe {
@Override
public String getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_);
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 687f7df

Please sign in to comment.