Search in sources :

Example 1 with PackageDeclarationTree

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();
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree)

Example 2 with PackageDeclarationTree

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);
}
Also used : PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) Test(org.junit.Test)

Example 3 with PackageDeclarationTree

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);
}
Also used : CompilationUnitTreeImpl(org.sonar.java.model.JavaTree.CompilationUnitTreeImpl) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ImmutableList(com.google.common.collect.ImmutableList) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ModuleNameTree(org.sonar.plugins.java.api.tree.ModuleNameTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ListTree(org.sonar.plugins.java.api.tree.ListTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) ModuleDirectiveTree(org.sonar.plugins.java.api.tree.ModuleDirectiveTree) ModuleDeclarationTree(org.sonar.plugins.java.api.tree.ModuleDeclarationTree)

Example 4 with PackageDeclarationTree

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 + "\".");
        }
    }
}
Also used : PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) File(java.io.File)

Aggregations

PackageDeclarationTree (org.sonar.plugins.java.api.tree.PackageDeclarationTree)4 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 ImmutableList (com.google.common.collect.ImmutableList)1 File (java.io.File)1 Test (org.junit.Test)1 JavaTree (org.sonar.java.model.JavaTree)1 CompilationUnitTreeImpl (org.sonar.java.model.JavaTree.CompilationUnitTreeImpl)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 ImportClauseTree (org.sonar.plugins.java.api.tree.ImportClauseTree)1 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)1 ListTree (org.sonar.plugins.java.api.tree.ListTree)1 ModifierTree (org.sonar.plugins.java.api.tree.ModifierTree)1 ModuleDeclarationTree (org.sonar.plugins.java.api.tree.ModuleDeclarationTree)1 ModuleDirectiveTree (org.sonar.plugins.java.api.tree.ModuleDirectiveTree)1 ModuleNameTree (org.sonar.plugins.java.api.tree.ModuleNameTree)1 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)1 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1