Is there any eclipse refactoring API that I can call programmatically?

I need to refactor code in a wide term. I know that from inside the Eclipse IDE I can refactor my classes. But is there any API that I can use in a java project so that I can refactor projects dynamically through code?



I need some idea on how to achieve the following: a program that calls all the eclipse refactorings for renaming and moving in a loop to refactoring the entire project in one shot!



I don't want to introduce new refactoring types by extending the refactoring classes. I just want to call them programmatically.



Thanks again to Paul Webster, Leif Frenzer, Mohsen Vakilian, and Arno for their great articles and help!

-------------Problems Reply------------

Something like this?

Anyone who supports a programming language in an Eclipse-based IDE will be asked sooner or later to offer automated refactorings - similar to what is provided by the Java Development Tools (JDT). Since the release of Eclipse 3.1, at least part of this task (which is by no means simple) is supported by a language neutral API: the Language Toolkit (LTK). But how is this API used?

EDIT:

If you want to programmatically run refactorings without using the UI, RefactoringDescriptors (see article) can be used to fill in the parameters and execute the refactoring programmatically. If you create a plugin that depends on org.eclipse.core.runtime and add the org.eclipse.core.runtime.applications extension, you will be able to run an IApplication class from eclipse similar to a main(String[]) class in plain java apps. An example of calling the API can be found on the post.

ICompilationUnit cu = ... // an ICompilationUnit to rename

RefactoringContribution contribution =
RefactoringCore.getRefactoringContribution(IJavaRefactorings .RENAME_COMPILATION_UNIT);
RenameJavaElementDescriptor descriptor =
(RenameJavaElementDescriptor) contribution.createDescriptor();
descriptor.setProject(cu.getResource().getProject().getName( ));
descriptor.setNewName("NewClass"); // new name for a Class
descriptor.setJavaElement(cu);

RefactoringStatus status = new RefactoringStatus();
try {
Refactoring refactoring = descriptor.createRefactoring(status);

IProgressMonitor monitor = new NullProgressMonitor();
refactoring.checkInitialConditions(monitor);
refactoring.checkFinalConditions(monitor);
Change change = refactoring.createChange(monitor);
change.perform(monitor);

} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

If you have more detailed questions about using the JDT APIs (AST, Refactoring, etc) I'd suggest you ask on the JDT Forum.

The answer below is great, but I answered with a wider perspective for the people who need a more bulky and tasty crunch of this wonderful cake:

RefactoringStatus status = new RefactoringStatus();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject[] projects = root.getProjects();

then:

for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
IType primary = unit.findPrimaryType();
IMethod[] methods = primary.getMethods();
int i = 1;
for (IMethod method : methods) {
if (method.isConstructor()) {
continue;
}
makeChangetoMethods(status, method,"changedMethodVersion_" + i);
++i;
}
}

After that:

IProgressMonitor monitor = new NullProgressMonitor();
status = new RefactoringStatus();
Refactoring refactoring = performMethodsRefactoring(status, methodToRename, newName);

then:

Change change = refactoring.createChange(monitor);
change.perform(monitor);

find below the code for setting the descriptor:

String id = IJavaRefactorings.RENAME_METHOD;
RefactoringContribution contrib = RefactoringCore.getRefactoringContribution(id);
RenameJavaElementDescriptor desc = contrib.createDescriptor();
desc.setUpdateReferences(true);
desc.setJavaElement(methodToRename);
desc.setNewName(newName);
desc.createRefactoring(status);

Category:java Views:2 Time:2012-02-03

