ExtraStuff is a set of classes and interfaces that allows a Java bean instance to add a collection of extra "stuff" at runtime, such as new properties or methods. These extra members can be examined and used by other beans, either through a dynamic API or by compiling them into a new bean.
ExtraStuff exploits the capabilities of dynamic introspection in JavaBeans. There are several types of applications that can use these features of Bean Extender ExtraStuff. For example, using the ExtraStuff interfaces is an alternative to conventional methods of SQL database access through Java.
In conventional methods, a tool gathers the metadata from the database and compiles a specific class that presents properties with the appropriate name and types for each of the columns selected from the database. If the user changes the number of columns, the selection statement, or any of a number of things, the user must recompile the object. If the designer provides a data access part based on Bean Extender ExtraStuff, the same changes can be mapped into calls into the ExtraStuff interfaces of a standard object. The new columns, the new properties, appear dynamically as the queries are updated.
To add and use extra stuff at runtime, a bean allocates an ExtraStuffAggregator object. This help object handles most of the work needed to maintain a collection of extra stuff. A bean that has a collection of extra stuff can implement the HasExtraStuff interface to allow other beans to access this collection.
A presenting bean needs to implement the HasExtraStuff interface to let other beans find and access the collection of extra stuff. The interface is easy to implement. The interface defines two methods that return preallocated objects. These methods are:
To allocate a new ExtraStuffAggregator, call the default ExtraStuffAggregator constructor. Beans may allocate multiple ExtraStuffAggregators, but the HasExtraStuff interface assumes that only one of these aggregators is exposed to other beans at a given time.
A bean may discard an ExtraStuffAggregator when it is no longed needed. If other beans have a reference to the discarded aggregator, they can still access the extra members stored in that aggregator, but they will not see any changes that the presenter bean is making to its current aggregator. To see whether a cached aggregator is the current aggregator for a presenter bean, use the presenter's getExtraStuffAggregator() method and compare the value of the two objects.
Once a bean has used its ExtraStuffAggregator to compile extra members into a bean, it cannot add or remove extra members using that aggregator. Attempting to add or remove extra members from a compiled aggregator causes an ExtraStuffAlreadyCompiledException to be thrown. If changes are needed after compilation, the bean can allocate a new ExtraStuffAggregator, copy the desired extra members into it, and then discard the older one.
The only task not handled for a bean by the ExtraStuffAggregator is finding extra properties and methods. There are several strategies a bean can use to locate this information.
To add an extra property or method to an ExtraStuffAggregator, call one of the following methods.
You need to provide an owner object, a Method or PropertyDescriptor object, and a new name for the extra member.
To remove an extra property or method from an ExtraStuffAggregator, call one of the following methods.
To identify the extra member to be removed, you must provide either the owning object, the Method or PropertyDescriptor object, or the new name associated with the extra member.
To emit, or generate, a compiled bean containing declared members corresponding to your beans extra members, use the compileExtraStuff(String) method.
To use extra stuff dynamically at runtime, other beans can acquire a reference to your bean's ExtraStuffAggregator object by calling the getExtraStuffAggregator() method of your bean's HasExtraStuff interface.
To see if another bean supports HasExtraStuff, your bean can use the getInterfaces() method on the Class object of the other bean.
Class extraStuffInterface = Class.forName("com.ibm.beans.util.es.HasExtraStuff"); Class otherBeanInterface[] = otherBean.getClass().getInterfaces() for (int i=1 < otherBeanInterfaces.length; i++) { if (otherBeanInterfaces == extraStuffInterface) { // Okay to call HasExtraStuff methods } }
Alternately, you can call the HasExtraStuff interface methods and catch any NoSuchMethodExceptions thrown by beans that do not support the interface.
After determining that a bean supports the HasExtraStuff interface, call the bean's getExtraStuffAggregator() method to acquire a reference to its current ExtraStuffAggregator.
To get a list of all extra members in an ExtraStuffAggregator, use one of the following methods:
To find all of the extra members with a given name, use the getExtraPropertyInfo(String) method for property names and the getExtraMethodInfo(String) method for method names. These extra names are assigned by the bean that presents the members and may not be the original names declared by the owner object.
To find all the extra members with a given owner, use the getExtraPropertyInfo(Object) method for property names and the getExtraMethodInfo(Object) method for method names.
When a bean gets the ExtraMethodRecord or ExtraPropertyRecord for an extra member, it can retrieve the Method or PropertyDescriptor object by calling the getDescriptor() method. The method behavior or the property value can be dynamically accessed by using the appropriate Java Core Reflection and Java Beans Introspection methods on the descriptor.
To dynamically invoke an extra method, use Method.invoke(). The return type can be discovered by calling Method.getReturnType(). The parameter types can be discovered by calling Method.getParameterTypes().
To dynamically access an extra property value, you must find the Method objects for the getters (get accessor) and the setters (set accessor) methods using either PropertyDescriptor.getReadMethod() or PropertyDescriptor.getWriteMethod(). The Method object can then be dynamically invoked using the same procedure as for invoking extra members.
Beans can add or remove extra members using ExtraStuffAggregators acquired from other beans. These aggregators work the same as using your own aggregator.
Some beans may not request to use the ExtraStuffAggregator and then the Java Core Reflection API to dynamically invoke extra methods. The ExtraStuffAggregator can produce a compiled new bean that declares real properties and methods corresponding to the extra members stored in the aggregator. Other beans can call this new bean's methods and access this new bean's properties directly.
To see if a bean supports the Has ExtraStuff interface, invoke the Class.getInterfaces() method to get a list of all the interfaces it supports.
After determining that a bean support the HasExtraStuff interface, call the bean's getCompiledExtraStuff() method to get its compiled extra stuff bean.