9

My aim is to programmatically call the Refactor >> Rename Eclipse command for a method inside a Java Source File. Renaming a method as such should also apply the change to all the instances where this method is being used/referred.

I believe that JDT has a Refactoring API, but not able to find any documents or tutorials for the same.

Can somebody point me in the right direction.

Edit: The change is not needed at runtime.

Unni Kris
  • 3,081
  • 4
  • 35
  • 57
  • http://stackoverflow.com/questions/1712271/use-jdt-to-get-full-method-name might hint you in the right direction - it shows at least some method handling jdt functionality – Wolfgang Fahl Oct 19 '12 at 06:13
  • I don't know if it's possible. You can do that by using `ANT` script before compilation. Your classes are not dynamic and you doesn't use dynamic class loading. – Maxim Shoustin Oct 19 '12 at 06:14
  • @Fess: I think u misunderstood my question. Its not for class files but for Java source files. I have updated the question to reflect this. – Unni Kris Oct 19 '12 at 06:17
  • Try use `JCodeModel` [link](http://stackoverflow.com/questions/121324/a-java-api-to-generate-java-source-files#136010) or Eclipse JDT's `AST`: [link](http://stackoverflow.com/questions/121324/a-java-api-to-generate-java-source-files#136016) – Maxim Shoustin Oct 19 '12 at 06:46
  • Did I understood you correctly that you want to do this ***at runtime***? – Philipp Oct 23 '12 at 12:01

1 Answers1

4

I think your most promising approach is to go to the eclipse source code.

  1. Download the release you want, with source code. In particular, you want the source for the JDT plugins, which is included in the 'classic' release. All of the following is based on 4.2.1.
  2. Boot up in to an empty workspace.
  3. File->Import: Plug-ins and Fragments
  4. Import from "The active target platform", "Select from all...", "Projects with source folders"
  5. Select at least org.eclipse.jdt.ui and org.eclipse.ltk.core.refactoring.

The starting point corresponding to Refactor >> Rename is org.eclipse.jdt.ui.actions.RenameAction. That's for the overall rename refactoring though, it can rename anything from methods to files. More relevant for you is RenameSupport.create(IMethod, String, int).

You can see there that a RenameRefactoring class is created around either a processor, either a RenameVirtualMethodProcessor or a RenameNonVirtualMethodProcessor, and then sent to a new instance of RenameSupport. RenameSupport handles all the UI to configure your refactoring, but since you're doing it programatically you just need the RenameRefactoring and the processor, configured using the various processor.set*() methods.

Now you have a configured instance of RenameRefactoring. Now what? The actual operation in Eclipse is executed across two Job implementations. Take a look at RefactoringExecutionHelper.Operation and PerformChangeOperation for the details.

What does this all boil down to? Leaving aside all the fine details of exception handling, having an undo stack, and workspace checkpoints, you could rename a 'virtual' method using these 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's a lot of support code in there for undo, progress bars, async execution, workspace checkpoints etc, which you may or may need depending on how you want to run this. But that's the guts of how to run the refactoring.

sharakan
  • 6,821
  • 1
  • 34
  • 61
  • Thanks! that was exactly a type of answer what i needed, +1, let me check if can get the code to work. Do you know any websites resources or explanations online for this ?? – Unni Kris Oct 25 '12 at 05:54
  • Not really. A few about how to write new refactorings to plug in to the IDE (eg http://stackoverflow.com/questions/1314208/writing-a-new-refactoring-plugin-for-eclipse) but not anything about calling the existing refactorings in different ways. – sharakan Oct 25 '12 at 12:39
  • hi thanks for the link. waiting answers from other persons too. – Unni Kris Oct 25 '12 at 15:07