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

Dynamic constants for primitive types get default value in Java

    XMLWordPrintableJSON

Details

    Description

      If we define a constant that is resolved during runtime in a Groovy class and reference it in a Java class, it's value gets a static default value in the Java class. This seems to be a regression in Groovy 3.0.10 as earlier versions don't have the problem. Also affects all 4.0.x versions that I tested.

      Example:

      // G.groovy
      class G {
          public static final int DYNAMIC_CONSTANT = (9.9).intValue()
      } 
      // J.java
      public class J {
          public static void main(String[] args) {
              System.out.println(G.DYNAMIC_CONSTANT);
          }
      } 

      Groovy 4.0.7
      Prints 0 with the following bytecode for J#main

      0: getstatic     #2 // Field java/lang/System.out:Ljava/io/PrintStream;
      3: iconst_0
      4: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
      7: return

      Groovy 3.0.9
      Correctly prints 9 with the following bytecode:

      0: getstatic     #2 // Field java/lang/System.out:Ljava/io/PrintStream;
      3: getstatic     #3 // Field G.DYNAMIC_CONSTANT:I
      6: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
      9: return 

       

       

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              okoskine Onni Koskinen
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: