You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2005/03/12 02:21:46 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata FieldTypeClasses.java

arminw      2005/03/11 17:21:46

  Modified:    src/java/org/apache/ojb/broker/metadata Tag: OJB_1_0_RELEASE
                        FieldTypeClasses.java
  Log:
  add "copy" helper methods, minor changes
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.2   +71 -22    db-ojb/src/java/org/apache/ojb/broker/metadata/Attic/FieldTypeClasses.java
  
  Index: FieldTypeClasses.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/Attic/FieldTypeClasses.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- FieldTypeClasses.java	10 Mar 2005 17:55:30 -0000	1.1.2.1
  +++ FieldTypeClasses.java	12 Mar 2005 01:21:46 -0000	1.1.2.2
  @@ -6,9 +6,11 @@
   import java.sql.Timestamp;
   import java.sql.Types;
   import java.util.Arrays;
  +import java.io.Serializable;
   
   import org.apache.commons.lang.ArrayUtils;
   import org.apache.commons.lang.ObjectUtils;
  +import org.apache.commons.lang.SerializationUtils;
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.ojb.broker.OJBRuntimeException;
   
  @@ -172,6 +174,52 @@
               return sqlType;
           }
   
  +        /**
  +         * Helper method to copy an object if possible.
  +         *
  +         * @param toCopy The object to copy.
  +         * @return The copy of the object or <em>null</em> clone is not supported.
  +         */
  +        Object copyIfCloneable(Object toCopy)
  +        {
  +            Object result = null;
  +            if(toCopy instanceof Cloneable)
  +            {
  +                try
  +                {
  +                    Method m = toCopy.getClass().getMethod("clone", ArrayUtils.EMPTY_CLASS_ARRAY);
  +                    /*
  +                    arminw:
  +                    By definition the overrided object.clone() method has to be public
  +                    so we don't need to make it accessible
  +                    */
  +                    //m.setAccessible(true);
  +                    result = m.invoke(toCopy, null);
  +                }
  +                catch(Exception e)
  +                {
  +                    throw new OJBRuntimeException("Can't invoke method 'clone' on object: " + toCopy, e);
  +                }
  +            }
  +            return result;
  +        }
  +
  +        /**
  +         * Helper method to copy an object if possible.
  +         *
  +         * @param toCopy The object to copy.
  +         * @return The copy of the object or <em>null</em> if serialization is not supported.
  +         */
  +        Object copyIfSerializeable(Object toCopy)
  +        {
  +            Object result = null;
  +            if(toCopy instanceof Serializable)
  +            {
  +                result = SerializationUtils.clone((Serializable) toCopy);
  +            }
  +            return result;
  +        }
  +
           public String toString()
           {
               return new ToStringBuilder(this)
  @@ -262,40 +310,42 @@
       }
   
       /**
  -     * Here we have a change to
  +     * When using SQL UDT's it's possible that the jdbc-driver returns
  +     * full materialized java objects defined by the user.
        */
       public static class StructFieldType extends MutableFieldType
       {
  +        // TODO: does this make sense?? or Struct instances always Locator objects?
           public Object copy(Object fieldValue)
           {
  -            return fieldValue;
  -        }
  +            if(fieldValue == null) return null;
   
  -        public boolean equals(Object firstValue, Object secondValue)
  -        {
  -            return false;
  +            Object copy = copyIfCloneable(fieldValue);
  +            if(copy == null)
  +            {
  +                copy = copyIfSerializeable(fieldValue);
  +            }
  +            return copy == null ? fieldValue : copy;
           }
       }
   
  +    /**
  +     * If a user-defined object is used, we can check if object is
  +     * {@link Cloneable} or {@link Serializable} to copy the object.
  +     * If not possible return the specified object instance.
  +     */
       public static class JavaObjectFieldType extends MutableFieldType
       {
           public Object copy(Object fieldValue)
           {
  -            Object result = fieldValue;
  -            if(fieldValue instanceof Cloneable)
  +            if(fieldValue == null) return null;
  +
  +            Object copy = copyIfCloneable(fieldValue);
  +            if(copy == null)
               {
  -                try
  -                {
  -                    Method m = fieldValue.getClass().getMethod("clone", ArrayUtils.EMPTY_CLASS_ARRAY);
  -                    m.setAccessible(true);
  -                    result = m.invoke(fieldValue, null);
  -                }
  -                catch(Exception e)
  -                {
  -                    throw new OJBRuntimeException("Can't invoke method 'clone' on object: " + fieldValue, e);
  -                }
  +                copy = copyIfSerializeable(fieldValue);
               }
  -            return result;
  +            return copy == null ? fieldValue : copy;
           }
       }
   
  @@ -346,8 +396,7 @@
               if(fieldValue != null)
               {
                   Timestamp source = (Timestamp) fieldValue;
  -                result = new Timestamp(source.getTime());
  -                result.setNanos(source.getNanos());
  +                result = (Timestamp) source.clone();
               }
               return result;
           }
  
  
  

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