use of org.sonar.plugins.java.api.tree.PackageDeclarationTree in project sonar-java by SonarSource.
the class FirstPass method visitCompilationUnit.
@Override
public void visitCompilationUnit(CompilationUnitTree tree) {
JavaSymbol.PackageJavaSymbol compilationUnitPackage = symbols.defaultPackage;
PackageDeclarationTree packageDeclaration = tree.packageDeclaration();
if (packageDeclaration != null) {
ExpressionTree packageName = packageDeclaration.packageName();
PackageResolverVisitor packageResolver = new PackageResolverVisitor();
packageName.accept(packageResolver);
compilationUnitPackage = (JavaSymbol.PackageJavaSymbol) resolve.findIdentInPackage(compilationUnitPackage, packageResolver.packageName, JavaSymbol.PCK);
semanticModel.associateSymbol(packageName, compilationUnitPackage);
}
compilationUnitPackage.members = new Scope(compilationUnitPackage);
env = new Resolve.Env();
env.packge = compilationUnitPackage;
env.scope = compilationUnitPackage.members;
env.namedImports = new Scope.ImportScope(compilationUnitPackage);
env.starImports = resolve.createStarImportScope(compilationUnitPackage);
env.staticStarImports = resolve.createStaticStarImportScope(compilationUnitPackage);
semanticModel.associateEnv(tree, env);
scan(tree.types());
restoreEnvironment(tree);
resolveImports(tree.imports());
completeSymbols();
}
use of org.sonar.plugins.java.api.tree.PackageDeclarationTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method package_declaration.
@Test
public void package_declaration() {
PackageDeclarationTree tree = compilationUnit("package myPackage;").packageDeclaration();
assertThat(tree.is(Tree.Kind.PACKAGE)).isTrue();
assertThat(tree.annotations()).isEmpty();
assertThat(tree.packageKeyword().text()).isEqualTo("package");
assertThat(tree.packageName()).isNotNull();
assertThat(tree.packageName().is(Tree.Kind.IDENTIFIER)).isTrue();
assertThat(tree.semicolonToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 3);
tree = compilationUnit("@Foo @Bar package org.myPackage;").packageDeclaration();
assertThat(tree.is(Tree.Kind.PACKAGE)).isTrue();
assertThat(tree.annotations()).hasSize(2);
assertThat(tree.packageKeyword().text()).isEqualTo("package");
assertThat(tree.packageName()).isNotNull();
assertThat(tree.packageName().is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(tree.semicolonToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 5);
}
use of org.sonar.plugins.java.api.tree.PackageDeclarationTree in project sonar-java by SonarSource.
the class TreeFactory method newCompilationUnit.
// End of literals
// Compilation unit
public CompilationUnitTreeImpl newCompilationUnit(JavaTree spacing, Optional<PackageDeclarationTree> packageDeclaration, Optional<List<ImportClauseTree>> importDeclarations, Optional<ModuleDeclarationTree> moduleDeclaration, Optional<List<Tree>> typeDeclarations, InternalSyntaxToken eof) {
ImmutableList.Builder<ImportClauseTree> imports = ImmutableList.builder();
if (importDeclarations.isPresent()) {
for (ImportClauseTree child : importDeclarations.get()) {
imports.add(child);
}
}
ImmutableList.Builder<Tree> types = ImmutableList.builder();
if (typeDeclarations.isPresent()) {
for (Tree child : typeDeclarations.get()) {
types.add(child);
}
}
return new CompilationUnitTreeImpl(packageDeclaration.orNull(), imports.build(), types.build(), moduleDeclaration.orNull(), eof);
}
use of org.sonar.plugins.java.api.tree.PackageDeclarationTree in project sonar-java by SonarSource.
the class MismatchPackageDirectoryCheck method visitCompilationUnit.
@Override
public void visitCompilationUnit(CompilationUnitTree tree) {
PackageDeclarationTree packageDeclaration = tree.packageDeclaration();
if (packageDeclaration != null) {
String packageName = PackageUtils.packageName(packageDeclaration, File.separator);
File javaFile = context.getFile();
String dir = javaFile.getParent();
if (!dir.endsWith(packageName)) {
context.reportIssue(this, packageDeclaration.packageName(), "This file \"" + javaFile.getName() + "\" should be located in \"" + packageName + "\" directory, not in \"" + dir + "\".");
}
}
}
Aggregations