Search in sources :

Example 31 with BlockTree

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

the class SyntaxTreeNameFinderTest method testMethodInvocationOnIdentifier.

@Test
public void testMethodInvocationOnIdentifier() {
    MethodTree tree = buildSyntaxTree("public void test() {String s; s.length();}");
    BlockTree block = tree.block();
    StatementTree statementTree = block.body().get(1);
    assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("length");
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 32 with BlockTree

use of org.sonar.plugins.java.api.tree.BlockTree 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)

Example 33 with BlockTree

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

the class SyntaxTreeNameFinderTest method testClassCast.

@Test
public void testClassCast() {
    MethodTree tree = buildSyntaxTree("public boolean equals(Object obj) {((String) obj).length;}");
    BlockTree block = tree.block();
    StatementTree statementTree = block.body().get(0);
    MemberSelectExpressionTree mse = (MemberSelectExpressionTree) ((ExpressionStatementTree) statementTree).expression();
    assertThat(SyntaxTreeNameFinder.getName(mse)).isEqualTo("obj");
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 34 with BlockTree

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

the class SyntaxTreeNameFinderTest method testMethodInvocationOnOtherInvocation.

@Test
public void testMethodInvocationOnOtherInvocation() {
    MethodTree tree = buildSyntaxTree("public void test() {int i = checkForNullMethod().length();}");
    BlockTree block = tree.block();
    StatementTree statementTree = block.body().get(0);
    assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("length");
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Aggregations

BlockTree (org.sonar.plugins.java.api.tree.BlockTree)34 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)14 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)13 Test (org.junit.Test)10 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)9 Tree (org.sonar.plugins.java.api.tree.Tree)5 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 JavaTree (org.sonar.java.model.JavaTree)3 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)3 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)2 ForEachStatement (org.sonar.plugins.java.api.tree.ForEachStatement)2 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2