Arrange Eclipse method to import

I need to know what method is called inside eclipse when I click " CTRL+ SHIFT+ O" (Organize Imports) to call it after generating the code. What is the name of this method and where can I find it (Package.Interface)

thank

+3


source to share


3 answers


The "Arrange Imports" action is provided org.eclipse.jdt.ui.actions.OrganizeImportsAction

, which in turn calls org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation

.



+3


source


Finaly Managed to do it with this code (targetSite is the IWorkbench site, initialized at ame time as a shell):



@Override
public void postLaunchAction(final IProject project, final IProgressMonitor monitor) throws CoreException {

    super.postLaunchAction(project, monitor);

    Runnable job = new Runnable() {

        @Override
        public void run() {
            OrganizeImportsAction org = new OrganizeImportsAction(SpringServicesAction.this.targetSite);
            try {
                IJavaProject prj = null;
                if (project.hasNature("org.eclipse.jdt.core.javanature")) {
                    prj = JavaCore.create(project);
                }

                IStructuredSelection selection = new StructuredSelection(prj);
                org.run(selection);
            } catch (CoreException ce) {
                ce.printStackTrace();
            }
        }

    };

    this.shell.getDisplay().syncExec(job);
}

      

+1


source


For reference, here's how I did it:

I made a big automatic refactor in our project codebase. Due to (I think so) a bug in eclipse refactoring static methods that are statically imported into another file, I had to call order the imports after each refactoring (also because I commit each move to git automatically):

private void organizeImports(ICompilationUnit cu)
        throws OperationCanceledException, CoreException {

    cu.becomeWorkingCopy(null);
    CompilationUnit unit = cu.reconcile(AST.JLS4, false, null, pm);
    NullProgressMonitor pm = new NullProgressMonitor();

    OrganizeImportsOperation op = new OrganizeImportsOperation(cu, unit,
            true, true, true, null);

    TextEdit edit = op.createTextEdit(pm);
    if (edit == null) {
        return;
    }

    JavaModelUtil.applyEdit(cu, edit, true, pm);
    cu.commitWorkingCopy(true, pm);
    cu.save(pm, true);
}

      

Disadvantagde: Prevented access. If anyone has an idea to properly call this action without creating a new runnable and without using a shell, etc. please comment.

0


source







All Articles