Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5625

Static initialization done in wrong order

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Cannot Reproduce
    • 1.5.5, 1.7.10, 1.8.4, 2.0.0
    • None
    • bytecode, class generator
    • Linux 2.6.32-41-generic Ubuntu SMP i686

    Description

      When a class variable, which is initialized in a static block, is assigned to another static variable, the latter will stay null throughout execution.

      The decompiled class file looks like this (verbosity omitted):

      StaticInitializerBugGroovy.class
      // ...
      public class StaticInitializerBug implements GroovyObject {
      
        private static String VAR1;
        private static String VAR2;
      
        // ...
      
        static {
          __$swapInit();
          // ...
          String str1 = (String)VAR1;
          VAR2 = (String)str1;
          String str2 = "Hi.";
          VAR1 = str2;
          // ...
        }
      
        // ...
      }
      

      I guess, the problem is clearly visible, as the swapping via temporary variables is done in the wrong order.

      The same scenario written and compiled in Java 6 and then decompiled again produces the following:

      StaticInitializerBugJava.class
      public class StaticInitializerBug {
        static String VAR1;
        static String VAR2;
      
        // ...
      
        static {
          VAR1 = "Hi.";
          VAR2 = VAR1;
        }
      
        // ...
      }
      

      Attachments

        1. StaticInitializerBug.groovy
          0.5 kB
          Benjamin Schmidt

        Activity

          People

            blackdrag Jochen Theodorou
            benschmidt Benjamin Schmidt
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: