Search code examples
javajavabeansintrospection

Bean introspection in Java


I'm learning for a job interview in Java. They told me to learn the concepts of Beans introspection, so I searched the web and read in a couple of sites including the next posts:

As far as I understood: Bean is like any other object class in Java, but this class must have the next features:

  1. All properties private (use getters/setters)
  2. A public no-argument constructor
  3. Implements Serializable.

General things:

  1. Introspection is giving me the possibility to "examine" an object during run-time, and that way I can get the class properties names, methods names constructors etc.
  2. Introspection uses Reflection to get the Information of a class.

I still have some questions:

  1. Why do I need this kind of a mechanism, meaning, in which cases should I use introspection instead of using any other thing?
  2. Is there any difference between bean's introspection and a regular introspection?
  3. How it's working besides the methods I can use?

I would be happy if someone could give me his own prospective about this subject, or to give me some kind of a link for useful information.


Solution

  • I'm not sure what they want to know.
    You can use frameworks such as apache-bean-utils to query information about bean structure.
    I developed such a code manually (big mistake! :) ) -
    I used a recursive mechanism based on java bean notation (i.e - setters must begin with "set",
    getters begin with "is" for boolean or "get" for all types)
    You then can us this code to automate some behavior -
    At my case fo example I wrote a tool that parses WSDL, and creates binding between WS calls and our application entitites via code.
    The user of our application provided an XML indicating how to perform a mapping -
    i.e - let's say that a WS call returned a Person object, but in our application we had a student entity
    so the XML defined how to perform the mapping, and I used code like apache-bean-utils to perform introspection
    and to understand what setters and getters to invoke.
    This was done in contrast to what is done usually in java applications:
    1. Generate Java clients (i.e - use wsdl2java) from WSDL
    2. Compile the application with the client code.

    I can assume introspection can be used in profilers code - for example,
    Since there are many frameworks that use getters and setters , it is very improtant that these methods will be efficient,
    so it's something that mabye profiles should first look into.

    Feel free to add more questions