?
Solved

How to use Reflection API or other method to call setter method

Posted on 2006-10-23
3
Medium Priority
?
297 Views
Last Modified: 2012-06-21
For, I have an class with several setter/getter methods
e.g.

Class1

setAddress(String address)
String getAddress()

setEmailAdddress(String email)
String getEmail

And I want to choose the setter method at a java application based on the param value

How to do so?

0
Comment
Question by:mscprojhk
[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
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17787212
Method m = o.getClass().getMethod("set"+paramName, new Object[] { String.class });

you will need to capitalize the param name if not already provided
0
 
LVL 92

Expert Comment

by:objects
ID: 17787214
sorry, typo should be:

Method m = o.getClass().getMethod("set"+paramName, new Class[] { String.class });

and to call it:

m.invoke(o, new Object[] { value});
0
 
LVL 9

Accepted Solution

by:
shinobun earned 200 total points
ID: 17790700
You can use Apache Jakarta Commons' Beanutils [1] for that kind of operation.  PropertyUtils [2] has most everything a bean manipulator wants.  :)

Class1 yourObject = new Class1();
String newAddress = "221B Baker St.";
PropertyUtils.setProperty(yourObject, "address", newAddress);

[1] http://jakarta.apache.org/commons/beanutils/
[2] http://jakarta.apache.org/commons/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/PropertyUtils.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

765 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