Bug 152224

Summary: [extract method] allow introducing parameters instead of using members and allow static modifier
Product: [Eclipse Project] JDT Reporter: Thorsten van Ellen <thorsten.van.ellen>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: ASSIGNED --- QA Contact:
Severity: enhancement    
Priority: P3    
Version: 3.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

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.