如何使用 Eclipse JDT Core 重新定义导入
有谁有如何使用 Eclipse JDT Core API 重新定义 java 源文件中的导入的示例吗?
我有以下内容(不起作用),这让我发疯。
try {
for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
if (root.getElementName().equals("src")) {
for (ICompilationUnit unit : root.getPackageFragment("soap.service.implementation.strongProfile.delegate").getCompilationUnits()) {
System.out.println(unit.getElementName());
for (IImportDeclaration dec : unit.getImports()) {
dec.rename("soap.service.implementation.strongProfile.reader.HeadlineReader", true, null);
}
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
我得到的例外是:
Java Model Exception: Java Model Status [Invalid name specified: soap.service.implementation.strongProfile.reader.HeadlineReader]
我获取导入名称并将其粘贴到我的 java 源文件中,它很完美,它不会给我任何错误。任何帮助或指导将不胜感激。
Does anyone have an example how to redefine an import in a java source file using the Eclipse JDT Core API?
I have the following (which does not work) and it's driving me mad.
try {
for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
if (root.getElementName().equals("src")) {
for (ICompilationUnit unit : root.getPackageFragment("soap.service.implementation.strongProfile.delegate").getCompilationUnits()) {
System.out.println(unit.getElementName());
for (IImportDeclaration dec : unit.getImports()) {
dec.rename("soap.service.implementation.strongProfile.reader.HeadlineReader", true, null);
}
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
The exception I get is:
Java Model Exception: Java Model Status [Invalid name specified: soap.service.implementation.strongProfile.reader.HeadlineReader]
I take the import name and paste it in to my java source file and it's perfect, it doesn't give me any errors. Any help or guidance would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明这是 Eclipse 中的一个错误。
Turns out this is a bug in Eclipse.
我遇到了同样的异常(即使使用 Eclipse 3.7.2)。这应该是错误报告: https://bugs.eclipse.org/bugs/show_bug .cgi?id=351940
这是有效的解决方案(而不是重命名):
或者接近原始位置
但是这种方法不会保留导入声明的原始位置。由于我的代码包含注释和注释,因此必须在原始位置更改导入声明。
使用 ImportRewrite 直接操作 AST 也只允许 removeImport 和 addImport。
是否有任何替代解决方案以编程方式重新定义/重命名导入声明?
I was stumbling over the same exception (even with Eclipse 3.7.2). This should be the bugreport: https://bugs.eclipse.org/bugs/show_bug.cgi?id=351940
Here is the solution that works(instead of the rename):
Alternatively to get close to the original position
However this approach does not maintain the original positions of the import declarations. As my code contains comments and annotations the import declaration must be changed at the original position.
Directly manipulating the AST with ImportRewrite does also only allow a removeImport and addImport.
Is there any alternative solution to redefine/rename an import declaration programmatically?