Java – how to rename methods programmatically using JDT

My goal is to programmatically call refactor > > to rename eclipse commands for methods in Java source files Renaming a method should also apply changes to all instances that use / reference this method

I believe JDT has a refactoring API, but I can't find any of the same documents or tutorials

Someone can point me in the right direction

Edit: no changes are required at run time

Solution

I think your most promising approach is to eclipse source code

>Use the source code to download the required version In particular, you need the source code of the JDT plug - in, which is included in the "classic" version All of the following are based on 4.2 1. > start to empty workspace. > Files – > Import: plug ins and clips > from the activity target platform, select from all..., import from projects with source folders > select at least org eclipse. jdt. UI and org eclipse. ltk. core. refactoring.

Corresponds to the starting point of refactor > > Rename to org eclipse. jdt. ui. actions. RenameAction. This is the whole rename refactoring, but it can rename anything from methods to files More relevant to you is renamesupport create(IMethod,String,int).

You can see that the renamerefactoring class is created around the processor (renamevirtualmethodprocessor or renamenonvirtualmethodprocessor), and then sent to the new instance of renamesupport Renamesupport handles all the UI used to configure refactoring, but since you execute it programmatically, you only need to use various processors Rename refactoring and processor configured by the set * () method

Now you have configured the rename refactoring instance What should I do? The actual operations in eclipse are performed across two job implementations For more information, see refactoringexecutionhelper Operation and performchangeoperation

Why does it all come down to? In addition to all the details of exception handling, with undo stack and workspace checkpoints, you can rename the virtual method using the following steps:

IMethod methodToRename = <....>
RenameMethodProcessor processor = new RenameVirtualMethodProcessor(methodToRename)
processor.setUpdateReferences(true);
processor.setNewElementName("newMethodName");

RenameRefactoring fRefactoring = new RenameRefactoring(processor);
fChange= fRefactoring.createChange(new NullProgressMonitor());
fChange.initializeValidationData(new NullProgressMonitor());
fChange.perform(new NullProgressMonitor())

There is a lot of support code for undo, progress bar, asynchronous execution, workspace checkpoints, etc. you may or may need this code, depending on how you want to run it But this is how to run refactoring courage

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>