harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r549619 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/Statement.java main/java/org/apache/harmony/beans/Handler.java test/java/org/apache/harmony/beans/tests/java/beans/XMLDecoderTest.java
Date Thu, 21 Jun 2007 21:31:23 GMT
Author: ayza
Date: Thu Jun 21 14:31:22 2007
New Revision: 549619

URL: http://svn.apache.org/viewvc?view=rev&rev=549619
Log:
Handling of special form of array constructor was added. This is a bug fix for HARMONY-1890
([classlib][beans] java.beans.XMLDecoder.readObject() throws unexpected ArrayIndexOutOfBoundsException)

Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Handler.java
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLDecoderTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java?view=diff&rev=549619&r1=549618&r2=549619
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
Thu Jun 21 14:31:22 2007
@@ -50,10 +50,12 @@
     public String toString() {
         StringBuilder sb = new StringBuilder();
         String targetVar = target != null ? convertClassName(target.getClass()) : "null";
//$NON-NLS-1$
+
         sb.append(targetVar);
         sb.append('.');
         sb.append(methodName);
         sb.append('(');
+
         if (arguments != null) {
             for (int i = 0; i < arguments.length; ++i) {
                 if (i > 0) {
@@ -72,6 +74,7 @@
         }
         sb.append(')');
         sb.append(';');
+
         return sb.toString();
     }
 
@@ -93,6 +96,7 @@
 
     Object invokeMethod() throws Exception {
         Object result = null;
+
         try {
             if (target.getClass().isArray()) {
                 Method method = findArrayMethod();
@@ -101,21 +105,27 @@
             } else if (methodName.equals("newInstance") //$NON-NLS-1$
                     && target == Array.class) {
                 Class<?> componentType = (Class) arguments[0];
-                int length = ((Integer) arguments[1]).intValue();
+                int length = (Integer) arguments[1];
                 result = Array.newInstance(componentType, length);
-            } else if (methodName.equals("new") //$NON-NLS-1$
-                    || methodName.equals("newInstance")) { //$NON-NLS-1$
-                if (target instanceof Class) {
-                    Constructor<?> constructor = findConstructor();
-                    result = constructor.newInstance(arguments);
+            } else if (target instanceof Class &&
+                       (methodName.equals("new") || //$NON-NLS-1$
+                        methodName.equals("newInstance"))) { //$NON-NLS-1$
+                Constructor<?> constructor;
+                Class<?> clazz = (Class <?>) target;
+                
+                if (clazz.isArray()) {
+                    // special form for constructing arrays, 
+                    // can be passed from decoder
+                    result = Array.newInstance(clazz.getComponentType(),
+                            (Integer) arguments[0]);
                 } else {
-                    // XXX should be investigated, dead code?
-                    Constructor<?> constructor = findConstructor();
+                    constructor = findConstructor();
                     result = constructor.newInstance(arguments);
                 }
             } else if (target instanceof Class) {
                 Method method = null;
                 boolean found = false;
+
                 try {
                     /*
                      * Try to look for a static method of class described by the
@@ -129,6 +139,7 @@
                     }
                 } catch (NoSuchMethodException e) {
                 }
+
                 if (!found) {
                     // static method was not found
                     // try to invoke method of Class object
@@ -187,6 +198,7 @@
 
     private Object[] getArrayMethodArguments() {
         Object[] args = new Object[arguments.length + 1];
+
         args[0] = target;
         for (int i = 0; i < arguments.length; ++i) {
             args[i + 1] = arguments[i];
@@ -199,10 +211,13 @@
         Class<?> targetClass = (Class) target;
         Constructor<?> result = null;
         Constructor<?>[] constructors = targetClass.getConstructors();
+
         for (Constructor<?> constructor : constructors) {
             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],
@@ -210,6 +225,7 @@
                     boolean paramIsPrimitive = parameterTypes[j].isPrimitive();
                     boolean paramIsAssignable = argIsNull ? false : parameterTypes[j]
                             .isAssignableFrom(argClasses[j]);
+
                     if (!argIsNull && !paramIsAssignable && !argIsPrimitiveWrapper
|| argIsNull
                             && paramIsPrimitive) {
                         found = false;
@@ -238,13 +254,17 @@
         Method[] methods = targetClass.getMethods();
         Vector<Method> foundMethods = new Vector<Method>();
         Method[] foundMethodsArr;
+
         for (Method method : methods) {
             int mods = method.getModifiers();
+
             if (method.getName().equals(methodName)
                     && (methodIsStatic ? Modifier.isStatic(mods) : true)) {
                 Class<?>[] parameterTypes = method.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],
@@ -252,6 +272,7 @@
                         boolean paramIsPrimitive = parameterTypes[j].isPrimitive();
                         boolean paramIsAssignable = argIsNull ? false : parameterTypes[j]
                                 .isAssignableFrom(argClasses[j]);
+
                         if (!argIsNull && !paramIsAssignable && !argIsPrimitiveWrapper
                                 || argIsNull && paramIsPrimitive) {
                             found = false;
@@ -275,9 +296,11 @@
     static boolean isStaticMethodCall(Statement stmt) {
         Object target = stmt.getTarget();
         String mName = stmt.getMethodName();
+
         if (!(target instanceof Class)) {
             return false;
         }
+
         try {
             Statement.findMethod((Class) target, mName, stmt.getArguments(), true);
             return true;

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Handler.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Handler.java?view=diff&rev=549619&r1=549618&r2=549619
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Handler.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Handler.java
Thu Jun 21 14:31:22 2007
@@ -110,7 +110,10 @@
         try {
             cmd.exec(references);
         } catch (Exception e) {
-            throw new SAXException(e);
+            SAXException e2 = new SAXException(e.getMessage());
+            
+            e2.initCause(e);
+            throw e2;
         }
 
         if (--tabCount < 0) {

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLDecoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLDecoderTest.java?view=diff&rev=549619&r1=549618&r2=549619
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLDecoderTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLDecoderTest.java
Thu Jun 21 14:31:22 2007
@@ -25,6 +25,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.util.Vector;
+import java.lang.reflect.Array;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -378,6 +379,29 @@
         decode("xml/Test7.xml");
     }
 
+
+    /**
+     * Regression test for HARMONY-1890
+     */
+    public void testDecodeEmptyStringArray1890() {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        XMLEncoder encoder = new XMLEncoder(out);
+        XMLDecoder decoder;
+        Object obj;
+
+        encoder.writeObject(new String[10]);
+        encoder.close();
+
+        decoder = new XMLDecoder(new ByteArrayInputStream(out.toByteArray()));
+        obj = decoder.readObject();
+        decoder.close();
+        
+        assertTrue("Returned object is not array", obj.getClass().isArray());
+        assertSame("String type expected", String.class,
+                obj.getClass().getComponentType());
+        assertEquals("Size mismatch", 10, Array.getLength(obj));
+    }
+    
     /*
      * The test checks the code generation for XML from MainTest.xml
      * 



Mime
View raw message