16

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
GingerHead
  • 8,130
  • 15
  • 59
  • 93

2 Answers2

17

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.

Paul Webster
  • 10,614
  • 1
  • 25
  • 32
  • 2
    I think its a file in your eclipse directory: Core and UI A subclass of org.eclipse.ltk.core.refactoring.Refactoring must always be created. Check the text at Core and UI and "Action!" –  Feb 03 '12 at 14:08
  • 5
    The LTK APIs allows its refactorings to be configured with an object as opposed to a UI. While it needs to be an eclipse plugin, you can write a headless IApplication that can fill in the refactoring configuration from the command line and execute it against your workspace. – Paul Webster Feb 06 '12 at 15:36
  • 3
    http://www.eclipse.org/articles/article.php?file=Article-Unleashing-the-Power-of-Refactoring/index.html is a discussion of the refactoring API. The `RefactoringDescriptors` can be used to programmitcally set up a change and execute it in an operation without involving the GUI or wizards. – Paul Webster Feb 06 '12 at 19:05
  • 2
    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. – Paul Webster Feb 06 '12 at 19:07
  • 2
    I'm on irc://freenode.net/#eclipse – Paul Webster Feb 07 '12 at 12:31
  • 2
    Yes, see the article I mentioned and this thread: http://www.eclipse.org/forums/index.php/mv/msg/78987/245682/#msg_245682 – Paul Webster Feb 10 '12 at 12:51
  • 2
    When you ran it, did it not work? The API should still be valid. – Paul Webster Feb 14 '12 at 12:16
  • 1
    If you'd like to run the refactoring in Eclipse and allow the user to undo it, perform the refactoring using `PerformRefactoringOperation`: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fltk%2Fcore%2Frefactoring%2FPerformRefactoringOperation.html – reprogrammer Aug 04 '12 at 14:03
8

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);
GingerHead
  • 8,130
  • 15
  • 59
  • 93