Search in sources :

Example 6 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project AutoRefactor by JnRouvignac.

the class ReplaceQualifiedNamesBySimpleNamesRefactoring method visit.

@Override
public boolean visit(TypeDeclaration node) {
    final ITypeBinding typeBinding = node.resolveBinding();
    if (typeBinding != null && !typeBinding.isNested() && node.getParent() instanceof CompilationUnit) {
        final CompilationUnit compilationUnit = (CompilationUnit) node.getParent();
        for (final ImportDeclaration importDecl : imports(compilationUnit)) {
            readImport(importDecl);
        }
        importTypesFromPackage("java.lang", compilationUnit);
        node.accept(new NamesCollector());
    }
    return VISIT_SUBTREE;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration)

Example 7 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project bndtools by bndtools.

the class NewTypeWizardPage method removeUnusedImports.

private void removeUnusedImports(ICompilationUnit cu, Set<String> existingImports, boolean needsSave) throws CoreException {
    ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    parser.setSource(cu);
    parser.setResolveBindings(true);
    CompilationUnit root = (CompilationUnit) parser.createAST(null);
    if (root.getProblems().length == 0) {
        return;
    }
    @SuppressWarnings("unchecked") 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) {
                // not visible problems hide unused -> remove both
                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()) {
                                //$NON-NLS-1$
                                name += ".*";
                            }
                            if (decl.isStatic()) {
                                imports.removeStaticImport(name);
                            } else {
                                imports.removeImport(name);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    imports.create(needsSave, null);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 8 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project flux by eclipse.

the class ScopeAnalyzer method getUsedVariableNames.

public Collection<String> getUsedVariableNames(int offset, int length) {
    HashSet<String> result = new HashSet<String>();
    IBinding[] bindingsBefore = getDeclarationsInScope(offset, VARIABLES);
    for (int i = 0; i < bindingsBefore.length; i++) {
        result.add(bindingsBefore[i].getName());
    }
    IBinding[] bindingsAfter = getDeclarationsAfter(offset + length, VARIABLES);
    for (int i = 0; i < bindingsAfter.length; i++) {
        result.add(bindingsAfter[i].getName());
    }
    List<ImportDeclaration> imports = fRoot.imports();
    for (int i = 0; i < imports.size(); i++) {
        ImportDeclaration decl = imports.get(i);
        if (decl.isStatic() && !decl.isOnDemand()) {
            result.add(ASTNodes.getSimpleNameIdentifier(decl.getName()));
        }
    }
    return result;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) HashSet(java.util.HashSet)

Example 9 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.

the class UnusedCodeFix method createRemoveUnusedImportFix.

public static UnusedCodeFix createRemoveUnusedImportFix(CompilationUnit compilationUnit, IProblemLocation problem) {
    if (isUnusedImport(problem)) {
        ImportDeclaration node = getImportDeclaration(problem, compilationUnit);
        if (node != null) {
            String label = FixMessages.UnusedCodeFix_RemoveImport_description;
            RemoveImportOperation operation = new RemoveImportOperation(node);
            Map<String, String> options = new Hashtable<String, String>();
            options.put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, CleanUpOptions.TRUE);
            return new UnusedCodeFix(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, options);
        }
    }
    return null;
}
Also used : Hashtable(java.util.Hashtable) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration)

Example 10 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.

the class ScopeAnalyzer method getUsedVariableNames.

public Collection<String> getUsedVariableNames(int offset, int length) {
    HashSet<String> result = new HashSet<String>();
    IBinding[] bindingsBefore = getDeclarationsInScope(offset, VARIABLES);
    for (int i = 0; i < bindingsBefore.length; i++) {
        result.add(bindingsBefore[i].getName());
    }
    IBinding[] bindingsAfter = getDeclarationsAfter(offset + length, VARIABLES);
    for (int i = 0; i < bindingsAfter.length; i++) {
        result.add(bindingsAfter[i].getName());
    }
    List<ImportDeclaration> imports = fRoot.imports();
    for (int i = 0; i < imports.size(); i++) {
        ImportDeclaration decl = imports.get(i);
        if (decl.isStatic() && !decl.isOnDemand()) {
            result.add(ASTNodes.getSimpleNameIdentifier(decl.getName()));
        }
    }
    return result;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) HashSet(java.util.HashSet)

Aggregations

ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)14 Name (org.eclipse.jdt.core.dom.Name)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 HashSet (java.util.HashSet)3 List (java.util.List)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)3 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)3 Type (org.eclipse.jdt.core.dom.Type)3 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 Iterator (java.util.Iterator)2 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 PackageDeclaration (org.eclipse.jdt.core.dom.PackageDeclaration)2