It is not possible to cast a child to a child nor was it possible to cast a child to the parent and then back to the parent. So I wrote a generic translator to copy all the data from one child class of BankInstruction to another.
/**
* Convert a BankInstruction Child class to another bank instruction child class
*
* @param incoming
* The incoming child class with data
* @param clazz
* The class we want to convert to and populate
* @return
* The class we requested with all the populated data
*/
public static <I extends BankInstruction, O extends BankInstruction> O convertBankInstructionModel(final I incoming,
final Class<O> clazz) {
// Create return data object
O outgoing = null;
// Attempt to instantiate the object request
try {
outgoing = clazz.newInstance();
// Catch and log any exception thrown
} catch (Exception exceptThrown) {
exceptThrown.printStackTrace();
return null;
}
// Loop through all of the methods in the class
for (Method method : BankInstruction.class.getMethods()) {
// Check if we have a set method
if (method.getName().substring(0, 3).equalsIgnoreCase("set")) {
// Create a corresponding get method
String getMethodName = method.getName().replace("set", "get");
// Check if we have a get method for the set method variable
if (Arrays.toString(BankInstruction.class.getMethods()).contains(getMethodName)) {
// Get the method from the method name
Method getMethod = getMethodFromName(BankInstruction.class, getMethodName);
try {
// If we have a method proceed
if (getMethod != null) {
// Attempt to invoke the method and get the response
Object getReturn = getMethod.invoke(incoming);
// If we have a response proceed
if (getReturn != null) {
// Attempt to invoke the set method passing it the object we just got from the get
method.invoke(outgoing, getReturn);
}
}
} catch (Exception except) {
except.printStackTrace();
}
}
}
}
// Return the found object
return outgoing;
}
Probably not the best solution but it is working for me.