harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From qi...@apache.org
Subject svn commit: r660122 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/ main/java/org/apache/harmony/beans/internal/nls/ test/java/org/apache/harmony/beans/tests/java/beans/ test/resources/xml/
Date Mon, 26 May 2008 08:33:19 GMT
Author: qiuxx
Date: Mon May 26 01:33:18 2008
New Revision: 660122

URL: http://svn.apache.org/viewvc?rev=660122&view=rev
Log:
Applying patch from HARMONY-5840 ([classlib][beans] Exception when XMLDecoder array is missing
length)

Added:
    harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Illegal.xml
  (with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Null.xml  
(with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Object.xml
  (with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Primitive.xml
  (with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Wrapper.xml
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Statement.java
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLDecoder.java
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
    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?rev=660122&r1=660121&r2=660122&view=diff
==============================================================================
--- 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
Mon May 26 01:33:18 2008
@@ -133,6 +133,33 @@
                 } else {
                     throw new NoSuchMethodException(this.toString());
                 }
+            } else if (theMethodName.equals("newArray")) {//$NON-NLS-1$
+                // create a new array instance without length attribute
+                int length = theArguments.length;
+                Class clazz = (Class) theTarget;
+
+                // check the element types of array
+                for (int i = 0; i < length; i++) {
+                    boolean isNull = theArguments[i] == null;
+                    boolean isPrimitiveWrapper = isNull ? false
+                            : isPrimitiveWrapper(theArguments[i].getClass(),
+                                    clazz);
+                    boolean isAssignable = isNull ? false : clazz
+                            .isAssignableFrom(theArguments[i].getClass());
+                    if (!isNull && !isPrimitiveWrapper && !isAssignable)
{
+                        throw new IllegalArgumentException(Messages
+                                .getString("beans.62")); //$NON-NLS-1$
+                    }
+                }
+                result = Array.newInstance(clazz, length);
+                if (clazz.isPrimitive()) {
+                    // Copy element according to primitive types
+                    arrayCopy(clazz, theArguments, result, length);
+                } else {
+                    // Copy element of Objects
+                    System.arraycopy(theArguments, 0, result, 0, length);
+                }
+                return result;
             } else if (theTarget instanceof Class) {
                 Method method = null;
                 boolean found = false;
@@ -197,6 +224,50 @@
         }
         return result;
     }
+    
+    private void arrayCopy(Class type, Object[] src, Object dest, int length) {
+        if (type == boolean.class) {
+            boolean[] destination = (boolean[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Boolean) src[i]).booleanValue();
+            }
+        } else if (type == short.class) {
+            short[] destination = (short[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Short) src[i]).shortValue();
+            }
+        } else if (type == byte.class) {
+            byte[] destination = (byte[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Byte) src[i]).byteValue();
+            }
+        } else if (type == char.class) {
+            char[] destination = (char[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Character) src[i]).charValue();
+            }
+        } else if (type == int.class) {
+            int[] destination = (int[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Integer) src[i]).intValue();
+            }
+        } else if (type == long.class) {
+            long[] destination = (long[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Long) src[i]).longValue();
+            }
+        } else if (type == float.class) {
+            float[] destination = (float[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Float) src[i]).floatValue();
+            }
+        } else if (type == double.class) {
+            double[] destination = (double[]) dest;
+            for (int i = 0; i < length; i++) {
+                destination[i] = ((Double) src[i]).doubleValue();
+            }
+        }
+    }
 
     private Method findArrayMethod(String theMethodName, Object[] theArguments) throws NoSuchMethodException
{
         // the code below reproduces exact RI exception throwing behavior

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLDecoder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLDecoder.java?rev=660122&r1=660121&r2=660122&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLDecoder.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLDecoder.java
Mon May 26 01:33:18 2008
@@ -197,19 +197,29 @@
             }
         }
 
-        @SuppressWarnings("nls")
         private void startArrayElem(Attributes attributes) {
             Elem elem = new Elem();
             elem.isExpression = true;
-            elem.id = attributes.getValue("id");
+            elem.id = attributes.getValue("id"); //$NON-NLS-1$
             try {
                 // find component class
-                Class<?> compClass = classForName(attributes.getValue("class"));
-                // find length
-                int length = Integer.parseInt(attributes.getValue("length"));
-                // execute, new array instance
-                elem.result = Array.newInstance(compClass, length);
-                elem.isExecuted = true;
+                Class<?> compClass = classForName(attributes.getValue("class")); //$NON-NLS-1$
+                String lengthValue = attributes.getValue("length"); //$NON-NLS-1$
+                if (lengthValue != null) {
+                    // find length
+                    int length = Integer
+                            .parseInt(attributes.getValue("length")); //$NON-NLS-1$
+                    // execute, new array instance
+                    elem.result = Array.newInstance(compClass, length);
+                    elem.isExecuted = true;
+                } else {
+                    // create array without length attribute,
+                    // delay the excution to the end,
+                    // get array length from sub element
+                    elem.target = compClass;
+                    elem.methodName = "newArray"; //$NON-NLS-1$
+                    elem.isExecuted = false;
+                }
             } catch (Exception e) {
                 listener.exceptionThrown(e);
             }

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties?rev=660122&r1=660121&r2=660122&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
Mon May 26 01:33:18 2008
@@ -114,3 +114,4 @@
 beans.60=Indexed write method must take an int as its first argument
 beans.61=Indexed write method is not compatible with indexed read method
 beans.62=Cannot decide which method to call to match {0}
+beans.62=The type of element is mismatch with the type of array
\ No newline at end of file

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?rev=660122&r1=660121&r2=660122&view=diff
==============================================================================
--- 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
Mon May 26 01:33:18 2008
@@ -166,6 +166,89 @@
         assertEquals(2, ints[1]);
         assertEquals(3, ints[2]);
     }
