我已经构建了一个eclipse插件,它将生成一个带有新类的新Java项目。
生成类后,插件必须遍历每个类并执行自动“组织导入”操作(必须以编程方式完成 - 而不是通过Eclipse SaveAction选项)。
我尝试过代码段来做同样的事情。
public void organizeImports(IProject iProj) {
try {
IPackageFragment[] packages = JavaCore.create(iProj)
.getPackageFragments();
for (IPackageFragment mypackage : packages) {
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
for (ICompilationUnit currentCompilationUnit : mypackage
.getCompilationUnits()) {
try {
System.out.println("CompilationUnit: " + currentCompilationUnit);
IEditorPart editorPart = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().activate(editorPart);
final IHandlerService handlerService = (IHandlerService) PlatformUI
.getWorkbench().getService(
IHandlerService.class);
IHandler handler = new AbstractHandler() {
public Object execute(ExecutionEvent event)
throws ExecutionException {
System.out.println("Inside execute");
return null;
}
};
handlerService
.activateHandler(
"org.eclipse.jdt.ui.edit.text.java.organize.imports",
handler);
handlerService
.executeCommand(
"org.eclipse.jdt.ui.edit.text.java.organize.imports",
null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
现在它为几个类成功导入了导入,而对于其他类,它会抛出类似这样的东西
MESSAGE A handler conflict occurred.
This may disable some commands.!MESSAGE Conflict for 'org.eclipse.jdt.ui.edit.text.java.organize.imports':
HandlerActivation(commandId=org.eclipse.jdt.ui.edit.text.java.organize.imports,
handler=com.plugin.generator.wizard.AdGenaratorWizard$1@2b8e2b8e,expression=,sourcePriority=0)
HandlerActivation(commandId=org.eclipse.jdt.ui.edit.text.java.organize.imports,
handler=com.plugin.generator.wizard.AdGenaratorWizard$1@25f025f0,expression=,sourcePriority=0)
如果您试图了解究竟发生了什么,假设您想要使用“Ctrl + Shift + O”进行手动整理导入,有时eclipse会提示您一个窗口,要求选择导入语句以在类似的包中进行选择。 (例如:选择“org.eclipse.ui.commands”或“org.eclipse.core.commands”)这就是为什么会出现上述错误信息的原因。
当我尝试通过我的代码自动运行组织导入时,它会进入选择导入的冲突并返回异常。
有没有办法解决这个问题?希望你明白究竟发生了什么。 请建议我如何做到这一点。
答案 0 :(得分:6)
我在这上面已经有一个星期了,终于让它工作了...... :)
try {
final IWorkbenchPartSite targetSite = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService()
.getActivePart().getSite();
if(targetSite!=null){
System.out.println("TargetSite obtained");
organizeImports(wiProject, targetSite);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void organizeImports(final IProject project, final IWorkbenchSite targetSite) throws CoreException {
Runnable job = new Runnable() {
@Override
public void run() {
OrganizeImportsAction org = new OrganizeImportsAction(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.getShell().getDisplay().syncExec(job);
}