Tuesday, October 30, 2012

Executing eclipse rename refactoring using LTK

With eclipse, one can use Language Tool Kit (LTK). This is an example of how to execute rename refactoring with the LTK API.

Finding a project

IProject is an interface to refer the Project object, and the project can be found using this code.
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(PROJECT_NAME);
project.open(null /* IProgressMonitor */);

Finding class

Then, we can find the class(IType), and from the type we can get CompilationUnit.
IJavaProject javaProject = JavaCore.create(project);
IType itype = javaProject.findType("smcho.NewName");
org.eclipse.jdt.core.ICompilationUnit icu = itype.getCompilationUnit();

Getting contribution and descriptor

We're renaming a class, so we use getRefactoringContribution() to get contribution, and from the contribution we can get the descriptor.
RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(IJavaRefactorings.RENAME_COMPILATION_UNIT);
RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) contribution.createDescriptor();
Then, you need to set the name of the project, new class name, and CompilationUnit.
descriptor.setProject(icu.getResource().getProject().getName( ));
descriptor.setNewName(NEW_CLASS_NAME); // new name for a Class
descriptor.setJavaElement(icu);

Executing the refactoring

One starts with the status and monitor. With them check initial and final conditions. And finally create change object and perform with a parameter monitor.
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();
}    

The renameRefactor() method

Combining all of them, one can have this method to refactor from oldName to newName in the projeceName.
public void renameRefactor(String projectName, String oldName, String newName) throws CoreException
{
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(projectName);
    project.open(null /* IProgressMonitor */);
    
    IJavaProject javaProject = JavaCore.create(project);
    IType itype = javaProject.findType(oldName); // you need to include the package name also "smcho.NewName" 
    org.eclipse.jdt.core.ICompilationUnit icu = itype.getCompilationUnit();

    RefactoringContribution contribution =
                RefactoringCore.getRefactoringContribution(IJavaRefactorings.RENAME_COMPILATION_UNIT);
    RenameJavaElementDescriptor descriptor =
                (RenameJavaElementDescriptor) contribution.createDescriptor();
    descriptor.setProject(icu.getResource().getProject().getName( )); // maybe, you can just use the String
    descriptor.setNewName(newName); // new name for a Class
    descriptor.setJavaElement(icu);

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

No comments:

Post a Comment