September 03, 2002

Fun with reflection

In the course of creating the XML files for use in our proposed testing framework (overview here), I created a simple file to do it for a bean at a time. It would be nice to be able to run this for multiple beans at a time. Hmm…

(To acclimate: every entity has an associated Wrapper session bean which contains all the relationships and queries. Every Wrapper has a method fetchByEntity( String ) method which retrieves all beans in the database with a particular entity number, which is a way of partitioning the data. EJBClient is just a service locator with some shortcuts, home caching, etc.)

 EJBClient client = new EJBClient();
 DataSerializer ds = new DataSerializer( null );
 String interfaceName = "com.optiron.readi.interfaces.";
 for ( int i = 1; i < argv.length; i++ ) {
     String beanName = argv[i];
     File xmlOutfile = new File( dataDir, beanName + ".xml" );
     ds.setFileSource( xmlOutfile );
     String beanInterface = interfaceName + beanName;
     Class wrapperHomeClass = Class.forName( beanInterface + "WrapperHome" );
     Object wrapperHome = client.getWrapperHome( beanName );
     Method createMethod = wrapperHomeClass.getMethod( "create", null );
     Object wrapper = createMethod.invoke( wrapperHome, null );
     Class wrapperClass = Class.forName( beanInterface + "Wrapper" );
     Method entityMethod = wrapperClass.getMethod(
                              "fetchByEntity", new Class[] { String.class } );
     List items = (List)entityMethod.invoke(
                              wrapper, new String[] { entno } );
     ds.writeXml( items );
     System.out.println( "Wrote bean [" + beanName + "] to " +
                         "[" + xmlOutfile.getAbsolutePath() + "]" );
 }

As a result of this and other work done over the weekend, I'm getting more comfortable with reflection. This is probably a dangerous thing since I use it all the time in Perl :-)

Next: Fundraising monster
Previous: Digital crime photography