summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2006-12-15 04:47:19 +0000
committerBrian S. O'Neill <bronee@gmail.com>2006-12-15 04:47:19 +0000
commit1eaba4475b929d3d8a84212eed1e5c3f5ccb0ca2 (patch)
tree923e1971fa13d6d0de8085c632243d0893fe2c78 /src
parentadb4710f51589b0ef019d7720a30fadae41c8cb6 (diff)
When a property evolves from a boxed primitive to an unboxed primitive, null is converted to zero or false instead of throwing a NullPointerException.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java36
1 files changed, 36 insertions, 0 deletions
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<S extends Storable> {
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<S extends Storable> {
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;
}