You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Onni Koskinen (Jira)" <ji...@apache.org> on 2023/01/13 10:23:00 UTC

[jira] [Commented] (GROOVY-10902) Dynamic constants for primitive types get default value in Java

    [ https://issues.apache.org/jira/browse/GROOVY-10902?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17676560#comment-17676560 ] 

Onni Koskinen commented on GROOVY-10902:
----------------------------------------

Initially encountered this when upgrading a Grails 3 project to Grails 5 but this minimal reproduction case was tested with the following bash function:
{code:bash}
test_with() {
    local version=$1
    sdk use groovy $version &&
    groovyc -j G.groovy J.java &&
    java -cp ~/.sdkman/candidates/groovy/$version/lib/groovy-$version.jar:. J &&
    javap -v J
}
{code}

> Dynamic constants for primitive types get default value in Java
> ---------------------------------------------------------------
>
>                 Key: GROOVY-10902
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10902
>             Project: Groovy
>          Issue Type: Bug
>          Components: Stub generator / Joint compiler
>    Affects Versions: 3.0.14, 4.0.7
>            Reporter: Onni Koskinen
>            Priority: Major
>
> 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:
> {code:groovy}
> // G.groovy
> class G {
>     public static final int DYNAMIC_CONSTANT = (9.9).intValue()
> } {code}
> {code:java}
> // J.java
> public class J {
>     public static void main(String[] args) {
>         System.out.println(G.DYNAMIC_CONSTANT);
>     }
> } {code}
> *Groovy 4.0.7*
> Prints 0 with the following bytecode for J#main
> {code:java}
> 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{code}
> *Groovy 3.0.9*
> Correctly prints 9 with the following bytecode:
> {code:java}
> 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 {code}
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)