Search in sources :

Example 26 with VariableTree

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

the class JavaTreeModelTest method local_variable_declaration.

/**
 * 14.4. Local Variable Declaration Statements
 */
@Test
public void local_variable_declaration() {
    BlockTree block = ((MethodTree) firstTypeMember("class T { void m() { int a = 42, b[]; final @Nullable int c = 42; } }")).block();
    List<StatementTree> declarations = block.body();
    assertThat(declarations).hasSize(3);
    VariableTree tree = (VariableTree) declarations.get(0);
    assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.modifiers().modifiers()).isEmpty();
    assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
    assertThat(tree.simpleName().name()).isEqualTo("a");
    assertThat(tree.initializer()).isNotNull();
    assertThat(tree.endToken()).isNotNull();
    assertThat(tree.endToken().text()).isEqualTo(",");
    assertThatChildrenIteratorHasSize(tree, 6);
    tree = (VariableTree) declarations.get(1);
    assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.modifiers().modifiers()).isEmpty();
    assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
    assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
    assertThat(tree.simpleName().name()).isEqualTo("b");
    assertThat(tree.initializer()).isNull();
    assertThat(tree.endToken()).isNotNull();
    assertThat(tree.endToken().text()).isEqualTo(";");
    assertThatChildrenIteratorHasSize(tree, 4);
    // TODO Test annotation
    tree = (VariableTree) declarations.get(2);
    assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.FINAL);
    assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
    assertThat(tree.simpleName().name()).isEqualTo("c");
    assertThat(tree.initializer()).isNotNull();
    assertThat(tree.endToken()).isNotNull();
    assertThat(tree.endToken().text()).isEqualTo(";");
    assertThatChildrenIteratorHasSize(tree, 6);
}
Also used : SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) EmptyStatementTree(org.sonar.plugins.java.api.tree.EmptyStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 27 with VariableTree

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

the class LambdaBlockReturnVisitorTest method test.

@Test
public void test() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {\n" + "        java.util.function.Consumer<String> c = s -> {\n" + "          if(s.length()>0) {\n" + "            return;\n" + "          }\n" + "          System.out.println(s);\n" + "        };\n" + "\n" + "        java.util.function.Function<String, String> f = s -> {\n" + "          if(s.length() > 0) {\n" + "            return s.replace('a', 'b');\n" + "          }\n" + "          return unknownSymbol;\n" + "        };\n" + "      }");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    List<VariableTree> vars = ((ClassTree) cut.types().get(0)).members().stream().map(m -> (VariableTree) m).collect(Collectors.toList());
    LambdaBlockReturnVisitor visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(0).initializer()).body().accept(visitor);
    assertThat(visitor.types).isEmpty();
    visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(1).initializer()).body().accept(visitor);
    assertThat(visitor.types).hasSize(1);
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) 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) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ActionParser(com.sonar.sslr.api.typed.ActionParser) List(java.util.List) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Collections(java.util.Collections) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 28 with VariableTree

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

the class ProtectedMemberInFinalClassCheck method checkMember.

private void checkMember(Tree member) {
    if (member.is(Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) member;
        checkVariableCompliance(variableTree);
    } else if (member.is(Kind.METHOD)) {
        MethodTree methodTree = (MethodTree) member;
        if (Boolean.FALSE.equals(methodTree.isOverriding())) {
            checkMethodCompliance(methodTree);
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 29 with VariableTree

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

the class ModulusEqualityCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.EQUAL_TO)) {
        BinaryExpressionTree equality = (BinaryExpressionTree) tree;
        checkModulusAndIntLiteral(equality.leftOperand(), equality.rightOperand());
        checkModulusAndIntLiteral(equality.rightOperand(), equality.leftOperand());
    } else {
        MethodTree methodTree = (MethodTree) tree;
        for (VariableTree variableTree : methodTree.parameters()) {
            methodParams.add(variableTree.symbol());
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 30 with VariableTree

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

the class RedundantTypeCastCheck method targetType.

@CheckForNull
private static Type targetType(TypeCastTree tree) {
    Tree parent = skipParentheses(tree.parent());
    Type target = null;
    if (parent.is(Tree.Kind.RETURN_STATEMENT)) {
        Tree method = parent;
        while (!method.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
            method = method.parent();
        }
        target = method.is(Tree.Kind.LAMBDA_EXPRESSION) ? null : ((MethodJavaType) ((MethodTree) method).symbol().type()).resultType();
    } else if (parent.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) parent;
        target = variableTree.symbol().type();
    } else if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) parent;
        if (mit.symbol().isMethodSymbol()) {
            JavaSymbol.MethodJavaSymbol sym = (JavaSymbol.MethodJavaSymbol) mit.symbol();
            int castArgIndex = mit.arguments().indexOf(tree);
            target = sym.parameterTypes().get(castArgIndex);
        }
    } else if (parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.CONDITIONAL_EXPRESSION)) {
        target = tree.type().symbolType();
    } else if (parent.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        target = ((ArrayAccessExpressionTree) parent).expression().symbolType();
    } else if (parent instanceof ExpressionTree) {
        target = ((ExpressionTree) parent).symbolType();
    }
    return target;
}
Also used : JavaType(org.sonar.java.resolve.JavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) CheckForNull(javax.annotation.CheckForNull)

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