Arrange Eclipse method to import
3 answers
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 to share
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 to share