Is there any Eclipse refactoring API that I can ca

2019-01-06 13:22发布

问题:

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.

回答1:

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.



回答2:

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