Search in sources :

Example 31 with ImportDeclaration

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

the class TypeNameDecider method getImportedTypes.

private static NavigableSet<String> getImportedTypes(CompilationUnit cu) {
    final TreeSet<String> results = new TreeSet<String>();
    for (ImportDeclaration importDecl : imports(cu)) {
        Name importName = importDecl.getName();
        results.add(importName.getFullyQualifiedName());
    }
    return results;
}
Also used : TreeSet(java.util.TreeSet) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) Name(org.eclipse.jdt.core.dom.Name)

Example 32 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project liferay-ide by liferay.

the class QuickFixGradleDep method _importNotFoundProposal.

private void _importNotFoundProposal(IInvocationContext context, IProblemLocation problem, Collection<IJavaCompletionProposal> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    ImportDeclaration importDeclaration = (ImportDeclaration) ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
    if (importDeclaration == null) {
        return;
    }
    String importName = importDeclaration.getName().toString();
    List<String> serviceWrapperList;
    List<String> servicesList;
    boolean depWrapperCanFixed = false;
    try {
        serviceWrapperList = TargetPlatformUtil.getServiceWrapperList().getServiceList();
        servicesList = TargetPlatformUtil.getServicesList().getServiceList();
        if (serviceWrapperList.contains(importName)) {
            ServiceContainer bundle = TargetPlatformUtil.getServiceWrapperBundle(importName);
            depWrapperCanFixed = true;
            _createDepProposal(context, proposals, bundle);
        }
        if (!depWrapperCanFixed) {
            if (servicesList.contains(importName)) {
                ServiceContainer bundle = TargetPlatformUtil.getServiceBundle(importName);
                _createDepProposal(context, proposals, bundle);
            }
        }
        if (TargetPlatformUtil.getThirdPartyBundleList(importName) != null) {
            ServiceContainer bundle = TargetPlatformUtil.getThirdPartyBundleList(importName);
            _createDepProposal(context, proposals, bundle);
        }
    } catch (Exception e) {
        GradleCore.logError("Gradle dependence got error", e);
    }
}
Also used : ServiceContainer(com.liferay.ide.project.core.modules.ServiceContainer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) IOException(java.io.IOException)

Example 33 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project liferay-ide by liferay.

the class JavaFileJDT method findImport.

@Override
public SearchResult findImport(String importName) {
    List<SearchResult> searchResults = new ArrayList<>();
    _ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(ImportDeclaration node) {
            String searchContext = node.getName().toString();
            if (importName.equals(searchContext)) {
                int startLine = _ast.getLineNumber(node.getName().getStartPosition());
                int startOffset = node.getName().getStartPosition();
                int endLine = _ast.getLineNumber(node.getName().getStartPosition() + node.getName().getLength());
                int endOffset = node.getName().getStartPosition() + node.getName().getLength();
                searchResults.add(createSearchResult(searchContext, startOffset, endOffset, startLine, endLine, true));
            }
            return false;
        }
    });
    if (ListUtil.isNotEmpty(searchResults)) {
        return searchResults.get(0);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) SearchResult(com.liferay.blade.api.SearchResult) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 34 with ImportDeclaration

use of org.eclipse.jdt.core.dom.ImportDeclaration in project eclipse.jdt.ls 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<>();
            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 35 with ImportDeclaration

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

the class CompilationUnitBuilder method addImportDeclaration.

public boolean addImportDeclaration(String typeName, boolean onDemand, boolean isStatic) {
    if (onDemand) {
        if (packageImportSet.contains(typeName))
            return true;
        packageImportSet.add(typeName);
    } else {
        String packageName = StringUtils.toPackageName(typeName);
        String simpleName = StringUtils.toSimpleName(typeName);
        if (classImportSet.contains(typeName))
            return true;
        if (// && StringUtils.isAmbiguous(simpleName)
        "java.lang".equals(packageName))
            return !simpleNameImportSet.contains(simpleName);
        if (!StringUtils.isAmbiguous(simpleName)) {
            if (packageNoImportSet.contains(packageName) || simpleNameImportSet.contains(simpleName))
                return false;
            else if (packageImportSet.contains(packageName)) {
                classImportSet.add(typeName);
                simpleNameImportSet.add(simpleName);
                return true;
            }
        }
        classImportSet.add(typeName);
        simpleNameImportSet.add(simpleName);
    }
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(newQualifiedName(typeName));
    id.setOnDemand(onDemand);
    id.setStatic(isStatic);
    cu.imports().add(id);
    return true;
}
Also used : ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration)

Aggregations

ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)55 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 List (java.util.List)20 ArrayList (java.util.ArrayList)17 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)16 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)14 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 Block (org.eclipse.jdt.core.dom.Block)11 Name (org.eclipse.jdt.core.dom.Name)11 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)10 TIntArrayList (gnu.trove.list.array.TIntArrayList)9 HashSet (java.util.HashSet)9 PackageDeclaration (org.eclipse.jdt.core.dom.PackageDeclaration)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)7 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)7