ChrisOz2008
asked on
Calling setters on abstract class
Hi,
In my code I am instantiating an abstract class need to call the setters on that class. How can I call the setters on the class in a generic form without knowing the name of the setter.
Here is an example of what I mean:
Class clazz = Class.forName(getClassName ());
if (clazz!=null)
Entity entity = (Entity)clazz.newInstance( );
At runtime will know the name of the field I want to set, eg "Description". I know that there will be an associated setter named setDescription(String desc). But since I will not know this until runtimes I can't code this right now. Basically in pseudocode I would would do it like this for a field named "Description".
String fieldName = "Description";
Method method = "set" + fieldName;
entity."method"(value);
But how to do this ?
I'm guessing I should be able to do this using reflection but since I have no experience with reflection I'm not sure how to proceed.
Thanks for any advice.
Chris
In my code I am instantiating an abstract class need to call the setters on that class. How can I call the setters on the class in a generic form without knowing the name of the setter.
Here is an example of what I mean:
Class clazz = Class.forName(getClassName
if (clazz!=null)
Entity entity = (Entity)clazz.newInstance(
At runtime will know the name of the field I want to set, eg "Description". I know that there will be an associated setter named setDescription(String desc). But since I will not know this until runtimes I can't code this right now. Basically in pseudocode I would would do it like this for a field named "Description".
String fieldName = "Description";
Method method = "set" + fieldName;
entity."method"(value);
But how to do this ?
I'm guessing I should be able to do this using reflection but since I have no experience with reflection I'm not sure how to proceed.
Thanks for any advice.
Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.