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