Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 95991 - [refactoring] Refactor: Extracting constant String of sum of Strings - not considered constant expression
Summary: [refactoring] Refactor: Extracting constant String of sum of Strings - not co...
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 minor (vote)
Target Milestone: ---   Edit
Assignee: Tobias Widmer CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 471349 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-05-19 14:10 EDT by Per Lauge Holst CLA
Modified: 2015-06-30 03:24 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Per Lauge Holst CLA 2005-05-19 14:10:43 EDT
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
Comment 1 Frederic Fusier CLA 2005-05-20 03:47:19 EDT
Move to JDT/UI
Comment 2 Per Lauge Holst CLA 2005-05-20 04:05:26 EDT
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.
Comment 3 Per Lauge Holst CLA 2005-06-09 09:36:04 EDT
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);
Comment 4 Tobias Widmer CLA 2005-06-09 09:54:23 EDT
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.
Comment 5 Tobias Widmer CLA 2005-10-19 13:34:58 EDT
Closing as wont fix, since data flow analysis will not be done
Comment 6 Noopur Gupta CLA 2015-06-30 03:24:49 EDT
*** Bug 471349 has been marked as a duplicate of this bug. ***