+    
+    public void testReadObject_Array_WithoutLength() {
+        // Read array of primitive types without length attribute
+        XMLDecoder dec = new XMLDecoder(this.getClass().getResourceAsStream(
+                "/xml/Array_Primitive.xml"));
+        Object[] arrays = (Object[]) dec.readObject();
+
+        boolean[] booleanArray = (boolean[]) arrays[0];
+        assertTrue(booleanArray[0]);
+        assertFalse(booleanArray[1]);
+
+        short[] shortArray = (short[]) arrays[1];
+        assertEquals(1, shortArray[0]);
+        assertEquals(1, shortArray[1]);
+
+        byte[] byteArray = (byte[]) arrays[2];
+        assertEquals(2, byteArray[0]);
+        assertEquals(2, byteArray[1]);
+
+        char[] charArray = (char[]) arrays[3];
+        assertEquals('c', charArray[0]);
+        assertEquals('c', charArray[1]);
+
+        int[] intArray = (int[]) arrays[4];
+        assertEquals(4, intArray[0]);
+        assertEquals(4, intArray[1]);
+        assertEquals(4, intArray[2]);
+        assertEquals(4, intArray[3]);
+
+        long[] longArray = (long[]) arrays[5];
+        assertEquals(5l, longArray[0]);
+        assertEquals(5l, longArray[1]);
+        assertEquals(5l, longArray[2]);
+        assertEquals(5l, longArray[3]);
+        assertEquals(5l, longArray[4]);
+
+        float[] floatArray = (float[]) arrays[6];
+        assertEquals(6f, floatArray[0]);
+        assertEquals(6f, floatArray[1]);
+
+        double[] doubleArray = (double[]) arrays[7];
+        assertEquals(7d, doubleArray[0]);
+        assertEquals(7d, doubleArray[1]);
+
+        // Read array of Object types without length attribute
+        dec = new XMLDecoder(this.getClass().getResourceAsStream(
+                "/xml/Array_Object.xml"));
+        Object[] array = (Object[]) dec.readObject();
+
+        assertTrue((Boolean) array[0]);
+        assertEquals(new Short((short) 1), (Short) array[1]);
+        assertEquals(new Byte((byte) 2), (Byte) array[2]);
+        assertEquals(new Character('c'), (Character) array[3]);
+        assertEquals(new Integer(4), (Integer) array[4]);
+        assertEquals(new Long(5), (Long) array[5]);
+        assertEquals(new Float(6), (Float) array[6]);
+        assertEquals(new Double(7), (Double) array[7]);
+        assertEquals("string", (String) array[8]);
+
+        // Read wrapper element in array of primitive types
+        dec = new XMLDecoder(this.getClass().getResourceAsStream(
+                "/xml/Array_Wrapper.xml"));
+        int[] integers = (int[]) dec.readObject();
+        assertEquals(11, integers[0]);
+        assertEquals(22, integers[1]);
+    }
+
+    public void testReadObject_Array_Special() {
+        // Read array of Object types in special case without length attribute
+        XMLDecoder dec = new XMLDecoder(this.getClass().getResourceAsStream(
+                "/xml/Array_Null.xml"));
+        Object[] array = (Object[]) dec.readObject();
+        assertNull(array[0]);
+        assertNull(array[1]);
+        assertEquals("", (String) array[2]);
+
+        // Read array with wrong type, it should return null,
+        // and throw a java.lang.IllegalArgumentException
+        dec = new XMLDecoder(this.getClass().getResourceAsStream(
+                "/xml/Array_Illegal.xml"));
+        array = (Object[]) dec.readObject();
+        assertNull(array);
+    }
 
     public void testReadObject_PropertyDependency() {
         XMLDecoder dec = new XMLDecoder(this.getClass().getResourceAsStream(

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Illegal.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Illegal.xml?rev=660122&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Illegal.xml
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Illegal.xml
Mon May 26 01:33:18 2008
@@ -0,0 +1,21 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<java version='1.4.0' class='java.beans.XMLDecoder'>
+    <array class="java.lang.String">
+        <int>2</int>
+    </array>
+</java>
\ No newline at end of file

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Illegal.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Null.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Null.xml?rev=660122&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Null.xml (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Null.xml Mon
May 26 01:33:18 2008
@@ -0,0 +1,23 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<java version='1.4.0' class='java.beans.XMLDecoder'>
+    <array class="java.lang.String">
+        <null/>
+        <null/>
+        <string></string>
+    </array>
+</java>
\ No newline at end of file

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Null.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Object.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Object.xml?rev=660122&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Object.xml
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Object.xml
Mon May 26 01:33:18 2008
@@ -0,0 +1,29 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<java version='1.4.0' class='java.beans.XMLDecoder'>
+     <array class="java.lang.Object">
+        <boolean>true</boolean>
+        <short>1</short>
+        <byte>2</byte>
+        <char>c</char>
+        <int>4</int>
+        <long>5</long>
+        <float>6</float>
+        <double>7</double>
+        <string>string</string>
+    </array>
+</java>
\ No newline at end of file

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Object.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Primitive.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Primitive.xml?rev=660122&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Primitive.xml
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Primitive.xml
Mon May 26 01:33:18 2008
@@ -0,0 +1,57 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+  <java version='1.4.0' class='java.beans.XMLDecoder'>
+     <array class="java.lang.Object">
+     	<array class='boolean'>
+	        <boolean>true</boolean>
+            <boolean>false</boolean>
+        </array>
+        <array class='short'>
+            <short>1</short>
+            <short>1</short>
+        </array>
+        <array class='byte'>
+            <byte>2</byte>
+            <byte>2</byte>
+        </array>
+        <array class='char'>
+            <char>c</char>
+            <char>c</char>
+        </array>
+        <array class='int'>
+            <int>4</int>
+            <int>4</int>
+            <int>4</int>
+            <int>4</int>
+        </array>
+        <array class='long'>
+            <long>5</long>
+            <long>5</long>
+            <long>5</long>
+            <long>5</long>
+            <long>5</long>
+        </array>
+        <array class='float'>
+            <float>6</float>
+            <float>6</float>
+        </array>
+        <array class='double'>
+            <double>7</double>
+            <double>7</double>
+        </array>
+    </array>
+</java>
\ No newline at end of file

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Primitive.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Wrapper.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Wrapper.xml?rev=660122&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Wrapper.xml
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Wrapper.xml
Mon May 26 01:33:18 2008
@@ -0,0 +1,23 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+ <java version='1.4.0' class='java.beans.XMLDecoder'>
+    <array class='int'>
+        <int>11</int>
+        <int>22</int>
+    </array>
+</java>

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Array_Wrapper.xml
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message