Example usage for org.eclipse.jdt.core.dom CompilationUnit getPackage

List of usage examples for org.eclipse.jdt.core.dom CompilationUnit getPackage

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom CompilationUnit getPackage.

Prototype

public PackageDeclaration getPackage() 

Source Link

Document

Returns the node for the package declaration of this compilation unit, or null if this compilation unit is in the default package.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(CompilationUnit node) {
    if (node.getPackage() != null) {
        node.getPackage().accept(this);
    }/*from  w  w  w .  j  a v a 2s  . c o m*/
    for (Iterator<ImportDeclaration> it = node.imports().iterator(); it.hasNext();) {
        ImportDeclaration d = it.next();
        d.accept(this);
    }
    for (Iterator<AbstractTypeDeclaration> it = node.types().iterator(); it.hasNext();) {
        AbstractTypeDeclaration d = it.next();
        d.accept(this);
    }
    return false;
}

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

private static Javadoc getPackageJavadocNode(IJavaElement element, String cuSource) {
    CompilationUnit cu = createAST(element, cuSource);
    if (cu != null) {
        PackageDeclaration packDecl = cu.getPackage();
        if (packDecl != null) {
            return packDecl.getJavadoc();
        }/*  w w  w .jav a2 s . c  o  m*/
    }
    return null;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(CompilationUnit node) {
    //      b.setPosition(pos.build());
    PackageDeclaration pkg = node.getPackage();
    if (pkg == null) {
        b.setName("");
    } else {/*from   w w w.  j a  v a 2  s  .  c o m*/
        b.setName(pkg.getName().getFullyQualifiedName());
        for (Object a : pkg.annotations()) {
            ((Annotation) a).accept(this);
            b.addModifiers(modifiers.pop());
        }
    }
    for (Object i : node.imports()) {
        ImportDeclaration id = (ImportDeclaration) i;
        String imp = "";
        if (id.isStatic())
            imp += "static ";
        imp += id.getName().getFullyQualifiedName();
        if (id.isOnDemand())
            imp += ".*";
        imports.add(imp);
    }
    for (Object t : node.types()) {
        declarations.push(new ArrayList<boa.types.Ast.Declaration>());
        ((AbstractTypeDeclaration) t).accept(this);
        for (boa.types.Ast.Declaration d : declarations.pop())
            b.addDeclarations(d);
    }
    for (Object c : node.getCommentList())
        ((org.eclipse.jdt.core.dom.Comment) c).accept(this);
    return false;
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

public static String getPackageName(CompilationUnit cu) {
    String packageName = null;//ww  w  .ja  va2  s. co m
    PackageDeclaration pDeclaration = cu.getPackage();

    if (pDeclaration != null) {
        packageName = pDeclaration.getName().getFullyQualifiedName();
    }

    return packageName;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static String getPackageFromFile(File file) throws IOException {
    String packageName = "";
    PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
    parser2.setStatementsRecovery(true);
    parser2.setResolveBindings(true);/* w  ww .ja  v a 2 s. com*/
    parser2.setSource(PPAResourceUtil.getContent(file).toCharArray());
    CompilationUnit cu = (CompilationUnit) parser2.createAST(null);
    PackageDeclaration pDec = cu.getPackage();
    if (pDec != null) {
        packageName = pDec.getName().getFullyQualifiedName();
    }
    return packageName;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(CompilationUnit node) {
    if (node.getPackage() != null) {
        node.getPackage().accept(this);
    }/*from  w  ww .  ja  v a2 s.c  o m*/
    for (Iterator it = node.imports().iterator(); it.hasNext();) {
        ImportDeclaration d = (ImportDeclaration) it.next();
        d.accept(this);
    }
    for (Iterator it = node.types().iterator(); it.hasNext();) {
        AbstractTypeDeclaration d = (AbstractTypeDeclaration) it.next();
        d.accept(this);
    }
    return false;
}

From source file:com.android.ide.eclipse.adt.internal.actions.RenamePackageAction.java

License:Open Source License

TextEdit updateJavaFileImports(CompilationUnit cu) {

    ImportVisitor import_visitor = new ImportVisitor(cu.getAST());
    cu.accept(import_visitor);
    TextEdit rewritten_imports = import_visitor.getTextEdit();

    // If the import of R was potentially implicit, insert an import statement
    if (cu.getPackage().getName().getFullyQualifiedName().equals(mOldPackageName.getFullyQualifiedName())) {

        ImportRewrite irw = ImportRewrite.create(cu, true);
        irw.addImport(mNewPackageName.getFullyQualifiedName() + '.' + AndroidConstants.FN_RESOURCE_BASE);

        try {//from   ww  w . j  a  v a 2 s  . c o  m
            rewritten_imports.addChild(irw.rewriteImports(null));
        } catch (MalformedTreeException e) {
            Status s = new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, e.getMessage(), e);
            AdtPlugin.getDefault().getLog().log(s);
        } catch (CoreException e) {
            Status s = new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, e.getMessage(), e);
            AdtPlugin.getDefault().getLog().log(s);
        }
    }

    return rewritten_imports;
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.renamepackage.ApplicationPackageNameRefactoring.java

License:Open Source License

TextEdit updateJavaFileImports(CompilationUnit cu) {

    ImportVisitor importVisitor = new ImportVisitor(cu.getAST());
    cu.accept(importVisitor);/*from   www.  j ava 2 s. c o m*/
    TextEdit rewrittenImports = importVisitor.getTextEdit();

    // If the import of R was potentially implicit, insert an import statement
    if (rewrittenImports != null && cu.getPackage().getName().getFullyQualifiedName()
            .equals(mOldPackageName.getFullyQualifiedName())) {

        UsageVisitor usageVisitor = new UsageVisitor();
        cu.accept(usageVisitor);

        if (usageVisitor.seenAny()) {
            ImportRewrite irw = ImportRewrite.create(cu, true);
            if (usageVisitor.hasSeenR()) {
                irw.addImport(mNewPackageName.getFullyQualifiedName() + '.' + FN_RESOURCE_BASE);
            }
            if (usageVisitor.hasSeenBuildConfig()) {
                irw.addImport(mNewPackageName.getFullyQualifiedName() + '.' + FN_BUILD_CONFIG_BASE);
            }
            if (usageVisitor.hasSeenManifest()) {
                irw.addImport(mNewPackageName.getFullyQualifiedName() + '.' + FN_MANIFEST_BASE);
            }

            try {
                rewrittenImports.addChild(irw.rewriteImports(null));
            } catch (MalformedTreeException e) {
                Status s = new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, e.getMessage(), e);
                AdtPlugin.getDefault().getLog().log(s);
            } catch (CoreException e) {
                Status s = new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, e.getMessage(), e);
                AdtPlugin.getDefault().getLog().log(s);
            }
        }
    }

    return rewrittenImports;
}

From source file:com.android.ide.eclipse.auidt.internal.refactorings.renamepackage.ApplicationPackageNameRefactoring.java

License:Open Source License

TextEdit updateJavaFileImports(CompilationUnit cu) {

    ImportVisitor importVisitor = new ImportVisitor(cu.getAST());
    cu.accept(importVisitor);/*  w w w  .j a  va 2  s . c  o m*/
    TextEdit rewrittenImports = importVisitor.getTextEdit();

    // If the import of R was potentially implicit, insert an import statement
    if (cu.getPackage().getName().getFullyQualifiedName().equals(mOldPackageName.getFullyQualifiedName())) {

        ImportRewrite irw = ImportRewrite.create(cu, true);
        irw.addImport(mNewPackageName.getFullyQualifiedName() + '.' + AdtConstants.FN_RESOURCE_BASE);

        try {
            rewrittenImports.addChild(irw.rewriteImports(null));
        } catch (MalformedTreeException e) {
            Status s = new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, e.getMessage(), e);
            AdtPlugin.getDefault().getLog().log(s);
        } catch (CoreException e) {
            Status s = new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, e.getMessage(), e);
            AdtPlugin.getDefault().getLog().log(s);
        }
    }

    return rewrittenImports;
}

From source file:com.architexa.diagrams.jdt.extractors.ContainmentHeirarchyExtractor.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration typeDecl) {
    TypeDeclaration parentType = (TypeDeclaration) getParent(TypeDeclaration.class);
    Resource parentRes;/*w  w  w.  ja va2 s .  co  m*/
    if (parentType != null) {
        // embedded type
        parentRes = bindingToResource(parentType.resolveBinding());
    } else {
        // top-level type (parent is package)
        CompilationUnit cu = (CompilationUnit) getRequiredParent(CompilationUnit.class);
        PackageDeclaration pckgDecl = cu.getPackage();
        if (pckgDecl == null)
            return true; // default package

        parentRes = bindingToResource(pckgDecl.resolveBinding());
    }

    Resource typeDeclRes = bindingToResource(typeDecl.resolveBinding());
    addContains(parentRes, typeDeclRes);
    return true;
}