use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class TypeSubstitutionSolverTest method type_hierarchy_visit_should_be_limited.
@Test
public void type_hierarchy_visit_should_be_limited() {
ParametrizedTypeCache parametrizedTypeCache = new ParametrizedTypeCache();
BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(new ArrayList<>()), parametrizedTypeCache);
Symbols symbols = new Symbols(bytecodeCompleter);
ActionParser<Tree> parser = JavaParser.createParser();
SemanticModel semanticModel = new SemanticModel(bytecodeCompleter);
Resolve resolve = new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache);
TypeAndReferenceSolver typeAndReferenceSolver = new TypeAndReferenceSolver(semanticModel, symbols, resolve, parametrizedTypeCache);
CompilationUnitTree tree = (CompilationUnitTree) parser.parse(new File("src/test/files/sym/ComplexHierarchy.java"));
new FirstPass(semanticModel, symbols, resolve, parametrizedTypeCache, typeAndReferenceSolver).visitCompilationUnit(tree);
typeAndReferenceSolver.visitCompilationUnit(tree);
ClassTree classTree = (ClassTree) tree.types().get(tree.types().size() - 1);
JavaType site = (JavaType) classTree.symbol().type();
MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) ((MethodTree) classTree.members().get(0)).block().body().get(0)).expression();
TypeSubstitutionSolver typeSubstitutionSolver = Mockito.spy(new TypeSubstitutionSolver(parametrizedTypeCache, symbols));
// call with empty formals should return.
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(new ArrayList<>(), site);
verify(typeSubstitutionSolver, times(0)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
JavaSymbol.MethodJavaSymbol methodJavaSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(((MethodJavaType) methodJavaSymbol.type).argTypes, site);
verify(typeSubstitutionSolver, times(11)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class UselessImportCheck method scanFile.
@Override
public void scanFile(JavaFileScannerContext context) {
this.context = context;
CompilationUnitTree cut = context.getTree();
if (cut.moduleDeclaration() != null) {
// imports can be used for types used in module directive or annotations
return;
}
ExpressionTree packageName = getPackageName(cut);
pendingReferences.clear();
lineByImportReference.clear();
pendingImports.clear();
currentPackage = ExpressionsHelper.concatenate(packageName);
for (ImportClauseTree importClauseTree : cut.imports()) {
ImportTree importTree = null;
if (importClauseTree.is(Tree.Kind.IMPORT)) {
importTree = (ImportTree) importClauseTree;
}
if (importTree == null || importTree.isStatic()) {
// discard empty statements, which can be part of imports
continue;
}
reportIssue(importTree);
}
// check references
scan(cut);
// check references from comments.
new CommentVisitor().checkImportsFromComments(cut, pendingImports);
leaveFile();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class GenericsTest method declaredTypesOfVariablesFromLastClass.
private static List<Type> declaredTypesOfVariablesFromLastClass(String... lines) {
CompilationUnitTree tree = treeOf(lines);
List<Tree> declaredClasses = tree.types();
Tree last = declaredClasses.get(declaredClasses.size() - 1);
if (!(last instanceof ClassTree)) {
return Collections.emptyList();
}
ClassTree testClass = (ClassTree) last;
List<Type> types = new ArrayList<>();
for (Tree member : testClass.members()) {
if (member instanceof VariableTree) {
types.add(((VariableTree) member).type().symbolType());
}
}
return types;
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class GenericsTest method declaredTypes.
private static List<Type> declaredTypes(String... lines) {
CompilationUnitTree tree = treeOf(lines);
List<Type> results = Lists.newLinkedList();
for (Tree classTree : tree.types()) {
Type type = ((ClassTree) classTree).symbol().type();
results.add(type);
}
return results;
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class RequiresDirectiveTreeImplTest method test_BaseTreeVisitor.
@Test
public void test_BaseTreeVisitor() {
CompilationUnitTree cut = createTree("@org.foo.Bar", "open module com.greetings {", " requires static transitive bos.gul;", " requires gla.qix;", "}");
RequiresDirectiveVisitor moduleDeclarationVisitor = new RequiresDirectiveVisitor();
cut.accept(moduleDeclarationVisitor);
assertThat(moduleDeclarationVisitor.visited).isTrue();
assertThat(moduleDeclarationVisitor.directives).hasSize(2);
assertThat(moduleDeclarationVisitor.identifiers).containsExactly("com", "greetings", "bos", "gul", "gla", "qix");
}
Aggregations