ClassCastException when serializing simple-typed multidimensional array bean fields -----------------------------------------------------------------------------------
Key: AXIS2-4026 URL: https://issues.apache.org/jira/browse/AXIS2-4026 Project: Axis 2.0 (Axis2) Issue Type: Bug Components: adb Affects Versions: nightly Environment: Tomcat 5.5.26, JDK 1.5.0_11 Reporter: Detelin Yordanov Hi guys, I found a problem in the latest multidimensional array support stuff added by Amila. The problem appears when serializing a response bean containing a simple-typed multidimensional array field, e.g.: public class SimpleBean { public int[][] numbers; public int[][] getNumbers() { return numbers; } public void setNumbers(int[][] numbers) { this.numbers = numbers; } } When serializing this to XML on the server side an exception gets thrown: Caused by: java.lang.ClassCastException: [I at org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl.processProperties(ADBXMLStreamReaderImpl.java:948) at org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl.next(ADBXMLStreamReaderImpl.java:833) at org.apache.axis2.util.StreamWrapper.next(StreamWrapper.java:71) at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:506) at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:161) The reason for this seems to be the code in the ADBXMLStreamReaderImpl that handles array types: } else if (propertyValue.getClass().isArray()) { // this is an arrary object and we need to get the pull parser for that Object[] objectArray = (Object[]) propertyValue; if (objectArray.length == 0) { //advance the index currentPropertyIndex = currentPropertyIndex + 2; return processProperties(); } else { The propertyValue here happens to be int[], and trying to cast it to Object[] causes the exception. The real reason for propertyValue being int[] (not Integer[] for example) is hidden in the BeanUtils.getPullParser(..) method where array fields are handled: (See line 164 of BeanUtil.java at rev 692771): Object value; if (readMethod != null) { readMethod.setAccessible(true); value = readMethod.invoke(beanObject, null); } The problem is due to a subtle change in the Java reflection API from 1.4.2 to 1.5.0, in 1.5.0 a new sentence appears describing the return type: "If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. However, if the value has the type of an array of a primitive type, the elements of the array are not wrapped in objects; in other words, an array of primitive type is returned." This means that when reading int[][] { new int[] {1, 2}, new int[] {3, 4} } and converting this to Object[], we get e.g. new Object[] { int[] {1, 2}, new int[] {3, 4} }, here the elements in the Object array are one dimensional arrays of int and this is OK (these elements are Objects), but when trying to cast each element to one dimensional array of Object in the ADBXMLStreamReaderImpl we got the ClassCastException since this conversion is not possible, e.g. : Object[] objArray = new Object[] { int[] {1, 2}, new int[] {3, 4} }; Object obj = objArray[0]; //FINE Object[] intArray = (Object[])objArray[0]; //ClassCastException It seems that some additional checks for primitive typed arrays are required. Regards, Detelin -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]