简体   繁体   English

在Eclipse中重命名重构

[英]rename refactor in eclipse

While developing Eclispe plugin, I am able to programmatically rename a class-field using the following code. 在开发Eclispe插件时,我可以使用以下代码以编程方式重命名类字段。

RenameSupport renameSupport = RenameSupport.create(field, newName, RenameSupport.UPDATE_REFERENCES);
renameSupport.perform(workbench.getShell(), workbench);

But it applies the changes to the actual source files. 但是它将更改应用到实际的源文件。 Is there anyway that can be prevented? 有什么可以避免的吗? I just need the renamed code internally (for performing some other computations), must not change the actual source. 我只需要内部重命名的代码(用于执行其他一些计算),不得更改实际源代码。

Please suggest. 请提出建议。

You could copy it into a temporary file with File.createTempFile() and then rename the code in your temporary file, if RenameSupport lets you do that. 您可以使用File.createTempFile()将其复制到临时文件中,然后在RenameSupport允许的情况下重命名临时文件中的代码。 If it doesn't, then you can copy the original to a temporary file and copy it back once your other computations are finished. 如果不是,那么您可以将原始文件复制到一个临时文件,并在其他计算完成后将其复制回。

First, create an instance of RefactoringDescriptor . 首先,创建一个RefactoringDescriptor实例。 Then, invoke createRefactoring on it to create an instance of Refactoring . 然后,对其调用createRefactoring以创建Refactoring的实例。 You can get the change object by invoking createChange on the Refactoring object. 您可以通过在Refactoring对象上调用createChange来获取更改对象。 The Change object will tell you how the refactoring is going to change the code. Change对象将告诉您重构将如何更改代码。 Finally, you can invoke the method perform on the Change object to apply it on the underlying files. 最后,您可以在Change对象上调用perform方法,以将其应用于基础文件。

The plugin org.eclipse.jdt.ui.tests.refactoring contains automated unit tests for Java refactorings in Eclipse. 插件org.eclipse.jdt.ui.tests.refactoring包含针对Eclipse中Java重构的自动化单元测试。 For a concrete example of how to invoke a refactoring programmatically, refer to org.eclipse.jdt.ui.tests.refactoring.RefactoringTest . 有关如何以编程方式调用重构的具体示例,请参阅org.eclipse.jdt.ui.tests.refactoring.RefactoringTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM