String folding stops when a non-constant object is reached

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 9
    • Component/s: tools

      Even after JDK-8134007, it seems javac only folds String-like constants from left to right, and it breaks as soon as non-constant is reached. See e.g.:

      public class StaticStringConst {

              static final int INT = 42;
              static final Object OBJ = new Object();
              static final String STR = "Foo";

              public static String m1() {
                      return "" + INT + STR + OBJ;
              }

              public static String m2() {
                      return "" + STR + INT + OBJ;
              }

              public static String m3() {
                      return "" + OBJ + INT + STR;
              }

              public static String m4() {
                      return "" + OBJ + STR + INT;
              }
      }

      m1() folds nicely to "42Foo" + OBJ, but m2() folds only to OBJ + 42 + "Foo".

            Assignee:
            Maurizio Cimadamore
            Reporter:
            Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: