Skip to content

Commit

Permalink
refactor: add @nullable to methods who may return null (#4622)
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 4ee1955 commit ae4e92e
Show file tree
Hide file tree
Showing 70 changed files with 191 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.github.classgraph.Resource;
import io.github.classgraph.ResourceList;
import io.github.classgraph.ScanResult;
import org.jspecify.annotations.Nullable;
import org.openrewrite.table.ClasspathReport;

import java.net.URI;
Expand All @@ -42,7 +43,7 @@ public String getDescription() {
}

@Override
public Void getInitialValue(ExecutionContext ctx) {
public @Nullable Void getInitialValue(ExecutionContext ctx) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.config;

import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Recipe;

import java.time.Duration;
Expand All @@ -41,7 +42,7 @@ public String getDescription() {
}

@Override
public Duration getEstimatedEffortPerOccurrence() {
public @Nullable Duration getEstimatedEffortPerOccurrence() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ static abstract class InnerNode extends Node {
final byte[] prefixKeys = new byte[PESSIMISTIC_PATH_COMPRESSION_LIMIT];
int prefixLen;
short noOfChildren;
final @Nullable Node[] child;
final Node @Nullable[] child;

InnerNode(int size) {
child = new Node[size + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.internal;

import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.marker.Markup;
Expand All @@ -39,7 +40,7 @@ public FindRecipeRunException(RecipeRunException rre) {
}

@Override
public Tree preVisit(Tree tree, Integer integer) {
public @Nullable Tree preVisit(Tree tree, Integer integer) {
if (nearestTree == null) {
return null;
} else if (tree == nearestTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static <T> List<T> concat(@Nullable List<T> ls, @Nullable T t) {
return newLs;
}

public static <T> List<T> concat(@Nullable T t, @Nullable List<T> ls) {
public static <T> @Nullable List<T> concat(@Nullable T t, @Nullable List<T> ls) {
if (t == null && ls == null) {
//noinspection ConstantConditions
return null;
Expand All @@ -288,7 +288,7 @@ public static <T> List<T> concat(@Nullable T t, @Nullable List<T> ls) {
return newLs;
}

public static <T> List<T> concatAll(@Nullable List<T> ls, @Nullable List<? extends T> t) {
public static <T> @Nullable List<T> concatAll(@Nullable List<T> ls, @Nullable List<? extends T> t) {
if (ls == null && t == null) {
//noinspection ConstantConditions
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class StringUtils {
private StringUtils() {
}

public static String trimIndentPreserveCRLF(@Nullable String text) {
public static @Nullable String trimIndentPreserveCRLF(@Nullable String text) {
if (text == null) {
//noinspection DataFlowIssue
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static <T extends Tree> T found(@Nullable T t) {
return found(t, null);
}

public static <T extends Tree> T found(@Nullable T t, @Nullable String description) {
public static <T extends Tree> @Nullable T found(@Nullable T t, @Nullable String description) {
if (t == null) {
//noinspection ConstantConditions
return null;
Expand All @@ -71,7 +71,7 @@ public static <T extends Tree> T mergingFound(@Nullable T t, String description)
* @param delimiter The delimiter to use when merging descriptions.
*/
@Incubating(since = "8.0.0")
public static <T extends Tree> T mergingFound(@Nullable T t, String description, String delimiter) {
public static <T extends Tree> @Nullable T mergingFound(@Nullable T t, String description, String delimiter) {
Objects.requireNonNull(delimiter, "delimiter must not be null");
if (t == null) {
//noinspection ConstantConditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ static class GroupArtifactVersionBecause {
}

static class RemoveConstraints extends GroovyIsoVisitor<List<GroupArtifactVersionBecause>> {

@SuppressWarnings("DataFlowIssue")
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, List<GroupArtifactVersionBecause> groupArtifactVersions) {
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, List<GroupArtifactVersionBecause> groupArtifactVersions) {
J.MethodInvocation m = super.visitMethodInvocation(method, groupArtifactVersions);
if ("constraints".equals(m.getSimpleName()) && isInDependenciesBlock(getCursor())) {
if (!(m.getArguments().get(0) instanceof J.Lambda)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) {
}

@Override
public J visitReturn(J.Return return_, ExecutionContext ctx) {
public @Nullable J visitReturn(J.Return return_, ExecutionContext ctx) {
boolean dependencyInvocation = return_.getExpression() instanceof J.MethodInvocation && dependencyDsl.matches((J.MethodInvocation) return_.getExpression());
J.Return r = (J.Return) super.visitReturn(return_, ctx);
if (dependencyInvocation && r.getExpression() == null) {
Expand All @@ -134,7 +134,7 @@ public J visitReturn(J.Return return_, ExecutionContext ctx) {
}

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

GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.java.tree.J;
Expand All @@ -43,8 +44,9 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new IsSettingsGradle<>(), new GroovyIsoVisitor<ExecutionContext>() {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if ("enableFeaturePreview".equals(method.getSimpleName()) &&
method.getArguments().size() == 1 &&
J.Literal.isLiteralValue(method.getArguments().get(0), previewFeatureName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.groovy.GroovyVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -44,8 +45,9 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
MethodMatcher repositories = new MethodMatcher("org.gradle.api.artifacts.dsl.RepositoryHandler " + repository + "()");
return Preconditions.check(new IsBuildGradle<>(), new GroovyVisitor<ExecutionContext>() {

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
public @Nullable J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if(repositories.matches(method)) {
//noinspection ConstantConditions
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
private static class RemoveVersionVisitor extends GroovyIsoVisitor<ExecutionContext> {

@Override
public J.Return visitReturn(J.Return _return, ExecutionContext ctx) {
public J.@Nullable Return visitReturn(J.Return _return, ExecutionContext ctx) {
J.Return r = super.visitReturn(_return, ctx);
if(r.getExpression() == null) {
//noinspection DataFlowIssue
Expand All @@ -527,7 +527,7 @@ public J.Return visitReturn(J.Return _return, ExecutionContext ctx) {
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if("version".equals(m.getSimpleName()) && m.getArguments().size() == 1 && m.getArguments().get(0) instanceof J.Lambda) {
//noinspection DataFlowIssue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.tree.G;
Expand Down Expand Up @@ -91,7 +92,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
}

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

if (buildPluginsContainerMatcher.matches(m) || settingsPluginsContainerMatcher.matches(m)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2504,7 +2504,7 @@ private String name() {
Can contain a J.FieldAccess, as in a variable declaration with fully qualified type parameterization:
List<java.lang.String>
*/
private JContainer<Expression> visitTypeParameterizations(@Nullable GenericsType[] genericsTypes) {
private JContainer<Expression> visitTypeParameterizations(GenericsType @Nullable[] genericsTypes) {
Space prefix = sourceBefore("<");
List<JRightPadded<Expression>> parameters;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
Expand Down Expand Up @@ -47,8 +48,9 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
JsonPathMatcher pathMatcher = new JsonPathMatcher(contentPath);
return new HclIsoVisitor<ExecutionContext>() {

@Override
public BodyContent visitBodyContent(BodyContent bodyContent, ExecutionContext ctx) {
public @Nullable BodyContent visitBodyContent(BodyContent bodyContent, ExecutionContext ctx) {
BodyContent b = super.visitBodyContent(bodyContent, ctx);
if (pathMatcher.matches(getCursor())) {
//noinspection ConstantConditions
Expand Down
4 changes: 2 additions & 2 deletions rewrite-hcl/src/main/java/org/openrewrite/hcl/HclVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public Hcl visitExpression(Expression expression, P p) {
return expression;
}

public <T> HclLeftPadded<T> visitLeftPadded(HclLeftPadded<T> left, HclLeftPadded.Location loc, P p) {
public <T> @Nullable HclLeftPadded<T> visitLeftPadded(HclLeftPadded<T> left, HclLeftPadded.Location loc, P p) {
setCursor(new Cursor(getCursor(), left));

Space before = visitSpace(left.getBefore(), loc.getBeforeLocation(), p);
Expand All @@ -408,7 +408,7 @@ public <T> HclLeftPadded<T> visitLeftPadded(HclLeftPadded<T> left, HclLeftPadded
return (before == left.getBefore() && t == left.getElement()) ? left : new HclLeftPadded<>(before, t, left.getMarkers());
}

public <T> HclRightPadded<T> visitRightPadded(@Nullable HclRightPadded<T> right, HclRightPadded.Location loc, P p) {
public <T> @Nullable HclRightPadded<T> visitRightPadded(@Nullable HclRightPadded<T> right, HclRightPadded.Location loc, P p) {
if (right == null) {
//noinspection ConstantConditions
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Object visitJsonPath(JsonPathParser.JsonPathContext ctx) {
}

@Override
public Object visitRecursiveDecent(JsonPathParser.RecursiveDecentContext ctx) {
public @Nullable Object visitRecursiveDecent(JsonPathParser.RecursiveDecentContext ctx) {
if (scope == null) {
return null;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public Object visitRecursiveDecent(JsonPathParser.RecursiveDecentContext ctx) {
}

@Override
public Object visitBracketOperator(JsonPathParser.BracketOperatorContext ctx) {
public @Nullable Object visitBracketOperator(JsonPathParser.BracketOperatorContext ctx) {
if (!ctx.property().isEmpty()) {
if (ctx.property().size() == 1) {
return visitProperty(ctx.property(0));
Expand Down Expand Up @@ -256,7 +256,7 @@ public Object visitIndexes(JsonPathParser.IndexesContext ctx) {
}

@Override
public Object visitProperty(JsonPathParser.PropertyContext ctx) {
public @Nullable Object visitProperty(JsonPathParser.PropertyContext ctx) {
if (scope instanceof Hcl.Block) {
Hcl.Block block = (Hcl.Block) scope;
List<Object> matches = new ArrayList<>();
Expand Down Expand Up @@ -320,7 +320,7 @@ private JsonPathParser.ExpressionContext getExpressionContext(ParserRuleContext
}

@Override
public Object visitWildcard(JsonPathParser.WildcardContext ctx) {
public @Nullable Object visitWildcard(JsonPathParser.WildcardContext ctx) {
if (scope instanceof Hcl.Attribute) {
Hcl.Attribute attr = (Hcl.Attribute) scope;
return attr.getValue();
Expand Down Expand Up @@ -376,7 +376,7 @@ public Object visitLiteralExpression(JsonPathParser.LiteralExpressionContext ctx
}

@Override
public Object visitUnaryExpression(JsonPathParser.UnaryExpressionContext ctx) {
public @Nullable Object visitUnaryExpression(JsonPathParser.UnaryExpressionContext ctx) {
if (ctx.AT() != null) {
if (scope instanceof Hcl.Literal) {
if (ctx.Identifier() == null && ctx.StringLiteral() == null) {
Expand Down Expand Up @@ -440,7 +440,7 @@ public Object visitUnaryExpression(JsonPathParser.UnaryExpressionContext ctx) {
}

@Override
public Object visitRegexExpression(JsonPathParser.RegexExpressionContext ctx) {
public @Nullable Object visitRegexExpression(JsonPathParser.RegexExpressionContext ctx) {
if (scope == null || scope instanceof List && ((List<Object>) scope).isEmpty()) {
return null;
}
Expand All @@ -465,7 +465,7 @@ public Object visitRegexExpression(JsonPathParser.RegexExpressionContext ctx) {

// Checks if a string contains the specified substring (case-sensitive), or an array contains the specified element.
@Override
public Object visitContainsExpression(JsonPathParser.ContainsExpressionContext ctx) {
public @Nullable Object visitContainsExpression(JsonPathParser.ContainsExpressionContext ctx) {
Object originalScope = scope;
if (ctx.children.get(0) instanceof JsonPathParser.UnaryExpressionContext) {
Object lhs = visitUnaryExpression(ctx.unaryExpression());
Expand Down Expand Up @@ -507,7 +507,7 @@ public Object visitContainsExpression(JsonPathParser.ContainsExpressionContext c
}

@Override
public Object visitBinaryExpression(JsonPathParser.BinaryExpressionContext ctx) {
public @Nullable Object visitBinaryExpression(JsonPathParser.BinaryExpressionContext ctx) {
Object lhs = ctx.children.get(0);
Object rhs = ctx.children.get(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
Path dest = Paths.get(destinationPath);

return Preconditions.check(acc.toMove != null, new HclIsoVisitor<ExecutionContext>() {

@Override
public BodyContent visitBodyContent(BodyContent bodyContent, ExecutionContext ctx) {
public @Nullable BodyContent visitBodyContent(BodyContent bodyContent, ExecutionContext ctx) {
BodyContent b = super.visitBodyContent(bodyContent, ctx);
Path sourcePath = getCursor().firstEnclosingOrThrow(Hcl.ConfigFile.class).getSourcePath();
if (sourcePath.equals(from) && pathMatcher.matches(getCursor())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public Space visitSpace(Space space, Space.Location loc, P p) {
}

@Override
public <T> HclRightPadded<T> visitRightPadded(@Nullable HclRightPadded<T> right, HclRightPadded.Location loc, P p) {
public <T> @Nullable HclRightPadded<T> visitRightPadded(@Nullable HclRightPadded<T> right, HclRightPadded.Location loc, P p) {
if (right == null) {
//noinspection ConstantConditions
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RequiredArgsConstructor
Expand Down Expand Up @@ -63,7 +64,7 @@ public <H extends Hcl> List<H> unsubstitute(List<H> js) {
return ListUtils.map(js, this::unsubstitute);
}

public <H extends Hcl> H unsubstitute(H j) {
public <H extends Hcl> @Nullable H unsubstitute(H j) {
if (parameters.length == 0) {
return j;
}
Expand All @@ -90,7 +91,7 @@ public Hcl visitExpression(Expression expression, Integer integer) {

private @Nullable Integer parameterIndex(Space space) {
for (Comment comment : space.getComments()) {
java.util.regex.Matcher matcher = PATTERN_COMMENT.matcher(comment.getText());
Matcher matcher = PATTERN_COMMENT.matcher(comment.getText());
if (matcher.matches()) {
return Integer.valueOf(matcher.group(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private Space(@Nullable String whitespace, List<Comment> comments) {
}

@JsonCreator
public static Space build(@Nullable String whitespace, List<Comment> comments) {
public static @Nullable Space build(@Nullable String whitespace, List<Comment> comments) {
if (comments.isEmpty()) {
if (whitespace == null || whitespace.isEmpty()) {
return Space.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ public Tree visitProvides(ProvidesTree node, List<Javadoc> body) {
}

@Override
public J visitReference(@Nullable ReferenceTree node, List<Javadoc> body) {
public @Nullable J visitReference(@Nullable ReferenceTree node, List<Javadoc> body) {
DCTree.DCReference ref = (DCTree.DCReference) node;
if (node == null) {
//noinspection ConstantConditions
Expand Down
Loading

0 comments on commit ae4e92e

Please sign in to comment.