From be381dc0cb1cd28e479cb466f88f33e7e0425f68 Mon Sep 17 00:00:00 2001 From: Niels de Bruin Date: Mon, 28 Oct 2024 11:47:07 +0100 Subject: [PATCH] refactor: add @Nullable to methods who may return null (#383) Use this link to re-run the recipe: https://app.moderne.io/builder/ji8mLIdUI?organizationId=T3BlblJld3JpdGU%3D Co-authored-by: Moderne --- .../staticanalysis/CombineSemanticallyEqualCatchBlocks.java | 4 ++-- .../staticanalysis/RemoveCallsToObjectFinalize.java | 4 +++- .../org/openrewrite/staticanalysis/RemoveCallsToSystemGc.java | 4 +++- .../staticanalysis/RemoveEmptyJavaDocParameters.java | 3 ++- .../openrewrite/staticanalysis/RemoveSystemOutPrintln.java | 4 +++- .../openrewrite/staticanalysis/RemoveUnneededAssertion.java | 3 ++- .../staticanalysis/RemoveUnusedLocalVariables.java | 2 +- .../openrewrite/staticanalysis/RemoveUnusedPrivateFields.java | 4 ++-- .../staticanalysis/RemoveUnusedPrivateMethods.java | 3 ++- .../openrewrite/staticanalysis/SimplifyCompoundVisitor.java | 3 ++- 10 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java b/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java index 9976e1c21..ed9f3ed4d 100644 --- a/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java +++ b/src/main/java/org/openrewrite/staticanalysis/CombineSemanticallyEqualCatchBlocks.java @@ -149,7 +149,7 @@ static class RemoveCatches extends JavaVisitor { } @Override - public J visitMultiCatch(J.MultiCatch multiCatch, ExecutionContext ctx) { + public @Nullable J visitMultiCatch(J.MultiCatch multiCatch, ExecutionContext ctx) { Cursor parentCursor = getCursor().dropParentUntil(is -> is instanceof J.Try.Catch || is instanceof J.Try); if (removeCatches != null && parentCursor.getValue() instanceof J.Try.Catch) { if (removeCatches.contains((J.Try.Catch) parentCursor.getValue())) { @@ -160,7 +160,7 @@ public J visitMultiCatch(J.MultiCatch multiCatch, ExecutionContext ctx) { } @Override - public J visitCatch(J.Try.Catch _catch, ExecutionContext ctx) { + public @Nullable J visitCatch(J.Try.Catch _catch, ExecutionContext ctx) { if (removeCatches != null) { if (removeCatches.contains(_catch)) { return null; diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToObjectFinalize.java b/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToObjectFinalize.java index 067fa55dc..6451b2ffc 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToObjectFinalize.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToObjectFinalize.java @@ -15,6 +15,7 @@ */ package org.openrewrite.staticanalysis; +import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; @@ -55,8 +56,9 @@ public Duration getEstimatedEffortPerOccurrence() { @Override public TreeVisitor getVisitor() { return Preconditions.check(new UsesMethod<>(OBJECT_FINALIZE), new JavaIsoVisitor() { + @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation invocation = super.visitMethodInvocation(method, ctx); if (invocation.getMethodType() != null && "finalize".equals(invocation.getMethodType().getName()) && diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToSystemGc.java b/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToSystemGc.java index a53b105ed..de297e84c 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToSystemGc.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveCallsToSystemGc.java @@ -15,6 +15,7 @@ */ package org.openrewrite.staticanalysis; +import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; @@ -56,8 +57,9 @@ public Duration getEstimatedEffortPerOccurrence() { @Override public TreeVisitor getVisitor() { return Preconditions.check(Preconditions.or(new UsesMethod<>(SYSTEM_GC), new UsesMethod<>(RUNTIME_GC)), new JavaIsoVisitor() { + @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation invocation = super.visitMethodInvocation(method, ctx); if (SYSTEM_GC.matches(invocation) || RUNTIME_GC.matches(invocation)) { doAfterVisit(new EmptyBlock().getVisitor()); diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveEmptyJavaDocParameters.java b/src/main/java/org/openrewrite/staticanalysis/RemoveEmptyJavaDocParameters.java index 033d5060e..2d473f852 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveEmptyJavaDocParameters.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveEmptyJavaDocParameters.java @@ -15,6 +15,7 @@ */ package org.openrewrite.staticanalysis; +import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Incubating; import org.openrewrite.Recipe; @@ -74,7 +75,7 @@ public RemoveEmptyParamVisitor() { } @Override - public Javadoc visitDocComment(Javadoc.DocComment javadoc, ExecutionContext ctx) { + public @Nullable Javadoc visitDocComment(Javadoc.DocComment javadoc, ExecutionContext ctx) { List newBody = new ArrayList<>(javadoc.getBody().size()); boolean useNewBody = false; diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintln.java b/src/main/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintln.java index 5a07e54ab..2d8126e8e 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintln.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintln.java @@ -15,6 +15,7 @@ */ package org.openrewrite.staticanalysis; +import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; @@ -40,8 +41,9 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { return Preconditions.check(new UsesMethod<>(SYSTEM_OUT_PRINTLN), new JavaIsoVisitor() { + @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { if (SYSTEM_OUT_PRINTLN.matches(method)) { //noinspection DataFlowIssue return null; diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveUnneededAssertion.java b/src/main/java/org/openrewrite/staticanalysis/RemoveUnneededAssertion.java index 1530a77cd..f5b7dd9ae 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveUnneededAssertion.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveUnneededAssertion.java @@ -15,6 +15,7 @@ */ package org.openrewrite.staticanalysis; +import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; @@ -137,7 +138,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon } @Override - public J.Assert visitAssert(J.Assert anAssert, ExecutionContext ctx) { + public J.@Nullable Assert visitAssert(J.Assert anAssert, ExecutionContext ctx) { if (anAssert.getCondition() instanceof J.Literal) { if (J.Literal.isLiteralValue(anAssert.getCondition(), true)) { //noinspection ConstantConditions diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java index dc08fde4c..54b0f1504 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java @@ -103,7 +103,7 @@ private Cursor getCursorToParentScope(Cursor cursor) { } @Override - public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) { + public J.VariableDeclarations.@Nullable NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) { // skip matching ignored variable names right away if (ignoreVariableNames != null && ignoreVariableNames.contains(variable.getSimpleName())) { return variable; diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateFields.java b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateFields.java index b3872fcea..842fcb5b8 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateFields.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateFields.java @@ -200,7 +200,7 @@ public RemoveUnusedField(J.VariableDeclarations.NamedVariable namedVariable) { } @Override - public J visitVariableDeclarations(J.VariableDeclarations multiVariable, AtomicBoolean declarationDeleted) { + public @Nullable J visitVariableDeclarations(J.VariableDeclarations multiVariable, AtomicBoolean declarationDeleted) { if (multiVariable.getVariables().size() == 1 && multiVariable.getVariables().contains(namedVariable)) { declarationDeleted.set(true); //noinspection ConstantConditions @@ -210,7 +210,7 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, AtomicB } @Override - public J visitVariable(J.VariableDeclarations.NamedVariable variable, AtomicBoolean declarationDeleted) { + public @Nullable J visitVariable(J.VariableDeclarations.NamedVariable variable, AtomicBoolean declarationDeleted) { if (variable == namedVariable) { //noinspection ConstantConditions return null; diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java index 6b422b900..6549d3111 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedPrivateMethods.java @@ -15,6 +15,7 @@ */ package org.openrewrite.staticanalysis; +import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; @@ -81,7 +82,7 @@ private boolean unusedWarningsSuppressed(J classDeclaration) { } @Override - public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, + public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx); JavaType.Method methodType = method.getMethodType(); diff --git a/src/main/java/org/openrewrite/staticanalysis/SimplifyCompoundVisitor.java b/src/main/java/org/openrewrite/staticanalysis/SimplifyCompoundVisitor.java index e386db1af..380b1541a 100644 --- a/src/main/java/org/openrewrite/staticanalysis/SimplifyCompoundVisitor.java +++ b/src/main/java/org/openrewrite/staticanalysis/SimplifyCompoundVisitor.java @@ -25,8 +25,9 @@ import org.openrewrite.java.tree.JLeftPadded; public class SimplifyCompoundVisitor extends JavaVisitor { + @Override - public J visitAssignmentOperation(J.AssignmentOperation assignOp, ExecutionContext ctx) { + public @Nullable J visitAssignmentOperation(J.AssignmentOperation assignOp, ExecutionContext ctx) { Expression cleanedUpAssignment = cleanupBooleanExpression(assignOp.getAssignment(), ctx); if (assignOp.getOperator() == J.AssignmentOperation.Type.BitAnd) { if (isLiteralTrue(cleanedUpAssignment)) {