use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method array_method_return_type.
@Test
public void array_method_return_type() {
MethodTree method;
ArrayTypeTree arrayTypeTree, childArrayTypeTree;
method = (MethodTree) firstTypeMember("class T { int[] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
method = (MethodTree) firstTypeMember("class T { int @Foo [] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
method = (MethodTree) firstTypeMember("class T { int @Foo @bar [] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 2);
assertThatChildrenIteratorHasSize(arrayTypeTree, 5);
method = (MethodTree) firstTypeMember("class T { int[] @Foo [] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("class T { int[] m()[]; }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("class T { int[] m() @Foo []; }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method getBytecodeCFG.
public static BytecodeCFG getBytecodeCFG(String methodName, String filename) {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File(filename);
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
List<Tree> classMembers = ((ClassTree) tree.types().get(0)).members();
Symbol.MethodSymbol symbol = classMembers.stream().filter(m -> m instanceof MethodTree).map(m -> ((MethodTree) m).symbol()).filter(s -> methodName.equals(s.name())).findFirst().orElseThrow(IllegalStateException::new);
return SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) symbol).completeSignature(), squidClassLoader);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_extract_fields_read.
@Test
public void should_extract_fields_read() {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { bar(p -> { System.out.println(a + field1); foo(this.field2); }); }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), true);
statementTree.accept(extractor);
// local variable "a" and fields "field1" and "field2"
assertThat(extractor.usedVariables()).hasSize(3);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method should_not_extract_local_vars_written.
@Test
public void should_not_extract_local_vars_written() throws Exception {
MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { new A().field1 = 0; a = false;} }; }");
StatementTree statementTree = methodTree.block().body().get(0);
VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false);
statementTree.accept(extractor);
assertThat(extractor.usedVariables()).isEmpty();
methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { a = !a;} }; }");
statementTree = methodTree.block().body().get(0);
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 buildMethodTree.
private static MethodTree buildMethodTree(String methodCode) {
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field1; int field2; " + methodCode + " }");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
return (MethodTree) ((ClassTree) cut.types().get(0)).members().get(2);
}
Aggregations