Skip to content

Commit 72fe0f1

Browse files
committed
SONARJAVA-7236 Mark local variables and parameters that are never reassigned as effectively final
1 parent cd4a3a4 commit 72fe0f1

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) SonarSource Sàrl
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* You can redistribute and/or modify this program under the terms of
7+
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java.model;
18+
19+
import java.util.ArrayList;
20+
import java.util.IdentityHashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
import org.sonar.plugins.java.api.semantic.Symbol;
24+
import org.sonar.plugins.java.api.tree.AssignmentExpressionTree;
25+
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
26+
import org.sonar.plugins.java.api.tree.ExpressionTree;
27+
import org.sonar.plugins.java.api.tree.IdentifierTree;
28+
import org.sonar.plugins.java.api.tree.Tree;
29+
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
30+
import org.sonar.plugins.java.api.tree.VariableTree;
31+
32+
/**
33+
* Marks the local variables and parameters that are never reassigned (JLS 4.12.4), as the compiler does.
34+
* A variable declared without a value stays effectively final after one plain assignment.
35+
*/
36+
final class EffectivelyFinalVariables extends BaseTreeVisitor {
37+
38+
private final Map<Symbol, int[]> assignments = new IdentityHashMap<>();
39+
private final List<VariableTree> variables = new ArrayList<>();
40+
41+
private EffectivelyFinalVariables() {
42+
}
43+
44+
static void mark(JavaTree.CompilationUnitTreeImpl unit) {
45+
EffectivelyFinalVariables visitor = new EffectivelyFinalVariables();
46+
visitor.scan(unit);
47+
for (VariableTree variable : visitor.variables) {
48+
if (variable.symbol() instanceof JVariableSymbol symbol) {
49+
int[] counts = visitor.assignments.getOrDefault(symbol, new int[2]);
50+
boolean unassigned = variable.initializer() == null && isLocalDeclaration(variable);
51+
symbol.effectivelyFinal(counts[1] == 0 && counts[0] <= (unassigned ? 1 : 0));
52+
}
53+
}
54+
}
55+
56+
private static boolean isLocalDeclaration(VariableTree variable) {
57+
Tree parent = variable.parent();
58+
return parent != null && parent.is(Tree.Kind.BLOCK, Tree.Kind.CASE_GROUP, Tree.Kind.STATIC_INITIALIZER, Tree.Kind.INITIALIZER, Tree.Kind.FOR_STATEMENT);
59+
}
60+
61+
@Override
62+
public void visitVariable(VariableTree tree) {
63+
if (tree.symbol() instanceof JVariableSymbol symbol && (symbol.isLocalVariable() || symbol.isParameter())) {
64+
variables.add(tree);
65+
}
66+
super.visitVariable(tree);
67+
}
68+
69+
@Override
70+
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
71+
record(tree.variable(), tree.is(Tree.Kind.ASSIGNMENT) ? 0 : 1);
72+
super.visitAssignmentExpression(tree);
73+
}
74+
75+
@Override
76+
public void visitUnaryExpression(UnaryExpressionTree tree) {
77+
if (tree.is(Tree.Kind.PREFIX_INCREMENT, Tree.Kind.PREFIX_DECREMENT, Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.POSTFIX_DECREMENT)) {
78+
record(tree.expression(), 1);
79+
}
80+
super.visitUnaryExpression(tree);
81+
}
82+
83+
private void record(ExpressionTree target, int slot) {
84+
ExpressionTree expression = target;
85+
while (expression instanceof org.sonar.plugins.java.api.tree.ParenthesizedTree parenthesized) {
86+
expression = parenthesized.expression();
87+
}
88+
if (expression instanceof IdentifierTree identifier) {
89+
assignments.computeIfAbsent(identifier.symbol(), key -> new int[2])[slot]++;
90+
}
91+
}
92+
}

‎java-frontend/src/main/java/org/sonar/java/model/SemsitterTranslator.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ static JavaTree.CompilationUnitTreeImpl translate(JSema sema, JsonObject documen
174174
unit.sema = sema;
175175
setParents(unit);
176176
CompilerWarnings.addTo(unit);
177+
EffectivelyFinalVariables.mark(unit);
177178
return unit;
178179
}
179180

0 commit comments

Comments
 (0)