• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamically set data via setters at run time

 
glen croteau
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also Introspection, which is specifically oriented towards Java Bean features like getters and setters.
 
glen croteau
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I got it using reflection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic