Example usage for org.eclipse.jdt.core.dom.rewrite ImportRewrite getAddedImports

List of usage examples for org.eclipse.jdt.core.dom.rewrite ImportRewrite getAddedImports

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom.rewrite ImportRewrite getAddedImports.

Prototype

public String[] getAddedImports() 

Source Link

Document

Returns all non-static imports that are recorded to be added.

Usage

From source file:org.eclim.plugin.jdt.command.correct.CodeCorrectCommand.java

License:Open Source License

/**
 * Gets possible corrections for the supplied problem.
 *
 * @param src The src file.//from  w  ww.j  a v a 2  s . c  o m
 * @param problem The problem.
 * @return Returns a List of ChangeCorrectionProposal.
 */
private List<ChangeCorrectionProposal> getProposals(ICompilationUnit src, IProblem problem) throws Exception {
    IProject project = src.getJavaProject().getProject();

    ArrayList<ChangeCorrectionProposal> results = new ArrayList<ChangeCorrectionProposal>();
    int length = (problem.getSourceEnd() + 1) - problem.getSourceStart();
    AssistContext context = new AssistContext(src, problem.getSourceStart(), length);

    IProblemLocation[] locations = new IProblemLocation[] { new ProblemLocation(problem) };
    IQuickFixProcessor[] processors = JavaUtils.getQuickFixProcessors(src);
    for (int ii = 0; ii < processors.length; ii++) {
        if (processors[ii] != null && processors[ii].hasCorrections(src, problem.getID())) {
            // we currently don't support the ajdt processor since it relies on
            // PlatformUI.getWorkbench().getActiveWorkbenchWindow() which is null
            // here.
            if (processors[ii].getClass().getName()
                    .equals("org.eclipse.ajdt.internal.ui.editor.quickfix.QuickFixProcessor")) {
                continue;
            }

            IJavaCompletionProposal[] proposals = processors[ii].getCorrections(context, locations);
            if (proposals != null) {
                for (IJavaCompletionProposal proposal : proposals) {
                    if (!(proposal instanceof ChangeCorrectionProposal)) {
                        continue;
                    }

                    // skip proposal requiring gui dialogs, etc.
                    if (IGNORE_BY_TYPE.contains(proposal.getClass())
                            || IGNORE_BY_INFO.contains(proposal.getAdditionalProposalInfo())) {
                        continue;
                    }

                    // honor the user's import exclusions
                    if (proposal instanceof ASTRewriteCorrectionProposal) {
                        ImportRewrite rewrite = ((ASTRewriteCorrectionProposal) proposal).getImportRewrite();
                        if (rewrite != null && (rewrite.getAddedImports().length != 0
                                || rewrite.getAddedStaticImports().length != 0)) {
                            boolean exclude = true;
                            for (String fqn : rewrite.getAddedImports()) {
                                if (!ImportUtils.isImportExcluded(project, fqn)) {
                                    exclude = false;
                                    break;
                                }
                            }
                            for (String fqn : rewrite.getAddedStaticImports()) {
                                if (!ImportUtils.isImportExcluded(project, fqn)) {
                                    exclude = false;
                                    break;
                                }
                            }

                            if (exclude) {
                                continue;
                            }
                        }
                    }

                    results.add((ChangeCorrectionProposal) proposal);
                }
            }
        }
    }

    Collections.sort(results, new CompletionProposalComparator());
    return results;
}