harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mloe...@apache.org
Subject svn commit: r413807 - in /incubator/harmony/enhanced/classlib/trunk/modules/beans: make/common/ src/main/java/java/beans/ src/main/java/org/apache/harmony/beans/
Date Tue, 13 Jun 2006 04:51:42 GMT
Author: mloenko
Date: Mon Jun 12 21:51:41 2006
New Revision: 413807

URL: http://svn.apache.org/viewvc?rev=413807&view=rev
Log:
fixes for HARMONY-588
[classlib] [beans] Encoder and ArrayPersistenceDelegate handle arrays incorrectly

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/beans/make/common/build.xml
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ArrayPersistenceDelegate.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_ClassPersistenceDelegate.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/make/common/build.xml
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/make/common/build.xml?rev=413807&r1=413806&r2=413807&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/make/common/build.xml (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/make/common/build.xml Mon Jun
12 21:51:41 2006
@@ -122,7 +122,6 @@
                 <fileset dir="${hy.beans.src.test.internal}">
                     <include name="**/*Test.java"/>
                 	<!-- Excluded pending investigation -->
-                	<exclude name="org/apache/harmony/beans/ArrayPersistenceDelegateTest.java"
/>
                 	<exclude name="org/apache/harmony/beans/ClassPersistenceDelegateTest.java"
/>
                     <exclude name="org/apache/harmony/beans/StringPersistenceDelegateTest.java"
/>
                 </fileset>
@@ -169,11 +168,9 @@
                     <exclude name="org/apache/harmony/beans/tests/java/beans/ExpressionTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/FeatureDescriptorTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/IndexedPropertyDescriptorTest.java"
/>
-                    <exclude name="org/apache/harmony/beans/tests/java/beans/IntrospectionExceptionTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/IntrospectorTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/MethodDescriptorTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java"
/>
-                    <exclude name="org/apache/harmony/beans/tests/java/beans/PropertyChangeEventTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/PropertyChangeSupportTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/PropertyDescriptorTest.java"
/>
                     <exclude name="org/apache/harmony/beans/tests/java/beans/PropertyEditorManagerTest.java"
/>

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java?rev=413807&r1=413806&r2=413807&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java
Mon Jun 12 21:51:41 2006
@@ -20,6 +20,7 @@
  */
 package java.beans;
 
+import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.Vector;
 
@@ -107,11 +108,12 @@
            PersistenceDelegate pd = (object != null) ?
                getPersistenceDelegate(object.getClass()) :
                    new NullPersistenceDelegate();
-               
+
+
            if(pd == null) {
                pd = new DefaultPersistenceDelegate();
            }
-           
+
            pd.writeObject(object, this);
     }
 
@@ -123,8 +125,8 @@
         if(node != null) {
             try {
                 Object[] oldArgs = oldStm.getArguments();
-                Object[] newArgs = write(oldArgs);
-                
+                write(oldArgs);
+
                 Statement statement = new Statement(node.getObjectValue(),
                         oldStm.getMethodName(), oldArgs);
                 node.addStatement(statement);
@@ -146,7 +148,7 @@
             
             ObjectNode node = null;
             Class type = null;
-            
+
             if(oldInstance != null) {
                 type = oldInstance.getClass();
                 node = (ObjectNode) nodes.get(oldInstance);
@@ -154,17 +156,21 @@
             
             if(node == null) {
                 if(isNull(type) || isPrimitive(type) || isString(type)
-                        || isClass(type)) {
+                        //|| isClass(type)
+                   ) 
+                {
                     node = new ObjectNode(oldExp);
                 } else {
                     write(oldExp.getArguments());
                     node = new ObjectNode(oldExp, nodes);
                 }
-                
+
                 nodes.put(oldInstance, node);
-                
+
                 // if an expression is not a constructor
-                if(!(oldExp.getTarget() instanceof Class)) {
+                if(!(oldExp.getTarget() instanceof Class ||
+                     oldExp.getTarget() instanceof Field))
+                {
                     ObjectNode parent = (ObjectNode) nodes.get(
                             oldExp.getTarget());
                     parent.addExpression(oldExp);
@@ -211,14 +217,7 @@
         if(node == null) {
             Class type = oldInstance.getClass();
             
-            if(isPrimitive(type) || isString(type) || isClass(type)) {
-                Expression expr = new Expression(type, "new",
-                        new Object[] { oldInstance });
-                nodes.put(oldInstance, new ObjectNode(expr));
-            } else {
-                doWriteObject(oldInstance);
-            }
-            
+            doWriteObject(oldInstance);
             node = (ObjectNode) nodes.get(oldInstance);
         } else {
             node.addReference();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java?rev=413807&r1=413806&r2=413807&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
Mon Jun 12 21:51:41 2006
@@ -60,7 +60,7 @@
                 && (newInstance == null);
         
         if(bothInstancesAreNull) {
-            return true;
+            return false;
         } else {
             return (oldInstance != null) && (newInstance != null) ? 
                 oldInstance.getClass() == newInstance.getClass() : false;
@@ -73,7 +73,8 @@
     public void writeObject(Object oldInstance, Encoder out) {
         Object newInstance = (oldInstance != null) ? out.get(oldInstance)
                 : null;
-        
+       
+        // FIXME rewrite, handling of nulls is not obvious
        if(mutatesTo(oldInstance, newInstance)) {
            if(oldInstance != null) {
                initialize(oldInstance.getClass(), oldInstance, newInstance,

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java?rev=413807&r1=413806&r2=413807&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
Mon Jun 12 21:51:41 2006
@@ -97,29 +97,37 @@
     
     Object invokeMethod() throws Exception {
         Object result = null;
-        
+
         try {
             if(target.getClass().isArray()) {
                 Method method = findArrayMethod();
                 Object[] ama = getArrayMethodArguments();
+
                 result = method.invoke(null, ama);
-            } else if(methodName.equals("new")) {
+            } else if (methodName.equals("newInstance") &&
+                    target instanceof Class &&
+                    ((Class) target).getName().equals("java.lang.reflect.Array"))
+            {
+                Class componentType = (Class) arguments[0];
+                int length = ((Integer) arguments[1]).intValue();
+
+                result = Array.newInstance(componentType, length);
+            } else if(methodName.equals("new") ||
+                      methodName.equals("newInstance"))
+            {
                 if(target instanceof Class) {
-                    Class type = (Class) target;
-                    if(type.isArray()) {
-                        Class componentType = type.getComponentType();
-                        int length = ((Integer) arguments[0]).intValue();
-                        result = Array.newInstance(componentType, length);
-                    } else {
-                        Constructor constructor = findConstructor();
-                        result = constructor.newInstance(arguments);
-                    }
+                    Constructor constructor = findConstructor();
+                    
+                    result = constructor.newInstance(arguments);
                 } else {
+                    // XXX should be investigated, dead code?
                     Constructor constructor = findConstructor();
+
                     result = constructor.newInstance(arguments);
                 }
             } else if(target instanceof Class) {
                 Method method = findStaticMethod();
+
                 result = method.invoke(null, arguments);
             } else {
                 final Method method = findMethod();
@@ -130,10 +138,12 @@
                         return null;
                     }
                 });
+
                 result = method.invoke(target, arguments);
             }
         } catch (InvocationTargetException ite) {
             Throwable t = ite.getCause();
+
             throw (t != null) && (t instanceof Exception) ? (Exception) t : ite;
         }
         return result;
@@ -173,47 +183,47 @@
     private Constructor findConstructor() throws NoSuchMethodException {
         Class[] argClasses = getClasses();
         Class targetClass = (Class) target;
-        
+
         Constructor result = null;
-        
-           Constructor[] constructors = targetClass.getConstructors();
-           for(int i = 0; i < constructors.length; ++i) {
-               Constructor constructor = constructors[i];
-               Class[] parameterTypes = constructor.getParameterTypes();
-               
-               if(parameterTypes.length == argClasses.length) {
-                   boolean found = true;
-                   
-                   for(int j = 0; j < parameterTypes.length; ++j) {
-                       boolean argIsNull = argClasses[j] == null;
-                       boolean argIsPrimitiveWrapper = isPrimitiveWrapper(
-                               argClasses[j], parameterTypes[j]);
-                       boolean paramIsPrimitive =
-                               parameterTypes[j].isPrimitive();
-                       boolean paramIsAssignable = argIsNull ? false
-                               : parameterTypes[j].isAssignableFrom(
-                                       argClasses[j]);
-                        
-                       if(!argIsNull && !paramIsAssignable
-                               && !argIsPrimitiveWrapper
-                               || argIsNull && paramIsPrimitive) {
-                           found = false;
-                           break;
-                       }
-                   }
-                   
-                   if(found) {
-                       result = constructor;
-                       break;
-                   }
-               }
+        Constructor[] constructors = targetClass.getConstructors();
+
+        for(int i = 0; i < constructors.length; ++i) {
+            Constructor constructor = constructors[i];
+            Class[] parameterTypes = constructor.getParameterTypes();
+
+            if(parameterTypes.length == argClasses.length) {
+                boolean found = true;
+
+                for(int j = 0; j < parameterTypes.length; ++j) {
+                    boolean argIsNull = argClasses[j] == null;
+                    boolean argIsPrimitiveWrapper = isPrimitiveWrapper(
+                            argClasses[j], parameterTypes[j]);
+                    boolean paramIsPrimitive =
+                            parameterTypes[j].isPrimitive();
+                    boolean paramIsAssignable = argIsNull ? false
+                            : parameterTypes[j].isAssignableFrom(
+                                   argClasses[j]);
+                    
+                    if(!argIsNull && !paramIsAssignable
+                                && !argIsPrimitiveWrapper
+                                || argIsNull && paramIsPrimitive) {
+                        found = false;
+                        break;
+                    }
+                }
+
+                if(found) {
+                    result = constructor;
+                    break;
+                }
+            }
         }
            
-           if(result == null) {
-               throw new NoSuchMethodException(
-                   "No constructor for class " + targetClass.getName()
-                   + " found");
-           }
+        if(result == null) {
+            throw new NoSuchMethodException(
+                    "No constructor for class " + targetClass.getName()
+                    + " found");
+        }
            
         return result;
     }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java?rev=413807&r1=413806&r2=413807&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
Mon Jun 12 21:51:41 2006
@@ -148,8 +148,8 @@
             
             Tag tag = new Tag("array");
             
-            tag.addAttr("class", object.getClass().getComponentType().getName());
-            tag.addAttr("length", ((Integer) args[0]).toString());
+            tag.addAttr("class", ((Class) args[0]).getName());
+            tag.addAttr("length", ((Integer) args[1]).toString());
             
             printBytes(tabCount, tag.toStringOnOpen());
             

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ArrayPersistenceDelegate.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ArrayPersistenceDelegate.java?rev=413807&r1=413806&r2=413807&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ArrayPersistenceDelegate.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ArrayPersistenceDelegate.java
Mon Jun 12 21:51:41 2006
@@ -16,6 +16,7 @@
 
 /**
  * @author Maxim V. Berkultsev
+ * @author Alexei Y. Zakharov
  * @version $Revision: 1.1.2.1 $
  */
 package org.apache.harmony.beans;
@@ -28,11 +29,12 @@
 
 /**
  * @author Maxim V. Berkultsev
+ * @author Alexei Y. Zakharov
  * @version $Revision: 1.1.2.1 $
  */
 
 public class ArrayPersistenceDelegate extends PersistenceDelegate {
-    
+
     private static PersistenceDelegate pd = null;
     
     public static PersistenceDelegate getInstance() {
@@ -43,12 +45,13 @@
     }
 
     protected Expression instantiate(Object oldInstance, Encoder out) {
-        Class type = getArrayWrapperClass(oldInstance.getClass());
         int length = Array.getLength(oldInstance);
-        return new Expression(oldInstance, type, "new",
-                new Object[] { new Integer(length) });
+        Class componentType = oldInstance.getClass().getComponentType();
+
+        return new Expression(oldInstance, Array.class, "newInstance",
+                new Object[] { componentType, new Integer(length) });
     }
-    
+
     protected void initialize(
             Class type, Object oldInstance, Object newInstance, Encoder out) {
         int length = Array.getLength(oldInstance);
@@ -77,32 +80,30 @@
             
         }
     }
-    
-    private static Class getArrayWrapperClass(Class type) {
-        Class result = type;
-        if(type == boolean[].class) {
-            result = Boolean[].class;
-        } else if(type == byte[].class) {
-            result = Byte[].class;
-        } else if(type == char[].class) {
-            result = Character[].class;
-        } else if(type == double[].class) {
-            result = Double[].class;
-        } else if(type == float[].class) {
-            result = Float[].class;
-        } else if(type == int[].class) {
-            result = Integer[].class;
-        } else if(type == long[].class) {
-            result = Long[].class;
-        } else if(type == short[].class) {
-            result = Short[].class;
-        }
-        return result;
-    }
-    
-    // Added for testing purposes
+
     protected boolean mutatesTo(Object oldInstance, Object newInstance) {
+        if (oldInstance != null && newInstance != null) {
+            Class oldCl = oldInstance.getClass();
+            Class newCl = newInstance.getClass();
+
+            if (oldCl.isArray() && !newCl.isArray() ||
+                    newCl.isArray() && !oldCl.isArray()) {
+                return false;
+            } else if (oldCl.isArray() && newCl.isArray()) {
+                // both are arrays
+                int l1 = Array.getLength(oldInstance);
+                int l2 = Array.getLength(newInstance);
+                Class cType1 = oldCl.getComponentType();
+                Class cType2 = newCl.getComponentType();
+
+                if (l1 == l2 && cType1.equals(cType2)) {
+                    return true;
+                } else {
+                    return false;
+                }
+            }
+        }
+        // both are nulls or have non-Array type
         return super.mutatesTo(oldInstance, newInstance);
     }
-    
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_ClassPersistenceDelegate.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_ClassPersistenceDelegate.java?rev=413807&r1=413806&r2=413807&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_ClassPersistenceDelegate.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/java_lang_ClassPersistenceDelegate.java
Mon Jun 12 21:51:41 2006
@@ -16,6 +16,7 @@
 
 /**
  * @author Maxim V. Berkultsev
+ * @author Alexei Y. Zakharov
  * @version $Revision: 1.1.2.1 $
  */
 package org.apache.harmony.beans;
@@ -23,9 +24,11 @@
 import java.beans.Encoder;
 import java.beans.Expression;
 import java.beans.PersistenceDelegate;
+import java.lang.reflect.Field;
 
 /**
  * @author Maxim V. Berkultsev
+ * @author Alexei Y. Zakharov
  * @version $Revision: 1.1.2.1 $
  */
 
@@ -33,8 +36,41 @@
     
     protected Expression instantiate(Object oldInstance, Encoder out) {
         Class value = (Class) oldInstance;
-        return new Expression(oldInstance, Class.class, "forName",
+        Field fld = null;
+        final String TYPE = "TYPE";
+        Expression result;
+
+        try {
+            if (value.equals(Integer.TYPE)) {
+                fld = Integer.class.getField(TYPE);
+            } else if (value.equals(Short.TYPE)) {
+                fld = Short.class.getField(TYPE);
+            } else if (value.equals(Long.TYPE)) {
+                fld = Long.class.getField(TYPE);
+            } else if (value.equals(Float.TYPE)) {
+                fld = Float.class.getField(TYPE);
+            } else if (value.equals(Double.TYPE)) {
+                fld = Double.class.getField(TYPE);
+            } else if (value.equals(Byte.TYPE)) {
+                fld = Byte.class.getField(TYPE);
+            } else if (value.equals(Character.TYPE)) {
+                fld = Character.class.getField(TYPE);
+            } else if (value.equals(Boolean.TYPE)) {
+                fld = Boolean.class.getField(TYPE);
+            }
+        } catch (NoSuchFieldException e) {
+            // impossible situation for valid java.lang classes 
+            // implementation with version >= 1.1 
+        }
+        if (fld != null) {
+            // we have primitive type 
+            result = new Expression(oldInstance, fld, "get",
+                    new Object[] {null});
+        } else {
+            result = new Expression(oldInstance, Class.class, "forName",
                 new Object[] { new String(value.getName()) });
+        }
+        return result;
     }
     
     protected void initialize(



Mime
View raw message