Writing Beans (Advanced)

Writing DynaBeans

A DynaBean is a pseudo-bean whose properties may differ between instances of the same Class. Pseudo-introspection of the available properties uses a pseudo-class of type DynaClass. For more details see BeanUtils .

Betwixt now supports the (correct) writing of objects implementating DynaBean. Rather than using standard introspection for these objects, Betwixt now interrogates the DynaClass. Using .betwixt files with DynaBeans is not yet supported and special behaviour of DynaBeans can be overridden by specifying a .betwixt file.

Writing Entity Beans

Entity beans are a kind of Enterprise Java Bean. For more details see the J2EE specification. They are a common way to persist data. When dealing with an entity bean, you usually deal with the remote interface rather than the concrete implementation. There is no gaurantee that the class presented by the container will be the class your created to provide the bean's functionality. Indeed, Proxy implementations are one common way in which the remote interfaces are implemented.

This causes some difficulties for Betwixt. Betwixt (by default) will introspect the actual implementation presented by the container. Fortunately, the normalization mechanism described here can be used to allow betwixt to introspect the actual interface (rather than the implementation).

There are two different strategies that can be used. The first is to create a special ClassNormalizer which extracts an interface from a Proxy. (Thanks to Christoph Gaffga for suggesting this.) For example:

        XMLIntrospector introspector = ...;
        introspector.setClassNormalizer( new ClassNormalizer() {
                public Class normalize( Class clazz ) {
                    if ( Proxy.isProxyClass(clazz) && clazz.getInterfaces().length >0 ) {
                        return clazz.getInterfaces()[0];
                    }
                    return clazz;
                }
            });

Of course, this will only work if your J2EE implementation uses Proxy classes to implement it's Entity Beans.

The alternative is to use a ListedClassNormalizer and register all remote interfaces. For example:

        XMLIntrospector introspector = ...;
        ListedClassNormalizer classNormalizer = new ListedClassNormalizer();
        classNormalizer.addSubstitution( MyRemoteInterfaceOne.class );
        classNormalizer.addSubstitution( MyRemoteInterfaceTwo.class );
        ...
        introspector.setClassNormalizer( classNormalizer );