Tag Archives: Introspection

Java Introspection

What JavaBeans know about them selves.

In another article I already wrote about JavaBeans and briefly mentioned Introspection. In this post I include a more advanced example of Introspection. The example below uses the Introspector class which provides a standard way for tools to learn about the properties, events, and methods supported by a target JavaBean.
For each of those three kinds of information, the Introspector will separately analyze the bean’s class and superclasses looking for either explicit or implicit information and use that information to build a BeanInfo object that comprehensively describes the target bean:

try {
    BeanInfo bi = Introspector.getBeanInfo(MyBean.class);
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (int i=0; i<pds.length; i++) {
        // Get property name
        String propName = pds[i].getName();
    }
    // class, prop1, prop2, PROP3
} catch (java.beans.IntrospectionException e) {
}

public class MyBean {
    // Property prop1
    public String getProp1() {
        return null;
    }
    public void setProp1(String s) {
    }

    // Property prop2
    public int getProp2() {
        return 0;
    }
    public void setProp2(int i) {
    }

    // Property PROP
    public byte[] getPROP3() {
        return null;
    }
    public void setPROP3(byte[] bytes) {
    }
}

What are JavaBeans?

evertwagenaar.com logo

What are JavaBeans?

According to Wikipedia:

JavaBeans are classes that encapsulate many objects into a single object (the bean). They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name “Bean” was given to encompass this standard, which aims to create reusable software components for Java.

According to Evert:

A JavaBean is a normal class with a few properties:

  • Javabeans have a zero argument Constructor. They cannot be instantiated with Data. The data is always added after the instantiation.
  • JavaBeans implement the Serializable interface which make them serializable.
  • All class-members are private.
  • To access data inside the class you need to create so-called ‘Accessor methods’ these are the getters and setters. You can only change the values of the class members by calling setProperty(). The same thing is true to get read the private class-members. This is done by the getters. getProperty().
  • Accessor Methods have a naming convention as follows: Lowecas get or set, followed by the property name (capitalized) so if the bean has a property called id, it’s accessor-methods become getId() and setId().
  • JavaBeans can be serialized.
  • Javabeans know things about theirselves. This is called Introspection.
So what are they used for?

JavaBeans are typically used as data-carriers, for example a JavaBean can be used to represent a Database Table Row, which makes sense in a DAO (Data Access  Object). This is indeed one of the most common applications of JavaBeans.

Utilities
Example of Introspection.

As I already mentioned, JavaBeans can look inside themselves, for example to get the class name:

// Get the fully-qualified name of a class
Class cls = java.lang.String.class;
String name = cls.getName();        // java.lang.String

// Get the fully-qualified name of a inner class
cls = java.util.Map.Entry.class;
name = cls.getName();               // java.util.Map$Entry

// Get the unqualified name of a class
cls = java.util.Map.Entry.class;
name = cls.getName();
if (name.lastIndexOf('.') > 0) {
    name = name.substring(name.lastIndexOf('.')+1);  // Map$Entry
}
// The $ can be converted to a .
name = name.replace('$', '.');      // Map.Entry


// Get the name of a primitive type
name = int.class.getName();         // int

// Get the name of an array
name = boolean[].class.getName();   // [Z
name = byte[].class.getName();      // [B
name = char[].class.getName();      // [C
name = short[].class.getName();     // [S
name = int[].class.getName();       // [I
name = long[].class.getName();      // [J
name = float[].class.getName();     // [F
name = double[].class.getName();    // [D
name = String[].class.getName();    // [Ljava.lang.String;
name = int[][].class.getName();     // [[I

// Get the name of void
cls = Void.TYPE;
name = cls.getName();               // void

A more complex example of Introspection can be found here.