rdonkin     2004/12/01 11:33:01

  Modified:    betwixt/src/java/org/apache/commons/betwixt
                        BeanProperty.java
               betwixt/src/java/org/apache/commons/betwixt/expression
                        MethodUpdater.java
  Log:
  Improved introspection support for dynabeans
  
  Revision  Changes    Path
  1.11      +2 -1      
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/BeanProperty.java
  
  Index: BeanProperty.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/BeanProperty.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- BeanProperty.java 8 Nov 2004 22:29:11 -0000       1.10
  +++ BeanProperty.java 1 Dec 2004 19:33:01 -0000       1.11
  @@ -21,6 +21,7 @@
   
   import org.apache.commons.beanutils.DynaProperty;
   import org.apache.commons.betwixt.expression.DynaBeanExpression;
  +import org.apache.commons.betwixt.expression.DynaBeanUpdater;
   import org.apache.commons.betwixt.expression.Expression;
   import org.apache.commons.betwixt.expression.IteratorExpression;
   import org.apache.commons.betwixt.expression.MethodExpression;
  @@ -97,7 +98,7 @@
           this.propertyName = dynaProperty.getName();
           this.propertyType = dynaProperty.getType();
           this.propertyExpression = new DynaBeanExpression( propertyName );
  -        // todo: add updater
  +        this.propertyUpdater = new DynaBeanUpdater( propertyName, 
propertyType );
       }
   
       /**
  
  
  
  1.14      +19 -79    
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/MethodUpdater.java
  
  Index: MethodUpdater.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/MethodUpdater.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- MethodUpdater.java        13 Jun 2004 21:32:45 -0000      1.13
  +++ MethodUpdater.java        1 Dec 2004 19:33:01 -0000       1.14
  @@ -15,9 +15,8 @@
    */ 
   package org.apache.commons.betwixt.expression;
   
  -import java.lang.reflect.Array;
  +
   import java.lang.reflect.Method;
  -import java.util.Collection;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -29,7 +28,7 @@
     * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
     * @version $Revision$
     */
  -public class MethodUpdater implements Updater {
  +public class MethodUpdater extends TypedUpdater {
   
       /** Logger */
       private static Log log = LogFactory.getLog( MethodUpdater.class );
  @@ -44,9 +43,6 @@
       
       /** The method to call on the bean */
       private Method method;
  -    /** The type of the first parameter of the method */
  -    private Class valueType;
  -    
       /** Base constructor */
       public MethodUpdater() {
       }
  @@ -60,69 +56,6 @@
       }
   
       /** 
  -     * Updates the current bean context with the given String value 
  -     * @param context the Context to be updated
  -     * @param newValue the update to this new value 
  -     */
  -    public void update(Context context, Object newValue) {
  -        Object bean = context.getBean();
  -        if ( bean != null ) {
  -            if ( newValue instanceof String ) {
  -                // try to convert into primitive types
  -                if ( log.isTraceEnabled() ) {
  -                    log.trace("Converting primitive to " + valueType);
  -                }
  -                newValue = context.getObjectStringConverter()
  -                    .stringToObject( (String) newValue, valueType, null, 
context );
  -            }
  -            if ( newValue != null ) {
  -                // check that it is of the correct type
  -/*                
  -                if ( ! valueType.isAssignableFrom( newValue.getClass() ) ) {
  -                    log.warn( 
  -                        "Cannot call setter method: " + method.getName() + " 
on bean: " + bean
  -                        + " with type: " + bean.getClass().getName() 
  -                        + " as parameter should be of type: " + 
valueType.getName() 
  -                        + " but is: " + newValue.getClass().getName() 
  -                    );
  -                    return;
  -                }
  -*/                
  -            }
  -            // special case for collection objects into arrays               
     
  -            if (newValue instanceof Collection && valueType.isArray()) {
  -                Collection valuesAsCollection = (Collection) newValue;
  -                Class componentType = valueType.getComponentType();
  -                if (componentType != null) {
  -                    Object[] valuesAsArray = 
  -                        (Object[]) Array.newInstance(componentType, 
valuesAsCollection.size());
  -                    newValue = valuesAsCollection.toArray(valuesAsArray);
  -                }
  -            }
  -            
  -            Object[] arguments = { newValue };
  -            try {
  -                if ( log.isDebugEnabled() ) {
  -                    log.debug( 
  -                        "Calling setter method: " + method.getName() + " on 
bean: " + bean 
  -                        + " with new value: " + newValue 
  -                    );
  -                }
  -                method.invoke( bean, arguments );
  -                
  -            } catch (Exception e) {
  -                String valueTypeName = (newValue != null) ? 
newValue.getClass().getName() : "null";
  -                log.warn( 
  -                    "Cannot evaluate method: " + method.getName() + " on 
bean: " + bean 
  -                    + " of type: " + bean.getClass().getName() + " with 
value: " + newValue 
  -                    + " of type: " + valueTypeName 
  -                );
  -                handleException(context, e);
  -            }
  -        }
  -    }
  -
  -    /** 
        * Gets the method which will be invoked by the update
        *
        * @return the Method to be invoked by the update
  @@ -141,20 +74,13 @@
           if ( types == null || types.length <= 0 ) {
               throw new IllegalArgumentException( "The Method must have at 
least one parameter" );
           }
  -        this.valueType = types[0];
  +        setValueType(types[0]);
       }
       
       // Implementation methods
       
//-------------------------------------------------------------------------    
       
  -    /** 
  -     * Strategy method to allow derivations to handle exceptions differently.
  -     * @param context the Context being updated when this exception occured
  -     * @param e the Exception that occured during the update
  -     */
  -    protected void handleException(Context context, Exception e) {
  -        log.info( "Caught exception: " + e, e );
  -    }
  +    
       
       /**
        * Returns something useful for logging.
  @@ -162,5 +88,19 @@
        */
       public String toString() {
           return "MethodUpdater [method=" + method + "]";
  +    }
  +
  +    /**
  +     * Updates the bean by method invocation.    
  +     */
  +    protected void executeUpdate(Context context, Object bean, Object 
newValue) throws Exception {
  +        if ( log.isDebugEnabled() ) {
  +            log.debug( 
  +                "Calling setter method: " + method.getName() + " on bean: " 
+ bean 
  +                + " with new value: " + newValue 
  +            );
  +        }
  +        Object[] arguments = { newValue };
  +        method.invoke( bean, arguments );
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to