Search in sources :

Example 66 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class StringConcatenationInLoopCheck method isNotLoopLocalVar.

private boolean isNotLoopLocalVar(AssignmentExpressionTree tree) {
    IdentifierTree idTree = getIdentifierTree(tree.variable());
    Tree envTree = semanticModel.getTree(semanticModel.getEnv(idTree.symbol()));
    Tree loopTree = loopLevel.peek();
    return envTree == null || !(envTree.equals(loopTree) || envTree.equals(loopStatement(loopTree)));
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 67 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class StringToStringCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree tree) {
    ExpressionTree expressionTree = extractBaseExpression(((MemberSelectExpressionTree) tree.methodSelect()).expression());
    if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
        reportIssue(expressionTree, String.format("\"%s\" is already a string, there's no need to call \"toString()\" on it.", ((IdentifierTree) expressionTree).identifierToken().text()));
    } else if (expressionTree.is(Tree.Kind.STRING_LITERAL)) {
        reportIssue(expressionTree, "there's no need to call \"toString()\" on a string literal.");
    } else if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        IdentifierTree methodName = ExpressionUtils.methodName((MethodInvocationTree) expressionTree);
        reportIssue(methodName, "\"" + methodName + "\" returns a string, there's no need to call \"toString()\".");
    } else if (expressionTree.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        ArrayAccessExpressionTree arrayAccess = (ArrayAccessExpressionTree) expressionTree;
        IdentifierTree name = extractName(arrayAccess.expression());
        if (name == null) {
            reportIssue(arrayAccess.expression(), "There's no need to call \"toString()\" on an array of String.");
        } else {
            reportIssue(name, String.format("\"%s\" is an array of strings, there's no need to call \"toString()\".", name.identifierToken().text()));
        }
    }
}
Also used : ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 68 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class StaticFieldInitializationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    switch(tree.kind()) {
        case CLASS:
            classWithSynchronizedMethod.push(hasSynchronizedMethod((ClassTree) tree));
            break;
        case STATIC_INITIALIZER:
            withinStaticInitializer.push(true);
            break;
        case METHOD:
            methodUsesLocks.push(false);
            break;
        case METHOD_INVOCATION:
            if (locks.anyMatch((MethodInvocationTree) tree) && methodUsesLocks.size() != 1) {
                methodUsesLocks.pop();
                methodUsesLocks.push(true);
            }
            break;
        case ASSIGNMENT:
            AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
            if (hasSemantic() && aet.variable().is(Tree.Kind.IDENTIFIER) && !isInSyncBlock() && !isInStaticInitializer() && !isUsingLock() && isInClassWithSynchronizedMethod()) {
                IdentifierTree variable = (IdentifierTree) aet.variable();
                if (isStaticNotVolatileObject(variable)) {
                    reportIssue(variable, "Synchronize this lazy initialization of '" + variable.name() + "'");
                }
            }
            break;
        default:
    }
    super.visitNode(tree);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 69 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.

private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
    ExpressionTree arg = null;
    ExpressionTree expression = null;
    if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
        Symbol symbol = mit.symbol();
        ExpressionTree methodSelect = mit.methodSelect();
        if (mit.arguments().size() == 1) {
            arg = mit.arguments().get(0);
            if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
                expression = ((MemberSelectExpressionTree) methodSelect).expression();
            }
        }
    } else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
        BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
        arg = equalTo.leftOperand();
        expression = equalTo.rightOperand();
    }
    if (arg != null && expression != null) {
        if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
            return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
        } else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
            return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
        }
    }
    return Optional.empty();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 70 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class SwitchWithLabelsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    CaseGroupTree cgt = (CaseGroupTree) tree;
    for (StatementTree statementTree : cgt.body()) {
        if (statementTree.is(LABELED_STATEMENT)) {
            IdentifierTree label = ((LabeledStatementTree) statementTree).label();
            reportIssue(label, "Remove this misleading \"" + label.name() + "\" label.");
        }
    }
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7