Skip to content

Commit

Permalink
refactor: add @nullable to methods who may return null (#621)
Browse files Browse the repository at this point in the history
Use this link to re-run the recipe: https://app.moderne.io/builder/ji8mLIdUI?organizationId=T3BlblJld3JpdGU%3D

Co-authored-by: Moderne <[email protected]>
  • Loading branch information
nielsdebruin and TeamModerne authored Oct 28, 2024
1 parent 36074f0 commit 386f3d4
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java.testing.cleanup;

import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
Expand Down Expand Up @@ -53,8 +54,9 @@ public Set<String> getTags() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new FindEmptyMethods(false), new JavaVisitor<ExecutionContext>() {

@Override
public J visitMethodDeclaration(MethodDeclaration method, ExecutionContext ctx) {
public @Nullable J visitMethodDeclaration(MethodDeclaration method, ExecutionContext ctx) {
if (hasTestAnnotation(method) && isEmptyMethod(method)) {
//noinspection ConstantConditions
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon

@SuppressWarnings("ConstantConditions")
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if ((mi.getSelect() != null && TypeUtils.isOfClassType(mi.getSelect().getType(), "junit.framework.TestCase")) ||
(mi.getMethodType() != null && TypeUtils.isOfClassType(mi.getMethodType().getDeclaringType(), "junit.framework.TestCase"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private boolean isRuleAnnotatedTemporaryFolder(J.VariableDeclarations vd) {
}

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
public @Nullable J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
updateCursor(mi);
if (mi.getSelect() != null && mi.getMethodType() != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}

@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
public J.@Nullable NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
J.NewClass nc = super.visitNewClass(newClass, ctx);
if (TypeUtils.isOfClassType(nc.getType(), "org.junit.rules.TestName")) {
//noinspection ConstantConditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public J visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx
}

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
public @Nullable J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);

Map<String, J.MethodInvocation> mockStaticInvocationsByClassName = getCursor().getNearestMessage(MOCK_STATIC_INVOCATIONS);
Expand Down

0 comments on commit 386f3d4

Please sign in to comment.