Search code examples
eclipseeclipse-jdteclipse-pluginltk

How to execute inline refactoring programmatically using JDT/LTK?


I could use Refactor->Inine when I need to inline a method.

enter image description here enter image description here enter image description here enter image description here

This the code skeleton that I tried, I used the code in this post - Is there any eclipse refactoring API that I can call programmatically?.

// 1. Get ICompiationUnit for type "smcho.Hello"
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Hello");
project.open(null /* IProgressMonitor */);

IJavaProject javaProject = JavaCore.create(project);
IType itype = javaProject.findType("smcho.Hello");
org.eclipse.jdt.core.ICompilationUnit icu = itype.getCompilationUnit();

// 2. Contribution and Description creation
RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(IJavaRefactorings.INLINE_METHOD);
InlineMethodDescriptor descriptor = (InlineMethodDescriptor) contribution.createDescriptor();

descriptor.setProject(icu.getResource().getProject().getName( ));

// 3. executing the refactoring
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();
} 

When I execute the code, I got this error

org.eclipse.core.runtime.CoreException: The refactoring script argument 'input' is missing 
in the refactoring script.  

I think I need to give the refactored method name to the API. What might be wrong in the code?


Solution

  • This is the code that works with inline refactoring JDT API. It requires start position and length to be inlined.

    int[] selection= {start, length}; // getSelection();
    InlineMethodRefactoring refactoring= InlineMethodRefactoring.create(this.icu, new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(this.icu, true), selection[0], selection[1]);
    refactoring.setDeleteSource(true);
    refactoring.setCurrentMode(Mode.INLINE_ALL); // or INLINE SINGLE based on the user's intervention
    
    IProgressMonitor pm= new NullProgressMonitor();
    RefactoringStatus res = refactoring.checkInitialConditions(pm);
    res = refactoring.checkFinalConditions(pm);
    
    final PerformRefactoringOperation op= new PerformRefactoringOperation(
    refactoring, getCheckingStyle());
    op.run(new NullProgressMonitor());
    

    When you know the name of the method that is going to be inlined, you can use the code in - Getting startPosition and length of a method invocation using JDT