You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/01/28 17:15:24 UTC
[groovy] branch GROOVY-9916 created (now d26e5e8)
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a change to branch GROOVY-9916
in repository https://gitbox.apache.org/repos/asf/groovy.git.
at d26e5e8 GROOVY-9916: primitive boolean corner case: null casts to false
This branch includes the following new commits:
new d26e5e8 GROOVY-9916: primitive boolean corner case: null casts to false
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[groovy] 01/01: GROOVY-9916: primitive boolean corner case: null
casts to false
Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-9916
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit d26e5e82b47b2f371185e2df4db90db3ba66965a
Author: Eric Milles <er...@thomsonreuters.com>
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