Skip to content

Commit

Permalink
refactor: add @nullable to methods who may return null (openrewrite#383)
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 a220385 commit be381dc
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static class RemoveCatches extends JavaVisitor<ExecutionContext> {
}

@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())) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,8 +56,9 @@ public Duration getEstimatedEffortPerOccurrence() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(OBJECT_FINALIZE), new JavaIsoVisitor<ExecutionContext>() {

@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()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,8 +57,9 @@ public Duration getEstimatedEffortPerOccurrence() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.or(new UsesMethod<>(SYSTEM_GC), new UsesMethod<>(RUNTIME_GC)), new JavaIsoVisitor<ExecutionContext>() {

@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Javadoc> newBody = new ArrayList<>(javadoc.getBody().size());
boolean useNewBody = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,8 +41,9 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(SYSTEM_OUT_PRINTLN), new JavaIsoVisitor<ExecutionContext>() {

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import org.openrewrite.java.tree.JLeftPadded;

public class SimplifyCompoundVisitor extends JavaVisitor<ExecutionContext> {

@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)) {
Expand Down

0 comments on commit be381dc

Please sign in to comment.