fr.jussieu.gla.wasa.util
Class VarFactory

java.lang.Object
  |
  +--fr.jussieu.gla.wasa.util.VarFactory

public class VarFactory
extends java.lang.Object

Helper class for instantiating Vars accessing the value of a Buisness Object property.

Performance issues
The Var instances created by a VarFactory use Reflexion for accessing a Buisness Object property value. This implies strong overhead, and the VarFactory should be considered as syntactic sugar for developing prototypes. Real-world implementations should subclass Var and provide their own accessors, since calling a virtual method is much faster than using Reflexion.

Value objects cloning
Since Object.clone() method is not public, values of Buisness Objects cannot be cloned as expected. So, there is no (simple) generic way to get those values ; the developer must provide his own accessor methods. That's why the VarFactory can only create PropertyVar instances, which access to immutable instances, avoiding also cloning.
Since there is no way to determine dynamically if a given type is immutable or not, we only create PropertyVar instances for properties whose type is known as immutable (as said by isImmutableType( Class ).
That's why one can find useful to derive this class in order to provide his own definition of immutability, regarding the clases he uses.

Version:
$Revision: 1.2 $ $Date: 2002/03/29 18:46:33 $
Author:
Laurent Caillette, Florent Selva

Field Summary
static VarFactory INSTANCE
          The default instance of this class.
 
Constructor Summary
VarFactory()
           
 
Method Summary
 Var createFieldVar(Explorer explorer, java.lang.Object bean, java.lang.String fieldName)
           
 Var createPropertyVar(Explorer explorer, java.lang.Object bean, java.lang.String propertyName)
          Creates an instance of Var for accessing a given property.
static java.lang.Class[] getImmutableTypes()
          Returns the array of Class representing types known as immutables.
 boolean isImmutableType(java.lang.Class clazz)
          Returns if a given Class is known to have immutable instances.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE

public static final VarFactory INSTANCE
The default instance of this class.
Constructor Detail

VarFactory

public VarFactory()
Method Detail

createPropertyVar

public Var createPropertyVar(Explorer explorer,
                             java.lang.Object bean,
                             java.lang.String propertyName)
Creates an instance of Var for accessing a given property.

Property type
This method assumes that the setter method manipulates exactly the same type as the getter method does.

Parameters:
problem - The Problem the Var belongs to.
bean - The JavaBean instance whose property will be accessed.
propertyName - The name of the property, according to JavaBeans specifications.
Throws:
ApplicationError - if something goes wrong with Reflexion, or if the type of the property is not recognized as immutable.
See Also:
isImmutableType( Class )

createFieldVar

public Var createFieldVar(Explorer explorer,
                          java.lang.Object bean,
                          java.lang.String fieldName)

getImmutableTypes

public static java.lang.Class[] getImmutableTypes()
Returns the array of Class representing types known as immutables.
Returns:
A copy of the array used internally, for avoiding accidental data modification.

isImmutableType

public boolean isImmutableType(java.lang.Class clazz)
Returns if a given Class is known to have immutable instances. The classes known as "immutable" are :
  Integer.TYPE,
  Byte.TYPE,
  Character.TYPE,
  Double.TYPE,
  Float.TYPE,
  Long.TYPE,
  Short.TYPE,
  String.class

One can add custom immutable types by overriding this method like the following :

 public boolean isImmutableType( Class clazz ) {
   if( super.isImmutableType( clazz ) {
     return true ;
   } else {
     if( MyClass.class.equals( clazz ) {
       return true ;
     }
     return false ;
   }
 }
Then don't forget to call
  new MyFactory().createPropertyVar( ... ) ;
instead of
  VarFactory.INSTANCE.createPropertyVar( ... ) ;