KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > tool > JavaMarshallingGenerator


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.openwire.tool;
19
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.codehaus.jam.JAnnotation;
30 import org.codehaus.jam.JAnnotationValue;
31 import org.codehaus.jam.JClass;
32 import org.codehaus.jam.JPackage;
33 import org.codehaus.jam.JProperty;
34
35 /**
36  *
37  * @version $Revision: 384826 $
38  */

39 public class JavaMarshallingGenerator extends MultiSourceGenerator {
40
41     protected List JavaDoc concreteClasses = new ArrayList JavaDoc();
42     protected File JavaDoc factoryFile;
43     protected String JavaDoc factoryFileName = "MarshallerFactory";
44     protected String JavaDoc indent = " ";
45     protected String JavaDoc targetDir="src/main/java";
46     
47     public Object JavaDoc run() {
48         if (destDir == null) {
49             destDir = new File JavaDoc(targetDir+"/org/apache/activemq/openwire/v" + getOpenwireVersion());
50         }
51         Object JavaDoc answer = super.run();
52         processFactory();
53         return answer;
54     }
55
56     protected void generateFile(PrintWriter JavaDoc out) throws Exception JavaDoc {
57
58         generateLicence(out);
59 out.println("");
60 out.println("package org.apache.activemq.openwire.v" + getOpenwireVersion() + ";");
61 out.println("");
62 out.println("import java.io.DataInput;");
63 out.println("import java.io.DataOutput;");
64 out.println("import java.io.IOException;");
65 out.println("");
66 out.println("import org.apache.activemq.openwire.*;");
67 out.println("import org.apache.activemq.command.*;");
68 out.println("");
69 out.println("");
70         for (int i = 0; i < getJclass().getImportedPackages().length; i++) {
71             JPackage pkg = getJclass().getImportedPackages()[i];
72             for (int j = 0; j < pkg.getClasses().length; j++) {
73                 JClass clazz = pkg.getClasses()[j];
74 out.println("import " + clazz.getQualifiedName() + ";");
75             }
76         }
77
78 out.println("");
79 out.println("/**");
80 out.println(" * Marshalling code for Open Wire Format for "+getClassName()+"");
81 out.println(" *");
82 out.println(" *");
83 out.println(" * NOTE!: This file is auto generated - do not modify!");
84 out.println(" * if you need to make a change, please see the modify the groovy scripts in the");
85 out.println(" * under src/gram/script and then use maven openwire:generate to regenerate ");
86 out.println(" * this file.");
87 out.println(" *");
88 out.println(" * @version $Revision$");
89 out.println(" */");
90 out.println("public " + getAbstractClassText() + "class " + getClassName() + " extends " + getBaseClass() + " {");
91 out.println("");
92
93         if (!isAbstractClass()) {
94
95 out.println(" /**");
96 out.println(" * Return the type of Data Structure we marshal");
97 out.println(" * @return short representation of the type data structure");
98 out.println(" */");
99 out.println(" public byte getDataStructureType() {");
100 out.println(" return "+getJclass().getSimpleName()+".DATA_STRUCTURE_TYPE;");
101 out.println(" }");
102 out.println(" ");
103 out.println(" /**");
104 out.println(" * @return a new object instance");
105 out.println(" */");
106 out.println(" public DataStructure createObject() {");
107 out.println(" return new "+getJclass().getSimpleName()+"();");
108 out.println(" }");
109 out.println("");
110         }
111
112 out.println(" /**");
113 out.println(" * Un-marshal an object instance from the data input stream");
114 out.println(" *");
115 out.println(" * @param o the object to un-marshal");
116 out.println(" * @param dataIn the data input stream to build the object from");
117 out.println(" * @throws IOException");
118 out.println(" */");
119 out.println(" public void tightUnmarshal(OpenWireFormat wireFormat, Object o, DataInput dataIn, BooleanStream bs) throws IOException {");
120 out.println(" super.tightUnmarshal(wireFormat, o, dataIn, bs);");
121
122         if (!getProperties().isEmpty()) {
123 out.println("");
124 out.println(" " + getJclass().getSimpleName() + " info = (" + getJclass().getSimpleName() + ")o;");
125         }
126
127         if (isMarshallerAware()) {
128 out.println("");
129 out.println(" info.beforeUnmarshall(wireFormat);");
130 out.println(" ");
131         }
132
133         generateTightUnmarshalBody(out);
134
135         if (isMarshallerAware()) {
136 out.println("");
137 out.println(" info.afterUnmarshall(wireFormat);");
138         }
139
140 out.println("");
141 out.println(" }");
142 out.println("");
143 out.println("");
144 out.println(" /**");
145 out.println(" * Write the booleans that this object uses to a BooleanStream");
146 out.println(" */");
147 out.println(" public int tightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs) throws IOException {");
148
149         if (!getProperties().isEmpty()) {
150 out.println("");
151 out.println(" " + getJclass().getSimpleName() + " info = (" + getJclass().getSimpleName() + ")o;");
152         }
153
154         if (isMarshallerAware()) {
155 out.println("");
156 out.println(" info.beforeMarshall(wireFormat);");
157         }
158
159 out.println("");
160 out.println(" int rc = super.tightMarshal1(wireFormat, o, bs);");
161         int baseSize = generateTightMarshal1Body(out);
162
163 out.println("");
164 out.println(" return rc + " + baseSize + ";");
165 out.println(" }");
166 out.println("");
167 out.println(" /**");
168 out.println(" * Write a object instance to data output stream");
169 out.println(" *");
170 out.println(" * @param o the instance to be marshaled");
171 out.println(" * @param dataOut the output stream");
172 out.println(" * @throws IOException thrown if an error occurs");
173 out.println(" */");
174 out.println(" public void tightMarshal2(OpenWireFormat wireFormat, Object o, DataOutput dataOut, BooleanStream bs) throws IOException {");
175 out.println(" super.tightMarshal2(wireFormat, o, dataOut, bs);");
176         if (!getProperties().isEmpty()) {
177 out.println("");
178 out.println(" " + getJclass().getSimpleName() + " info = (" + getJclass().getSimpleName() + ")o;");
179         }
180
181         generateTightMarshal2Body(out);
182
183         if (isMarshallerAware()) {
184 out.println("");
185 out.println(" info.afterMarshall(wireFormat);");
186         }
187
188 out.println("");
189 out.println(" }");
190 out.println("");
191 out.println(" /**");
192 out.println(" * Un-marshal an object instance from the data input stream");
193 out.println(" *");
194 out.println(" * @param o the object to un-marshal");
195 out.println(" * @param dataIn the data input stream to build the object from");
196 out.println(" * @throws IOException");
197 out.println(" */");
198 out.println(" public void looseUnmarshal(OpenWireFormat wireFormat, Object o, DataInput dataIn) throws IOException {");
199 out.println(" super.looseUnmarshal(wireFormat, o, dataIn);");
200
201         if (!getProperties().isEmpty()) {
202 out.println("");
203 out.println(" " + getJclass().getSimpleName() + " info = (" + getJclass().getSimpleName() + ")o;");
204         }
205
206         if (isMarshallerAware()) {
207 out.println("");
208 out.println(" info.beforeUnmarshall(wireFormat);");
209 out.println(" ");
210         }
211
212         generateLooseUnmarshalBody(out);
213
214         if (isMarshallerAware()) {
215 out.println("");
216 out.println(" info.afterUnmarshall(wireFormat);");
217         }
218
219 out.println("");
220 out.println(" }");
221 out.println("");
222 out.println("");
223 out.println(" /**");
224 out.println(" * Write the booleans that this object uses to a BooleanStream");
225 out.println(" */");
226 out.println(" public void looseMarshal(OpenWireFormat wireFormat, Object o, DataOutput dataOut) throws IOException {");
227
228         if (!getProperties().isEmpty()) {
229 out.println("");
230 out.println(" " + getJclass().getSimpleName() + " info = (" + getJclass().getSimpleName() + ")o;");
231         }
232
233         if (isMarshallerAware()) {
234 out.println("");
235 out.println(" info.beforeMarshall(wireFormat);");
236         }
237
238 out.println("");
239 out.println(" super.looseMarshal(wireFormat, o, dataOut);");
240
241         generateLooseMarshalBody(out);
242
243 out.println("");
244 out.println(" }");
245 out.println("}");
246     }
247
248     private void generateLicence(PrintWriter JavaDoc out) {
249 out.println("/**");
250 out.println(" *");
251 out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
252 out.println(" * contributor license agreements. See the NOTICE file distributed with");
253 out.println(" * this work for additional information regarding copyright ownership.");
254 out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
255 out.println(" * (the \"License\"); you may not use this file except in compliance with");
256 out.println(" * the License. You may obtain a copy of the License at");
257 out.println(" *");
258 out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
259 out.println(" *");
260 out.println(" * Unless required by applicable law or agreed to in writing, software");
261 out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
262 out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
263 out.println(" * See the License for the specific language governing permissions and");
264 out.println(" * limitations under the License.");
265 out.println(" */");
266     }
267
268     protected void processFactory() {
269         if (factoryFile == null) {
270             factoryFile = new File JavaDoc(destDir, factoryFileName + filePostFix);
271         }
272         PrintWriter JavaDoc out = null;
273         try {
274             out = new PrintWriter JavaDoc(new FileWriter JavaDoc(factoryFile));
275             generateFactory(out);
276         } catch (Exception JavaDoc e) {
277             throw new RuntimeException JavaDoc(e);
278         } finally {
279             if (out != null) {
280                 out.close();
281             }
282         }
283     }
284
285     protected void generateFactory(PrintWriter JavaDoc out) {
286         generateLicence(out);
287 out.println("");
288 out.println("package org.apache.activemq.openwire.v"+getOpenwireVersion()+";");
289 out.println("");
290 out.println("import org.apache.activemq.openwire.DataStreamMarshaller;");
291 out.println("import org.apache.activemq.openwire.OpenWireFormat;");
292 out.println("");
293 out.println("/**");
294 out.println(" * MarshallerFactory for Open Wire Format.");
295 out.println(" *");
296 out.println(" *");
297 out.println(" * NOTE!: This file is auto generated - do not modify!");
298 out.println(" * if you need to make a change, please see the modify the groovy scripts in the");
299 out.println(" * under src/gram/script and then use maven openwire:generate to regenerate ");
300 out.println(" * this file.");
301 out.println(" *");
302 out.println(" * @version $Revision$");
303 out.println(" */");
304 out.println("public class MarshallerFactory {");
305 out.println("");
306 out.println(" /**");
307 out.println(" * Creates a Map of command type -> Marshallers");
308 out.println(" */");
309 out.println(" static final private DataStreamMarshaller marshaller[] = new DataStreamMarshaller[256];");
310 out.println(" static {");
311 out.println("");
312
313         List JavaDoc list = new ArrayList JavaDoc(getConcreteClasses());
314         Collections.sort(list, new Comparator JavaDoc(){
315             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
316                 JClass c1 = (JClass) o1;
317                 JClass c2 = (JClass) o2;
318                 return c1.getSimpleName().compareTo(c2.getSimpleName());
319             }});
320         
321         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
322             JClass jclass = (JClass) iter.next();
323 out.println(" add(new " + jclass.getSimpleName() + "Marshaller());");
324         }
325
326 out.println("");
327 out.println(" }");
328 out.println("");
329 out.println(" static private void add(DataStreamMarshaller dsm) {");
330 out.println(" marshaller[dsm.getDataStructureType()] = dsm;");
331 out.println(" }");
332 out.println(" ");
333 out.println(" static public DataStreamMarshaller[] createMarshallerMap(OpenWireFormat wireFormat) {");
334 out.println(" return marshaller;");
335 out.println(" }");
336 out.println("}");
337     }
338
339     protected void processClass(JClass jclass) {
340         super.processClass(jclass);
341
342         if (!jclass.isAbstract()) {
343             concreteClasses.add(jclass);
344         }
345     }
346
347     protected String JavaDoc getClassName(JClass jclass) {
348         return super.getClassName(jclass) + "Marshaller";
349     }
350
351     protected String JavaDoc getBaseClassName(JClass jclass) {
352         String JavaDoc answer = "BaseDataStreamMarshaller";
353         JClass superclass = jclass.getSuperclass();
354         if (superclass != null) {
355             String JavaDoc superName = superclass.getSimpleName();
356             if (!superName.equals("Object") && !superName.equals("JNDIBaseStorable") && !superName.equals("DataStructureSupport")) {
357                 answer = superName + "Marshaller";
358             }
359         }
360         return answer;
361     }
362
363     protected void initialiseManuallyMaintainedClasses() {
364     }
365
366     protected void generateTightUnmarshalBody(PrintWriter JavaDoc out) {
367         List JavaDoc properties = getProperties();
368         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
369             JProperty property = (JProperty) iter.next();
370             JAnnotation annotation = property.getAnnotation("openwire:property");
371             JAnnotationValue size = annotation.getValue("size");
372             JClass propertyType = property.getType();
373             String JavaDoc propertyTypeName = propertyType.getSimpleName();
374
375             if (propertyType.isArrayType() && !propertyTypeName.equals("byte[]")) {
376                 generateTightUnmarshalBodyForArrayProperty(out, property, size);
377             } else {
378                 generateTightUnmarshalBodyForProperty(out, property, size);
379             }
380         }
381     }
382
383     protected void generateTightUnmarshalBodyForProperty(PrintWriter JavaDoc out, JProperty property, JAnnotationValue size) {
384         String JavaDoc setter = property.getSetter().getSimpleName();
385         String JavaDoc type = property.getType().getSimpleName();
386
387         if (type.equals("boolean")) {
388             out.println(" info." + setter + "(bs.readBoolean());");
389         } else if (type.equals("byte")) {
390             out.println(" info." + setter + "(dataIn.readByte());");
391         } else if (type.equals("char")) {
392             out.println(" info." + setter + "(dataIn.readChar());");
393         } else if (type.equals("short")) {
394             out.println(" info." + setter + "(dataIn.readShort());");
395         } else if (type.equals("int")) {
396             out.println(" info." + setter + "(dataIn.readInt());");
397         } else if (type.equals("long")) {
398             out.println(" info." + setter + "(tightUnmarshalLong(wireFormat, dataIn, bs));");
399         } else if (type.equals("String")) {
400             out.println(" info." + setter + "(tightUnmarshalString(dataIn, bs));");
401         } else if (type.equals("byte[]")) {
402             if (size != null) {
403                 out.println(" info." + setter + "(tightUnmarshalConstByteArray(dataIn, bs, " + size.asInt() + "));");
404             } else {
405                 out.println(" info." + setter + "(tightUnmarshalByteArray(dataIn, bs));");
406             }
407         } else if (type.equals("ByteSequence")) {
408             out.println(" info." + setter + "(tightUnmarshalByteSequence(dataIn, bs));");
409         } else if (isThrowable(property.getType())) {
410             out.println(" info." + setter + "((" + property.getType().getQualifiedName() + ") tightUnmarsalThrowable(wireFormat, dataIn, bs));");
411         } else if (isCachedProperty(property)) {
412             out.println(" info." + setter + "((" + property.getType().getQualifiedName() + ") tightUnmarsalCachedObject(wireFormat, dataIn, bs));");
413         } else {
414             out.println(" info." + setter + "((" + property.getType().getQualifiedName() + ") tightUnmarsalNestedObject(wireFormat, dataIn, bs));");
415         }
416     }
417
418     protected void generateTightUnmarshalBodyForArrayProperty(PrintWriter JavaDoc out, JProperty property, JAnnotationValue size) {
419         JClass propertyType = property.getType();
420         String JavaDoc arrayType = propertyType.getArrayComponentType().getQualifiedName();
421         String JavaDoc setter = property.getSetter().getSimpleName();
422         out.println();
423         if (size != null) {
424             out.println(" {");
425             out.println(" " + arrayType + " value[] = new " + arrayType + "[" + size.asInt() + "];");
426             out.println(" " + "for( int i=0; i < " + size.asInt() + "; i++ ) {");
427             out.println(" value[i] = (" + arrayType + ") tightUnmarsalNestedObject(wireFormat,dataIn, bs);");
428             out.println(" }");
429             out.println(" info." + setter + "(value);");
430             out.println(" }");
431         } else {
432             out.println(" if (bs.readBoolean()) {");
433             out.println(" short size = dataIn.readShort();");
434             out.println(" " + arrayType + " value[] = new " + arrayType + "[size];");
435             out.println(" for( int i=0; i < size; i++ ) {");
436             out.println(" value[i] = (" + arrayType + ") tightUnmarsalNestedObject(wireFormat,dataIn, bs);");
437             out.println(" }");
438             out.println(" info." + setter + "(value);");
439             out.println(" }");
440             out.println(" else {");
441             out.println(" info." + setter + "(null);");
442             out.println(" }");
443         }
444     }
445
446     protected int generateTightMarshal1Body(PrintWriter JavaDoc out) {
447         List JavaDoc properties = getProperties();
448         int baseSize = 0;
449         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
450             JProperty property = (JProperty) iter.next();
451             JAnnotation annotation = property.getAnnotation("openwire:property");
452             JAnnotationValue size = annotation.getValue("size");
453             JClass propertyType = property.getType();
454             String JavaDoc type = propertyType.getSimpleName();
455             String JavaDoc getter = "info." + property.getGetter().getSimpleName() + "()";
456
457             if (type.equals("boolean")) {
458                 out.println(" bs.writeBoolean(" + getter + ");");
459             } else if (type.equals("byte")) {
460                 baseSize += 1;
461             } else if (type.equals("char")) {
462                 baseSize += 2;
463             } else if (type.equals("short")) {
464                 baseSize += 2;
465             } else if (type.equals("int")) {
466                 baseSize += 4;
467             } else if (type.equals("long")) {
468                 out.println(" rc+=tightMarshalLong1(wireFormat, " + getter + ", bs);");
469             } else if (type.equals("String")) {
470                 out.println(" rc += tightMarshalString1(" + getter + ", bs);");
471             } else if (type.equals("byte[]")) {
472                 if (size == null) {
473                     out.println(" rc += tightMarshalByteArray1(" + getter + ", bs);");
474                 } else {
475                     out.println(" rc += tightMarshalConstByteArray1(" + getter + ", bs, " + size.asInt() + ");");
476                 }
477             } else if (type.equals("ByteSequence")) {
478                 out.println(" rc += tightMarshalByteSequence1(" + getter + ", bs);");
479             } else if (propertyType.isArrayType()) {
480                 if (size != null) {
481                     out.println(" rc += tightMarshalObjectArrayConstSize1(wireFormat, " + getter + ", bs, " + size.asInt() + ");");
482                 } else {
483                     out.println(" rc += tightMarshalObjectArray1(wireFormat, " + getter + ", bs);");
484                 }
485             } else if (isThrowable(propertyType)) {
486                 out.println(" rc += tightMarshalThrowable1(wireFormat, " + getter + ", bs);");
487             } else {
488                 if (isCachedProperty(property)) {
489                     out.println(" rc += tightMarshalCachedObject1(wireFormat, (DataStructure)" + getter + ", bs);");
490                 } else {
491                     out.println(" rc += tightMarshalNestedObject1(wireFormat, (DataStructure)" + getter + ", bs);");
492                 }
493             }
494         }
495         return baseSize;
496     }
497
498     protected void generateTightMarshal2Body(PrintWriter JavaDoc out) {
499         List JavaDoc properties = getProperties();
500         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
501             JProperty property = (JProperty) iter.next();
502             JAnnotation annotation = property.getAnnotation("openwire:property");
503             JAnnotationValue size = annotation.getValue("size");
504             JClass propertyType = property.getType();
505             String JavaDoc type = propertyType.getSimpleName();
506             String JavaDoc getter = "info." + property.getGetter().getSimpleName() + "()";
507
508             if (type.equals("boolean")) {
509                 out.println(" bs.readBoolean();");
510             } else if (type.equals("byte")) {
511                 out.println(" dataOut.writeByte(" + getter + ");");
512             } else if (type.equals("char")) {
513                 out.println(" dataOut.writeChar(" + getter + ");");
514             } else if (type.equals("short")) {
515                 out.println(" dataOut.writeShort(" + getter + ");");
516             } else if (type.equals("int")) {
517                 out.println(" dataOut.writeInt(" + getter + ");");
518             } else if (type.equals("long")) {
519                 out.println(" tightMarshalLong2(wireFormat, " + getter + ", dataOut, bs);");
520             } else if (type.equals("String")) {
521                 out.println(" tightMarshalString2(" + getter + ", dataOut, bs);");
522             } else if (type.equals("byte[]")) {
523                 String JavaDoc mandatory = getMandatoryFlag(annotation);
524                 if (size != null) {
525                     out.println(" tightMarshalConstByteArray2(" + getter + ", dataOut, bs, " + size.asInt() + ");");
526                 } else {
527                     out.println(" tightMarshalByteArray2(" + getter + ", dataOut, bs);");
528                 }
529             } else if (type.equals("ByteSequence")) {
530                 out.println(" tightMarshalByteSequence2(" + getter + ", dataOut, bs);");
531             } else if (propertyType.isArrayType()) {
532                 if (size != null) {
533                     out.println(" tightMarshalObjectArrayConstSize2(wireFormat, " + getter + ", dataOut, bs, " + size.asInt() + ");");
534                 } else {
535                     out.println(" tightMarshalObjectArray2(wireFormat, " + getter + ", dataOut, bs);");
536                 }
537             } else if (isThrowable(propertyType)) {
538                 out.println(" tightMarshalThrowable2(wireFormat, " + getter + ", dataOut, bs);");
539             } else {
540                 if (isCachedProperty(property)) {
541                     out.println(" tightMarshalCachedObject2(wireFormat, (DataStructure)" + getter + ", dataOut, bs);");
542                 } else {
543                     out.println(" tightMarshalNestedObject2(wireFormat, (DataStructure)" + getter + ", dataOut, bs);");
544                 }
545             }
546         }
547     }
548
549     protected void generateLooseMarshalBody(PrintWriter JavaDoc out) {
550         List JavaDoc properties = getProperties();
551         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
552             JProperty property = (JProperty) iter.next();
553             JAnnotation annotation = property.getAnnotation("openwire:property");
554             JAnnotationValue size = annotation.getValue("size");
555             JClass propertyType = property.getType();
556             String JavaDoc type = propertyType.getSimpleName();
557             String JavaDoc getter = "info." + property.getGetter().getSimpleName() + "()";
558
559             if (type.equals("boolean")) {
560                 out.println(" dataOut.writeBoolean(" + getter + ");");
561             } else if (type.equals("byte")) {
562                 out.println(" dataOut.writeByte(" + getter + ");");
563             } else if (type.equals("char")) {
564                 out.println(" dataOut.writeChar(" + getter + ");");
565             } else if (type.equals("short")) {
566                 out.println(" dataOut.writeShort(" + getter + ");");
567             } else if (type.equals("int")) {
568                 out.println(" dataOut.writeInt(" + getter + ");");
569             } else if (type.equals("long")) {
570                 out.println(" looseMarshalLong(wireFormat, " + getter + ", dataOut);");
571             } else if (type.equals("String")) {
572                 out.println(" looseMarshalString(" + getter + ", dataOut);");
573             } else if (type.equals("byte[]")) {
574                 if (size != null) {
575                     out.println(" looseMarshalConstByteArray(wireFormat, " + getter + ", dataOut, " + size.asInt() + ");");
576                 } else {
577                     out.println(" looseMarshalByteArray(wireFormat, " + getter + ", dataOut);");
578                 }
579             } else if (type.equals("ByteSequence")) {
580                 out.println(" looseMarshalByteSequence(wireFormat, " + getter + ", dataOut);");
581             } else if (propertyType.isArrayType()) {
582                 if (size != null) {
583                     out.println(" looseMarshalObjectArrayConstSize(wireFormat, " + getter + ", dataOut, " + size.asInt() + ");");
584                 } else {
585                     out.println(" looseMarshalObjectArray(wireFormat, " + getter + ", dataOut);");
586                 }
587             } else if (isThrowable(propertyType)) {
588                 out.println(" looseMarshalThrowable(wireFormat, " + getter + ", dataOut);");
589             } else {
590                 if (isCachedProperty(property)) {
591                     out.println(" looseMarshalCachedObject(wireFormat, (DataStructure)" + getter + ", dataOut);");
592                 } else {
593                     out.println(" looseMarshalNestedObject(wireFormat, (DataStructure)" + getter + ", dataOut);");
594                 }
595             }
596         }
597     }
598
599     protected void generateLooseUnmarshalBody(PrintWriter JavaDoc out) {
600         List JavaDoc properties = getProperties();
601         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
602             JProperty property = (JProperty) iter.next();
603             JAnnotation annotation = property.getAnnotation("openwire:property");
604             JAnnotationValue size = annotation.getValue("size");
605             JClass propertyType = property.getType();
606             String JavaDoc propertyTypeName = propertyType.getSimpleName();
607
608             if (propertyType.isArrayType() && !propertyTypeName.equals("byte[]")) {
609                 generateLooseUnmarshalBodyForArrayProperty(out, property, size);
610             } else {
611                 generateLooseUnmarshalBodyForProperty(out, property, size);
612             }
613         }
614     }
615
616     protected void generateLooseUnmarshalBodyForProperty(PrintWriter JavaDoc out, JProperty property, JAnnotationValue size) {
617         String JavaDoc setter = property.getSetter().getSimpleName();
618         String JavaDoc type = property.getType().getSimpleName();
619
620         if (type.equals("boolean")) {
621             out.println(" info." + setter + "(dataIn.readBoolean());");
622         } else if (type.equals("byte")) {
623             out.println(" info." + setter + "(dataIn.readByte());");
624         } else if (type.equals("char")) {
625             out.println(" info." + setter + "(dataIn.readChar());");
626         } else if (type.equals("short")) {
627             out.println(" info." + setter + "(dataIn.readShort());");
628         } else if (type.equals("int")) {
629             out.println(" info." + setter + "(dataIn.readInt());");
630         } else if (type.equals("long")) {
631             out.println(" info." + setter + "(looseUnmarshalLong(wireFormat, dataIn));");
632         } else if (type.equals("String")) {
633             out.println(" info." + setter + "(looseUnmarshalString(dataIn));");
634         } else if (type.equals("byte[]")) {
635             if (size != null) {
636                 out.println(" info." + setter + "(looseUnmarshalConstByteArray(dataIn, " + size.asInt() + "));");
637             } else {
638                 out.println(" info." + setter + "(looseUnmarshalByteArray(dataIn));");
639             }
640         } else if (type.equals("ByteSequence")) {
641             out.println(" info." + setter + "(looseUnmarshalByteSequence(dataIn));");
642         } else if (isThrowable(property.getType())) {
643             out.println(" info." + setter + "((" + property.getType().getQualifiedName() + ") looseUnmarsalThrowable(wireFormat, dataIn));");
644         } else if (isCachedProperty(property)) {
645             out.println(" info." + setter + "((" + property.getType().getQualifiedName() + ") looseUnmarsalCachedObject(wireFormat, dataIn));");
646         } else {
647             out.println(" info." + setter + "((" + property.getType().getQualifiedName() + ") looseUnmarsalNestedObject(wireFormat, dataIn));");
648         }
649     }
650
651     protected void generateLooseUnmarshalBodyForArrayProperty(PrintWriter JavaDoc out, JProperty property, JAnnotationValue size) {
652         JClass propertyType = property.getType();
653         String JavaDoc arrayType = propertyType.getArrayComponentType().getQualifiedName();
654         String JavaDoc setter = property.getSetter().getSimpleName();
655         out.println();
656         if (size != null) {
657             out.println(" {");
658             out.println(" " + arrayType + " value[] = new " + arrayType + "[" + size.asInt() + "];");
659             out.println(" " + "for( int i=0; i < " + size.asInt() + "; i++ ) {");
660             out.println(" value[i] = (" + arrayType + ") looseUnmarsalNestedObject(wireFormat,dataIn);");
661             out.println(" }");
662             out.println(" info." + setter + "(value);");
663             out.println(" }");
664         } else {
665             out.println(" if (dataIn.readBoolean()) {");
666             out.println(" short size = dataIn.readShort();");
667             out.println(" " + arrayType + " value[] = new " + arrayType + "[size];");
668             out.println(" for( int i=0; i < size; i++ ) {");
669             out.println(" value[i] = (" + arrayType + ") looseUnmarsalNestedObject(wireFormat,dataIn);");
670             out.println(" }");
671             out.println(" info." + setter + "(value);");
672             out.println(" }");
673             out.println(" else {");
674             out.println(" info." + setter + "(null);");
675             out.println(" }");
676         }
677     }
678
679     /**
680      * Returns whether or not the given annotation has a mandatory flag on it or
681      * not
682      */

