This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes dynamically set data via setters at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dynamically set data via setters at run time" Watch "dynamically set data via setters at run time" New topic
Author

dynamically set data via setters at run time

glen croteau
Greenhorn

Joined: Apr 10, 2008
Posts: 15
I would like to dynamically set data via setters at run time.
For example:
I have a HashMap of name-value pairs:
color, blue


I would like to iterate this list and query a specific class to determine if it has a method setColor(String color).
Is this possible and also is it costly?

Thanks

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

You can very well use 'Reflections' - java.lang.reflect package to determine whether a class has a method with that name or not. Also, it is not costly as when ever a class is loaded by the JVM into its memory, a Class object is created for it internally and you are just going to query it. Just try diggin that package API, you'll get whatever you want.


OCPJP 6
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's also Introspection, which is specifically oriented towards Java Bean features like getters and setters.
glen croteau
Greenhorn

Joined: Apr 10, 2008
Posts: 15
Thanks! I got it using reflection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamically set data via setters at run time