GingerHead GingerHead - 8 months ago 44
Java Question

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!


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();


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

After that:

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


Change change = refactoring.createChange(monitor);

find below the code for setting the descriptor:

String id = IJavaRefactorings.RENAME_METHOD;
RefactoringContribution contrib = RefactoringCore.getRefactoringContribution(id);
RenameJavaElementDescriptor desc = contrib.createDescriptor();