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");
}
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");
}
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");
}
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");
}
Aggregations