KPax
asked on
Reflection in Java method return type and signature
There is this static method in code that I inherited and have to make minimal changes whatever I do in order not to affect other code.
I have to reuse this code so that instead of AbsDisc can be used other class AuthDisc.
Both classes actually have almost same fields, but I plan to check that with if statement so if it is one then populate one set of fields and if it is another then populate with other set.
What interest me is how can I change this static method to have either AbsDisc or AuthDisc as return type as well as in method signature as I am sending object which is instance of one of those two classes and then returning that object back to the caller method.
I know I should be using reflection, but not quite sure how to use it in this situation?
I have to reuse this code so that instead of AbsDisc can be used other class AuthDisc.
Both classes actually have almost same fields, but I plan to check that with if statement so if it is one then populate one set of fields and if it is another then populate with other set.
What interest me is how can I change this static method to have either AbsDisc or AuthDisc as return type as well as in method signature as I am sending object which is instance of one of those two classes and then returning that object back to the caller method.
I know I should be using reflection, but not quite sure how to use it in this situation?
// Populate disclosure
private static AbsDisc populateDisclosure(AbstractSummary abstractSummary, String tagName,
DisclaimerResponse response, AbsDisc absDisc) throws Exception
{
Disclosure disclosure = response.getDisclosure();
if (disclosure != null)
{
Disclaimer disclaimer = response.getDisclaimer();
absDisc.setId(Integer.toString(response.getId()));
absDisc.setName(disclosure.getTitle());
absDisc.setQuestion(disclosure.getInstruction());
absDisc.setAnswer((disclaimer != null ? disclaimer.getStatement() : ""));
}
return absDisc;
}
(class is shortened for brevity)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.