db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess PersistentFieldIntrospectorImpl.java
Date Thu, 24 Jun 2004 22:05:15 GMT
arminw      2004/06/24 15:05:15

  Modified:    src/java/org/apache/ojb/broker/metadata/fieldaccess
                        PersistentFieldIntrospectorImpl.java
  Log:
  fix OJB273: add support for nested fields
  
  Revision  Changes    Path
  1.8       +107 -54   db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldIntrospectorImpl.java
  
  Index: PersistentFieldIntrospectorImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldIntrospectorImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PersistentFieldIntrospectorImpl.java	4 Apr 2004 23:53:35 -0000	1.7
  +++ PersistentFieldIntrospectorImpl.java	24 Jun 2004 22:05:15 -0000	1.8
  @@ -15,15 +15,18 @@
    * limitations under the License.
    */
   
  -import org.apache.ojb.broker.metadata.MetadataException;
  -import org.apache.ojb.broker.util.logging.Logger;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
  -
   import java.beans.BeanInfo;
   import java.beans.IntrospectionException;
   import java.beans.Introspector;
   import java.beans.PropertyDescriptor;
   import java.lang.reflect.Method;
  +import java.util.ArrayList;
  +import java.util.List;
  +
  +import org.apache.commons.lang.StringUtils;
  +import org.apache.ojb.broker.metadata.MetadataException;
  +import org.apache.ojb.broker.util.logging.Logger;
  +import org.apache.ojb.broker.util.logging.LoggerFactory;
   
   /**
    * A {@link PersistentField} implementation using
  @@ -38,7 +41,8 @@
   public class PersistentFieldIntrospectorImpl extends AbstractPersistentField
   {
       private static final long serialVersionUID = 8805309492150404444L;
  -    private transient PropertyDescriptor propertyDescriptor;
  +    private Class type;
  +    private transient List propertyGraph;
   
       public PersistentFieldIntrospectorImpl()
       {
  @@ -50,7 +54,6 @@
           super(aClass, aPropertyName);
       }
   
  -
       /*
       need to overwrite these three methods to avoid
       building of Field instances
  @@ -59,23 +62,72 @@
       {
           return this.fieldName;
       }
  +
       public Class getType()
       {
  -        return getPropertyDescriptor().getPropertyType();
  +        if(type == null)
  +        {
  +            type = getPropertyDescriptor().getPropertyType();
  +        }
  +        return type;
       }
  +
       public Class getDeclaringClass()
       {
           return this.rootObjectType;
       }
   
  +    private void writeValue(Object target, Object value)
  +    {
  +        List propertyDescriptors = getPropertyGraph();
  +        int size = propertyDescriptors.size() - 1;
  +        PropertyDescriptor pd;
  +        for (int i = 0; i < size; i++)
  +        {
  +            pd =  (PropertyDescriptor) propertyDescriptors.get(i);
  +            target = getValueFrom(pd, target);
  +        }
  +        pd = (PropertyDescriptor) propertyDescriptors.get(size);
  +        setValueFor(pd, target, value);
  +    }
   
  -    /**
  -     * Sets aValue for anObject
  -     */
  -    public void doSet(Object anObject, Object aValue) throws MetadataException
  +    private Object readValue(Object target)
  +    {
  +        List propertyDescriptors = getPropertyGraph();
  +        for (int i = 0; i < propertyDescriptors.size(); i++)
  +        {
  +            PropertyDescriptor pd =  (PropertyDescriptor) propertyDescriptors.get(i);
  +            target = getValueFrom(pd, target);
  +        }
  +        return target;
  +    }
  +
  +    private Object getValueFrom(PropertyDescriptor pd, Object target)
       {
  -        Method m = getPropertyDescriptor().getWriteMethod();
  -        Object[] args = {aValue};
  +        if(target == null) return null;
  +        Method m = pd.getReadMethod();
  +        if (m != null)
  +        {
  +            try
  +            {
  +                return m.invoke(target, null);
  +            }
  +            catch (Throwable e)
  +            {
  +                throw new MetadataException("Error invoking method:" + m.getName() + "
in object " + target.getClass().getName(), e);
  +            }
  +        }
  +        else
  +        {
  +            throw new MetadataException(
  +                    "Can't get ReadMethod for property:" + pd.getName() + " in object "
+ target.getClass().getName());
  +        }
  +    }
  +
  +    private void setValueFor(PropertyDescriptor pd, Object target, Object value)
  +    {
  +        Method m = pd.getWriteMethod();
  +        Object[] args = {value};
           if (m != null)
           {
               try
  @@ -86,52 +138,67 @@
                    * we need to be able to set values to null. We can only set something
to null if
                    * the type is not a primitive (assignable from Object).
                    */
  -                if ((aValue != null) || !m.getParameterTypes()[0].isPrimitive())
  +                if ((value != null) || !m.getParameterTypes()[0].isPrimitive())
                   {
  -                    m.invoke(anObject, args);
  +                    m.invoke(target, args);
                   }
               }
               catch (Throwable e)
               {
  -                logProblem(anObject, aValue, e.getClass().getName());
  -                throw new MetadataException("Error invoking method:" + m.getName() + "
in object:" + anObject.getClass().getName(), e);
  +                throw new MetadataException("Error invoking method:" + m.getName() + "
in object:" + target.getClass().getName(), e);
               }
           }
           else
           {
  -            logProblem(anObject, aValue, "set: getWriteMethod returned null");
               throw new MetadataException(
  -                    "Can't get WriteMethod for property:" + getPropertyDescriptor().getName()
+ " in object:" + anObject.getClass().getName());
  +                    "Can't get WriteMethod for property:" + pd.getName() + " in object:"
+ target.getClass().getName());
           }
       }
   
  -    /**
  -     * Get the Value from anObject
  -     */
  -    public Object doGet(Object anObject) throws MetadataException
  +    private List getPropertyGraph()
       {
  -        Method m = getPropertyDescriptor().getReadMethod();
  -        Object[] args = null;
  +        if(propertyGraph == null)
  +        {
  +            propertyGraph = buildPropertyGraph();
  +        }
  +        return propertyGraph;
  +    }
   
  -        if (m != null)
  +    private List buildPropertyGraph()
  +    {
  +        List result = new ArrayList();
  +        String[] fields = StringUtils.split(getName(), PATH_TOKEN);
  +        PropertyDescriptor pd = null;
  +        for (int i = 0; i < fields.length; i++)
           {
  -            try
  +            String fieldName = fields[i];
  +            if(pd == null)
               {
  -                Object result = m.invoke(anObject, args);
  -                return result;
  +                pd = findPropertyDescriptor( getDeclaringClass(), fieldName);
               }
  -            catch (Throwable e)
  +            else
               {
  -                logProblem(anObject, null, e.getClass().getName());
  -                throw new MetadataException("Error invoking method:" + m.getName() + "
in object:" + anObject.getClass().getName(), e);
  +                pd = findPropertyDescriptor(pd.getPropertyType(), fieldName);
               }
  +            result.add(pd);
           }
  -        else
  -        {
  -            logProblem(anObject, null, "get: getReadMethod returned null");
  -            throw new MetadataException(
  -                    "Can't get ReadMethod for property:" + getPropertyDescriptor().getName()
+ " in object:" + anObject.getClass().getName());
  -        }
  +        return result;
  +    }
  +
  +    /**
  +     * Sets aValue for anObject
  +     */
  +    public void doSet(Object anObject, Object aValue) throws MetadataException
  +    {
  +        writeValue(anObject, aValue);
  +    }
  +
  +    /**
  +     * Get the Value from anObject
  +     */
  +    public Object doGet(Object anObject) throws MetadataException
  +    {
  +        return readValue(anObject);
       }
   
       /**
  @@ -173,7 +240,6 @@
   			 */
               throw new MetadataException("Can't find property " + aPropertyName + " in "
+ aClass.getName(), ex);
           }
  -
       }
   
       /**
  @@ -182,20 +248,7 @@
        */
       protected PropertyDescriptor getPropertyDescriptor()
       {
  -        if (propertyDescriptor == null)
  -        {
  -            setPropertyDescriptor(findPropertyDescriptor(getDeclaringClass(), getName()));
  -        }
  -        return propertyDescriptor;
  -    }
  -
  -    /**
  -     * Set PropertyDescriptor.
  -     * @param newPropertyDescriptor java.beans.PropertyDescriptor
  -     */
  -    protected void setPropertyDescriptor(PropertyDescriptor newPropertyDescriptor)
  -    {
  -        propertyDescriptor = newPropertyDescriptor;
  +        return (PropertyDescriptor) getPropertyGraph().get(getPropertyGraph().size()-1);
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message