| 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: 16483
|
|
|
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.
|
 |
 |
|
|
subject: dynamically set data via setters at run time
|
|
|