?
Solved

Calling setters on abstract class

Posted on 2009-07-03
1
Medium Priority
?
538 Views
Last Modified: 2013-11-24
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



0
Comment
Question by:ChrisOz2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 26

Accepted Solution

by:
ksivananth earned 2000 total points
ID: 24771603
Method[] methods = clazz.getMethods() will return all the methods

methods[0].getName() will give the name of the method, you can check if the name start with "set".

then invoke the method,

methods[0].invoke( entity, yourValue ) ;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question