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

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

Introduction

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

Prototype

ChildListPropertyDescriptor IMPORTS_PROPERTY

To view the source code for org.eclipse.jdt.core.dom CompilationUnit IMPORTS_PROPERTY.

Click Source Link

Document

The "imports" structural property of this node type (element type: ImportDeclaration ).

Usage

From source file:com.android.icu4j.srcgen.RunWithAnnotator.java

License:Apache License

private void appendImport(CompilationUnit cu, ASTRewrite rewriter, Name name) {
    ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);
    AST ast = cu.getAST();//from  w  w  w .  j  a  v a2  s . c  o  m
    ImportDeclaration importDeclaration = ast.newImportDeclaration();
    importDeclaration.setName(name);
    lrw.insertLast(importDeclaration, null);
}

From source file:com.gowan.plugin.handlers.JUnit3Handler.java

License:Open Source License

/**
 * //from ww  w  . j a  va 2 s .c o  m
 * @param unit
 * @throws JavaModelException
 */
private void createAST(ICompilationUnit unit) throws JavaModelException {
    CompilationUnit parse = parse(unit);
    JUnit3Visitor visitor = new JUnit3Visitor();
    parse.accept(visitor);

    IDocument doc = new Document(unit.getSource());
    AST ast = parse.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    JUnit3 junit = visitor.getJUnit3();

    TypeDeclaration td = (TypeDeclaration) parse.types().get(0);
    ITrackedNodePosition tdLocation = rewrite.track(td);

    if (junit.getKlass() != null) {
        rewrite.replace(td.getSuperclassType(), null, null);
    } else {
        return; // Skip if the class does not extend junit.framework.TestCase
    }

    //         imports
    ImportDeclaration afterImport = ast.newImportDeclaration();
    afterImport.setName(ast.newName(AFTER));
    ImportDeclaration beforeImport = ast.newImportDeclaration();
    beforeImport.setName(ast.newName(BEFORE));
    ImportDeclaration testImport = ast.newImportDeclaration();
    testImport.setName(ast.newName(TEST));

    ListRewrite lrw = rewrite.getListRewrite(parse, CompilationUnit.IMPORTS_PROPERTY);
    if (junit.getTestCaseImport() != null) {
        lrw.remove(junit.getTestCaseImport(), null);

        lrw.insertLast(afterImport, null);
        lrw.insertLast(beforeImport, null);
        lrw.insertLast(testImport, null);
    }

    if (junit.getSetUp() != null) {
        transformSetUp(ast, rewrite, junit);
    }
    if (junit.getTearDown() != null) {
        transformTearDown(ast, rewrite, junit);
    }
    if (junit.getTest() != null && !junit.getTest().isEmpty()) {
        transformTest(ast, rewrite, junit);
    }

    TextEdit edits = rewrite.rewriteAST(doc, null);

    unit.applyTextEdit(edits, null);

}

From source file:de.ovgu.cide.language.jdt.SimplePrintVisitor.java

License:Open Source License

