Search in sources :

Example 21 with SyntaxToken

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

the class SillyBitOperationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree expression;
    SyntaxToken operatorToken;
    if (tree.is(Kind.OR, Kind.XOR, Kind.AND)) {
        BinaryExpressionTree binary = (BinaryExpressionTree) tree;
        expression = binary.rightOperand();
        operatorToken = binary.operatorToken();
    } else {
        AssignmentExpressionTree assignment = (AssignmentExpressionTree) tree;
        expression = assignment.expression();
        operatorToken = assignment.operatorToken();
    }
    Long evaluatedExpression = LiteralUtils.longLiteralValue(expression);
    if (evaluatedExpression != null && getBitwiseOperationIdentityElement(tree).equals(evaluatedExpression)) {
        reportIssue(operatorToken, "Remove this silly bit operation.");
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 22 with SyntaxToken

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

the class RightCurlyBraceToNextBlockAbstractVisitor method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatementTree = (IfStatementTree) tree;
        StatementTree thenStatement = ifStatementTree.thenStatement();
        if (ifStatementTree.elseKeyword() != null && thenStatement.is(Tree.Kind.BLOCK)) {
            checkTokenPosition(ifStatementTree.elseKeyword(), (BlockTree) thenStatement);
        }
    } else {
        TryStatementTree tryStatementTree = (TryStatementTree) tree;
        BlockTree block = tryStatementTree.block();
        for (CatchTree catchTree : tryStatementTree.catches()) {
            checkTokenPosition(catchTree.catchKeyword(), block);
            block = catchTree.block();
        }
        SyntaxToken finallyKeyword = tryStatementTree.finallyKeyword();
        if (finallyKeyword != null) {
            checkTokenPosition(finallyKeyword, block);
        }
    }
}
Also used : TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 23 with SyntaxToken

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

the class ShiftOnIntOrLongCheck method checkShiftTree.

private void checkShiftTree(Tree tree, int treeIndex) {
    String identifier;
    ExpressionTree shift;
    SyntaxToken operatorToken;
    if (tree.is(Kind.LEFT_SHIFT, Kind.RIGHT_SHIFT)) {
        BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
        if (isZeroMaskShift(binaryExpressionTree)) {
            // No issue should be reported for "1 << 0" or "1 >> 0"
            return;
        }
        identifier = getIdentifierName(binaryExpressionTree.leftOperand());
        shift = binaryExpressionTree.rightOperand();
        operatorToken = binaryExpressionTree.operatorToken();
    } else {
        AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) tree;
        identifier = getIdentifierName(assignmentExpressionTree.variable());
        shift = assignmentExpressionTree.expression();
        operatorToken = assignmentExpressionTree.operatorToken();
    }
    checkShift((ExpressionTree) tree, shift, identifier, operatorToken, treeIndex);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 24 with SyntaxToken

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

the class ListTreeImplTest method separators_order_in_children_iteration.

@Test
public void separators_order_in_children_iteration() throws Exception {
    Tree tree1 = new EmptyStatementTreeImpl(null);
    Tree tree2 = new EmptyStatementTreeImpl(null);
    Tree tree3 = new EmptyStatementTreeImpl(null);
    List<Tree> trees = Lists.newArrayList(tree1, tree2, tree3);
    SyntaxToken token1 = createToken("token1");
    SyntaxToken token2 = createToken("token2");
    List<SyntaxToken> separators = Lists.newArrayList(token1, token2);
    ListTreeImpl<Tree> listTree = new MyList(trees, separators);
    Iterable<Tree> result = listTree.children();
    assertThat(Lists.newArrayList(result)).containsExactly(tree1, token1, tree2, token2, tree3);
}
Also used : InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) EmptyStatementTreeImpl(org.sonar.java.model.statement.EmptyStatementTreeImpl) Tree(org.sonar.plugins.java.api.tree.Tree) Test(org.junit.Test)

Example 25 with SyntaxToken

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

the class JavaTreeModelTest method literal.

@Test
public void literal() {
    LiteralTree tree = (LiteralTree) expressionOfReturnStatement("class T { int m() { return 1; } }");
    assertThat(tree.is(Tree.Kind.INT_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1");
    assertThatChildrenIteratorHasSize(tree, 1);
    SyntaxToken token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(27);
    tree = (LiteralTree) expressionOfReturnStatement("class T { long m() { return 1L; } }");
    assertThat(tree.is(Tree.Kind.LONG_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1L");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(28);
    tree = (LiteralTree) expressionOfReturnStatement("class T { float m() { return 1F; } }");
    assertThat(tree.is(Tree.Kind.FLOAT_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1F");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(29);
    tree = (LiteralTree) expressionOfReturnStatement("class T { double m() { return 1d; } }");
    assertThat(tree.is(Tree.Kind.DOUBLE_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1d");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(30);
    tree = (LiteralTree) expressionOfReturnStatement("class T { boolean m() { return true; } }");
    assertThat(tree.is(Tree.Kind.BOOLEAN_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("true");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(31);
    tree = (LiteralTree) expressionOfReturnStatement("class T { boolean m() { return false; } }");
    assertThat(tree.is(Tree.Kind.BOOLEAN_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("false");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(31);
    tree = (LiteralTree) expressionOfReturnStatement("class T { char m() { return 'c'; } }");
    assertThat(tree.is(Tree.Kind.CHAR_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("'c'");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(28);
    tree = (LiteralTree) expressionOfReturnStatement("class T { String m() { return \"s\"; } }");
    assertThat(tree.is(Tree.Kind.STRING_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("\"s\"");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(30);
    tree = (LiteralTree) expressionOfReturnStatement("class T { Object m() { return null; } }");
    assertThat(tree.is(Tree.Kind.NULL_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("null");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(30);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Test(org.junit.Test)

Aggregations

SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)47 Tree (org.sonar.plugins.java.api.tree.Tree)9 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)7 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)7 Test (org.junit.Test)6 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)6 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)5 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)5 ImmutableList (com.google.common.collect.ImmutableList)4 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 CheckForNull (javax.annotation.CheckForNull)2