Solved

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

Posted on 2006-10-23
3
292 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 50 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

688 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