Search in sources :

Example 6 with IdentifierTree

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

the class ForLoopTerminationConditionCheck method visitForStatement.

@Override
public void visitForStatement(ForStatementTree forStatement) {
    ExpressionTree condition = forStatement.condition();
    if (condition == null || !condition.is(Tree.Kind.NOT_EQUAL_TO)) {
        return;
    }
    BinaryExpressionTree inequalityCondition = (BinaryExpressionTree) condition;
    IntInequality loopVarAndTerminalValue = IntInequality.of(inequalityCondition);
    if (loopVarAndTerminalValue != null) {
        IdentifierTree loopIdentifier = loopVarAndTerminalValue.identifier;
        int terminationValue = loopVarAndTerminalValue.literalValue;
        Integer initialValue = initialValue(loopIdentifier, forStatement);
        if (initialValue != null && initialValue != terminationValue) {
            checkIncrement(forStatement, loopIdentifier, initialValue < terminationValue);
        }
    }
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 7 with IdentifierTree

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

the class ExceptionsShouldBeImmutableCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    if (isException(classTree)) {
        for (Tree member : classTree.members()) {
            if (member.is(Tree.Kind.VARIABLE) && !isFinal((VariableTree) member)) {
                IdentifierTree simpleName = ((VariableTree) member).simpleName();
                reportIssue(simpleName, "Make this \"" + simpleName.name() + "\" field final.");
            }
        }
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 8 with IdentifierTree

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

the class FileCreateTempFileCheck method checkAndAdvanceState.

@Nullable
private State checkAndAdvanceState(MethodInvocationTree mit, State requiredState, State nextState) {
    ExpressionTree methodSelect = mit.methodSelect();
    if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        ExpressionTree expressionTree = ((MemberSelectExpressionTree) methodSelect).expression();
        if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
            Symbol symbol = ((IdentifierTree) expressionTree).symbol();
            Map<Symbol, State> symbolStateMap = symbolStack.peek();
            if (symbolStateMap != null && symbolStateMap.containsKey(symbol) && requiredState.equals(symbolStateMap.get(symbol))) {
                symbolStateMap.put(symbol, nextState);
                return nextState;
            }
        }
    }
    return null;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Nullable(javax.annotation.Nullable)

Example 9 with IdentifierTree

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

the class IndexOfStartPositionCheck method checkIndexOfInvocation.

private void checkIndexOfInvocation(MethodInvocationTree mit, ExpressionTree other) {
    if (INDEX_OF_METHOD.matches(mit)) {
        String replaceMessage;
        ExpressionTree firstPar = mit.arguments().get(0);
        if (firstPar.is(Tree.Kind.STRING_LITERAL)) {
            replaceMessage = ((LiteralTree) firstPar).value();
        } else if (firstPar.is(Tree.Kind.IDENTIFIER)) {
            replaceMessage = ((IdentifierTree) firstPar).name();
        } else {
            replaceMessage = "xxx";
        }
        Long otherValue = LiteralUtils.longLiteralValue(other);
        if (otherValue != null && otherValue != -1 && otherValue != 0) {
            reportIssue(ExpressionUtils.methodName(mit), "Use \".indexOf(" + replaceMessage + ",n) > -1\" instead.");
        }
    }
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 10 with IdentifierTree

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

the class CFG method buildBreakStatement.

private void buildBreakStatement(BreakStatementTree tree) {
    IdentifierTree label = tree.label();
    Block targetBlock = null;
    if (label == null) {
        if (breakTargets.isEmpty()) {
            if (!ignoreBreakAndContinue) {
                throw new IllegalStateException("'break' statement not in loop or switch statement");
            }
        } else {
            targetBlock = breakTargets.getLast();
        }
    } else {
        targetBlock = labelsBreakTarget.get(label.name());
    }
    currentBlock = createUnconditionalJump(tree, targetBlock);
    if (currentBlock.exitBlock != null) {
        currentBlock.exitBlock = null;
    }
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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