Search in sources :

Example 1 with UnaryExpressionTree

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

the class LiteralUtils method longLiteralValue.

@CheckForNull
public static Long longLiteralValue(ExpressionTree tree) {
    ExpressionTree expression = tree;
    int sign = tree.is(Kind.UNARY_MINUS) ? -1 : 1;
    if (tree.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) {
        expression = ((UnaryExpressionTree) tree).expression();
    }
    if (expression.is(Kind.INT_LITERAL, Kind.LONG_LITERAL)) {
        String value = trimLongSuffix(((LiteralTree) expression).value());
        // long as hexadecimal can be written using underscore to separate groups
        value = value.replaceAll("\\_", "");
        try {
            return sign * Long.decode(value);
        } catch (NumberFormatException e) {
        // Long.decode() may fail in case of very large long number written in hexadecimal. In such situation, we ignore the number.
        // Note that Long.MAX_VALUE = "0x7FFF_FFFF_FFFF_FFFFL", but it is possible to write larger numbers in hexadecimal
        // to be used as mask in bitwise operation. For instance:
        // 0x8000_0000_0000_0000L (MAX_VALUE + 1),
        // 0xFFFF_FFFF_FFFF_FFFFL (only ones),
        // 0xFFFF_FFFF_FFFF_FFFEL (only ones except least significant bit), ...
        }
    }
    return null;
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) CheckForNull(javax.annotation.CheckForNull)

Example 2 with UnaryExpressionTree

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

the class AbsOnNegativeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodTree = (MethodInvocationTree) tree;
        if (MATH_ABS_METHODS.anyMatch(methodTree)) {
            ExpressionTree firstArgument = methodTree.arguments().get(0);
            checkForIssue(firstArgument);
        }
    } else {
        ExpressionTree operand = ((UnaryExpressionTree) tree).expression();
        checkForIssue(operand);
    }
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree)

Example 3 with UnaryExpressionTree

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

the class UselessIncrementCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    if (tree.is(Tree.Kind.RETURN_STATEMENT)) {
        ExpressionTree returnExpression = ((ReturnStatementTree) tree).expression();
        if (returnExpression != null && isPostfix(returnExpression)) {
            UnaryExpressionTree unaryExpression = (UnaryExpressionTree) returnExpression;
            ExpressionTree expression = ExpressionUtils.skipParentheses(unaryExpression.expression());
            if (expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).symbol().owner().isMethodSymbol()) {
                reportIssue(unaryExpression);
            }
        }
    } else {
        AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
        if (isPostfix(aet.expression())) {
            UnaryExpressionTree postfix = (UnaryExpressionTree) aet.expression();
            if (SyntacticEquivalence.areEquivalent(aet.variable(), postfix.expression())) {
                reportIssue(postfix);
            }
        }
    }
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 4 with UnaryExpressionTree

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

the class JavaTreeModelTest method unary_operators.

/**
 * 15.15. Unary Operators
 */
@Test
public void unary_operators() {
    UnaryExpressionTree tree;
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { ++i; } }"))).expression();
    assertThat(tree.is(Tree.Kind.PREFIX_INCREMENT)).isTrue();
    assertThat(tree.operatorToken().text()).isEqualTo("++");
    assertThat(tree.expression()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { --i; } }"))).expression();
    assertThat(tree.is(Tree.Kind.PREFIX_DECREMENT)).isTrue();
    assertThat(tree.operatorToken().text()).isEqualTo("--");
    assertThat(tree.expression()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Example 5 with UnaryExpressionTree

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

the class JavaTreeModelTest method postfix_expression.

/**
 * 15.14. Postfix Expressions
 */
@Test
public void postfix_expression() {
    UnaryExpressionTree tree;
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i++; } }"))).expression();
    assertThat(tree.is(Tree.Kind.POSTFIX_INCREMENT)).isTrue();
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("++");
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i--; } }"))).expression();
    assertThat(tree.is(Tree.Kind.POSTFIX_DECREMENT)).isTrue();
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("--");
    assertThatChildrenIteratorHasSize(tree, 2);
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Aggregations

UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)5 CheckForNull (javax.annotation.CheckForNull)2 Test (org.junit.Test)2 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)2 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)1 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)1