如何使用Eclipse JDT Core重新定义导入

时间:2023-01-14 12:09:18

Does anyone have an example how to redefine an import in a java source file using the Eclipse JDT Core API?

有没有人有一个例子如何使用Eclipse JDT Core API重新定义java源文件中的导入?

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.

我把导入名称粘贴到我的java源文件中,它很完美,它不会给我任何错误。任何帮助或指导将不胜感激。

2 个解决方案

#1


0  

Turns out this is a bug in Eclipse.

事实证明这是Eclipse中的一个错误。

#2


0  

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

我绊倒了同样的异常(即使使用Eclipse 3.7.2)。这应该是bug报告:https://bugs.eclipse.org/bugs/show_bug.cgi?id = 351940

Here is the solution that works(instead of the rename):

这是有效的解决方案(而不是重命名):

dec.delete(false, null);
unit.createImport(redefinedImport, null, dec.getFlags(), null);

Alternatively to get close to the original position

或者接近原来的位置

unit.createImport(redefinedImport, dec, dec.getFlags(), null);
dec.delete(false, null);

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.

使用ImportRewrite直接操作AST也只允许removeImport和addImport。

Is there any alternative solution to redefine/rename an import declaration programmatically?

是否有任何替代解决方案以编程方式重新定义/重命名导入声明?

#1


0  

Turns out this is a bug in Eclipse.

事实证明这是Eclipse中的一个错误。

#2


0  

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

我绊倒了同样的异常(即使使用Eclipse 3.7.2)。这应该是bug报告:https://bugs.eclipse.org/bugs/show_bug.cgi?id = 351940

Here is the solution that works(instead of the rename):

这是有效的解决方案(而不是重命名):

dec.delete(false, null);
unit.createImport(redefinedImport, null, dec.getFlags(), null);

Alternatively to get close to the original position

或者接近原来的位置

unit.createImport(redefinedImport, dec, dec.getFlags(), null);
dec.delete(false, null);

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.

使用ImportRewrite直接操作AST也只允许removeImport和addImport。

Is there any alternative solution to redefine/rename an import declaration programmatically?

是否有任何替代解决方案以编程方式重新定义/重命名导入声明?