2

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";
}
Stefan
  • 10,010
  • 7
  • 61
  • 117
  • do you want that though? even if a method impl can be static at the moment, it might be better left as instance method, from a design point of view. – ZhongYu May 12 '15 at 20:00
  • Yes. For pros and cons on static modifiers see for example here: http://stackoverflow.com/questions/11240178/what-is-the-gain-from-declaring-a-method-as-static – Stefan May 12 '15 at 20:10
  • it'll be quite dangerous to do that automatically. the programmer needs to make the call whether it really should be static. – ZhongYu May 12 '15 at 20:40
  • Hi. Your last comment let me think again about it and I further played around with the example. Now my understanding about the issue is better. I will modify the question and include the fact that if I add a static modifier to the public parent method, the extracted method will also be static. If the public method is not static I have to manually add the modifier for the extracted method. If we look at the special case where the extracted method does not modify any static state, would you still find it dangerous? – Stefan May 13 '15 at 05:12

0 Answers0