Community
Participate
Working Groups
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
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.