Bug 152224 - [extract method] allow introducing parameters instead of using members and allow static modifier
Summary: [extract method] allow introducing parameters instead of using members and al...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows 2000
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-28 18:28 EDT by Thorsten van Ellen CLA
Modified: 2006-08-02 13:02 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thorsten van Ellen CLA 2006-07-28 18:28:31 EDT
Hi!

I had the following code:

	String sourceFileData =
		source.getAbsolutePath()+Constants.CSV_SEPARATOR
		+source.getName()+Constants.CSV_SEPARATOR
		+FileHelper.getExtension(source.getName())+CSV_SEPARATOR
		+""+source.length()+Constants.CSV_SEPARATOR;

source is a non-static member/attribute of the current class of type File.

I wanted to extract a method for all those String stuff. The result should look like this:

		String sourceFileData = getFileData(source);

But I got code like this:

		String sourceFileData = getFileData();

The difference is, that 
* the parameter was not passed and 
* the extracted method used the attribute instead
* the extracted method can not be static:

The extracted method looked like this:

	private String getFileData() {
		return source.getAbsolutePath()+Constants.CSV_SEPARATOR
		+source.getName()+Constants.CSV_SEPARATOR
		+FileHelper.getExtension(source.getName())+CSV_SEPARATOR
		+""+source.length()+Constants.CSV_SEPARATOR;
	}

But I wanted this:

	public static String getFileData(File file) {
		return file.getAbsolutePath()+Constants.CSV_SEPARATOR
		+file.getName()+Constants.CSV_SEPARATOR
		+FileHelper.getExtension(file.getName())+CSV_SEPARATOR
		+""+file.length()+Constants.CSV_SEPARATOR;
	}

Note the static modifier!

Would be nice, if "extract method" would offer a possibility
* to introduce by choice a parameter for each member that is within the extracted code and
* to make the extracted method static, which implies introducing parameters for all non-static members

best regards

Thorsten van Ellen
Comment 1 Martin Aeschlimann CLA 2006-08-02 13:02:07 EDT
We currently don't offer UI for this, but the workaround is to first create local variables for the field access.
That way it is also clear that 'source' is constant during the call sequence. In general it isn't clear that all accesses to source result in the same result.

Also see bug 97870.