Bug 225716 - [extract method] support extraction to existing / new class
Summary: [extract method] support extraction to existing / new class
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3.2   Edit
Hardware: PC Windows 2000
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-04-04 05:18 EDT by Kai Grabfelder CLA
Modified: 2011-08-31 15:55 EDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kai Grabfelder CLA 2008-04-04 05:18:51 EDT
see summary...
Comment 1 Kai Grabfelder CLA 2008-04-04 05:26:47 EDT
sorry, not able to change description: enhance "extract method": support extraction to existing / new class
Comment 2 Martin Aeschlimann CLA 2008-04-08 05:37:26 EDT
Can you give some example where this is most useful?

Why not using the combination 'extract method' and 'move method'?

Comment 3 Mike Haller CLA 2008-04-13 05:19:06 EDT
Martin, imagine the scenario where you want to move a block of code into a new utility class.

First, the "Extract Method" refactoring does not provide a way to extract into a new class, thus you would need to create the class first, or extract the method into the same class first. The "Move..." refactoring is extremely unhelpful sometimes, esp. when the target class is not "related" in any way to the current class. 

Suppose something like this:

public class Sourceclass {

	public void showCaseForRefactoring() throws Exception {
		// Some String manipulation.
		// This is what we want to move to a new util class "StringUtils"
		String input = "FooBar";
		input = input == null ? "" : input.trim();
		input=input.toLowerCase(Locale.ENGLISH);
		
		// Use the thing
		System.out.println(input);
	}
	
}

Select the three lines where "input" is created and hit "Extract Method...", move to the new method "lowerString". You will get this:

public void showCaseForRefactoring() throws Exception {
		// Some String manipulation.
		// This is what we want to move to a new util class "StringUtils"
		String input = lowerString();
		
		// Use the thing
		System.out.println(input);
	}

	public String lowerString() {
		String input = "FooBar";
		input = input == null ? "" : input.trim();
		input=input.toLowerCase(Locale.ENGLISH);
		return input;
	}


If you now select the method "lowerString" and hit the "Move..." refactoring, you will end up with the following error message:

"This method cannot be moved, since no possible targets have been found. An instance method can be moved to source classes that are used as types of its parameters or types of fields declared in the same class as the method."

Of course, this is correct, but for the example above, it's of no use. I would have to mark the new method "static", but the "Extract Method" refactoring does not allow this.

So, i make the method manually "public static", then use "Move..." as you suggested. I enter the new name of the new class "StringUtils" I want to create. But I get the error message "Destination type does not exist (fully qualified type name expected)".

Okay, so it gets more complicated now. I have to create the StringUtils class first. Abort the "Move...", create new Class using the New Class Wizard, come back where I have been editing and use again the "Move..." refactoring.

All these steps above could be done within a single one.

Suggestion:
- Add a "[] static" option in the "Extract method..."
- Allow the user to enter a classname as a destination
- Allow the user to enter a non-existing classname as a destination