Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Executing inline refactoring programmatically
Executing inline refactoring programmatically [message #945997] Mon, 15 October 2012 17:10 Go to previous message
Sungmin Cho is currently offline Sungmin Cho
Messages: 9
Registered: October 2012
Junior Member
Using Refactor -> Inline menu, one can transform this code

public class SimpleTest {
  	public int hello(int x, int y)
	{
            return (x + y)
	}
	
	public void caller()
	{
		int z = hello(10, 20);
	}
}


into this code:

public class SimpleTest {
	public void caller()
	{
		int z = (10 + 20);
	}
}


How can I do it using JDT/LTK api?

This is my code trying to execute inlining. However, I got org.eclipse.core.runtime.CoreException: The refactoring script argument 'input' is missing
in the refactoring script.
error, and I guess I need to fill in arguments to make it known that hello is the function to be inlined. Or, I might need to use different API.

    	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    	IProject project = root.getProject("Hello");
    	project.open(null /* IProgressMonitor */);
    	
    	IJavaProject javaProject = JavaCore.create(project);
    	IType itype = javaProject.findType("SimpleTest");
    	org.eclipse.jdt.core.ICompilationUnit icu = itype.getCompilationUnit();
  	
    	String projectName = icu.getResource().getProject().getName();
    	String description = "Inline method programmatically";
    	String comments = "";
    	Map arguments = new HashMap();
    	//arguments.put("", ""); <-- ???
    	int flags = 0;
    	
    	InlineMethodDescriptor descriptor = new InlineMethodDescriptor(
    			projectName, description, comments, arguments, flags);

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


How can I make the code working?
 
Read Message
Read Message
Read Message
Read Message
Previous Topic:How can I get the execution environment JavaSE-1.8 in Eclipse?
Next Topic:OpenJDK and Oracle JDK can exist together?
Goto Forum:
  


Current Time: Sun May 26 02:49:45 EDT 2013

Powered by FUDForum. Page generated in 0.03712 seconds