001: /*
002: * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * COMPONENT_NAME: idl.toJava
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)SequenceGen.java 1.20 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040: // -F46082.51<daz> Remove -stateful feature; javaStatefulName() obsolete.
041: // -D61056 <klr> Use Util.helperName
042:
043: import java.io.PrintWriter;
044: import java.util.Hashtable;
045:
046: import com.sun.tools.corba.se.idl.InterfaceEntry;
047: import com.sun.tools.corba.se.idl.PrimitiveEntry;
048: import com.sun.tools.corba.se.idl.SequenceEntry;
049: import com.sun.tools.corba.se.idl.StringEntry;
050: import com.sun.tools.corba.se.idl.SymtabEntry;
051:
052: import com.sun.tools.corba.se.idl.constExpr.Expression;
053:
054: /**
055: *
056: **/
057: public class SequenceGen implements
058: com.sun.tools.corba.se.idl.SequenceGen, JavaGenerator {
059: /**
060: * Public zero-argument constructor.
061: **/
062: public SequenceGen() {
063: } // ctor
064:
065: /**
066: *
067: **/
068: public void generate(Hashtable symbolTable, SequenceEntry s,
069: PrintWriter stream) {
070: } // generator
071:
072: ///////////////
073: // From JavaGenerator
074:
075: public int helperType(int index, String indent,
076: TCOffsets tcoffsets, String name, SymtabEntry entry,
077: PrintWriter stream) {
078: int offsetOfType = tcoffsets.offset(entry.type().fullName());
079: if (offsetOfType >= 0) {
080: // This code uses the deprecated create_recursive_sequence_tc()
081: // It should be eliminated when the API is removed from the ORB class
082: // Regardles, this code will not be emitted since updated emitters invoke
083: // method type() below instead of helperType() when handling sequences
084:
085: // This is a recursive sequence
086: tcoffsets.set(null);
087: Expression maxSize = ((SequenceEntry) entry).maxSize();
088: if (maxSize == null)
089: stream
090: .println(indent
091: + name
092: + " = org.omg.CORBA.ORB.init ().create_recursive_sequence_tc (0, "
093: + (offsetOfType - tcoffsets
094: .currentOffset()) + ");");
095: else
096: stream
097: .println(indent
098: + name
099: + " = org.omg.CORBA.ORB.init ().create_recursive_sequence_tc ("
100: + Util.parseExpression(maxSize)
101: + ", "
102: + (offsetOfType - tcoffsets
103: .currentOffset()) + ");");
104: tcoffsets.bumpCurrentOffset(4); // add indirection field
105: } else {
106: // This is a normal sequence
107: tcoffsets.set(entry);
108: index = ((JavaGenerator) entry.type().generator())
109: .helperType(index + 1, indent, tcoffsets, name,
110: entry.type(), stream);
111: Expression maxSize = ((SequenceEntry) entry).maxSize();
112: if (maxSize == null)
113: stream
114: .println(indent
115: + name
116: + " = org.omg.CORBA.ORB.init ().create_sequence_tc (0, "
117: + name + ");");
118: else
119: stream
120: .println(indent
121: + name
122: + " = org.omg.CORBA.ORB.init ().create_sequence_tc ("
123: + Util.parseExpression(maxSize) + ", "
124: + name + ");");
125: }
126: tcoffsets.bumpCurrentOffset(4); // add on the seq max size
127: return index;
128: } // helperType
129:
130: public int type(int index, String indent, TCOffsets tcoffsets,
131: String name, SymtabEntry entry, PrintWriter stream) {
132: int offsetOfType = tcoffsets.offset(entry.type().fullName());
133: if (offsetOfType >= 0) {
134: // This is a recursive sequence
135: tcoffsets.set(null);
136:
137: // Need to fix later: how to get repositoryId of IDL type containing this sequence?
138: // entry.repositoryID().ID() returns empty string and
139: // Util.javaQualifiedName(entry) returns internal name which is not valid repId
140:
141: stream
142: .println(indent
143: + name
144: + " = org.omg.CORBA.ORB.init ().create_recursive_tc ("
145: + "\"\"" + ");");
146: tcoffsets.bumpCurrentOffset(4); // add indirection field
147: } else {
148: // This is a normal sequence
149: tcoffsets.set(entry);
150: index = ((JavaGenerator) entry.type().generator()).type(
151: index + 1, indent, tcoffsets, name, entry.type(),
152: stream);
153: Expression maxSize = ((SequenceEntry) entry).maxSize();
154: if (maxSize == null)
155: stream
156: .println(indent
157: + name
158: + " = org.omg.CORBA.ORB.init ().create_sequence_tc (0, "
159: + name + ");");
160: else
161: stream
162: .println(indent
163: + name
164: + " = org.omg.CORBA.ORB.init ().create_sequence_tc ("
165: + Util.parseExpression(maxSize) + ", "
166: + name + ");");
167: }
168: //stream.println (indent + name + " = " + Util.helperName (entry, true) + ".type ();"); // <d61056>
169: return index;
170: } // type
171:
172: public void helperRead(String entryName, SymtabEntry entry,
173: PrintWriter stream) {
174: } // helperRead
175:
176: public void helperWrite(SymtabEntry entry, PrintWriter stream) {
177: } // helperWrite
178:
179: public int read(int index, String indent, String name,
180: SymtabEntry entry, PrintWriter stream) {
181: SequenceEntry seq = (SequenceEntry) entry;
182: String length = "_len" + index++;
183: stream.println(indent + "int " + length
184: + " = istream.read_long ();");
185: if (seq.maxSize() != null) {
186: stream.println(indent + "if (" + length + " > ("
187: + Util.parseExpression(seq.maxSize()) + "))");
188: stream
189: .println(indent
190: + " throw new org.omg.CORBA.MARSHAL (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);");
191: }
192: String seqOfName;
193: try {
194: seqOfName = Util.sansArrayInfo((String) seq
195: .dynamicVariable(Compile.typedefInfo));
196: } catch (NoSuchFieldException e) {
197: seqOfName = seq.name();
198: }
199: int startArray = seqOfName.indexOf('[');
200: String arrayDcl = seqOfName.substring(startArray);
201: seqOfName = seqOfName.substring(0, startArray);
202:
203: // For interfaces having state, e.g., valuetypes.
204: SymtabEntry seqOfEntry = (SymtabEntry) Util.symbolTable
205: .get(seqOfName.replace('.', '/'));
206: if (seqOfEntry != null && seqOfEntry instanceof InterfaceEntry
207: && ((InterfaceEntry) seqOfEntry).state() != null)
208: // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
209: //seqOfName = Util.javaStatefulName ((InterfaceEntry)seqOfEntry);
210: seqOfName = Util.javaName((InterfaceEntry) seqOfEntry);
211:
212: arrayDcl = arrayDcl.substring(2);
213: stream.println(indent + name + " = new " + seqOfName + '['
214: + length + ']' + arrayDcl + ';');
215: if (seq.type() instanceof PrimitiveEntry)
216: // <d61961> Check for CORBA::Principal, too
217: //if (seq.type ().name ().equals ("any") || seq.type ().name ().equals ("TypeCode"))
218: if (seq.type().name().equals("any")
219: || seq.type().name().equals("TypeCode")
220: || seq.type().name().equals("Principal")) {
221: String loopIndex = "_o" + index;
222: stream.println(indent + "for (int " + loopIndex
223: + " = 0;" + loopIndex + " < " + name
224: + ".length; ++" + loopIndex + ')');
225: stream.println(indent + " " + name + '[' + loopIndex
226: + "] = istream.read_" + seq.type().name()
227: + " ();");
228: } else { // special case for ValueBox: if name is "xxx tmp", drop xxx
229: String varName = name;
230: int nameIndex = varName.indexOf(' ');
231: if (nameIndex != -1)
232: varName = varName.substring(nameIndex + 1);
233: stream.println(indent + "istream.read_"
234: + Util.collapseName(entry.type().name())
235: + "_array (" + varName + ", 0, " + length
236: + ");");
237: }
238: else if (entry.type() instanceof StringEntry) {
239: String loopIndex = "_o" + index;
240: stream.println(indent + "for (int " + loopIndex + " = 0;"
241: + loopIndex + " < " + name + ".length; ++"
242: + loopIndex + ')');
243: stream.println(indent + " " + name + '[' + loopIndex
244: + "] = istream.read_" + seq.type().name() + " ();");
245: } else if (entry.type() instanceof SequenceEntry) {
246: String loopIndex = "_o" + index;
247: stream.println(indent + "for (int " + loopIndex + " = 0;"
248: + loopIndex + " < " + name + ".length; ++"
249: + loopIndex + ')');
250: stream.println(indent + '{');
251: index = ((JavaGenerator) seq.type().generator()).read(
252: index, indent + " ", name + '[' + loopIndex + ']',
253: seq.type(), stream);
254: stream.println(indent + '}');
255: } else { // special case for ValueBox: if name is "xxx tmp", drop xxx
256: String varName = name;
257: int nameIndex = varName.indexOf(' ');
258: if (nameIndex != -1)
259: varName = varName.substring(nameIndex + 1);
260: String loopIndex = "_o" + index;
261: stream.println(indent + "for (int " + loopIndex + " = 0;"
262: + loopIndex + " < " + varName + ".length; ++"
263: + loopIndex + ')');
264: stream.println(indent + " " + varName + '[' + loopIndex
265: + "] = " + Util.helperName(seq.type(), true)
266: + ".read (istream);"); // <d61056>
267: }
268: return index;
269: } // read
270:
271: public int write(int index, String indent, String name,
272: SymtabEntry entry, PrintWriter stream) {
273: SequenceEntry seq = (SequenceEntry) entry;
274: if (seq.maxSize() != null) {
275: stream.println(indent + "if (" + name + ".length > ("
276: + Util.parseExpression(seq.maxSize()) + "))");
277: stream
278: .println(indent
279: + " throw new org.omg.CORBA.MARSHAL (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);");
280: }
281: stream.println(indent + "ostream.write_long (" + name
282: + ".length);");
283: if (entry.type() instanceof PrimitiveEntry)
284: // <d61961> Check for CORBA::Principal, too.
285: //if (entry.type ().name ().equals ("any") || entry.type ().name ().equals ("TypeCode"))
286: if (entry.type().name().equals("any")
287: || entry.type().name().equals("TypeCode")
288: || entry.type().name().equals("Principal")) {
289: String loopIndex = "_i" + index++;
290: stream.println(indent + "for (int " + loopIndex
291: + " = 0;" + loopIndex + " < " + name
292: + ".length; ++" + loopIndex + ')');
293: stream.println(indent + " ostream.write_"
294: + seq.type().name() + " (" + name + '['
295: + loopIndex + "]);");
296: } else
297: stream.println(indent + "ostream.write_"
298: + Util.collapseName(entry.type().name())
299: + "_array (" + name + ", 0, " + name
300: + ".length);");
301: else if (entry.type() instanceof StringEntry) {
302: String loopIndex = "_i" + index++;
303: stream.println(indent + "for (int " + loopIndex + " = 0;"
304: + loopIndex + " < " + name + ".length; ++"
305: + loopIndex + ')');
306: stream.println(indent + " ostream.write_"
307: + seq.type().name() + " (" + name + '[' + loopIndex
308: + "]);");
309: } else if (entry.type() instanceof SequenceEntry) {
310: String loopIndex = "_i" + index++;
311: stream.println(indent + "for (int " + loopIndex + " = 0;"
312: + loopIndex + " < " + name + ".length; ++"
313: + loopIndex + ')');
314: stream.println(indent + '{');
315: index = ((JavaGenerator) seq.type().generator()).write(
316: index, indent + " ", name + '[' + loopIndex + ']',
317: seq.type(), stream);
318: stream.println(indent + '}');
319: } else {
320: String loopIndex = "_i" + index++;
321: stream.println(indent + "for (int " + loopIndex + " = 0;"
322: + loopIndex + " < " + name + ".length; ++"
323: + loopIndex + ')');
324: stream.println(indent + " "
325: + Util.helperName(seq.type(), true)
326: + ".write (ostream, " + name + '[' + loopIndex
327: + "]);"); // <d61056>
328: }
329: return index;
330: } // write
331:
332: // From JavaGenerator
333: ///////////////
334: } // class SequenceGen
|