Example usage for org.eclipse.jdt.core.dom ImportDeclaration getStartPosition

List of usage examples for org.eclipse.jdt.core.dom ImportDeclaration getStartPosition

Introduction

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

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

Usage

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(ImportDeclaration node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    languageConstructs.add(/*  w  w  w.  ja  v a2s  .c  o  m*/
            new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn));

    return true;
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.CompilationUnitCreator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void removeUnusedImports(ICompilationUnit cu, Set<String> existingImports, boolean needsSave)
        throws CoreException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(cu);//from  www.jav a 2 s . c  om
    parser.setResolveBindings(true);

    CompilationUnit root = (CompilationUnit) parser.createAST(null);
    if (root.getProblems().length == 0) {
        return;
    }

    List<ImportDeclaration> importsDecls = root.imports();
    if (importsDecls.isEmpty()) {
        return;
    }
    ImportsManager imports = new ImportsManager(root);

    int importsEnd = ASTNodes.getExclusiveEnd((ASTNode) importsDecls.get(importsDecls.size() - 1));
    IProblem[] problems = root.getProblems();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        if (curr.getSourceEnd() < importsEnd) {
            int id = curr.getID();
            if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) {
                int pos = curr.getSourceStart();
                for (int k = 0; k < importsDecls.size(); k++) {
                    ImportDeclaration decl = importsDecls.get(k);
                    if (decl.getStartPosition() <= pos && pos < decl.getStartPosition() + decl.getLength()) {
                        if (existingImports.isEmpty() || !existingImports.contains(ASTNodes.asString(decl))) {
                            String name = decl.getName().getFullyQualifiedName();
                            if (decl.isOnDemand()) {
                                name += ".*"; //$NON-NLS-1$
                            }
                            if (decl.isStatic()) {
                                imports.removeStaticImport(name);
                            } else {
                                imports.removeImport(name);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    imports.create(needsSave, null);
}

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void removeUnusedImports(ICompilationUnit cu, Set<String> existingImports, boolean needsSave)
        throws CoreException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(cu);// ww  w .  j a  va 2 s  .  c o  m
    parser.setResolveBindings(true);

    CompilationUnit root = (CompilationUnit) parser.createAST(null);
    if (root.getProblems().length == 0) {
        return;
    }

    List<ImportDeclaration> importsDecls = root.imports();
    if (importsDecls.isEmpty()) {
        return;
    }
    ImportsManager imports = new ImportsManager(root);

    int importsEnd = ASTNodes.getExclusiveEnd(importsDecls.get(importsDecls.size() - 1));
    IProblem[] problems = root.getProblems();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        if (curr.getSourceEnd() < importsEnd) {
            int id = curr.getID();
            if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) {
                int pos = curr.getSourceStart();
                for (int k = 0; k < importsDecls.size(); k++) {
                    ImportDeclaration decl = importsDecls.get(k);
                    if (decl.getStartPosition() <= pos && pos < decl.getStartPosition() + decl.getLength()) {
                        if (existingImports.isEmpty() || !existingImports.contains(ASTNodes.asString(decl))) {
                            String name = decl.getName().getFullyQualifiedName();
                            if (decl.isOnDemand()) {
                                name += ".*"; //$NON-NLS-1$
                            }
                            if (decl.isStatic()) {
                                imports.removeStaticImport(name);
                            } else {
                                imports.removeImport(name);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    imports.create(needsSave, null);
}

From source file:com.google.googlejavaformat.java.RemoveUnusedImports.java

License:Open Source License

/** Construct replacements to fix unused imports. */
private static RangeMap<Integer, String> buildReplacements(String contents, CompilationUnit unit,
        Set<String> usedNames, Multimap<String, ASTNode> usedInJavadoc, JavadocOnlyImports javadocOnlyImports) {
    RangeMap<Integer, String> replacements = TreeRangeMap.create();
    for (ImportDeclaration importTree : (List<ImportDeclaration>) unit.imports()) {
        String simpleName = importTree.getName() instanceof QualifiedName
                ? ((QualifiedName) importTree.getName()).getName().toString()
                : importTree.getName().toString();
        if (usedNames.contains(simpleName)) {
            continue;
        }//from w w w .  j ava2s  .c om
        if (usedInJavadoc.containsKey(simpleName) && javadocOnlyImports == JavadocOnlyImports.KEEP) {
            continue;
        }
        // delete the import
        int endPosition = importTree.getStartPosition() + importTree.getLength();
        endPosition = Math.max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition);
        String sep = System.lineSeparator();
        if (endPosition + sep.length() < contents.length()
                && contents.subSequence(endPosition, endPosition + sep.length()).equals(sep)) {
            endPosition += sep.length();
        }
        replacements.put(Range.closedOpen(importTree.getStartPosition(), endPosition), "");
        // fully qualify any javadoc references with the same simple name as a deleted
        // non-static import
        if (!importTree.isStatic()) {
            for (ASTNode doc : usedInJavadoc.get(simpleName)) {
                String replaceWith = importTree.getName().toString();
                Range<Integer> range = Range.closedOpen(doc.getStartPosition(),
                        doc.getStartPosition() + doc.getLength());
                replacements.put(range, replaceWith);
            }
        }
    }
    return replacements;
}

From source file:com.tsc9526.monalisa.plugin.eclipse.generator.SourceUnit.java

License:Open Source License

public SourceUnit(CompilationUnit unit, String javaCode) {
    this.unit = unit;
    this.javaCode = javaCode;

    unitType = (TypeDeclaration) unit.types().get(0);
    unitName = unitType.getName().toString();
    endpos = unitType.getStartPosition() + unitType.getLength();

    project = unit.getJavaElement().getJavaProject();

    for (Object i : unit.imports()) {
        ImportDeclaration id = (ImportDeclaration) i;
        String name = id.getName().getFullyQualifiedName();
        int p = name.lastIndexOf(".");
        if (p > 0) {
            imports.put(name.substring(p + 1), name);
        } else {//  w w w .jav  a  2  s. com
            imports.put(name, name);
        }

        importPosition = id.getStartPosition() + id.getLength();
    }

}

From source file:org.bundlemaker.core.ui.editor.sourceviewer.referencedetail.JdtAstVisitor.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom. ImportDeclaration )
 *///ww w.j  a  v a2 s. c  om
@Override
public boolean visit(ImportDeclaration node) {

    if (node.isOnDemand()) {
        return false;
    }

    // binding exists
    if (node.resolveBinding() != null && !node.resolveBinding().isRecovered()) {

        // get the binding
        IBinding binding = node.resolveBinding();

        // add referenced package
        if (binding instanceof IPackageBinding) {

            /* Reference packageReference = */
            // _javaSourceResource.recordReference(((IPackageBinding) binding).getName());
        }

        // add referenced type
        else if (binding instanceof ITypeBinding) {
            resolveTypeBinding((ITypeBinding) binding, node.getStartPosition(), node.getLength());
        }

        // add referenced type
        else if (binding instanceof IVariableBinding) {

            IVariableBinding variableBinding = (IVariableBinding) binding;
            ITypeBinding typeBinding = variableBinding.getDeclaringClass();

            resolveTypeBinding(typeBinding, node.getStartPosition(), node.getLength());
        }
    }

    // no binding exists
    else {

        if (!node.isOnDemand() && !node.isStatic()) {

            addReferencedType(node.getName().getFullyQualifiedName(), node.getStartPosition(),
                    node.getLength());

        } else if (node.isOnDemand() && !node.isStatic()) {

            addReferencedType(node.getName().getFullyQualifiedName(), node.getStartPosition(),
                    node.getLength());

        } else if (!node.isOnDemand() && node.isStatic()) {

            Name importElementName = node.getName();
            String fullQualifiedName = importElementName.getFullyQualifiedName().substring(0,
                    importElementName.getFullyQualifiedName().lastIndexOf('.'));

            addReferencedType(fullQualifiedName, node.getStartPosition(), node.getLength());
        }
    }

    // don't visit children
    return false;
}

From source file:org.eclim.plugin.jdt.command.include.ImportCommand.java

License:Open Source License

private TextEdit importGroupingEdit(ICompilationUnit src, int offset) throws Exception {
    int separationLevel = getPreferences().getIntValue(src.getJavaProject().getProject(),
            "org.eclim.java.import.package_separation_level");
    String lineDelim = src.findRecommendedLineSeparator();
    CompilationUnit astRoot = SharedASTProvider.getAST(src, SharedASTProvider.WAIT_YES, null);
    ASTNode node = NodeFinder.perform(astRoot, offset, 1);
    MultiTextEdit edit = new MultiTextEdit();
    if (node != null && node.getNodeType() == ASTNode.IMPORT_DECLARATION) {
        ImportDeclaration imprt = (ImportDeclaration) node;

        ASTNode next = getNext(astRoot, node, lineDelim);
        while (next != null && next.getNodeType() == ASTNode.IMPORT_DECLARATION) {
            ImportDeclaration nextImprt = (ImportDeclaration) next;
            if (!ImportUtils.importsInSameGroup(separationLevel, imprt, nextImprt)) {
                int end = imprt.getStartPosition() + imprt.getLength() + lineDelim.length();
                addLineDelim(astRoot, edit, end, lineDelim);
            }//from   w ww .  j  av  a  2  s . c o m
            next = getNext(astRoot, next, lineDelim);
            imprt = nextImprt;
        }

        // reset imprt ref back to the one we are importing.
        imprt = (ImportDeclaration) node;
        ASTNode prev = getPrev(astRoot, node, lineDelim);
        if (prev != null && prev.getNodeType() == ASTNode.IMPORT_DECLARATION) {
            ImportDeclaration prevImprt = (ImportDeclaration) prev;
            if (!ImportUtils.importsInSameGroup(separationLevel, imprt, prevImprt)) {
                int end = prev.getStartPosition() + prev.getLength() + lineDelim.length();
                addLineDelim(astRoot, edit, end, lineDelim);
            }
        }
    }
    return edit.getChildrenSize() > 0 ? edit : null;
}

From source file:org.eclim.plugin.jdt.command.include.ImportUtils.java

License:Open Source License

public static TextEdit importGroupingEdit(ICompilationUnit src, Preferences preferences) throws Exception {
    int separationLevel = preferences.getIntValue(src.getJavaProject().getProject(),
            "org.eclim.java.import.package_separation_level");
    CompilationUnit astRoot = SharedASTProvider.getAST(src, SharedASTProvider.WAIT_YES, null);

    @SuppressWarnings("unchecked")
    List<ImportDeclaration> imports = astRoot.imports();
    String lineDelim = src.findRecommendedLineSeparator();
    MultiTextEdit edit = new MultiTextEdit();
    ImportDeclaration next = null;/*w  w  w . j a va 2 s .  co m*/
    for (int i = imports.size() - 1; i >= 0; i--) {
        ImportDeclaration imprt = imports.get(i);
        int end = imprt.getStartPosition() + imprt.getLength() + lineDelim.length();
        if (next != null && end == next.getStartPosition()
                && !ImportUtils.importsInSameGroup(separationLevel, imprt, next)) {
            edit.addChild(new InsertEdit(end, lineDelim));
        }
        next = imprt;
    }

    return edit.getChildrenSize() > 0 ? edit : null;
}

From source file:org.eclipse.ajdt.internal.ui.wizards.NewTypeWizardPage.java

License:Open Source License

private void removeUnusedImports(ICompilationUnit cu, Set existingImports, boolean needsSave)
        throws CoreException {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setSource(cu);/*from  w w w .  j av  a 2  s.  c o m*/
    parser.setResolveBindings(true);

    CompilationUnit root = (CompilationUnit) parser.createAST(null);
    if (root.getProblems().length == 0) {
        return;
    }

    List importsDecls = root.imports();
    if (importsDecls.isEmpty()) {
        return;
    }
    ImportsManager imports = new ImportsManager(root);

    int importsEnd = ASTNodes.getExclusiveEnd((ASTNode) importsDecls.get(importsDecls.size() - 1));
    IProblem[] problems = root.getProblems();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        if (curr.getSourceEnd() < importsEnd) {
            int id = curr.getID();
            if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) { // not visible problems hide unused -> remove both
                int pos = curr.getSourceStart();
                for (int k = 0; k < importsDecls.size(); k++) {
                    ImportDeclaration decl = (ImportDeclaration) importsDecls.get(k);
                    if (decl.getStartPosition() <= pos && pos < decl.getStartPosition() + decl.getLength()) {
                        if (existingImports.isEmpty() || !existingImports.contains(ASTNodes.asString(decl))) {
                            String name = decl.getName().getFullyQualifiedName();
                            if (decl.isOnDemand()) {
                                name += ".*"; //$NON-NLS-1$
                            }
                            if (decl.isStatic()) {
                                imports.removeStaticImport(name);
                            } else {
                                imports.removeImport(name);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    imports.create(needsSave, null);
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ASTHelper.java

License:Open Source License

private static void removeUnusedImports(ICompilationUnit cu, Set existingImports, boolean needsSave)
        throws CoreException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(cu);//from ww w  . j av a2  s  .  c om
    parser.setResolveBindings(true);

    CompilationUnit root = (CompilationUnit) parser.createAST(null);
    if (root.getProblems().length == 0) {
        return;
    }

    List importsDecls = root.imports();
    if (importsDecls.isEmpty()) {
        return;
    }
    ImportsManager imports = new ImportsManager(root);

    int importsEnd = ASTNodes.getExclusiveEnd((ASTNode) importsDecls.get(importsDecls.size() - 1));
    IProblem[] problems = root.getProblems();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        if (curr.getSourceEnd() < importsEnd) {
            int id = curr.getID();
            if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) { // not visible
                // problems hide
                // unused -> remove
                // both
                int pos = curr.getSourceStart();
                for (int k = 0; k < importsDecls.size(); k++) {
                    ImportDeclaration decl = (ImportDeclaration) importsDecls.get(k);
                    if (decl.getStartPosition() <= pos && pos < decl.getStartPosition() + decl.getLength()) {
                        if (existingImports.isEmpty() || !existingImports.contains(ASTNodes.asString(decl))) {
                            String name = decl.getName().getFullyQualifiedName();
                            if (decl.isOnDemand()) {
                                name += ".*"; //$NON-NLS-1$
                            }
                            if (decl.isStatic()) {
                                imports.removeStaticImport(name);
                            } else {
                                imports.removeImport(name);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    imports.create(needsSave, null);
}