Patch from Nathan Curtis: gracefully ignore any field contained in the

runtime class that's not in the persistent class.
This commit is contained in:
Ray Greenwell
2010-04-09 20:38:33 +00:00
parent 26ece1f2c0
commit a23cb7e4d8
@@ -69,8 +69,8 @@ public class RuntimeUtil
/** /**
* Creates a function that creates an instance of R and initializes all accessible (ie. public) * Creates a function that creates an instance of R and initializes all accessible (ie. public)
* fields of R from fields of P with matching name. If the function is applied to null, null * fields of R from fields of P with matching name. If the function is applied to null, null
* will be returned. Fields of P that do not exist in R will be ignored. See the class * will be returned. Fields of P that do not exist in R will be ignored and vice versa. See the
* documentation for a list of field conversions that will be made automatically and the * class documentation for a list of field conversions that will be made automatically and the
* mechanism for performing other conversions. * mechanism for performing other conversions.
*/ */
public static <P extends PersistentRecord, R> Function<P, R> makeToRuntime ( public static <P extends PersistentRecord, R> Function<P, R> makeToRuntime (
@@ -86,7 +86,9 @@ public class RuntimeUtil
try { try {
R object = rclass.newInstance(); R object = rclass.newInstance();
for (int ii = 0, ll = rfields.length; ii < ll; ii++) { for (int ii = 0, ll = rfields.length; ii < ll; ii++) {
rfields[ii].set(object, getters[ii].get(record)); if (getters[ii] != null) {
rfields[ii].set(object, getters[ii].get(record));
}
} }
return object; return object;
} catch (Exception e) { } catch (Exception e) {
@@ -116,7 +118,9 @@ public class RuntimeUtil
try { try {
P record = pclass.newInstance(); P record = pclass.newInstance();
for (int ii = 0, ll = rfields.length; ii < ll; ii++) { for (int ii = 0, ll = rfields.length; ii < ll; ii++) {
setters[ii].set(record, rfields[ii].get(object)); if (setters[ii] != null) {
setters[ii].set(record, rfields[ii].get(object));
}
} }
return record; return record;
} catch (Exception e) { } catch (Exception e) {
@@ -177,10 +181,7 @@ public class RuntimeUtil
// if we have no persistent field for the runtime field, we're now out of luck // if we have no persistent field for the runtime field, we're now out of luck
if (pfield == null) { if (pfield == null) {
throw new IllegalArgumentException( return null;
"Cannot create mapping for " + rfield + ". Neither " +
pclass.getSimpleName() + "." + rfield.getName() + " nor " +
rfield.getType() + " " + getter + "() exist.");
} }
// if the fields match exactly, return a getter that just gets the field // if the fields match exactly, return a getter that just gets the field
@@ -203,7 +204,7 @@ public class RuntimeUtil
} }
// if we have exhausted all other approaches, we're SOL // if we have exhausted all other approaches, we're SOL
throw new IllegalArgumentException("Cannot map " + pfield + " to " + rfield + "."); return null;
} }
protected static Setter makeSetter (Class<?> pclass, final Field pfield, Field rfield) protected static Setter makeSetter (Class<?> pclass, final Field pfield, Field rfield)
@@ -223,8 +224,7 @@ public class RuntimeUtil
// if we have no persistent field for this runtime field, we're now out of luck // if we have no persistent field for this runtime field, we're now out of luck
if (pfield == null) { if (pfield == null) {
throw new IllegalArgumentException("Cannot create setter for " + rfield + ". " + return null;
"No corresponding field exists in " + pclass + ".");
} }
// if the fields match exactly, return a setter that just sets the field // if the fields match exactly, return a setter that just sets the field
@@ -247,7 +247,7 @@ public class RuntimeUtil
} }
// if we have exhausted all other approaches, we're SOL // if we have exhausted all other approaches, we're SOL
throw new IllegalArgumentException("Cannot map " + rfield + " to " + pfield + "."); return null;
} }
protected static Field[] getPersistentFields (Class<?> pclass, Field[] rfields) protected static Field[] getPersistentFields (Class<?> pclass, Field[] rfields)