Search in sources :

Example 26 with BlockTree

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());
        }
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 27 with BlockTree

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));
        }
    }
}
Also used : HashMap(java.util.HashMap) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 28 with BlockTree

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;
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 29 with BlockTree

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();
        }
    }
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 30 with BlockTree

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);
            }
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Aggregations

BlockTree (org.sonar.plugins.java.api.tree.BlockTree)34 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)14 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)13 Test (org.junit.Test)10 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)9 Tree (org.sonar.plugins.java.api.tree.Tree)5 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 JavaTree (org.sonar.java.model.JavaTree)3 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)3 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)2 ForEachStatement (org.sonar.plugins.java.api.tree.ForEachStatement)2 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2