You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Shil Sinha (JIRA)" <ji...@apache.org> on 2016/06/13 23:34:58 UTC
[jira] [Commented] (GROOVY-7861) asType(null) called in
@CompileStatic method returns a string 'null'
[ https://issues.apache.org/jira/browse/GROOVY-7861?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15328607#comment-15328607 ]
Shil Sinha commented on GROOVY-7861:
------------------------------------
I tested this on 2.0.0, 2.1.0, and 2.4.0; it seems to always have been an issue. The difference in behavior between {{o.asType(String)}} and {{o as String}} (which returns the expected null) when o is null is due to the latter form taking the ScriptBytecodeAdapter#asType path, which sets null variables to a NullObject before proceeding.
> asType(null) called in @CompileStatic method returns a string 'null'
> --------------------------------------------------------------------
>
> Key: GROOVY-7861
> URL: https://issues.apache.org/jira/browse/GROOVY-7861
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 2.4.7
> Environment: Win, jdk1.7.79
> Reporter: Jacek Karas
> Priority: Minor
>
> The following code:
> {code}
> import groovy.transform.CompileStatic
> class Test {
> @CompileStatic
> static Object castToType(Object o, Class clazz) {
> return o.asType(clazz)
> }
> }
> def foo = Test.castToType(null, String.class)
> println "'${foo}' class: ${foo.getClass()} bytes: ${foo?.getBytes()}"
> {code}
> prints:
> {panel}
> 'null' class: class java.lang.String bytes: [110, 117, 108, 108]
> {panel}
> i.e. the method annotated with @CompileStatic converts null to the actual string 'null' (which is quite a nasty surprise when trying to "speed things up" by adding @CompileStatic to existing non-static code).
> The same without @CompileStatic returns:
> {panel}
> 'null' class: class org.codehaus.groovy.runtime.NullObject bytes: null
> {panel}
> as expected.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)