Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.semsitter;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import org.sonar.java.model.JavaTree;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.AssignmentExpressionTree;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
import org.sonar.plugins.java.api.tree.VariableTree;

/**
* Marks the local variables and parameters that are never reassigned (JLS 4.12.4), as the compiler does.
* A variable declared without a value stays effectively final after one plain assignment.
*/
final class EffectivelyFinalVariables extends BaseTreeVisitor {

private final Map<Symbol, int[]> assignments = new IdentityHashMap<>();
private final List<VariableTree> variables = new ArrayList<>();

private EffectivelyFinalVariables() {
}

static void mark(JavaTree.CompilationUnitTreeImpl unit) {
EffectivelyFinalVariables visitor = new EffectivelyFinalVariables();
visitor.scan(unit);
for (VariableTree variable : visitor.variables) {
if (variable.symbol() instanceof JVariableSymbol symbol) {
int[] counts = visitor.assignments.getOrDefault(symbol, new int[2]);
boolean unassigned = variable.initializer() == null && isLocalDeclaration(variable);
symbol.effectivelyFinal(counts[1] == 0 && counts[0] <= (unassigned ? 1 : 0));
}
}
}

private static boolean isLocalDeclaration(VariableTree variable) {
Tree parent = variable.parent();
return parent != null && parent.is(Tree.Kind.BLOCK, Tree.Kind.CASE_GROUP, Tree.Kind.STATIC_INITIALIZER, Tree.Kind.INITIALIZER, Tree.Kind.FOR_STATEMENT);
}

@Override
public void visitVariable(VariableTree tree) {
if (tree.symbol() instanceof JVariableSymbol symbol && (symbol.isLocalVariable() || symbol.isParameter())) {
variables.add(tree);
}
super.visitVariable(tree);
}

@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
countAssignment(tree.variable(), tree.is(Tree.Kind.ASSIGNMENT) ? 0 : 1);
super.visitAssignmentExpression(tree);
}

@Override
public void visitUnaryExpression(UnaryExpressionTree tree) {
if (tree.is(Tree.Kind.PREFIX_INCREMENT, Tree.Kind.PREFIX_DECREMENT, Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.POSTFIX_DECREMENT)) {
countAssignment(tree.expression(), 1);
}
super.visitUnaryExpression(tree);
}

private void countAssignment(ExpressionTree target, int slot) {
ExpressionTree expression = target;
while (expression instanceof org.sonar.plugins.java.api.tree.ParenthesizedTree parenthesized) {
expression = parenthesized.expression();
}
if (expression instanceof IdentifierTree identifier) {
assignments.computeIfAbsent(identifier.symbol(), key -> new int[2])[slot]++;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.semsitter;

import java.util.Collections;
import java.util.List;
import org.sonar.java.model.Symbols;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.SymbolMetadata;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MethodTree;

/**
* Symbol type to represent initializer blocks. It's a type of method symbol and was introduced, so that variables
* defined inside an initializer block can have an owner other than the containing class. This is necessary to properly
* detect them as local variables.
*/
final class JInitializerBlockSymbol implements Symbol.MethodSymbol {
private final TypeSymbol owner;
private final boolean isStatic;

public JInitializerBlockSymbol(TypeSymbol owner, boolean isStatic) {
this.owner = owner;
this.isStatic = isStatic;
}

@Override
public MethodTree declaration() {
return null;
}

@Override
public List<Type> parameterTypes() {
return Collections.emptyList();
}

@Override
public List<Symbol> declarationParameters() {
return Collections.emptyList();
}

@Override
public Symbol.TypeSymbol returnType() {
return TypeSymbol.UNKNOWN_TYPE;
}

@Override
public List<Type> thrownTypes() {
return Collections.emptyList();
}

@Override
public List<Symbol.MethodSymbol> overriddenSymbols() {
return Collections.emptyList();
}

@Override
public Symbol owner() {
return owner;
}

@Override
public Type type() {
return Type.UNKNOWN;
}

@Override
public boolean isVariableSymbol() {
return false;
}

@Override
public boolean isTypeSymbol() {
return false;
}

@Override
public boolean isMethodSymbol() {
return true;
}

@Override
public boolean isPackageSymbol() {
return false;
}

@Override
public boolean isStatic() {
return isStatic;
}

@Override
public boolean isFinal() {
return false;
}

@Override
public boolean isEnum() {
return false;
}

@Override
public boolean isInterface() {
return false;
}

@Override
public boolean isAbstract() {
return false;
}

@Override
public boolean isPublic() {
return false;
}

@Override
public boolean isPrivate() {
return false;
}

@Override
public boolean isProtected() {
return false;
}

@Override
public boolean isPackageVisibility() {
return false;
}

@Override
public boolean isDeprecated() {
return false;
}

@Override
public boolean isVolatile() {
return false;
}

@Override
public boolean isUnknown() {
return false;
}

@Override
public SymbolMetadata metadata() {
return Symbols.EMPTY_METADATA;
}

@Override
public TypeSymbol enclosingClass() {
return owner;
}

@Override
public List<IdentifierTree> usages() {
return Collections.emptyList();
}

@Override
public String name() {
return isStatic ? "<clinit> (initializer block)" : "<init> (initializer block)";
}

@Override
public String signature() {
return owner.name() + "." + name();
}

@Override
public boolean isOverridable() {
return false;
}

@Override
public boolean isParametrizedMethod() {
return false;
}

@Override
public boolean isDefaultMethod() {
return false;
}

@Override
public boolean isSynchronizedMethod() {
return false;
}

@Override
public boolean isVarArgsMethod() {
return false;
}

@Override
public boolean isNativeMethod() {
return false;
}
}
Loading
Loading