I enabled the Warning Method can be static (in Preferences => Java => Compiler => Errors/Warnings) in Eclipse 4.4.2.
If I extract a method getPath with "Refactor=>Extract Method" (Alt+Shift+M) as shown in the following example, I get a warning because the extracted method can be static. How do I set up Eclipse to automatically make the extracted method static if it can be static?
Edit:
After a comment of bayou.io I saw that if there is a static modifier for the parent method, the extracted method will automatically also get a static modifier. If the public parent method does not have a static modifier, I have to manually add it to the extracted method ... if I want to do so. The reasoning seems to be that static methods might be dangerous.
If I look at the special case where the extracted method does not modify any static state (it is a "pure static helper method" that just works with the passed arguments), I would still prefer an automatically added modifier.
If something would be dangerous about this behavior please let me know. Well, maybe it would be hard/is not possible to automatically find out if the extracted method would be a dangerous static method or not.
I was refactoring some code and after that I forgot to bring down the number of additional warnings to zero. Then a colleague pointed me to my warnings and my ego asked the question "Why did Eclipse did not take care about the static modifier if it already suggests to add one?" :)
Before extraction:
public String getAbsolutePath() {
boolean runningInEclipse = true;
if (runningInEclipse) {
return "baa complicated long expressions";
} else {
return "foo";
}
}
After extraction:
public String getAbsolutePath() {
boolean runningInEclipse = true;
if (runningInEclipse) {
return getPath();
} else {
return "foo";
}
}
private String getPath() {
return "baa complicated long expressions";
}