How do I introspect a bean at run time to discover its type(s) and available methods?

Richard Monson-Haefel

Client applications or beans can access meta data about a bean from its EJBMetaData object. The EJBMetaData object is obtained from the bean's EJB home reference using the EJBHome.getEJBMetaData( ) method as shown below:

AccountHome acctHome = ... get a reference to the bean's EJB home.

EJBMetaData ejbMetaData = acctHome.getEJBMetaData( );

The EJBMetaData object implements the javax.ejb.EJBMetaData interface which defines methods for obtaining the class of the bean's remote interface, home interface, bean type (entity, stateful or stateless session), and the primary keys type (entity only). A reference to the bean's EJB home can also be obtained. Below is the interface definition of EJBMetaData.

package javax.ejb;

public interface EJBMetaData {
 // Obtain the home interface of the enterprise Bean.
   public EJBHome getEJBHome();
   //Obtain the home interface of the enterprise Bean.
   java.lang.Class getHomeInterfaceClass();
   //Obtain the Class object for the enterprise Bean's home interface.
   java.lang.Class getPrimaryKeyClass();
   //Obtain the Class object for the enterprise Bean's primary key class.
   java.lang.Class getRemoteInterfaceClass();
   //Obtain the Class object for the enterprise Bean's remote interface.
   boolean isSession();
   //Test if the enterprise Bean's type is "session".
   boolean isStatelessSession(); 
}

Once a client application has a reference to bean's remote and home interface classes, normal Java reflection can be used to introspect the methods avaiable to client. Below is an example:

Class remoteClass = ejbMetaData.getRemoteInterfaceClass();

java.lang.reflect.Method [] methods = remoteClass.getDeclaredMethods();

for(int i = 0; i  methods.length; i++){
	System.out.println(methods[i].getName());

}
  

There are no mechanisms a client can use to introspect on a the bean class itself. This makes sense since a bean, as a component, is represented by its remote and home interfaces. The bean class itself should not be visible to the client.

The EJBMetaData is designed to be used by IDEs and other builder tools that may need generic methods for obtaining information about a bean at runtime.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact