@Override public boolean canStore(Object value) { if (type == null) { return false; } else if (type == int.class) { return value instanceof Integer; } else if (type == long.class) { return value instanceof Long || (layout.isAllowedIntToLong() && value instanceof Integer); } else if (type == double.class) { return value instanceof Double || (layout.isAllowedIntToDouble() && value instanceof Integer); } else if (type == boolean.class) { return value instanceof Boolean; } else if (type == Object.class) { return true; } else { throw new IllegalStateException(); } } }
@Override public void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException { if (type == Object.class) { ((LocationImpl) objectLocation).setInternal(store, value); } else { long rawValue; if (type == int.class && value instanceof Integer) { rawValue = (int) value; } else if (type == long.class && value instanceof Long) { rawValue = (long) value; } else if (type == long.class && layout.isAllowedIntToLong() && value instanceof Integer) { rawValue = (int) value; } else if (type == double.class && value instanceof Double) { rawValue = Double.doubleToRawLongBits((double) value); } else if (type == double.class && layout.isAllowedIntToDouble() && value instanceof Integer) { rawValue = Double.doubleToRawLongBits((int) value); } else if (type == boolean.class && value instanceof Boolean) { rawValue = (boolean) value ? 1 : 0; } else { throw incompatibleLocation(); } primitiveLocation.setLongInternal(store, rawValue); } }
protected boolean isLocationAssignableFrom(Location destination, Location source) { LayoutImpl layout = this; if (destination.isFinal()) { // allowed Final<X>Location => Final<X>Location // allowed FinalIntLocation => Final{Int,Double}Location // allowed: Final{Int,Double,TypedObject}Location => FinalObjectLocation if (!source.isFinal()) { return false; } } if (destination instanceof IntLocation) { return (source instanceof IntLocation); } else if (destination instanceof DoubleLocation) { return (source instanceof DoubleLocation || (layout.isAllowedIntToDouble() && source instanceof IntLocation)); } else if (destination instanceof LongLocation) { return (source instanceof LongLocation || (layout.isAllowedIntToLong() && source instanceof IntLocation)); } else if (destination instanceof BooleanLocation) { return (source instanceof BooleanLocation); } else if (destination instanceof TypedObjectLocation) { return source instanceof TypedObjectLocation && ((TypedObjectLocation<?>) destination).getType().isAssignableFrom(((TypedObjectLocation<?>) source).getType()); } else if (destination instanceof ValueLocation) { return false; } else { assert destination instanceof ObjectLocation || destination instanceof DualLocation; return true; } }
if (oldLocation instanceof IntLocation && value instanceof Integer) { newLocation = oldLocation; } else if (oldLocation instanceof DoubleLocation && (value instanceof Double || this.isAllowedIntToDouble() && value instanceof Integer)) { newLocation = oldLocation; } else if (oldLocation instanceof LongLocation && (value instanceof Long || this.isAllowedIntToLong() && value instanceof Long)) {
@Override protected Location locationForValueUpcast(Object value, Location oldLocation) { assert !oldLocation.canSet(value); if (oldLocation instanceof DeclaredLocation) { return locationForValue(value); } else if (oldLocation instanceof ConstantLocation) { return constantLocation(value); } else if (oldLocation instanceof TypedLocation && ((TypedLocation) oldLocation).getType().isPrimitive()) { if (!shared && ((TypedLocation) oldLocation).getType() == int.class) { InternalLongLocation primLocation = ((PrimitiveLocationDecorator) oldLocation).getInternalLocation(); if (layout.isAllowedIntToLong() && value instanceof Long) { return new LongLocationDecorator(primLocation, true); } else if (layout.isAllowedIntToDouble() && value instanceof Double) { return new DoubleLocationDecorator(primLocation, true); } } return newObjectLocation(oldLocation.isFinal(), value != null); } return locationForValue(value); } }
@Override protected Location locationForValueUpcast(Object value, Location oldLocation) { assert !oldLocation.canSet(value); if (oldLocation instanceof DeclaredLocation) { return locationForValue(value); } else if (oldLocation instanceof ConstantLocation) { return constantLocation(value); } else if (oldLocation instanceof TypedLocation && ((TypedLocation) oldLocation).getType().isPrimitive()) { if (!shared && ((TypedLocation) oldLocation).getType() == int.class) { InternalLongLocation primLocation = ((PrimitiveLocationDecorator) oldLocation).getInternalLocation(); if (layout.isAllowedIntToLong() && value instanceof Long) { return new LongLocationDecorator(primLocation, true); } else if (layout.isAllowedIntToDouble() && value instanceof Double) { return new DoubleLocationDecorator(primLocation, true); } } return newObjectLocation(oldLocation.isFinal(), value != null); } return locationForValue(value); } }