public boolean visit(IASTNode node) {
    if (node instanceof ASTStringNode) {
        printToken(((ASTStringNode) node).getValue());
        return false;
    }/*from   w w  w  . j av a  2  s  .  c  o m*/
    if (node instanceof ASTTextNode) {
        printToken(((ASTTextNode) node).getValue());
        return false;
    }
    if (node instanceof UnifiedASTNode) {
        UnifiedASTNode unode = (UnifiedASTNode) node;

        if (unode.getEclipseASTNodeClass().equals("AnnotationTypeDeclaration")) {

            accept(node, AnnotationTypeDeclaration.JAVADOC_PROPERTY.getId());

        }

        if (unode.getEclipseASTNodeClass().equals(CompilationUnit.class.getSimpleName())) {
            accept(unode, CompilationUnit.PACKAGE_PROPERTY.getId());
            accept(unode, CompilationUnit.IMPORTS_PROPERTY.getId());
            accept(unode, CompilationUnit.TYPES_PROPERTY.getId());
        }

        if (unode.getEclipseASTNodeClass().equals(TypeDeclaration.class.getSimpleName())) {

            accept(node, TypeDeclaration.JAVADOC_PROPERTY.getId());
            accept(node, TypeDeclaration.MODIFIERS2_PROPERTY.getId());
            accept(node, TypeDeclaration.INTERFACE_PROPERTY.getId());
            accept(node, TypeDeclaration.NAME_PROPERTY.getId());
            accept(node, TypeDeclaration.TYPE_PARAMETERS_PROPERTY.getId(), "<", ",", ">", true);
            //            this.buffer.append(" ");//$NON-NLS-1$

            accept(node, TypeDeclaration.SUPERCLASS_TYPE_PROPERTY.getId(), "extends", "", "", false);
            accept(node, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY.getId(), "implements", ",", "", false);
            printToken("{");
            hintNewLine();
            hintIncIndent();

            accept(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY.getId());

            hintDecIndent();
            printToken("}");
            hintNewLine();
        }

        printToken(unode.getEclipseASTNodeClass());
    }
    return false;
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.util.ASTUtil.java

License:Open Source License

/**
 * Adds <CODE>ImportDeclaration</CODE>s to the list of imports in the
 * specified <CODE>CompilationUnit</CODE>. If an import already exists, the
 * import will not be inserted. The imports are inserted in an ordered way.
 * The <CODE>Comparator</CODE> of the <CODE>SortedSet</CODE> is used to sort
 * the imports./*from  w ww  . ja  va  2 s .co  m*/
 *
 * @param rewrite
 *            the <CODE>ASTRewrite</CODE>, that stores the edits.
 * @param compilationUnit
 *            the <CODE>CompilationUnit</CODE>.
 * @param imports
 *            the new <CODE>ImportDeclaration</CODE>s to add.
 */
public static void addImports(ASTRewrite rewrite, CompilationUnit compilationUnit,
        SortedSet<ImportDeclaration> imports) {
    requireNonNull(rewrite, "ast-rewrite");
    requireNonNull(compilationUnit, "compilation-unit");
    requireNonNull(imports, "imports");
    ListRewrite importRewrite = rewrite.getListRewrite(compilationUnit, CompilationUnit.IMPORTS_PROPERTY);
    addImports(importRewrite, imports.comparator(), imports.iterator());
}

From source file:edu.umd.cs.findbugs.quickfix.util.ASTUtil.java

License:Open Source License

/**
 * Adds <CODE>ImportDeclaration</CODE>s to the list of imports in the
 * specified <CODE>CompilationUnit</CODE>. If an import already exists, the
 * import will not be inserted. The imports are inserted in an ordered way.
 * The <CODE>Comparator</CODE> of the <CODE>SortedSet</CODE> is used to sort
 * the imports./* w ww.ja  va  2s .c o m*/
 * 
 * @param rewrite
 *            the <CODE>ASTRewrite</CODE>, that stores the edits.
 * @param compilationUnit
 *            the <CODE>CompilationUnit</CODE>.
 * @param imports
 *            the new <CODE>ImportDeclaration</CODE>s to add.
 */
public static void addImports(ASTRewrite rewrite, CompilationUnit compilationUnit,
        SortedSet<ImportDeclaration> imports) {
    checkForNull(rewrite, "ast-rewrite");
    checkForNull(compilationUnit, "compilation-unit");
    checkForNull(imports, "imports");
    ListRewrite importRewrite = rewrite.getListRewrite(compilationUnit, CompilationUnit.IMPORTS_PROPERTY);
    addImports(importRewrite, imports.comparator(), imports.iterator());
}

From source file:lang.java.jdt.internal.UnqualifyTypeNames.java

License:Open Source License

private void visitCompilationUnit() {
    ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setResolveBindings(true);/* www.  j  av  a2 s.  c  o  m*/
    parser.setSource(icu);

    CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    IProblem[] problems = cu.getProblems();
    for (int i = 0; i < problems.length; i++) {
        if (problems[i].isError()) {
            int offset = problems[i].getSourceStart();
            int length = problems[i].getSourceEnd() - offset;
            int sl = problems[i].getSourceLineNumber();
            ISourceLocation pos = VF.sourceLocation(loc.getURI(), offset, length, sl, sl, 0, 0);
            throw new Throw(VF.string("Error(s) in compilation unit: " + problems[i].getMessage()), pos, null);
        }
    }

    // Figure out which imports we need to keep and/or need to add
    cu.accept(new GatherUsedPackages());
    calculateImportsToAdd();

    // Now, start the rewrite process, first with the imports, then with the various
    // types found in the code in the file
    rewriter = ASTRewrite.create(cu.getAST());
    ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);

    // Throw away the current imports
    List<?> imps = lrw.getOriginalList();
    for (int i = 0; i < imps.size(); ++i) {
        lrw.remove((ASTNode) imps.get(i), null);
    }

    // Add the statically imported types back in
    for (String s : importedFullNames) {
        ImportDeclaration id = cu.getAST().newImportDeclaration();
        id.setName(cu.getAST().newName(s));
        id.setStatic(true);
        lrw.insertLast(id, null);
    }

    // Add the new imports back in
    String[] whatever = { "A" };
    String[] sortedImports = importsToAdd.toArray(whatever);
    Arrays.sort(sortedImports);

    for (String s : sortedImports) {
        ImportDeclaration id = cu.getAST().newImportDeclaration();
        id.setName(cu.getAST().newName(s));
        id.setStatic(false);
        lrw.insertLast(id, null);
    }

    cu.accept(this);

    try {
        IPath path = file.getFullPath();

        ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
        bufferManager.connect(path, LocationKind.IFILE, new NullProgressMonitor());

        ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);

        IDocument doc = textFileBuffer.getDocument();
        TextEdit te = rewriter.rewriteAST(doc, null);
        te.apply(doc);
        textFileBuffer.commit(new NullProgressMonitor(), true);

        bufferManager.disconnect(path, LocationKind.IFILE, new NullProgressMonitor());
    } catch (CoreException e) {
        throw new Throw(VF.string("Error(s) in rewrite of compilation unit: " + e.getMessage()), loc, null);
    } catch (MalformedTreeException e) {
        throw new Throw(VF.string("Error(s) in rewrite of compilation unit: " + e.getMessage()), loc, null);
    } catch (BadLocationException e) {
        throw new Throw(VF.string("Error(s) in rewrite of compilation unit: " + e.getMessage()), loc, null);
    }
}

