• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to generate Dynamic Setter Method Name  RSS feed

 
Saurabh KumarD
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to dynamically generate Setter Name in my Application. I am getting name of attributes somewhere in the class . I want to call corresponding setter name .
I know reflection can be helpful in this.

Please illustarte with some fine code.

Ex- Suppose I am getting retriving name,age, gender, height etc from some code in my class. Now I want to run setName() when I get name and setAge() when I get age . I want all this to happen dynamically.

Please help. I need this very urgent.

Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check your private messages.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EaseUp.

Calling methods via reflection is pretty straight-forward; where are you stuck?
 
Saurabh KumarD
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m not sure how to call. Please provide a illustration in the code.

Thanks your help would be much appriciated.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the java.beans.Introspector class.

Basically, given a class and propertyname, you can get the beaninfo for the class, then get the property descriptor for the property -- and from there, get the setter method.

Henry
 
Saurabh KumarD
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
But actually my problem is different. I know the class , property and getter & setter names.

I want to dynamically call setter method.

Say in a loop I am getting xx=lastName, xx=fisrtName, xx=age, etc in different iteration.

I want to call setxx(); method for all these. And I want that It will take xx dynamically.

Please help.

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Calling methods via reflection is pretty straight-forward; where are you stuck?

There is your answer: reflection. You get a property xxx, you capitalize it to Xxx, prepend get, and call the method through reflection.
 
Saurabh KumarD
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
David Newton wrote:Calling methods via reflection is pretty straight-forward; where are you stuck?

There is your answer: reflection. You get a property xxx, you capitalize it to Xxx, prepend get, and call the method through reflection.


Please explain the last part , how to call the method through reflcetion. Please put some sample code.

Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!