Search in sources :

Example 71 with VariableTree

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

the class LiteralUtilsTest method test_int_and_long_value.

@Test
public void test_int_and_long_value() throws Exception {
    Integer[] expectedIntegerValues = { 42, -7, 3, null, null, null, null, 5678 };
    Long[] expectedLongValues = { 42L, 42L, -7L, -7L, +3L, +3L, null, null, 255L, null, null, null, Long.MAX_VALUE, Long.MAX_VALUE, null, 10010L };
    int i = 0;
    int j = 0;
    for (VariableTree variableTree : variables) {
        if (variableTree.simpleName().name().startsWith("x")) {
            assertThat(LiteralUtils.intLiteralValue(variableTree.initializer())).isEqualTo(expectedIntegerValues[i++]);
        } else if (variableTree.simpleName().name().startsWith("y")) {
            assertThat(LiteralUtils.longLiteralValue(variableTree.initializer())).isEqualTo(expectedLongValues[j++]);
        }
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Example 72 with VariableTree

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

the class LiteralUtilsTest method testEmptyString.

@Test
public void testEmptyString() {
    boolean[] expectedStringEmptyResult = { true, false, false, false };
    int i = 0;
    for (VariableTree variableTree : variables) {
        if (variableTree.simpleName().name().startsWith("s")) {
            assertThat(LiteralUtils.isEmptyString(variableTree.initializer())).isEqualTo(expectedStringEmptyResult[i++]);
        }
    }
    Optional<VariableTree> nonStringVariable = variables.stream().filter(v -> v.simpleName().name().startsWith("x")).findFirst();
    assertThat(nonStringVariable).isPresent();
    assertThat(LiteralUtils.isEmptyString(nonStringVariable.get().initializer())).isFalse();
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Optional(java.util.Optional) Test(org.junit.Test) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) File(java.io.File) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Example 73 with VariableTree

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

the class SyntaxTreeDebug method methodString.

private static String methodString(MethodTree syntaxNode) {
    StringBuilder buffer = new StringBuilder();
    for (ModifierTree modifier : syntaxNode.modifiers()) {
        if (modifier.is(Tree.Kind.TOKEN)) {
            buffer.append(((SyntaxToken) modifier).text());
            buffer.append(' ');
        }
    }
    buffer.append(syntaxNode.returnType().symbolType().toString());
    buffer.append(' ');
    buffer.append(syntaxNode.simpleName().name());
    buffer.append('(');
    boolean first = true;
    for (VariableTree parameter : syntaxNode.parameters()) {
        if (first) {
            first = false;
        } else {
            buffer.append(", ");
        }
        buffer.append(parameter.type().symbolType().toString());
        buffer.append(' ');
        buffer.append(parameter.simpleName().toString());
    }
    buffer.append(')');
    return buffer.toString();
}
Also used : ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 74 with VariableTree

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

the class SyntaxTreeDebug method memberSelectString.

private static String memberSelectString(MemberSelectExpressionTree expression) {
    StringBuilder buffer = new StringBuilder();
    ExpressionTree target = expression.expression();
    switch(target.kind()) {
        case IDENTIFIER:
            buffer.append(identifierString((IdentifierTree) target));
            break;
        case METHOD_INVOCATION:
            buffer.append(methodInvocationString((MethodInvocationTree) target));
            break;
        case VARIABLE:
            buffer.append(variableString((VariableTree) target));
            break;
        case INT_LITERAL:
            buffer.append(literalString((LiteralTree) target));
            break;
        default:
            break;
    }
    buffer.append('.');
    buffer.append(identifierString(expression.identifier()));
    return buffer.toString();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 75 with VariableTree

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

the class ImmediateReverseBoxingCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
        visitMethodInvocationTree((MethodInvocationTree) tree);
    } else if (tree.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) tree;
        ExpressionTree initializer = variableTree.initializer();
        if (initializer != null) {
            checkExpression(initializer, variableTree.type().symbolType());
        }
    } else if (tree.is(Tree.Kind.ASSIGNMENT)) {
        AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) tree;
        checkExpression(assignmentTree.expression(), assignmentTree.symbolType());
    } else {
        NewClassTree newClassTree = (NewClassTree) tree;
        Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
        if (classSymbol != null) {
            ExpressionTree arg0 = newClassTree.arguments().get(0);
            checkForUnboxing(arg0);
            checkForUselessUnboxing(newClassTree.symbolType(), newClassTree.identifier(), arg0);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Aggregations

VariableTree (org.sonar.plugins.java.api.tree.VariableTree)86 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)32 Tree (org.sonar.plugins.java.api.tree.Tree)32 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)27 Test (org.junit.Test)25 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)22 Symbol (org.sonar.plugins.java.api.semantic.Symbol)18 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)16 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 List (java.util.List)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)12 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)11 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9