From source file:org.eclipse.babel.tapiji.tools.java.util.ASTutils.java

License:Open Source License

public static void createImport(IDocument doc, IResource resource, CompilationUnit cu, AST ast,
        ASTRewrite rewriter, String qualifiedClassName) throws CoreException, BadLocationException {

    ImportFinder impFinder = new ImportFinder(qualifiedClassName);

    cu.accept(impFinder);/* www . j  a  v a  2  s .c  o m*/

    if (!impFinder.isImportFound()) {
        ImportDeclaration id = ast.newImportDeclaration();
        id.setName(ast.newName(qualifiedClassName.split("\\.")));
        id.setStatic(false);

        ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);
        lrw.insertFirst(id, null);
    }

}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJCompilationUnit.java

License:Open Source License

/**
 * Returns the list of children in order: package declaration (<code>JPackage</code>), imports (<code>JImport</code>), and types (<code>JType</code>).
 * //w w w.j av a 2s . c  o m
 * @see org.eclipse.emf.codegen.merge.java.facade.AbstractJNode#getChildren()
 */
@Override
public List<JNode> getChildren() {
    if (!isDisposed()) {
        CompilationUnit astCompilationUnit = getASTNode();
        List<JNode> children = new ArrayList<JNode>();
        PackageDeclaration astPackage = astCompilationUnit.getPackage();
        if (astPackage != null) {
            JNode child = getFacadeHelper().convertToNode(astPackage);
            if (child != null) {
                children.add(child);
            }
        }

        ListRewrite importsListRewrite = rewriter.getListRewrite(astCompilationUnit,
                CompilationUnit.IMPORTS_PROPERTY);
        for (Object importDeclaration : importsListRewrite.getRewrittenList()) {
            JNode child = getFacadeHelper().convertToNode(importDeclaration);
            if (child != null) {
                children.add(child);
            }
        }

        ListRewrite typesListRewrite = rewriter.getListRewrite(astCompilationUnit,
                CompilationUnit.TYPES_PROPERTY);
        for (Object type : typesListRewrite.getRewrittenList()) {
            JNode child = getFacadeHelper().convertToNode(type);
            if (child != null) {
                children.add(child);
            }
        }
        if (!children.isEmpty()) {
            return Collections.unmodifiableList(children);
        }
    }
    return Collections.emptyList();
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJCompilationUnit.java

License:Open Source License

@Override
public boolean addChild(ASTJNode<?> child) {
    if (child.getParent() != null) {
        return false;
    }/*from  www .  ja v a  2  s .co m*/

    if (child instanceof ASTJImport) {
        insertLast(child, CompilationUnit.IMPORTS_PROPERTY);
    } else if (child instanceof ASTJAbstractType<?>) {
        insertLast(child, CompilationUnit.TYPES_PROPERTY);
    } else if (child instanceof ASTJPackage) {
        setNodeProperty(getASTNode(), child.getASTNode(), CompilationUnit.PACKAGE_PROPERTY);
    } else {
        return false;
    }
    child.setParent(this);
    return true;
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJCompilationUnit.java

License:Open Source License

@Override
public boolean insertSibling(ASTJNode<?> node, ASTJNode<?> newSibling, boolean before) {
    if (newSibling.getParent() != null) {
        return false;
    }/* w ww  .  j  av  a2s. co  m*/

    if (newSibling instanceof ASTJImport) {
        if (node instanceof ASTJImport) {
            insert(newSibling, CompilationUnit.IMPORTS_PROPERTY, node, before);
        } else if (node instanceof ASTJPackage) {
            insertFirst(newSibling, CompilationUnit.IMPORTS_PROPERTY);
        } else if (node instanceof ASTJAbstractType<?>) {
            insertLast(newSibling, CompilationUnit.IMPORTS_PROPERTY);
        } else {
            return false;
        }
    } else if (newSibling instanceof ASTJAbstractType<?>) {
        if (node instanceof ASTJAbstractType<?>) {
            insert(newSibling, CompilationUnit.TYPES_PROPERTY, node, before);
        } else if (node instanceof ASTJImport) {
            insertFirst(newSibling, CompilationUnit.TYPES_PROPERTY);
        } else if (node instanceof ASTJPackage) {
            insertFirst(newSibling, CompilationUnit.TYPES_PROPERTY);
        } else {
            return false;
        }
    } else if (newSibling instanceof ASTJPackage) {
        setNodeProperty(getASTNode(), newSibling.getASTNode(), CompilationUnit.PACKAGE_PROPERTY);
    } else {
        return false;
    }
    newSibling.setParent(this);
    return true;
}