Skip to content

Commit

Permalink
Added auto conversions between Integer and Long when POJO's attribute
Browse files Browse the repository at this point in the history
data type and database data type don't match.
  • Loading branch information
randomf committed Oct 7, 2015
1 parent 48db771 commit bdd0187
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/main/java/com/dieselpoint/norm/sqlmakers/StandardPojoInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,39 @@ public void putValue(Object pojo, String name, Object value) {
}

if (value != null) {
// Perform deserialization if non-trivial data type is retrieved
if (prop.serializer != null) {
value = prop.serializer.deserialize((String) value, prop.dataType);

// Convert value to enum type
} else if (prop.isEnumField) {
value = getEnumConst(prop.enumClass, prop.enumType, value);
// Convert Integer to Long if POJO write method expects Long
} else if (value.getClass().equals(Integer.class) && prop.writeMethod.getParameterTypes().length == 1
&& prop.writeMethod.getParameterTypes()[0].equals(Long.class)) {
value = Long.valueOf(((Integer) value).longValue());

// Convert Long to Integer if POJO write method expects Integer and the Long value doesn't overflow
} else if (value.getClass().equals(Long.class) && prop.writeMethod.getParameterTypes().length == 1
&& prop.writeMethod.getParameterTypes()[0].equals(Integer.class)) {
// Check for overflow
long valueAsLong = ((Long) value).longValue();
if (valueAsLong > Integer.valueOf(Integer.MAX_VALUE).longValue()) {
throw new DbException("Provided value: " + value + " for write method " + prop.writeMethod.toString()
+ " has overflown.");
}
// Check for underflow
if (valueAsLong < Integer.valueOf(Integer.MIN_VALUE).longValue()) {
throw new DbException("Provided value: " + value + " for write method " + prop.writeMethod.toString()
+ " has underflown.");
}
// Convert if ok
value = Integer.valueOf(((Long) value).intValue());
}

}

if (prop.writeMethod != null) {
try {
try {
prop.writeMethod.invoke(pojo, value);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new DbException("Could not write value into pojo. Property: " + prop.name + " of type: "
Expand Down

0 comments on commit bdd0187

Please sign in to comment.