3

Reflection or hashmaps? Suppose you are writting a graphical application where you have objects lying around and users have the posibility of clicking on them. That said, you want to display the clicked object information somehow.

I end up reading Accessible interface from accessibility java package and AccessibleContext class... Before that, I wrote testing code using hash maps for properties instead of class fields. And lastly, I thought in Reflection after reading that JavaBeans use instrospection.

What would you recommend in such applications?

1
  • I'll take it as accepted, but there was a missunderstanding of needs (maybe was my spelling). Instrospection or Reflection is a good approach when you need to know something about an object when the object is not yet instantiated. But, when you have an object running, and you know that object, probably the best is to call those methods instead. Upon those calls, you could build a "properties table". Anyway, your answer was useful for me, to dig into Commons BeanUtils. Regards. Commented Mar 31, 2012 at 18:18

1 Answer 1

4

Using reflection and introspection sounds like a better approach than using HashMaps. Instead of using this API directly, I would recommend an abstraction layer like Commons BeanUtils, which will allow you to work with classes adhering to the JavaBean standard in an easy manner.

Here's the User Guide complete with examples.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm on it. I have the same opinion about HashMaps for two main reasons: Obfuscates the code and increases the use of memory. And, AccessibleContext seems to be much more specific although Java API documentation insists on implementing the package interfaces.
There's still something missing. As you can see, I mentioned Reflection for a runtime program. That's because the need of representing a list of properties avoiding writting the correspondent getter/setter. That's also why I thought in having a HashMap iterating with its values.I read the User Guide but I didn't find any functionality that suits the need of "get all the properties I want to represent graphically" (note that it's not the same as "get all the object properties).I know that not follow JavaBean spec is enough for a property not being found, but still seems not fitting my needs
avoiding the netiquette as I need more characters, I don't see why wasting a line for invoking a method using Reflection should be different from invoking the method itself. Remember that we are at run-time. The main idea holds in getting a set of properties that could be represented, e.g., myObject.getProperties() implemented by the object (sort of java Properties). I'm looking for the best way of solving that.
Just to note something, After re-reading my question I think you missunderstood the idea. I'm not refering to apps like GUI builders and so on. Think of a drawing tool (in fact, thats my app objetive) or a math tool that when you click/step-over/interact with, for example, a 2D point you get its coordinates, if it's visible, etc, in a classic table label-value style.
anyway thanks for pointing me that out. I have found that java.beans.BeanInfo defines getPropertyDescriptors(). That should be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.