scheu 02/03/21 08:20:49 Modified: java/src/org/apache/axis/wsdl/toJava JavaStubWriter.java java/test/wsdl/arrays ArrayTest.wsdl PersonalInfoBookSOAPBindingImpl.java PersonalInfoBookServiceTestCase.java Log: Improved the Stub assignment and conversion of output parameters. There are a number of places in JavaStubWriter that assigned (or returned) the output value(s). If the expected type is an array, JavaUtils.convert is invoked to convert the value to the expected type. Otherwise the output value is cast to to the expected type. This processing fails if an array comes over the wire and the expected type is a Vector. The following changes are made to the stub generation: First an attempt is made to cast the output value to the expected type. If an exception is thrown, it is caught and an attempt is made to convert the output value to the expected type using JavaUtils.convert. This slightly improves performance for the array case (since most of the time a cast is sufficient). It also makes the stub more flexible, allowing conversions in many more situations. There was a lot of redundant code in JavaStubWriter, so I moved most of the processing into a private method (writeOutputAssign). I also cleaned up some bad logic and stale comments. I changed the arrays testcase so that it returns a Vector. This verifies the new stub code. Revision Changes Path 1.50 +40 -91 xml-axis/java/src/org/apache/axis/wsdl/toJava/JavaStubWriter.java Index: JavaStubWriter.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/wsdl/toJava/JavaStubWriter.java,v retrieving revision 1.49 retrieving revision 1.50 diff -u -r1.49 -r1.50 --- JavaStubWriter.java 20 Mar 2002 21:52:23 -0000 1.49 +++ JavaStubWriter.java 21 Mar 2002 16:20:48 -0000 1.50 @@ -635,115 +635,36 @@ pw.println(" java.util.Map output;"); pw.println(" output = call.getOutputParams();"); - // If expecting an array, need to call convert(..) because - // the runtime stores arrays in a different form (ArrayList). - // NOTE A: - // It seems that it should be the responsibility of the - // Call to convert the ArrayList into the proper array. - if (p.getType().getName().endsWith("[]")) { - pw.println(" // REVISIT THIS!"); - pw.println(" " + javifiedName - + ".value = (" + p.getType().getName() - + ") org.apache.axis.utils.JavaUtils.convert(output.get(" - + qnameName + "), " + p.getType().getName() - + ".class);"); - } - else { - pw.println(" " + javifiedName + ".value = " - + getResponseString(p.getType(), - "output.get(" + qnameName + ")")); - } + writeOutputAssign(javifiedName + ".value =", + p.getType(), + "output.get(" + qnameName + ")"); } else { // (parms.outputs == 1) // There is only one output and it is the return value. - - // If expecting an array, need to call convert(..) because - // the runtime stores arrays in a different form (ArrayList). - // (See NOTE A) - if (parms.returnType != null && - parms.returnType.getName() != null && - parms.returnType.getName().indexOf("[]") > 0) { - pw.println(" // REVISIT THIS!"); - pw.println(" return ("+parms.returnType.getName() + ")" - +"org.apache.axis.utils.JavaUtils.convert(resp," - + parms.returnType.getName()+".class);"); - } else { - pw.println(" return " + getResponseString(parms.returnType, "resp")); - } + writeOutputAssign("return ", + parms.returnType, "resp"); } } else { - // There is more than 1 output. resp is the first one. The rest are from - // call.getOutputParams (). Pull the Objects from the appropriate place - - // resp or call.getOutputParms - and put them in the appropriate place, - // either in a holder or as the return value. + // There is more than 1 output. Get the outputs from getOutputParams. pw.println(" java.util.Map output;"); pw.println(" output = call.getOutputParams();"); - boolean firstInoutIsResp = (parms.outputs == 0); for (int i = 0; i < parms.list.size (); ++i) { Parameter p = (Parameter) parms.list.get (i); String javifiedName = Utils.xmlNameToJava(p.getName()); String qnameName = Utils.getNewQName( Utils.getAxisQName(p.getQName())); if (p.getMode() != Parameter.IN) { - if (firstInoutIsResp) { - firstInoutIsResp = false; - // If expecting an array, need to call convert(..) because - // the runtime stores arrays in a different form (ArrayList). - // (See NOTE A) - if (p.getType().getName().endsWith("[]")) { - pw.println(" // REVISIT THIS!"); - pw.println (" " + javifiedName - + ".value = (" + p.getType().getName() - + ") org.apache.axis.utils.JavaUtils.convert(output.get(" + qnameName + "), " - + p.getType().getName() + ".class);"); - } - else { - pw.println (" " + javifiedName + - ".value = " + - getResponseString(p.getType(), "output.get(" + qnameName + ")")); - } - } - else { - // If expecting an array, need to call convert(..) because - // the runtime stores arrays in a different form (ArrayList). - // (See NOTE A) - if (p.getType().getName().endsWith("[]")) { - pw.println(" // REVISIT THIS!"); - pw.println (" " + javifiedName - + ".value = (" + p.getType().getName() - + ") org.apache.axis.utils.JavaUtils.convert(" - + "output.get(" + qnameName + "), " - + p.getType().getName() + ".class);"); - } - else { - pw.println (" " + javifiedName - + ".value = " + getResponseString(p.getType(), - "output.get(" + qnameName + ")")); - } - } + writeOutputAssign(javifiedName + ".value =", + p.getType(), + "output.get(" + qnameName + ")"); } - } if (parms.outputs > 0) { - - // If expecting an array, need to call convert(..) because - // the runtime stores arrays in a different form (ArrayList). - // (See NOTE A) - if (parms.returnType != null && - parms.returnType.getName() != null && - parms.returnType.getName().indexOf("[]") > 0) { - pw.println(" // REVISIT THIS!"); - pw.println(" return (" - + parms.returnType.getName() + ")" - + "org.apache.axis.utils.JavaUtils.convert(output.get(" - + Utils.getNewQName(parms.returnName) + ")," - + parms.returnType.getName()+".class);"); - } else if (parms.returnType != null) { - pw.println(" return " + getResponseString(parms.returnType, "resp")); - } - + writeOutputAssign("return ", + parms.returnType, + "resp"); } } @@ -753,4 +674,32 @@ pw.println(); } // writeOperation + /** + * writeOutputAssign + * @param target (either "return" or "something =" + * @param type (source TypeEntry) + * @param source (source String) + * + */ + private void writeOutputAssign(String target, + TypeEntry type, + String source) { + if (type != null && type.getName() != null) { + // Try casting the output to the expected output. + // If that fails, use JavaUtils.convert() + pw.println(" try {"); + pw.println(" " + target + + getResponseString(type, source)); + pw.println(" } catch (Exception e) {"); + pw.println(" " + target + + getResponseString(type, + "org.apache.axis.utils.JavaUtils.convert(" + + source + ", " + + type.getName() + ".class)")); + pw.println(" }"); + } else { + pw.println(" " + target + + getResponseString(type, source)); + } + } } // class JavaStubWriter 1.2 +4 -2 xml-axis/java/test/wsdl/arrays/ArrayTest.wsdl Index: ArrayTest.wsdl =================================================================== RCS file: /home/cvs/xml-axis/java/test/wsdl/arrays/ArrayTest.wsdl,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayTest.wsdl 4 Jan 2002 19:09:41 -0000 1.1 +++ ArrayTest.wsdl 21 Mar 2002 16:20:48 -0000 1.2 @@ -8,6 +8,7 @@ xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" + xmlns:soapy="http://xml.apache.org/xml-soap" xmlns="http://schemas.xmlsoap.org/wsdl/"> <!-- type defs --> @@ -51,10 +52,11 @@ <!-- Style 2: Array defined using derivation and wsdl:ArrayType --> <xsd:element name="hobbies" type="typens:hobbyArray"/> <!-- Style 3: Array defined using derivation of soapenc:Array --> - <xsd:element name="pets" type="typens:petArray"/> <xsd:element name="id" type="typens:intArray"/> <xsd:element name="id2" type="xsd:int"/> + <xsd:element name="pets" type="soapy:Vector"/> + <!-- Collection defined using sequence of single unbounded element --> <xsd:element name="foods" type="xsd:string" maxOccurs="unbounded"/> <!-- Array of Bytes (nillable=true causes byte[] to become Byte[])--> @@ -86,7 +88,7 @@ </message> <message name="GetPetsFromNameResponse"> - <part name="pets" type="typens:petArray"/> + <part name="pets" type="soapy:Vector"/> </message> <message name="GetIDFromNameRequest"> 1.2 +2 -1 xml-axis/java/test/wsdl/arrays/PersonalInfoBookSOAPBindingImpl.java Index: PersonalInfoBookSOAPBindingImpl.java =================================================================== RCS file: /home/cvs/xml-axis/java/test/wsdl/arrays/PersonalInfoBookSOAPBindingImpl.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PersonalInfoBookSOAPBindingImpl.java 4 Jan 2002 19:09:41 -0000 1.1 +++ PersonalInfoBookSOAPBindingImpl.java 21 Mar 2002 16:20:48 -0000 1.2 @@ -7,6 +7,7 @@ package test.wsdl.arrays; import java.util.HashMap; import java.util.Map; +import java.util.Vector; public class PersonalInfoBookSOAPBindingImpl implements test.wsdl.arrays.PersonalInfoBook { private Map table = new HashMap(); @@ -16,7 +17,7 @@ public test.wsdl.arrays.PersonalInfo getPersonalInfoFromName(java.lang.String name) throws java.rmi.RemoteException { return (test.wsdl.arrays.PersonalInfo) table.get(name); } - public String[] getPetsFromName(java.lang.String name) throws java.rmi.RemoteException { + public Vector getPetsFromName(java.lang.String name) throws java.rmi.RemoteException { return ((test.wsdl.arrays.PersonalInfo) table.get(name)).getPets(); } public int[] getIDFromName(java.lang.String name) throws java.rmi.RemoteException { 1.5 +9 -4 xml-axis/java/test/wsdl/arrays/PersonalInfoBookServiceTestCase.java Index: PersonalInfoBookServiceTestCase.java =================================================================== RCS file: /home/cvs/xml-axis/java/test/wsdl/arrays/PersonalInfoBookServiceTestCase.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- PersonalInfoBookServiceTestCase.java 23 Feb 2002 00:48:28 -0000 1.4 +++ PersonalInfoBookServiceTestCase.java 21 Mar 2002 16:20:48 -0000 1.5 @@ -7,6 +7,8 @@ package test.wsdl.arrays; +import java.util.Vector; + public class PersonalInfoBookServiceTestCase extends junit.framework.TestCase { public PersonalInfoBookServiceTestCase(String name) { super(name); @@ -20,7 +22,10 @@ String name = "Joe Geek"; String[] movies = new String[] { "Star Trek", "A.I." }; String[] hobbies= new String[] { "programming", "reading about programming" }; - String[] pets = new String[] { "Byte", "Nibbles" }; + Vector pets = new Vector(); + pets.add(new String("Byte")); + pets.add(new String("Nibbles")); + //String[] pets = new String[] { "Byte", "Nibbles" }; int[] id = new int[] { 0, 0, 7 }; int id2 = 123; String[] foods = new String[] { "Cheeze Whiz", "Jolt Cola" }; @@ -65,7 +70,7 @@ assertTrue("Hobbies are corrupted " + value, value.getHobbies()[1].equals(pi.getHobbies()[1])); assertTrue("Pets are corrupted " + value, - value.getPets()[1].equals(pi.getPets()[1])); + value.getPets().elementAt(1).equals(pi.getPets().elementAt(1))); assertTrue("Id is corrupted " + value, value.getId()[0] == 0 && value.getId()[1] == 0 && value.getId()[2] == 7); assertTrue("Id2 is corrupted " + value, @@ -80,10 +85,10 @@ } try { - java.lang.String[] value = null; + Vector value = null; value = binding.getPetsFromName(name); assertTrue("PetsFromName is broken " + value, - value[1].equals(pi.getPets()[1])); + value.elementAt(1).equals(pi.getPets().elementAt(1))); } catch (java.rmi.RemoteException re) { throw new junit.framework.AssertionFailedError("Remote Exception caught: " + re ); }