File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamically set data via setters at run time

 
glen croteau
Greenhorn
Posts: 15
  • 0
  • 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
  • 0
  • 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
Pie
Posts: 20164
24
MySQL Database
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I got it using reflection.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic