use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class ParameterReassignedToCheck method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
BlockTree block = tree.block();
if (block == null) {
return;
}
CFG cfg = CFG.build(tree);
LiveVariables analyze = LiveVariables.analyze(cfg);
Set<Symbol> live = analyze.getIn(cfg.entry());
for (VariableTree parameterTree : tree.parameters()) {
if (!live.contains(parameterTree.symbol())) {
variables.add(parameterTree.symbol());
}
}
super.visitMethod(tree);
for (VariableTree parameterTree : tree.parameters()) {
if (!live.contains(parameterTree.symbol())) {
variables.remove(parameterTree.symbol());
}
}
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class LoopsOnSameSetCheck method visitNode.
@Override
public void visitNode(Tree tree) {
Map<Symbol, Integer> forEachSymbols = new HashMap<>();
Tree previousForeachIterable = null;
for (Tree item : ((BlockTree) tree).body()) {
if (item.is(Tree.Kind.FOR_EACH_STATEMENT)) {
ForEachStatement forEachStatement = (ForEachStatement) item;
checkForEach(forEachSymbols, previousForeachIterable, forEachStatement);
previousForeachIterable = forEachStatement.expression();
} else {
previousForeachIterable = null;
item.accept(new InvalidatorVisitor(forEachSymbols));
}
}
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class EnumMutableFieldCheck method isSetter.
private static boolean isSetter(MethodTree methodTree) {
TypeTree returnType = methodTree.returnType();
BlockTree block = methodTree.block();
boolean returnsVoid = returnType.is(Tree.Kind.PRIMITIVE_TYPE) && "void".equals(((PrimitiveTypeTree) returnType).keyword().text());
boolean hasAtLeastOneStatement = block == null || !block.body().isEmpty();
return methodTree.simpleName().name().startsWith("set") && methodTree.parameters().size() == 1 && returnsVoid && hasAtLeastOneStatement;
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class UnusedLocalVariableCheck method leaveNode.
@Override
public void leaveNode(Tree tree) {
if (hasSemantic()) {
if (tree.is(Tree.Kind.BLOCK, Tree.Kind.STATIC_INITIALIZER)) {
BlockTree blockTree = (BlockTree) tree;
addVariables(blockTree.body());
} else if (tree.is(Tree.Kind.FOR_STATEMENT)) {
ForStatementTree forStatementTree = (ForStatementTree) tree;
addVariables(forStatementTree.initializer());
} else if (tree.is(Tree.Kind.FOR_EACH_STATEMENT)) {
ForEachStatement forEachStatement = (ForEachStatement) tree;
addVariable(forEachStatement.variable());
} else if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
leaveExpressionStatement((ExpressionStatementTree) tree);
} else {
checkVariableUsages();
variables.clear();
assignments.clear();
}
}
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class VariableDeclarationScopeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
BlockTree block = (BlockTree) tree;
List<StatementTree> body = block.body();
int bodySize = body.size();
for (int i = 0; i < bodySize; i++) {
StatementTree statement = body.get(i);
if (statement.is(Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) statement;
if (!variableTree.symbol().usages().isEmpty()) {
check(variableTree, body, bodySize, i + 1);
}
}
}
}
Aggregations