From 1eaba4475b929d3d8a84212eed1e5c3f5ccb0ca2 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Fri, 15 Dec 2006 04:47:19 +0000 Subject: When a property evolves from a boxed primitive to an unboxed primitive, null is converted to zero or false instead of throwing a NullPointerException. --- .../carbonado/raw/GenericEncodingStrategy.java | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java') diff --git a/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java b/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java index 2cd0807..76622b7 100644 --- a/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java +++ b/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java @@ -1840,6 +1840,7 @@ public class GenericEncodingStrategy { if (prop.getType() == type.toClass()) { break doDrop; } + // Types differ, but if primitive types, perform conversion. TypeDesc primType = type.toPrimitiveType(); if (primType != null) { @@ -1847,7 +1848,42 @@ public class GenericEncodingStrategy { TypeDesc primPropType = propType.toPrimitiveType(); if (primPropType != null) { // Apply conversion and store property. + + Label convertLabel = a.createLabel(); + Label convertedLabel = a.createLabel(); + + if (!type.isPrimitive() && propType.isPrimitive()) { + // Might attempt to unbox null, which + // throws NullPointerException. Instead, + // convert null to zero or false. + + a.dup(); + a.ifNullBranch(convertLabel, false); + a.pop(); // pop null off stack + + switch (propType.getTypeCode()) { + default: + a.loadConstant(0); + break; + case TypeDesc.LONG_CODE: + a.loadConstant(0L); + break; + case TypeDesc.FLOAT_CODE: + a.loadConstant(0.0f); + break; + case TypeDesc.DOUBLE_CODE: + a.loadConstant(0.0d); + break; + } + + a.branch(convertedLabel); + } + + convertLabel.setLocation(); a.convert(type, propType); + + convertedLabel.setLocation(); + type = propType; break doDrop; } -- cgit v1.2.3