use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class UsesDirectiveTreeImplTest method createTree.
private CompilationUnitTree createTree(String... lines) {
CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(Arrays.stream(lines).collect(Collectors.joining("\n")));
SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
return compilationUnitTree;
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method test_extract_identifier_mixed_access.
@Test
public void test_extract_identifier_mixed_access() throws Exception {
File file = new File("src/test/files/model/ExpressionUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(1);
List<AssignmentExpressionTree> assignments = findAssignmentExpressionTrees(methodTree);
// This should reflect method 'mixedReference'.
assertThat(assignments).hasSize(4);
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(0))).isTrue();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(1))).isTrue();
// Contains method invocation.
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
// Compound assignment
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
// The returned identifier should have the same symbol regardless of the explicit usage of this.
assertThat(ExpressionUtils.extractIdentifier(assignments.get(0)).symbol()).isEqualTo(ExpressionUtils.extractIdentifier(assignments.get(1)).symbol());
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method test_skip_parenthesis.
@Test
public void test_skip_parenthesis() throws Exception {
File file = new File("src/test/java/org/sonar/java/model/ExpressionUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
ExpressionTree parenthesis = ((ReturnStatementTree) methodTree.block().body().get(0)).expression();
assertThat(parenthesis.is(Tree.Kind.PARENTHESIZED_EXPRESSION)).isTrue();
ExpressionTree skipped = ExpressionUtils.skipParentheses(parenthesis);
assertThat(skipped.is(Tree.Kind.CONDITIONAL_AND)).isTrue();
assertThat(ExpressionUtils.skipParentheses(((BinaryExpressionTree) skipped).leftOperand()).is(Tree.Kind.IDENTIFIER)).isTrue();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method method_name.
@Test
public void method_name() throws Exception {
File file = new File("src/test/files/model/ExpressionUtilsMethodNameTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
MethodInvocationTree firstMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(0)).expression();
MethodInvocationTree secondMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(1)).expression();
assertThat(ExpressionUtils.methodName(firstMIT).name()).isEqualTo("foo");
assertThat(ExpressionUtils.methodName(secondMIT).name()).isEqualTo("foo");
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class SETestUtils method createSymbolicExecutionVisitorAndSemantic.
public static Pair<SymbolicExecutionVisitor, SemanticModel> createSymbolicExecutionVisitorAndSemantic(String fileName, boolean crossFileEnabled, SECheck... checks) {
File file = new File(fileName);
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse(file);
SemanticModel semanticModel = SemanticModel.createFor(cut, CLASSLOADER);
SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Arrays.asList(checks), new BehaviorCache(CLASSLOADER, crossFileEnabled));
sev.scanFile(new DefaultJavaFileScannerContext(cut, file, semanticModel, null, new JavaVersionImpl(8), true));
return new Pair<>(sev, semanticModel);
}
Aggregations