Search in sources :

Example 41 with MethodTree

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

the class VariableReadExtractorTest method should_not_extract_variable_declared.

@Test
public void should_not_extract_variable_declared() throws Exception {
    MethodTree methodTree = buildMethodTree("void foo(boolean a) { boolean b = a; }");
    StatementTree statementTree = methodTree.block().body().get(0);
    VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), true);
    statementTree.accept(extractor);
    // only "a" should be detected
    assertThat(extractor.usedVariables()).hasSize(1);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 42 with MethodTree

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

the class VariableReadExtractorTest method should_return_symbol_once.

@Test
public void should_return_symbol_once() {
    MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { foo(a); bar(a); } }; }");
    StatementTree statementTree = methodTree.block().body().get(0);
    VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false);
    statementTree.accept(extractor);
    assertThat(extractor.usedVariables()).hasSize(1);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 43 with MethodTree

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

the class VariableReadExtractorTest method should_extract_local_vars_read.

@Test
public void should_extract_local_vars_read() {
    MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { System.out.println(a);} };  }");
    StatementTree statementTree = methodTree.block().body().get(0);
    VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false);
    statementTree.accept(extractor);
    assertThat(extractor.usedVariables()).hasSize(1);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 44 with MethodTree

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

the class MethodInvocationTreeImplTest method symbol_should_be_set.

@Test
public void symbol_should_be_set() {
    CompilationUnitTree cut = createTree("class A { void foo(){} void bar(){foo();} }");
    ClassTree classTree = (ClassTree) cut.types().get(0);
    Symbol.MethodSymbol declaration = ((MethodTree) classTree.members().get(0)).symbol();
    StatementTree statementTree = ((MethodTree) classTree.members().get(1)).block().body().get(0);
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).expression();
    assertThat(mit.symbol()).isSameAs(declaration);
    assertThat(mit.arguments()).isNotNull();
    assertThat(mit.arguments().openParenToken()).isNotNull();
    assertThat(mit.arguments().closeParenToken()).isNotNull();
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 45 with MethodTree

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

the class ArrayDesignatorAfterTypeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    TypeTree returnType = methodTree.returnType();
    SyntaxToken identifierToken = methodTree.simpleName().identifierToken();
    while (returnType.is(Tree.Kind.ARRAY_TYPE)) {
        ArrayTypeTree arrayTypeTree = (ArrayTypeTree) returnType;
        SyntaxToken openBracketToken = arrayTypeTree.openBracketToken();
        if (isInvalidPosition(openBracketToken, identifierToken)) {
            reportIssue(openBracketToken, "Move the array designators \"[]\" to the end of the return type.");
            break;
        }
        returnType = arrayTypeTree.type();
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree)

Aggregations

MethodTree (org.sonar.plugins.java.api.tree.MethodTree)143 Test (org.junit.Test)59 Tree (org.sonar.plugins.java.api.tree.Tree)43 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)39 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)34 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)30 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)27 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)23 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)20 Type (org.sonar.plugins.java.api.semantic.Type)19 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)19 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)18 List (java.util.List)16 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)16 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)15 ArrayList (java.util.ArrayList)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)14 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)12