Bean Introspection
Posted On September 11, 2007 by Anush filed under Java
There are times when Application program files given by vendor are just java class files and you want in-house enhancement of your application, In such cases you can use Java Bean API for introspection. The Introspector class uses the JDK core reflection API to discover a Bean's methods, and then applies the JavaBeans design patterns to discover the Beans features. This discovery process is named introspection. For example consider the documentation available for java. It provides you with the list of methods and properties supported by the classes. However it does not provide you with the source code for the classes.
The Introspector class provides external IDEs with a statndard way of introspecting a bean.It examines the class file and returns a BeanInfo object.The BeanInfo object encapsulates the bean information. Table below shows two functions of Introspector class which return BeanInfo object.
The following are some of the funtions of BeanInfo interface which return array of PropertyDescriptor[], methodDescriptor[], EventDescriptor[] for properties, methods and events of the Bean class respectively.
Consider an example,intospecting a javabean QueryForm for the methods implemented by it.
| // QueryForm.java public class QueryForm { String password=null; String query=null; //------------------------------ public String getPassword() { return password; } public void setPassword(String b) { password=b; } //----------------------------- public String getQuery() { return query; } public void setQuery(String a) { query=a; } //--------------------------- } |
Now we would create an Introspection class which is a Panel, which introspects the QueryForm class
and displays the methods in the QueryForm class. Look at the use of getBeanInfo() function of Introspector
class and getMethodDescriptors() function of BeanInfo interface.
| // Introspection.java import javax.swing.*; import java.beans.*; import java.awt.*; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class Introspection extends JPanel { JLabel lblClassName; JTextArea tMethods; QueryForm login; BeanInfo bInfoObject; MethodDescriptor[] mDescArr; public Introspection() { super(); setLayout(new BorderLayout(2,1)); lblClassName = new JLabel("Signatures of Methods"); tMethods = new JTextArea(10,10); add(lblClassName,BorderLayout.NORTH); add(tMethods,BorderLayout.CENTER); Font f = new Font("Times New Roman",Font.BOLD,18); try { login = new QueryForm(); bInfoObject = Introspector.getBeanInfo(login.getClass(),login.getClass().getSuperclass()); mDescArr = bInfoObject.getMethodDescriptors(); displayMethods(); } catch (IntrospectionException e) { System.out.println("Error: "+e); } } public void displayMethods(){ for(int i=0;i<mDescArr.length;i++) { String methodName = mDescArr[i].getName(); String methodParams = new String(); Method methodObj = mDescArr[i].getMethod(); Class[] parameters = methodObj.getParameterTypes(); String returnType = methodObj.getReturnType().toString(); String modifier = Modifier.toString(methodObj.getModifiers()); if(parameters.length >0) { methodParams = parameters[0].getName(); for(int j=1;j<parameters.length;j++) methodParams = methodParams+ ","+parameters[j].getName(); } tMethods.append(modifier+ " " +returnType + " " +methodName + "("+methodParams+")"); tMethods.append("\n"); } } public static void main(String argv[]){ JFrame frame = new JFrame("Introspection"); frame.add(new Introspection()); frame.setSize(300,200); frame.setVisible(true); } } |
Compile both the java classes and run the Introspection class as
* java Introspection
Screenshot of sample output of Introspection class is as follows.

