Skip to content

Commit 1addb92

Browse files
authored
Various cleanups suggested by IntelliJ (#1423)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Refactor** * Code modernization: Updated internal implementation to use modern Java language features and improved patterns for better code clarity and maintainability. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 22aa129 commit 1addb92

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

nullaway/src/main/java/com/uber/nullaway/NullAway.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public NullAway(ErrorProneFlags flags) {
316316
genericsChecks = new GenericsChecks(this, config, handler);
317317
}
318318

319-
public boolean isMethodUnannotated(MethodInvocationNode invocationNode) {
319+
public boolean isMethodUnannotated(@Nullable MethodInvocationNode invocationNode) {
320320
return invocationNode == null
321321
|| codeAnnotationInfo.isSymbolUnannotated(
322322
ASTHelpers.getSymbol(invocationNode.getTree()), config, handler);
@@ -518,9 +518,8 @@ public Description matchAssignment(AssignmentTree tree, VisitorState state) {
518518
genericsChecks.checkTypeParameterNullnessForAssignability(tree, state);
519519
}
520520

521-
if (config.isJSpecifyMode() && tree.getVariable() instanceof ArrayAccessTree) {
521+
if (config.isJSpecifyMode() && tree.getVariable() instanceof ArrayAccessTree arrayAccess) {
522522
// check for a write of a @Nullable value into @NonNull array contents
523-
ArrayAccessTree arrayAccess = (ArrayAccessTree) tree.getVariable();
524523
ExpressionTree arrayExpr = arrayAccess.getExpression();
525524
ExpressionTree expression = tree.getExpression();
526525
Symbol arraySymbol = ASTHelpers.getSymbol(arrayExpr);
@@ -856,7 +855,7 @@ private Description checkParamOverriding(
856855
+ "functional interface method "
857856
+ ASTHelpers.enclosingClass(overriddenMethod)
858857
+ "."
859-
+ overriddenMethod.toString()
858+
+ overriddenMethod
860859
+ " is @Nullable";
861860
return errorBuilder.createErrorDescription(
862861
new ErrorMessage(MessageTypes.WRONG_OVERRIDE_PARAM, message),
@@ -898,7 +897,7 @@ private Description checkParamOverriding(
898897
+ "method "
899898
+ ASTHelpers.enclosingClass(overriddenMethod)
900899
+ "."
901-
+ overriddenMethod.toString()
900+
+ overriddenMethod
902901
+ " is @Nullable";
903902
Tree errorTree;
904903
if (memberReferenceTree != null) {
@@ -1109,14 +1108,14 @@ && getMethodReturnNullness(overridingMethod, state, Nullness.NONNULL)
11091108
"referenced method returns @Nullable, but functional interface method "
11101109
+ ASTHelpers.enclosingClass(overriddenMethod)
11111110
+ "."
1112-
+ overriddenMethod.toString()
1111+
+ overriddenMethod
11131112
+ " returns @NonNull";
11141113
} else {
11151114
message =
11161115
"method returns @Nullable, but superclass method "
11171116
+ ASTHelpers.enclosingClass(overriddenMethod)
11181117
+ "."
1119-
+ overriddenMethod.toString()
1118+
+ overriddenMethod
11201119
+ " returns @NonNull";
11211120
}
11221121

@@ -1375,7 +1374,7 @@ private ImmutableSet<Element> safeInitByCalleeBefore(
13751374
if (curStmt instanceof TryTree tryTree) {
13761375
// ToDo: Should we check initialization inside tryTree.getResources ? What is the scope of
13771376
// that initialization?
1378-
if (tryTree.getCatches().size() == 0) {
1377+
if (tryTree.getCatches().isEmpty()) {
13791378
if (tryTree.getBlock() != null) {
13801379
resultBuilder.addAll(
13811380
safeInitByCalleeBefore(
@@ -1458,7 +1457,7 @@ private ImmutableMultimap<Tree, Element> computeTree2Init(
14581457
}
14591458
}
14601459
// all the initializer blocks have run before any code inside a constructor
1461-
constructors.stream().forEach((c) -> builder.putAll(c, initThusFar));
1460+
constructors.forEach((c) -> builder.putAll(c, initThusFar));
14621461
Symbol.ClassSymbol classSymbol = ASTHelpers.getSymbol(enclosingClass);
14631462
FieldInitEntities entities = castToNonNull(class2Entities.get(classSymbol));
14641463
if (entities.instanceInitializerMethods().size() == 1) {
@@ -1506,7 +1505,7 @@ private boolean okToReadBeforeInitialized(TreePath path, VisitorState state) {
15061505
// ok if it's invoking castToNonNull and the read is the argument
15071506
Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodInvoke);
15081507
String qualifiedName =
1509-
ASTHelpers.enclosingClass(methodSymbol) + "." + methodSymbol.getSimpleName().toString();
1508+
ASTHelpers.enclosingClass(methodSymbol) + "." + methodSymbol.getSimpleName();
15101509
List<? extends ExpressionTree> arguments = methodInvoke.getArguments();
15111510
Integer castToNonNullArg;
15121511
if (qualifiedName.equals(config.getCastToNonNullMethod())
@@ -1517,10 +1516,7 @@ private boolean okToReadBeforeInitialized(TreePath path, VisitorState state) {
15171516
handler.castToNonNullArgumentPositionsForMethod(
15181517
arguments, null, new MethodAnalysisContext(this, state, methodSymbol));
15191518
}
1520-
if (castToNonNullArg != null && leaf.equals(arguments.get(castToNonNullArg))) {
1521-
return true;
1522-
}
1523-
return false;
1519+
return castToNonNullArg != null && leaf.equals(arguments.get(castToNonNullArg));
15241520
}
15251521
return false;
15261522
}
@@ -2034,7 +2030,7 @@ private Description checkCastToNonNullTakesNullable(
20342030
Symbol.MethodSymbol methodSymbol,
20352031
List<? extends ExpressionTree> actualParams) {
20362032
String qualifiedName =
2037-
ASTHelpers.enclosingClass(methodSymbol) + "." + methodSymbol.getSimpleName().toString();
2033+
ASTHelpers.enclosingClass(methodSymbol) + "." + methodSymbol.getSimpleName();
20382034
Integer castToNonNullPosition;
20392035
if (qualifiedName.equals(config.getCastToNonNullMethod())
20402036
&& methodSymbol.getParameters().size() == 1) {
@@ -2055,8 +2051,7 @@ private Description checkCastToNonNullTakesNullable(
20552051
throw new RuntimeException("no enclosing method, lambda or initializer!");
20562052
} else if (enclosingMethodOrLambda.getLeaf() instanceof LambdaExpressionTree) {
20572053
isInitializer = false;
2058-
} else if (enclosingMethodOrLambda.getLeaf() instanceof MethodTree) {
2059-
MethodTree enclosingMethod = (MethodTree) enclosingMethodOrLambda.getLeaf();
2054+
} else if (enclosingMethodOrLambda.getLeaf() instanceof MethodTree enclosingMethod) {
20602055
isInitializer = isInitializerMethod(state, ASTHelpers.getSymbol(enclosingMethod));
20612056
} else {
20622057
// Initializer block
@@ -2239,7 +2234,7 @@ private SetMultimap<MethodTree, Symbol> checkConstructorInitialization(
22392234
if (constructorInvokesAnother(constructor, state)) {
22402235
continue;
22412236
}
2242-
if (constructor.getParameters().size() == 0
2237+
if (constructor.getParameters().isEmpty()
22432238
&& (isExternalInitClass
22442239
|| symbolHasExternalInitAnnotation(ASTHelpers.getSymbol(constructor)))) {
22452240
// external framework initializes fields in this case
@@ -2281,18 +2276,16 @@ private ImmutableSet<Element> guaranteedNonNullForConstructor(
22812276
private boolean constructorInvokesAnother(MethodTree constructor, VisitorState state) {
22822277
BlockTree body = constructor.getBody();
22832278
List<? extends StatementTree> statements = body.getStatements();
2284-
if (statements.size() > 0) {
2279+
if (!statements.isEmpty()) {
22852280
StatementTree statementTree = statements.get(0);
2286-
if (isThisCall(statementTree, state)) {
2287-
return true;
2288-
}
2281+
return isThisCall(statementTree, state);
22892282
}
22902283
return false;
22912284
}
22922285

22932286
private Set<Symbol> notInitializedStatic(FieldInitEntities entities, VisitorState state) {
22942287
ImmutableSet<Symbol> nonNullStaticFields = entities.nonnullStaticFields();
2295-
Set<Element> initializedInStaticInitializers = new LinkedHashSet<Element>();
2288+
Set<Element> initializedInStaticInitializers = new LinkedHashSet<>();
22962289
AccessPathNullnessAnalysis nullnessAnalysis = getNullnessAnalysis(state);
22972290
for (BlockTree initializer : entities.staticInitializerBlocks()) {
22982291
Set<Element> nonnullAtExit =
@@ -2306,7 +2299,7 @@ private Set<Symbol> notInitializedStatic(FieldInitEntities entities, VisitorStat
23062299
new TreePath(state.getPath(), initializerMethod), state.context);
23072300
initializedInStaticInitializers.addAll(nonnullAtExit);
23082301
}
2309-
Set<Symbol> notInitializedStaticFields = new LinkedHashSet<Symbol>();
2302+
Set<Symbol> notInitializedStaticFields = new LinkedHashSet<>();
23102303
for (Symbol field : nonNullStaticFields) {
23112304
if (!initializedInStaticInitializers.contains(field)) {
23122305
notInitializedStaticFields.add(field);
@@ -2349,7 +2342,7 @@ private Set<Element> getSafeInitMethods(
23492342
// there is also an
23502343
// exception of the full method.
23512344
if (stmt instanceof TryTree tryTree) {
2352-
if (tryTree.getCatches().size() == 0) {
2345+
if (tryTree.getCatches().isEmpty()) {
23532346
if (tryTree.getBlock() != null) {
23542347
result.addAll(getSafeInitMethods(tryTree.getBlock(), classSymbol, state));
23552348
}
@@ -2683,12 +2676,10 @@ private boolean mayBeNullMethodCall(
26832676
"%s not found as a descendant of %s",
26842677
invocationTree,
26852678
path.getLeaf());
2686-
if (genericsChecks
2679+
return genericsChecks
26872680
.getGenericReturnNullnessAtInvocation(
26882681
exprSymbol, invocationTree, invocationPath, state, false)
2689-
.equals(Nullness.NULLABLE)) {
2690-
return true;
2691-
}
2682+
.equals(Nullness.NULLABLE);
26922683
}
26932684
return false;
26942685
}

nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private void reportInvalidTypeArgumentError(
271271
ErrorMessage.MessageTypes.TYPE_PARAMETER_CANNOT_BE_NULLABLE,
272272
String.format(
273273
"Type argument cannot be @Nullable, as method %s's type variable %s is not @Nullable",
274-
methodSymbol.toString(), typeVariable.tsym.toString()));
274+
methodSymbol, typeVariable.tsym.toString()));
275275
state.reportMatch(
276276
errorBuilder.createErrorDescription(
277277
errorMessage, analysis.buildDescription(tree), state, null));
@@ -420,9 +420,7 @@ private void reportInvalidOverridingMethodParamTypeError(
420420
private @Nullable Type getTreeType(Tree tree, VisitorState state) {
421421
tree = ASTHelpers.stripParentheses(tree);
422422
if (tree instanceof NewClassTree
423-
&& ((NewClassTree) tree).getIdentifier() instanceof ParameterizedTypeTree) {
424-
ParameterizedTypeTree paramTypedTree =
425-
(ParameterizedTypeTree) ((NewClassTree) tree).getIdentifier();
423+
&& ((NewClassTree) tree).getIdentifier() instanceof ParameterizedTypeTree paramTypedTree) {
426424
if (paramTypedTree.getTypeArguments().isEmpty()) {
427425
// diamond operator, which we do not yet support; for now, return null
428426
// TODO: support diamond operators
@@ -438,9 +436,8 @@ private void reportInvalidOverridingMethodParamTypeError(
438436
// type on the tree itself can be missing nested annotations for arrays; get the type from
439437
// the symbol for the variable instead
440438
result = castToNonNull(ASTHelpers.getSymbol(tree)).type;
441-
} else if (tree instanceof IdentifierTree) {
439+
} else if (tree instanceof IdentifierTree identifierTree) {
442440
// handle "this" specially, for cases where it appears inside an anonymous class
443-
IdentifierTree identifierTree = (IdentifierTree) tree;
444441
Symbol symbol = castToNonNull(ASTHelpers.getSymbol(identifierTree));
445442
if (identifierTree.getName().contentEquals("this")) {
446443
Symbol owner = symbol.owner;
@@ -469,10 +466,9 @@ private void reportInvalidOverridingMethodParamTypeError(
469466
if (result != null && !(result instanceof Type.TypeVar)) {
470467
result = symbol.type;
471468
}
472-
} else if (tree instanceof AssignmentTree) {
469+
} else if (tree instanceof AssignmentTree assignmentTree) {
473470
// type on the tree itself can be missing nested annotations for arrays; get the type from
474471
// the symbol for the assigned location instead, if available
475-
AssignmentTree assignmentTree = (AssignmentTree) tree;
476472
Symbol lhsSymbol = ASTHelpers.getSymbol(assignmentTree.getVariable());
477473
if (lhsSymbol != null) {
478474
result = lhsSymbol.type;
@@ -492,8 +488,7 @@ private void reportInvalidOverridingMethodParamTypeError(
492488
result =
493489
TypeSubstitutionUtils.restoreExplicitNullabilityAnnotations(
494490
returnType, result, config, Collections.emptyMap());
495-
} else if (tree instanceof MemberSelectTree) {
496-
MemberSelectTree memberSelectTree = (MemberSelectTree) tree;
491+
} else if (tree instanceof MemberSelectTree memberSelectTree) {
497492
Symbol memberSelectSymbol = ASTHelpers.getSymbol(memberSelectTree);
498493
if (memberSelectSymbol != null && memberSelectSymbol.getKind().isField()) {
499494
Type fieldType = memberSelectSymbol.type;
@@ -827,7 +822,7 @@ private void generateConstraintsForPseudoAssignment(
827822
allInvocations.add(invTree);
828823
generateConstraintsForCall(
829824
state, path, lhsType, false, solver, symbol, invTree, allInvocations);
830-
} else if (!(rhsExpr instanceof LambdaExpressionTree)) {
825+
} else if (!(rhsExpr instanceof LambdaExpressionTree lambda)) {
831826
Type argumentType = getTreeType(rhsExpr, state);
832827
if (argumentType == null) {
833828
// bail out of any checking involving raw types for now
@@ -836,7 +831,6 @@ private void generateConstraintsForPseudoAssignment(
836831
argumentType = refineArgumentTypeWithDataflow(argumentType, rhsExpr, state, path);
837832
solver.addSubtypeConstraint(argumentType, lhsType, false);
838833
} else {
839-
LambdaExpressionTree lambda = (LambdaExpressionTree) rhsExpr;
840834
handleLambdaInGenericMethodInference(state, path, solver, allInvocations, lhsType, lambda);
841835
}
842836
}
@@ -1632,17 +1626,15 @@ private InvocationAndContext getInvocationAndContextForInference(
16321626
NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer(parentPath);
16331627
// TODO handle lambdas; https://github.com/uber/NullAway/issues/1288
16341628
if (enclosingMethodOrLambda != null
1635-
&& enclosingMethodOrLambda.getLeaf() instanceof MethodTree) {
1636-
MethodTree enclosingMethod = (MethodTree) enclosingMethodOrLambda.getLeaf();
1629+
&& enclosingMethodOrLambda.getLeaf() instanceof MethodTree enclosingMethod) {
16371630
Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(enclosingMethod);
16381631
if (methodSymbol != null) {
16391632
return new InvocationAndContext(invocation, methodSymbol.getReturnType(), false);
16401633
}
16411634
}
1642-
} else if (parent instanceof ExpressionTree) {
1635+
} else if (parent instanceof ExpressionTree exprParent) {
16431636
// could be a parameter to another method call, or part of a conditional expression, etc.
16441637
// in any case, just return the type of the parent expression
1645-
ExpressionTree exprParent = (ExpressionTree) parent;
16461638
if (exprParent instanceof MethodInvocationTree parentInvocation) {
16471639
if (isGenericCallNeedingInference(parentInvocation)) {
16481640
// this is the case of a nested generic call, e.g., id(id(x)) where id is generic
@@ -1907,7 +1899,7 @@ private void checkTypeParameterNullnessForOverridingMethodParameterType(
19071899
for (int i = 0; i < methodParameters.size(); i++) {
19081900
Type overridingMethodParameterType = getTreeType(methodParameters.get(i), state);
19091901
Type overriddenMethodParameterType = overriddenMethodParameterTypes.get(i);
1910-
if (overriddenMethodParameterType != null && overridingMethodParameterType != null) {
1902+
if (overridingMethodParameterType != null) {
19111903
// allow contravariant subtyping
19121904
if (!subtypeParameterNullability(
19131905
overridingMethodParameterType, overriddenMethodParameterType, state)) {

0 commit comments

Comments
 (0)