Skip to main content



      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 next message
Eclipse UserFriend
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?
Re: Executing inline refactoring programmatically [message #975514 is a reply to message #945997] Wed, 07 November 2012 18:20 Go to previous messageGo to next message
Eclipse UserFriend
I am having the same issue trying to figure out how to use the Change_Method_Signature refactoring. Where does the map come from? Are there utility classes that will automatically create them? How do you specify those parameters if you created the descriptor from the factory method instead of using the constructor directly?
Re: Executing inline refactoring programmatically [message #976404 is a reply to message #945997] Thu, 08 November 2012 09:59 Go to previous messageGo to next message
Eclipse UserFriend
I was able to finally figure out my refactoring by looking at the processor source code and reverse engineering what the map entries should be.

I found my refactoring processor here:
www.java2s.com/Open-Source/Java/IDE-Eclipse/jdt/org/eclipse/jdt/internal/corext/refactoring/structure/Catalogstructure.htm

Maybe you can find the class that performs your refactoring and reverse engineer the required map entries?

Re: Executing inline refactoring programmatically [message #978182 is a reply to message #976404] Fri, 09 November 2012 17:24 Go to previous message
Eclipse UserFriend
Instead of reverse engineering the refactoring itself, you may want to drill into the tests at http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/tree/org.eclipse.jdt.ui.tests.refactoring/test%20cases/org/eclipse/jdt/ui/tests/refactoring and analyze how the refactoring is created & invoked by these tests.

HTH
Stephan

PS: It might be a good idea to clone the entire jdt.ui repo for easier browsing / debugging of those tests. You'll find the URLs for cloning at http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/
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: Tue May 13 00:05:12 EDT 2025

Powered by FUDForum. Page generated in 0.03592 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top