Back to the freakoutery. Nathan's going to use the existing (if somewhat
verbose) mechanisms to accomodate his needs. We freak out as early as possible to save the developer a time wasting debugging session in situations where they update only one or the other of the persistent and runtime class, or make a typo, or get the types wrong, etc.
This commit is contained in:
@@ -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 and vice versa. See the
|
* will be returned. Fields of P that do not exist in R will be ignored. See the class
|
||||||
* class documentation for a list of field conversions that will be made automatically and the
|
* 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,9 +86,7 @@ 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++) {
|
||||||
if (getters[ii] != null) {
|
rfields[ii].set(object, getters[ii].get(record));
|
||||||
rfields[ii].set(object, getters[ii].get(record));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return object;
|
return object;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -118,9 +116,7 @@ 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++) {
|
||||||
if (setters[ii] != null) {
|
setters[ii].set(record, rfields[ii].get(object));
|
||||||
setters[ii].set(record, rfields[ii].get(object));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return record;
|
return record;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -181,7 +177,10 @@ 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) {
|
||||||
return null;
|
throw new IllegalArgumentException(
|
||||||
|
"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
|
||||||
@@ -204,7 +203,7 @@ public class RuntimeUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if we have exhausted all other approaches, we're SOL
|
// if we have exhausted all other approaches, we're SOL
|
||||||
return null;
|
throw new IllegalArgumentException("Cannot map " + pfield + " to " + rfield + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Setter makeSetter (Class<?> pclass, final Field pfield, Field rfield)
|
protected static Setter makeSetter (Class<?> pclass, final Field pfield, Field rfield)
|
||||||
@@ -224,7 +223,8 @@ 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) {
|
||||||
return null;
|
throw new IllegalArgumentException("Cannot create setter for " + rfield + ". " +
|
||||||
|
"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
|
||||||
return null;
|
throw new IllegalArgumentException("Cannot map " + rfield + " to " + pfield + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Field[] getPersistentFields (Class<?> pclass, Field[] rfields)
|
protected static Field[] getPersistentFields (Class<?> pclass, Field[] rfields)
|
||||||
|
|||||||
Reference in New Issue
Block a user