Solved

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

Posted on 2006-10-23
3
280 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java / Linux and Regular Expressions 11 70
github account with ecipse 1 42
servlet  URL Rewriting 1 26
fibonacci ten numbers 4 28
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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now