This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 3128def  GROOVY-9916: primitive boolean corner case: null casts to 
false
3128def is described below

commit 3128def05ed2b35c7b1ca5fbaf169213a4c9b5fe
Author: Eric Milles <[email protected]>
AuthorDate: Thu Jan 28 11:11:04 2021 -0600

    GROOVY-9916: primitive boolean corner case: null casts to false
    
    class C {
      private boolean b
      void m() {
        b = null; // DefaultTypeTransformation.booleanUnbox(null)
        { ->
          b = null // DefaultTypeTransformation.castToType(null,boolean)
        }.call()
      }
    }
---
 .../groovy/runtime/typehandling/DefaultTypeTransformation.java   | 2 +-
 .../runtime/typehandling/DefaultTypeTransformationTest.groovy    | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
 
b/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
index ec42a9d..36264af 100644
--- 
a/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
+++ 
b/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
@@ -215,7 +215,7 @@ public class DefaultTypeTransformation {
     }
 
     public static Object castToType(Object object, Class type) {
-        if (object == null) return null;
+        if (object == null) return type == boolean.class ? Boolean.FALSE : 
null;
         if (type == Object.class) return object;
 
         final Class aClass = object.getClass();
diff --git 
a/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
 
b/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
index ab001e2..b5264ff 100644
--- 
a/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
+++ 
b/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
@@ -28,6 +28,15 @@ final class DefaultTypeTransformationTest {
     void testCastToType() {
         def input = null, result
 
+        result = DefaultTypeTransformation.castToType(input, int)
+        assert result === null
+
+        result = DefaultTypeTransformation.castToType(input, long)
+        assert result === null
+
+        result = DefaultTypeTransformation.castToType(input, boolean)
+        assert result === false // GROOVY-9916
+
         result = DefaultTypeTransformation.castToType(input, Object)
         assert result === null
 

Reply via email to