Search in sources :

Example 16 with ExpressionStatementTree

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

the class TypeAndReferenceSolverTest method typeOf.

private JavaType typeOf(String input) {
    SemanticModel semanticModel = mock(SemanticModel.class);
    when(semanticModel.getEnv(any(Tree.class))).thenReturn(env);
    TypeAndReferenceSolver visitor = new TypeAndReferenceSolver(semanticModel, symbols, new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache), parametrizedTypeCache);
    String p = "class Test { void wrapperMethod() { " + input + "; } }";
    CompilationUnitTree tree = parse(p);
    tree.accept(visitor);
    TestedNodeExtractor testedNodeExtractor = new TestedNodeExtractor(false);
    testedNodeExtractor.visitCompilationUnit(tree);
    return visitor.getType(((ExpressionStatementTree) testedNodeExtractor.testedNode).expression());
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 17 with ExpressionStatementTree

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

the class TypeSubstitutionSolverTest method type_hierarchy_visit_should_be_limited.

@Test
public void type_hierarchy_visit_should_be_limited() {
    ParametrizedTypeCache parametrizedTypeCache = new ParametrizedTypeCache();
    BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(new ArrayList<>()), parametrizedTypeCache);
    Symbols symbols = new Symbols(bytecodeCompleter);
    ActionParser<Tree> parser = JavaParser.createParser();
    SemanticModel semanticModel = new SemanticModel(bytecodeCompleter);
    Resolve resolve = new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache);
    TypeAndReferenceSolver typeAndReferenceSolver = new TypeAndReferenceSolver(semanticModel, symbols, resolve, parametrizedTypeCache);
    CompilationUnitTree tree = (CompilationUnitTree) parser.parse(new File("src/test/files/sym/ComplexHierarchy.java"));
    new FirstPass(semanticModel, symbols, resolve, parametrizedTypeCache, typeAndReferenceSolver).visitCompilationUnit(tree);
    typeAndReferenceSolver.visitCompilationUnit(tree);
    ClassTree classTree = (ClassTree) tree.types().get(tree.types().size() - 1);
    JavaType site = (JavaType) classTree.symbol().type();
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) ((MethodTree) classTree.members().get(0)).block().body().get(0)).expression();
    TypeSubstitutionSolver typeSubstitutionSolver = Mockito.spy(new TypeSubstitutionSolver(parametrizedTypeCache, symbols));
    // call with empty formals should return.
    typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(new ArrayList<>(), site);
    verify(typeSubstitutionSolver, times(0)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
    JavaSymbol.MethodJavaSymbol methodJavaSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
    typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(((MethodJavaType) methodJavaSymbol.type).argTypes, site);
    verify(typeSubstitutionSolver, times(11)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) File(java.io.File) Test(org.junit.Test)

Example 18 with ExpressionStatementTree

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

the class UnusedLocalVariableCheck method leaveNode.

@Override
public void leaveNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.BLOCK, Tree.Kind.STATIC_INITIALIZER)) {
            BlockTree blockTree = (BlockTree) tree;
            addVariables(blockTree.body());
        } else if (tree.is(Tree.Kind.FOR_STATEMENT)) {
            ForStatementTree forStatementTree = (ForStatementTree) tree;
            addVariables(forStatementTree.initializer());
        } else if (tree.is(Tree.Kind.FOR_EACH_STATEMENT)) {
            ForEachStatement forEachStatement = (ForEachStatement) tree;
            addVariable(forEachStatement.variable());
        } else if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
            leaveExpressionStatement((ExpressionStatementTree) tree);
        } else {
            checkVariableUsages();
            variables.clear();
            assignments.clear();
        }
    }
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 19 with ExpressionStatementTree

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

the class UnusedReturnedDataCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        CHECKED_METHODS.stream().map(matcher -> isTreeMethodInvocation(((ExpressionStatementTree) tree).expression(), matcher)).filter(Objects::nonNull).forEach(mit -> raiseIssue(ExpressionUtils.methodName(mit)));
    } else {
        BinaryExpressionTree expressionTree = (BinaryExpressionTree) tree;
        ExpressionTree leftOperand = expressionTree.leftOperand();
        ExpressionTree rightOperand = expressionTree.rightOperand();
        for (MethodMatcher matcher : CHECKED_METHODS) {
            MethodInvocationTree leftMit = isTreeMethodInvocation(leftOperand, matcher);
            if (leftMit != null && isTreeLiteralNull(rightOperand)) {
                raiseIssue(ExpressionUtils.methodName(leftMit));
            }
            MethodInvocationTree rightMit = isTreeMethodInvocation(rightOperand, matcher);
            if (rightMit != null && isTreeLiteralNull(leftOperand)) {
                raiseIssue(ExpressionUtils.methodName(rightMit));
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 20 with ExpressionStatementTree

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

the class SyntaxTreeNameFinderTest method testFieldAccess.

@Test
public void testFieldAccess() {
    MethodTree tree = buildSyntaxTree("public void test() {this.field = value;} Object field;");
    BlockTree block = tree.block();
    AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
    tree = buildSyntaxTree("public void test() {super.field = value;}");
    block = tree.block();
    assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
    tree = buildSyntaxTree("public void test() {A.field = value;}");
    block = tree.block();
    assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("A");
    tree = buildSyntaxTree("public void test() {foo().field = value;}");
    block = tree.block();
    assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("foo");
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Aggregations

ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)21 Test (org.junit.Test)11 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)11 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)11 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)10 Symbol (org.sonar.plugins.java.api.semantic.Symbol)7 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)7 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)6 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)6 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)5 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)5 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)4 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)3 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 CheckForNull (javax.annotation.CheckForNull)2 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)2