You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2017/02/01 23:17:53 UTC

[jira] [Closed] (GROOVY-7396) Static compiler prefers overloaded method taking primitive argument over object

     [ https://issues.apache.org/jira/browse/GROOVY-7396?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King closed GROOVY-7396.
-----------------------------

> Static compiler prefers overloaded method taking primitive argument over object
> -------------------------------------------------------------------------------
>
>                 Key: GROOVY-7396
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7396
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.3.8, 2.4.3
>         Environment: OSX Yosemite, JDK 1.8.0_40
>            Reporter: Shil Sinha
>            Assignee: Jochen Theodorou
>
> If a method is overloaded to accept either a primitive or object argument, and is called with a wrapper type argument (i.e. Integer, Long, etc.), the static compiler chooses the primitive version when generating bytecode (or possibly earlier in compilation). Example:
> {code}
> import groovy.transform.CompileStatic
> @CompileStatic
> class Foo {
>   void monkeyAdd(int a) {
>     println 'monkeyAdd int'
>   }
>   void monkeyAdd(Object a) {
>     println 'monkeyAdd Object'
>   }
>   public static void main(String[] args) {
>     Integer a = Integer.valueOf(4)
>     Integer b = null
>     def foo = new Foo()
>     foo.monkeyAdd(a) //prints 'monkeyAdd int'
>     foo.monkeyAdd(b) //throws NPE
>   }
> }
> {code}
> This is not expected behavior; `monkeyAdd(Object)` should be called in both cases, as it would be in Java. 
> The bytecode generated for the `foo.monkeyAdd(a)` call is: 
> {code}
> ALOAD 3
> ALOAD 1
> INVOKEVIRTUAL java/lang/Integer.intValue ()I
> INVOKEVIRTUAL me/anyti/Foo.monkeyAdd (I)V
> ACONST_NULL
> POP
> {code}
> Similarly, the bytecode generated for the `foo.monkeyAdd(b)` call is:
> {code}
> ALOAD 3
> ALOAD 2
> INVOKEVIRTUAL java/lang/Integer.intValue ()I
> INVOKEVIRTUAL me/anyti/Foo.monkeyAdd (I)V
> ACONST_NULL
> POP
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)