Solved

Calling setters on abstract class

Posted on 2009-07-03
1
534 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 500 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 74
sql import cannot be resolved jsp 3 75
Running JavaFX on the Raspberry Pi 27 140
netbeans can not run self signed app 8 13
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 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