Calling setters on abstract class
Posted on 2009-07-03
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());
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;
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.