683     protected String JavaDoc getMandatoryFlag(JAnnotation annotation) {
684         JAnnotationValue value = annotation.getValue("mandatory");
685         if (value != null) {
686             String JavaDoc text = value.asString();
687             if (text != null && text.equalsIgnoreCase("true")) {
688                 return "true";
689             }
690         }
691         return "false";
692     }
693
694     public List JavaDoc getConcreteClasses() {
695         return concreteClasses;
696     }
697
698     public void setConcreteClasses(List JavaDoc concreteClasses) {
699         this.concreteClasses = concreteClasses;
700     }
701
702     public File JavaDoc getFactoryFile() {
703         return factoryFile;
704     }
705
706     public void setFactoryFile(File JavaDoc factoryFile) {
707         this.factoryFile = factoryFile;
708     }
709
710     public String JavaDoc getFactoryFileName() {
711         return factoryFileName;
712     }
713
714     public void setFactoryFileName(String JavaDoc factoryFileName) {
715         this.factoryFileName = factoryFileName;
716     }
717
718     public String JavaDoc getIndent() {
719         return indent;
720     }
721
722     public void setIndent(String JavaDoc indent) {
723         this.indent = indent;
724     }
725
726     public String JavaDoc getTargetDir() {
727         return targetDir;
728     }
729
730     public void setTargetDir(String JavaDoc sourceDir) {
731         this.targetDir = sourceDir;
732     }
733 }
734
Popular Tags