Related post

  • How do I use Eclipse Refactoring Tools and stay in sync with SVN through Subclipse? 2009-02-05

    I have found that when I need to rename a Java class (and therefore the source file) or need to change the package name (and therefore move the source file[s]), I can either: Use the nice and useful Eclipse refactoring tools which is great since all

  • Error in Preview of Custom Eclipse refactoring 2009-11-18

    I am implementing an new eclipse refactoring. This will enable developers to Pull-up the preconditions statements from a child method to the parent method. This all works perfectly when I select "Finish" in the refactoring wizard, but when I select "

  • Eclipse refactoring tutorials 2010-05-10

    Does anyone know of a website that has a tutorial and example of each Refactoring technique used in Eclipse? I can't find one. --------------Solutions------------- http://www.ibm.com/developerworks/opensource/library/os-eclipse-refactoring/index.html

  • Eclipse Jobs API for a stand-alone Swing project 2012-01-05

    I am looking into the Eclipse Jobs API and was wondering if anyone has used it in a stand-alone Swing project (aka not Eclipse RCP)? Also is there any another competing framework like the Jobs API? --------------Solutions------------- You have to inc

  • Eclipse refactoring like "change method signature" for generic parameter lists? 2012-02-25

    Is there an Eclipse refactoring equivalent to the "change method signature" refactoring that would allow you to modify the "signature" of a class, enum, or interfaces's generic parameters? For instance, if I wanted to remove, reorder, or introduce ad

  • Eclipse Plugin API: how can I add javadoc support for specific platform plugins? 2009-04-11

    using Eclipse 3.4 Ganymede and I cannot find the way to add javadoc support for all platform plugin apis, but specifically to JFace e Ui Forms packages. Opening the property panel of any jar linked to the library named "plug-in dependecies" I receive

  • maven + eclipse + servlet-api.jar 2010-07-11

    I'm using Eclipse along with plugin m2eclipse to build and manage my project. In POM, I have included entry for servlet-api: <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version

  • Eclipse refactoring plugins 2010-12-26

    I am after more refactoring options than the default that comes in Eclipse 3.6. Can anyone list any other plugins that do refactoring and what they do ? My original problem was i wanted to re-order some type parameter declarations in some classes and

  • I can't delete a method using eclipse refactoring? 2011-01-15

    I have a method which I want to delete. This method is being called from n number of classes. I want to delete this method using refactoring and also make eclipse delete all calls to this method rather than go and clean up in each file. I could not f

  • Eclipse refactoring fails -> Keeps throwing exceptions! 2011-03-15

    I'm using Eclipse Helios and for every refactoring that spans multiple files it plainly fails! Nothing happens. On examining the logs here's the error that I see: !ENTRY org.eclipse.ltk.ui.refactoring 4 10000 2011-03-13 14:15:31.842 !MESSAGE Internal

  • How to access Eclipse refactoring scripts from command line for automation 2011-07-03

    Eclipse allows to create refactoring scripts through the GUI. I'm looking for a way to generate the script files with inputs from an external module and then automate the refactoring based on them. Would it be possible to do this by a mere jar/class

  • Eclipse: "Refactor - Rename" completely messed up, replacing arbitrary characters in file 2011-07-06

    When refactoring a class name, Eclipse lately shows a very strange behavior. For example, let's look at one of our classes: public class CampaignCSVPanel extends ContentPanel { private FileUploadField uploadFieldCsv = new FileUploadField(); ... } and

  • Eclipse refactoring methods (specifically extract method) have stopped working 2011-09-20

    I have been using eclipse indigo happily for android development for a while. In the last few days it has stopped refactoring. I have tried a project clean and of course restarting eclipse. I have also tried renaming the org.eclipse.core.resources di

  • Eclipse: Refactoring-renaming GWT uibinder java bean does not rename template ui.xml file 2011-10-02

    When using Eclipse Google plugin, when you create a gwt uibinder pair, you get a java bean file and its corresponding .ui.xml template file. I believe I used to be able to do this in Eclipse Helios: Right-click on the java bean of a uibinder set, ref

  • eclipse refactoring - pull up method to interface 2011-12-16

    How can I change the default behaviour of eclipse so that it does not prepend the methods it pulls up to an interface with "public abstract" example, I have a class A that implements interface IA. Suppose I added a method void a() to A, and I right c

  • Eclipse, refactoring a java method into a another class 2012-02-17

    How can I refactor(move) a Java method in classA into classB and have all references to the method to it updated? Is this supported in Eclipse ? --------------Solutions------------- For a static method, you can right click and select 'Move'. Obj1.myM

  • Usage of Eclipse PDE API Tooling 2014-12-29

    Several teams in our company use internal framework to build an Eclipse UI. This framework is composed of set of Eclipse plugins. Our team is responsible for the develpment of this framework and we're looking for some tools that would check API break

  • Eclipse refactor tool and ICU4J 2011-12-15

    I am using Eclipse on a Java project which involves ICU4J 3.4.4 which will be running on mobile devices. The ICU4J 3.4.4 is a few megabytes and I need to cut this down. I am only using the Normalizer class Is there an easy way in Eclipse to move the

  • Eclipse: refactoring functionality in Struts 2 app. Possible? 2012-02-07

    in struts.xml I have a list of action's. In .jsp pages I have a links on actions, that described in struts.xml. So, for example, I changed name for one action, on the next step - I must fix all links on all pages, I must set correct new action name.

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.096 (s). 11 q(s)