use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class LeastUpperBoundTest method lub_of_one_element_is_itself.
@Test
public void lub_of_one_element_is_itself() {
CompilationUnitTree cut = treeOf("class A<T> { A<String> var; }");
ClassTree classA = (ClassTree) cut.types().get(0);
Type varType = ((VariableTree) classA.members().get(0)).type().symbolType();
Type a = classA.symbol().type();
assertThat(leastUpperBound(a)).isSameAs(a);
assertThat(leastUpperBound(varType)).isSameAs(varType);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class ComplexityVisitorTest method lambda_complexity.
@Test
public void lambda_complexity() throws Exception {
CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A { Function f = s -> {if(s.isEmpty()) return s; return new MyClass(){ void foo(){if(a) return;} };};}");
ExpressionTree lambda = ((VariableTree) ((ClassTree) cut.types().get(0)).members().get(0)).initializer();
List<Tree> nodes = new ComplexityVisitor().getNodes(lambda);
assertThat(nodes).hasSize(2);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class ComplexityVisitorTest method method_complexity.
@Test
public void method_complexity() throws Exception {
CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {" + " Object foo(){" + " if(a) { " + " return new MyClass(){ " + " void foo(){" + " if(a) {return;} " + " } " + " };" + " } " + "}}");
MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
List<Tree> nodes = new ComplexityVisitor().getNodes(methodTree);
assertThat(nodes).hasSize(2);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class PublicApiCheckerTest method retrieveJavadoc.
@Test
public void retrieveJavadoc() {
new SubscriptionVisitor() {
@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.values());
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
checkApi(tree, variableTree.simpleName().name());
} else if (tree.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)) {
MethodTree methodTree = (MethodTree) tree;
checkApi(tree, methodTree.simpleName().name());
} else if (tree.is(Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE)) {
IdentifierTree idTree = ((ClassTree) tree).simpleName();
checkApi(tree, idTree == null ? "" : idTree.name());
} else {
checkApi(tree, "");
}
}
}.scanTree(cut);
}
use of org.sonar.plugins.java.api.tree.ClassTree 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());
}
Aggregations