groovy
  1. groovy
  2. GROOVY-5286

Constant pool is used for not final fields too

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.8.5
    • Fix Version/s: 1.8.6, 2.0-beta-3
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      As a side effect to GROOVY-5150, the class constant pool is used for non final fields too:

      I have this class:

      public class A {
        public static int i = 5;
      }
      

      compile it with javac and you get the following related to i (using javap):

      public static int i;
      
      static {};
        Code:
         Stack=1, Locals=0, Args_size=0
         0:   iconst_5
         1:   putstatic       #2; //Field i:I
         4:   return
      

      Compile it with groovyc and these days I seem to get this:

      public static int i;
        Constant value: int 5
      

      Now I thought the 'constant value' attribute for field objects was for proper constants (i.e. final fields), not for initialization values. That seems to be what it indicates here:
      ===
      4.7.2 The ConstantValue Attribute

      The ConstantValue attribute is a fixed-length attribute used in the attributes table of the field_info (§4.5) structures. A ConstantValue attribute represents the value of a constant field that must be (explicitly or implicitly) static;
      ===

      Indeed javac will only use that attribute if I make the field final.

      Now the JVM doesn't seem to be particularly strict here and the static does get the right value initially and allow it to be changed (at least on the VM version I'm using, haven't tried any others). It just seems to be a slight abuse of the meaning of constant value. I guess I just wanted to check it was deliberate.

      thanks,
      Andy

        Activity

        Cédric Champeau made changes -
        Field Original Value New Value
        Fix Version/s 1.8.7 [ 18317 ]
        Cédric Champeau made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.0-beta-3 [ 18244 ]
        Resolution Fixed [ 1 ]
        Cédric Champeau made changes -
        Resolution Fixed [ 1 ]
        Status Resolved [ 5 ] Reopened [ 4 ]
        Cédric Champeau made changes -
        Status Reopened [ 4 ] Resolved [ 5 ]
        Fix Version/s 1.8.6 [ 18245 ]
        Fix Version/s 1.8.7 [ 18317 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Cédric Champeau
            Reporter:
            Cédric Champeau
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: