EJB allows us to use meta-data in order to examine an EJB using reflection and to determine its methods, method parameters, and other information. In your EJB application, you would like to examine an EJB reference using reflection. However, since an EJB reference is actually a reference to a stub object in the EJB container (pointing to an EJB), you cannot examine the actual EJB itself. Or you might only have a reference to the EJB home object and you want to determine the EJB's available business methods before looking up or creating one. Using reflection on an EJB reference will not work because the reference points to an EJB stub object inside the EJB container, not to the EJB itself.

Here we define an application printing meta-information about a bean specified.


public class DescribeEJB {
    
    public static final void main(String[] args) {
        
        try {
            
            // prepare context:
            Context context = getInitialContext();
            
            // get home handle:
            EJBHome home = (EJBHomePortableRemoteObject.narrow(
                    (EJBHomecontext.lookup("test.someBean")
                    EJBHome.class);
            
            EJBMetaData metaData = home.getEJBMetaData();
            Class homeClass = metaData.getHomeInterfaceClass();
            Class remoteClass = metaData.getRemoteInterfaceClass();
            
            System.out.println("Meta data:");
            System.out.println("  home class:" + homeClass.getName());
            System.out.println("  remote class:" + remoteClass.getName());
            
            // convert handle to real home type:
            home = (EJBHomejavax.rmi.PortableRemoteObject.narrow(home, 
                    homeClass);
            
            System.out.println("  primary-key class:" 
                    metaData.getPrimaryKeyClass().getName());
            
            System.out.println("  is session: " + metaData.isSession());
            
            if (metaData.isSession()) {
                System.out.println("  is stateless:" 
                        metaData.isStatelessSession());
            }
            
            System.out.println("Home interface:");
            Method[] methods = homeClass.getDeclaredMethods();
            
            for (int i = 0; i < methods.length; i++) {
                
                Class[] params = remoteMethods[i].getParameterTypes();
                
                System.out.println("  " + methods[i].getName() ", " 
                        params.length + " parameters:");
                
                for (int j = 0; j < params.length; j++) {
                    System.out.println("    " + params[j].getName());
                }
            }
            
            System.out.println("Remote interface:");
            Method[] remoteMethods = remoteClass.getDeclaredMethods();
            
            for (int i = 0; i < remoteMethods.length; i++) {
                
                Class[] params = remoteMethods[i].getParameterTypes();
                System.out.println("  " + methods[i].getName() ", " 
                        params.length + " parameters:");
                
                Class[] params = remoteMethods[i].getParameterTypes();
                for (int j = 0; j < params.length; j++) {
                    System.out.println("    " + params[j].getName());
                }
            }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}