Community
Participate
Working Groups
I came across a minor bug, which just puzzles me. If I have a sum of Strings I cannot refactor the constant strings into a Constant if they are added to an unknown String. It is not possible to "Refactor |> Extract Constant" in the first line of the following, but it is in the 2 other cases: public class Ref { public static void main(String[] args) { // not possible to refactor "a"+"b" 'Cannot extract this expression - it is not a valid static constant' System.out.println( "a"+"b"+args[0]); // possible to extract "a"+"b" as a constant System.out.println( "a"+"b"+1); // possible to extract "a"+"b" as a constant System.out.println( "a"+"b"+"c"); } } I Haven't tested on other version, but the bug exists in: Eclipse 3.1 Build id: I20050513-1415 As an aside, I'd like to point out, that externalizing strings doesn't consider the constants ("a" and "b") as constants. Whether this has anything to do with the consideration that "a"+"b" is constant in some instances while being variable in others I cannot say. /per
Move to JDT/UI
I've just tested what actually happens if you actually extract the constant String "a"+"b" in one of the cases where you're able to. If you select "Replace all occurences of the selected expression with references to the constant" (default option) then "a"+"b" will be replaced in all 3 lines -- ergo in "a"+"b"+args[0] the "a"+"b" part is considered a constant somewhere else.
Seems it's not just a problem for Strings. int var = 0; System.out.println(var+5*10+1); 5*10+1 is not considered a constant expression in the above, but is in the expression below: System.out.println(5*10+1);
The behavior mentioned in comment 3 is correct, because it honors operator precedence (as reflected in the AST). We do not perform a full data flow analysis here.
Closing as wont fix, since data flow analysis will not be done
*** Bug 471349 has been marked as a duplicate of this bug. ***