It's not a secret anymore!*
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 Java 8 in Action this week in the Java 8 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: 18159
    
    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
 
Similar Threads
Castor or XStream???
URLyBird, File's fields length
Coloring the Row in JTable
Color object
JavaScript: Dynamic Radio button