Skip to content

Commit

Permalink
refactor: Move @Nullable method annotations to the return type
Browse files Browse the repository at this point in the history
  • Loading branch information
2 people authored and app committed Jul 16, 2024
1 parent c29138f commit 6f251ab
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ private boolean isConvertibleBigDecimalConstant(J elem) {
return false;
}

@Nullable
private String getTemplateText(J elem) {
private @Nullable String getTemplateText(J elem) {
String roundingName = null;
if (elem instanceof J.FieldAccess && ((J.FieldAccess) elem).getTarget().getType() instanceof JavaType.FullyQualified) {
J.FieldAccess fa = (J.FieldAccess) elem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ public static J.Binary concatAdditionBinary(Expression left, Expression right) {
/**
* Concat expressions to an expression with '+' connected.
*/
@Nullable
public static Expression additiveExpression(Expression... expressions) {
public static @Nullable Expression additiveExpression(Expression... expressions) {
Expression expression = null;
for (Expression element : expressions) {
if (element != null) {
Expand All @@ -143,8 +142,7 @@ public static Expression additiveExpression(Expression... expressions) {
return expression;
}

@Nullable
public static Expression additiveExpression(List<Expression> expressions) {
public static @Nullable Expression additiveExpression(List<Expression> expressions) {
return additiveExpression(expressions.toArray(new Expression[0]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ private J.VariableDeclarations maybeAddTypeExpression(J.VariableDeclarations mul
return multiVariable;
}

@Nullable
private TypeTree buildTypeTree(@Nullable JavaType type, Space space) {
private @Nullable TypeTree buildTypeTree(@Nullable JavaType type, Space space) {
if (type == null || type instanceof JavaType.Unknown) {
return null;
} else if (type instanceof JavaType.Primitive) {
Expand Down Expand Up @@ -209,8 +208,7 @@ private TypeTree buildTypeTree(@Nullable JavaType type, Space space) {
return null;
}

@Nullable
private JContainer<Expression> buildTypeParameters(List<JavaType> typeParameters) {
private @Nullable JContainer<Expression> buildTypeParameters(List<JavaType> typeParameters) {
List<JRightPadded<Expression>> typeExpressions = new ArrayList<>();

for (JavaType type : typeParameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ private static boolean isInLambda(Cursor cursor) {
@Value
@EqualsAndHashCode(callSuper = false)
private static class FindLastIdentifier extends JavaIsoVisitor<List<J.Identifier>> {

/**
* Find the last identifier in a J.FieldAccess. The purpose is to check whether it's a private field.
*
* @param j the subtree to search, supposed to be a J.FieldAccess
* @return the last Identifier if found, otherwise null.
*/
@Nullable
static J.Identifier getLastIdentifier(J j) {
static @Nullable J.Identifier getLastIdentifier(J j) {
List<J.Identifier> ids = new FindLastIdentifier().reduce(j, new ArrayList<>());
return !ids.isEmpty() ? ids.get(ids.size() - 1) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public J visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {

//noinspection ConstantConditions
f = f.withBody((Statement) new JavaVisitor<ExecutionContext>() {
@Nullable

@Override
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
return tree == unary ? null : super.visit(tree, ctx);
}
}.visit(f.getBody(), ctx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
return bl;
}

@Nullable
private String identReturned(List<Statement> stats) {
private @Nullable String identReturned(List<Statement> stats) {
Statement lastStatement = stats.get(stats.size() - 1);
if (lastStatement instanceof J.Return) {
J.Return return_ = (J.Return) lastStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ private String patternVariableName(J.InstanceOf instanceOf, Cursor cursor) {
return VariableNameUtils.generateVariableName(baseName, cursor, INCREMENT_NUMBER);
}

@Nullable
public J processTypeCast(J.TypeCast typeCast, Cursor cursor) {
public @Nullable J processTypeCast(J.TypeCast typeCast, Cursor cursor) {
J.InstanceOf instanceOf = replacements.get(typeCast);
if (instanceOf != null && instanceOf.getPattern() != null) {
String name = ((J.Identifier) instanceOf.getPattern()).getSimpleName();
Expand All @@ -288,8 +287,7 @@ public J processTypeCast(J.TypeCast typeCast, Cursor cursor) {
return null;
}

@Nullable
public J processVariableDeclarations(J.VariableDeclarations multiVariable) {
public @Nullable J processVariableDeclarations(J.VariableDeclarations multiVariable) {
return multiVariable.getVariables().stream().anyMatch(variablesToDelete::containsValue) ? null : multiVariable;
}
}
Expand All @@ -302,8 +300,7 @@ public UseInstanceOfPatternMatching(InstanceOfPatternReplacements replacements)
this.replacements = replacements;
}

@Nullable
static J refactor(@Nullable J tree, InstanceOfPatternReplacements replacements, Cursor cursor) {
static @Nullable J refactor(@Nullable J tree, InstanceOfPatternReplacements replacements, Cursor cursor) {
return new UseInstanceOfPatternMatching(replacements).visit(tree, 0, cursor);
}

Expand Down Expand Up @@ -362,8 +359,7 @@ public J visitTypeCast(J.TypeCast typeCast, Integer executionContext) {

@SuppressWarnings("NullableProblems")
@Override
@Nullable
public J visitVariableDeclarations(J.VariableDeclarations multiVariable, Integer integer) {
public @Nullable J visitVariableDeclarations(J.VariableDeclarations multiVariable, Integer integer) {
multiVariable = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, integer);
return replacements.processVariableDeclarations(multiVariable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ static J.MemberReference newInstanceMethodReference(Expression containing, Strin
);
}

@Nullable
static J.FieldAccess newClassLiteral(@Nullable JavaType type, boolean qualified) {
static @Nullable J.FieldAccess newClassLiteral(@Nullable JavaType type, boolean qualified) {
JavaType.Class classType = getClassType(type);
if (classType == null) {
return null;
Expand All @@ -142,8 +141,7 @@ static J.FieldAccess newClassLiteral(@Nullable JavaType type, boolean qualified)
);
}

@Nullable
private static JavaType.Class getClassType(@Nullable JavaType type) {
private static @Nullable JavaType.Class getClassType(@Nullable JavaType type) {
if (type instanceof JavaType.Class) {
JavaType.Class classType = (JavaType.Class) type;
if (classType.getFullyQualifiedName().equals("java.lang.Class")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return mi;
}

@Nullable
private Expression getSingleArg(@Nullable List<Expression> args) {
private @Nullable Expression getSingleArg(@Nullable List<Expression> args) {
if (args != null && args.size() == 1 && !(args.get(0) instanceof J.Empty)) {
return args.get(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,18 @@ public class RemoveMethodCallVisitor<P> extends JavaIsoVisitor<P> {
private final BiPredicate<Integer, Expression> argumentPredicate;

@SuppressWarnings("NullableProblems")
@Nullable
@Override
public J.NewClass visitNewClass(J.NewClass newClass, P p) {
public @Nullable J.NewClass visitNewClass(J.NewClass newClass, P p) {
return visitMethodCall(newClass, () -> super.visitNewClass(newClass, p));
}

@SuppressWarnings("NullableProblems")
@Nullable
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, P p) {
public @Nullable J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, P p) {
return visitMethodCall(method, () -> super.visitMethodInvocation(method, p));
}

@Nullable
private <M extends MethodCall> M visitMethodCall(M methodCall, Supplier<M> visitSuper) {
private <M extends MethodCall> @Nullable M visitMethodCall(M methodCall, Supplier<M> visitSuper) {
if (!methodMatcher.matches(methodCall)) {
return visitSuper.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ protected String computeKey(String identifier, J context) {
return identifier;
}

@Nullable
protected JavaType.Variable getFieldType(J tree) {
protected @Nullable JavaType.Variable getFieldType(J tree) {
if (tree instanceof J.Identifier) {
return ((J.Identifier) tree).getFieldType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
* @return The name of a numeric variable being assigned or null if not a numeric
* variable assignment.
*/
@Nullable
private String numericVariableName(Statement s) {
private @Nullable String numericVariableName(Statement s) {
if (s instanceof J.Assignment) {
return singleVariableName(((J.Assignment) s).getVariable());
} else if (s instanceof J.VariableDeclarations) {
Expand All @@ -114,8 +113,7 @@ private String numericVariableName(Statement s) {
return null;
}

@Nullable
private Expression numericVariableAccumulation(Statement s, String name) {
private @Nullable Expression numericVariableAccumulation(Statement s, String name) {
if (s instanceof J.Unary) {
if (name.equals(singleVariableName(((J.Unary) s).getExpression()))) {
return new J.Literal(Tree.randomId(), Space.EMPTY, Markers.EMPTY, 1, "1", null,
Expand All @@ -130,8 +128,7 @@ private Expression numericVariableAccumulation(Statement s, String name) {
return null;
}

@Nullable
private String numericVariableOperator(Statement s, String name) {
private @Nullable String numericVariableOperator(Statement s, String name) {
if (s instanceof J.Unary) {
if (name.equals(singleVariableName(((J.Unary) s).getExpression()))) {
switch (((J.Unary) s).getOperator()) {
Expand Down Expand Up @@ -175,8 +172,7 @@ private String numericVariableOperator(Statement s, String name) {
return null;
}

@Nullable
private String singleVariableName(Expression e) {
private @Nullable String singleVariableName(Expression e) {
JavaType.Primitive type = TypeUtils.asPrimitive(e.getType());
return type != null && type.isNumeric() && e instanceof J.Identifier ?
((J.Identifier) e).getSimpleName() :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
});
}

@Nullable
public static Long getConstantIntegralValue(Expression expression) {
public static @Nullable Long getConstantIntegralValue(Expression expression) {
if (expression instanceof J.Literal) {
J.Literal literal = (J.Literal) expression;
if (literal.getType() != JavaType.Primitive.Int && literal.getType() != JavaType.Primitive.Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ private Optional<J.Identifier> findConditionIdentifier(final J.Ternary ternary)

}

@Nullable
private static J.Identifier xorVariable(J first, J second) {
private static @Nullable J.Identifier xorVariable(J first, J second) {
J.Identifier result = null;
if (isVariable(first) && isVariable(second)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
if (!unusedThrows.isEmpty()) {

new JavaIsoVisitor<ExecutionContext>() {
@Nullable

@Override
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (unusedThrows.isEmpty()) {
return (J) tree;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
return nv;
}

@Nullable
private String upperCaseSuffix(@Nullable String valueSource) {
private @Nullable String upperCaseSuffix(@Nullable String valueSource) {
if (valueSource == null || valueSource.length() < 2) {
return valueSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ public J.Return visitReturn(J.Return _return, ExecutionContext ctx) {
return return_;
}

@Nullable
private List<JavaType> parameterizedTypes(J.ParameterizedType parameterizedType) {
private @Nullable List<JavaType> parameterizedTypes(J.ParameterizedType parameterizedType) {
if (parameterizedType.getTypeParameters() == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,8 @@ public J visitNewClass(J.NewClass newClass, List<String> variables) {
return newClass;
}

@Nullable
@Override
public J visit(@Nullable Tree tree, List<String> variables) {
public @Nullable J visit(@Nullable Tree tree, List<String> variables) {
if (getCursor().getNearestMessage("stop") != null) {
return (J) tree;
}
Expand Down Expand Up @@ -427,8 +426,7 @@ public J visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBo
}

// TODO consider moving to TypeUtils
@Nullable
private static JavaType.Method getSamCompatible(@Nullable JavaType type) {
private static @Nullable JavaType.Method getSamCompatible(@Nullable JavaType type) {
JavaType.Method sam = null;
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(type);
if (fullyQualified == null) {
Expand Down

0 comments on commit 6f251ab

Please sign in to comment.