ValueBoxGen.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * COMPONENT_NAME: idl.toJava
27 *
28 * ORIGINS: 27
29 *
30 * Licensed Materials - Property of IBM
31 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32 * RMI-IIOP v1.0
33 *
34 */
35
36package com.sun.tools.corba.se.idl.toJavaPortable;
37
38// NOTES:
39// -D60929   <klr> Update for RTF2.4 changes
40// -D61056   <klr> Use Util.helperName
41// -D62023   <klr> Fix generation botch in helper.read for boxed bounded strings
42
43import java.io.File;
44import java.io.PrintWriter;
45import java.util.Hashtable;
46import java.util.Enumeration;
47import java.util.Vector;
48
49import com.sun.tools.corba.se.idl.GenFileStream;
50import com.sun.tools.corba.se.idl.InterfaceEntry;
51import com.sun.tools.corba.se.idl.SymtabEntry;
52import com.sun.tools.corba.se.idl.TypedefEntry;
53import com.sun.tools.corba.se.idl.ValueEntry;
54import com.sun.tools.corba.se.idl.ValueBoxEntry;
55import com.sun.tools.corba.se.idl.InterfaceState;
56import com.sun.tools.corba.se.idl.MethodEntry;
57import com.sun.tools.corba.se.idl.PrimitiveEntry;
58import com.sun.tools.corba.se.idl.SequenceEntry;
59import com.sun.tools.corba.se.idl.StringEntry;
60
61/**
62 *
63 **/
64public class ValueBoxGen implements com.sun.tools.corba.se.idl.ValueBoxGen, JavaGenerator
65{
66  /**
67   * Public zero-argument constructor.
68   **/
69  public ValueBoxGen ()
70  {
71  } // ctor
72
73  /**
74   *
75   **/
76  public void generate (Hashtable symbolTable, ValueBoxEntry v, PrintWriter str)
77  {
78    this.symbolTable = symbolTable;
79    this.v = v;
80
81    TypedefEntry member = ((InterfaceState) v.state ().elementAt (0)).entry;
82    SymtabEntry mType = member.type ();
83    // if it's primitive type, generate a java class
84    if (mType instanceof PrimitiveEntry)
85    {
86      openStream ();
87      if (stream == null)
88        return;
89      writeHeading ();
90      writeBody ();
91      writeClosing ();
92      closeStream ();
93    }
94    else
95    {
96      // If a constructed type is nested in the value box,
97      // value v struct s {...};
98      // the bindings for the nested type must be handled here
99      Enumeration e = v.contained ().elements ();
100      while (e.hasMoreElements ())
101      {
102        SymtabEntry contained = (SymtabEntry) e.nextElement ();
103
104        // in case of value box w/ nested enum, ex: value v enum e {e0, e1,...};
105        // the SymtabEntry for the enum and labels are contained in the vector.
106        // Must check the type to ignore the SymtabEntry for labels.
107        if (contained.type () != null)
108          contained.type ().generate (symbolTable, stream);
109      }
110    }
111    generateHelper ();
112    generateHolder ();
113  } // generate
114
115  /**
116   *
117   **/
118  protected void openStream ()
119  {
120    stream = Util.stream (v, ".java");
121  } // openStream
122
123  /**
124   *
125   **/
126  protected void generateHelper ()
127  {
128    ((Factories)Compile.compiler.factories ()).helper ().generate (symbolTable, v);
129  } // generateHelper
130
131  /**
132   *
133   **/
134  protected void generateHolder ()
135  {
136    ((Factories)Compile.compiler.factories ()).holder ().generate (symbolTable, v);
137  } // generateHolder
138
139  /**
140   *
141   **/
142  protected void writeHeading ()
143  {
144    Util.writePackage (stream, v);
145    Util.writeProlog (stream, ((GenFileStream)stream).name ());
146    if (v.comment () != null)
147      v.comment ().generate ("", stream);
148//  stream.println ("public class " + v.name () + " implements org.omg.CORBA.portable.ValueBase, org.omg.CORBA.portable.Streamable");
149    stream.println ("public class " + v.name () + " implements org.omg.CORBA.portable.ValueBase"); // <d60929>
150    stream.println ("{");
151  } // writeHeading
152
153  /**
154   *
155   **/
156  protected void writeBody ()
157  {
158    InterfaceState member = (InterfaceState) v.state ().elementAt (0);
159    SymtabEntry entry = (SymtabEntry) member.entry;
160    Util.fillInfo (entry);
161    if (entry.comment () != null)
162      entry.comment ().generate (" ", stream);
163    stream.println ("  public " +  Util.javaName (entry) + " value;");
164    stream.println ("  public " +  v.name () + " (" + Util.javaName (entry) + " initial)");
165    stream.println ("  {");
166    stream.println ("    value = initial;");
167    stream.println ("  }");
168    stream.println ();
169    writeTruncatable (); // <d60929>
170//  writeStreamableMethods ();
171  } // writeBody
172
173  /**
174   *
175   **/
176  protected void writeTruncatable () // <d60929>
177  {
178   // Per Simon, 4/6/98, emit _truncatable_ids()
179      stream.println ("  public String[] _truncatable_ids() {");
180      stream.println ("      return " + Util.helperName(v, true) + ".get_instance().get_truncatable_base_ids();"); // <d61056>
181      stream.println ("  }");
182      stream.println ();
183  } // writeTruncatable
184
185  /**
186   *
187   **/
188  protected void writeClosing ()
189  {
190    stream.println ("} // class " + v.name ());
191  } // writeClosing
192
193  /**
194   *
195   **/
196  protected void closeStream ()
197  {
198    stream.close ();
199  } // closeStream
200
201  /**
202   *
203   **/
204  protected void writeStreamableMethods ()
205  {
206    stream.println ("  public void _read (org.omg.CORBA.portable.InputStream istream)");
207    stream.println ("  {");
208    streamableRead ("this", v, stream);
209    stream.println ("  }");
210    stream.println ();
211    stream.println ("  public void _write (org.omg.CORBA.portable.OutputStream ostream)");
212    stream.println ("  {");
213    write (0, "    ", "this", v, stream);
214    stream.println ("  }");
215    stream.println ();
216    stream.println ("  public org.omg.CORBA.TypeCode _type ()");
217    stream.println ("  {");
218    stream.println ("    return " + Util.helperName (v, false) + ".type ();"); // <d61056>
219    stream.println ("  }");
220  } // writeStreamableMethods
221
222  ///////////////
223  // From JavaGenerator
224
225  public int helperType (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream)
226  {
227    ValueEntry vt = (ValueEntry) entry;
228    TypedefEntry member = (TypedefEntry) ((InterfaceState) (vt.state ()).elementAt (0)).entry;
229    SymtabEntry mType = Util.typeOf (member);
230    index = ((JavaGenerator)mType.generator ()).type (index, indent, tcoffsets, name, mType, stream);
231    stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_value_box_tc ("
232      + "_id, "
233      + '"' + entry.name () + "\", "
234      + name
235      + ");");
236    return index;
237  } // helperType
238
239  public int type (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream) {
240    stream.println (indent + name + " = " + Util.helperName (entry, true) + ".type ();"); // <d61056>
241    return index;
242  } // type
243
244  public int read (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
245  {
246    return index;
247  } // read
248
249  public void helperRead (String entryName, SymtabEntry entry, PrintWriter stream)
250  {
251  // <d59418 - KLR> per Simon, make "static" read call istream.read_value.
252  //                put real marshalling code in read_value.
253    stream.println ("    return (" + entryName +") ((org.omg.CORBA_2_3.portable.InputStream) istream).read_value (get_instance());"); // <d60929>
254    stream.println ("  }");
255    stream.println ();
256
257    // done with "read", now do "read_value with real marshalling code.
258
259    stream.println ("  public java.io.Serializable read_value (org.omg.CORBA.portable.InputStream istream)"); // <d60929>
260    stream.println ("  {");
261    // end of <d59418> changes
262
263    String indent = "    ";
264    Vector vMembers = ((ValueBoxEntry) entry).state ();
265    TypedefEntry member = ((InterfaceState) vMembers.elementAt (0)).entry;
266    SymtabEntry mType = member.type ();
267    if (mType instanceof PrimitiveEntry ||
268        mType instanceof SequenceEntry ||
269        mType instanceof TypedefEntry ||
270        mType instanceof StringEntry ||
271        !member.arrayInfo ().isEmpty ()) {
272      stream.println (indent + Util.javaName (mType) + " tmp;"); // <d62023>
273      ((JavaGenerator)member.generator ()).read (0, indent, "tmp", member, stream);
274    }
275    else if (mType instanceof ValueEntry || mType instanceof ValueBoxEntry)
276      stream.println (indent + Util.javaQualifiedName (mType) + " tmp = (" +
277                      Util.javaQualifiedName (mType) + ") ((org.omg.CORBA_2_3.portable.InputStream)istream).read_value (" + Util.helperName (mType, true) + ".get_instance ());"); // <d60929> // <d61056>
278    else
279      stream.println (indent + Util.javaName (mType) + " tmp = " +
280                      Util.helperName ( mType, true ) + ".read (istream);"); // <d61056>
281    if (mType instanceof PrimitiveEntry)
282      stream.println (indent + "return new " + entryName + " (tmp);");
283    else
284      stream.println (indent + "return tmp;");
285  } // helperRead
286
287  public void helperWrite (SymtabEntry entry, PrintWriter stream)
288  {
289    // <d59418 - KLR> per Simon, make "static" write call istream.write_value.
290    //              put real marshalling code in write_value.
291    stream.println ("    ((org.omg.CORBA_2_3.portable.OutputStream) ostream).write_value (value, get_instance());"); // <d60929>
292    stream.println ("  }");
293    stream.println ();
294
295    // done with "write", now do "write_value with real marshalling code.
296
297    stream.println ("  public void write_value (org.omg.CORBA.portable.OutputStream ostream, java.io.Serializable obj)"); // <d60929>
298    stream.println ("  {");
299
300    String entryName = Util.javaName(entry);
301    stream.println ("    " + entryName + " value  = (" + entryName + ") obj;");
302    write (0, "    ", "value", entry, stream);
303  } // helperWrite
304
305  public int write (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
306  {
307    Vector vMembers = ( (ValueEntry) entry ).state ();
308    TypedefEntry member = ((InterfaceState) vMembers.elementAt (0)).entry;
309    SymtabEntry mType = member.type ();
310
311    if (mType instanceof PrimitiveEntry || !member.arrayInfo ().isEmpty ())
312      index = ((JavaGenerator)member.generator ()).write (index, indent, name + ".value", member, stream);
313    else if (mType instanceof SequenceEntry || mType instanceof StringEntry || mType instanceof TypedefEntry || !member.arrayInfo ().isEmpty ())
314      index = ((JavaGenerator)member.generator ()).write (index, indent, name, member, stream);
315    else if (mType instanceof ValueEntry || mType instanceof ValueBoxEntry)
316      stream.println (indent
317                      + "((org.omg.CORBA_2_3.portable.OutputStream)ostream).write_value ((java.io.Serializable) value, " // <d60929>
318                      +  Util.helperName (mType, true)  // <d61056>
319                      + ".get_instance ());"); // <d61056>
320    else
321      stream.println (indent + Util.helperName (mType, true) + ".write (ostream, " + name + ");"); // <d61056>
322    return index;
323  } // write
324
325  protected void writeAbstract ()
326  {
327  } // writeAbstract
328
329  protected void streamableRead (String entryName, SymtabEntry entry, PrintWriter stream)
330  {
331    Vector vMembers = ( (ValueBoxEntry) entry ).state ();
332    TypedefEntry member = ((InterfaceState) vMembers.elementAt (0)).entry;
333    SymtabEntry mType = member.type ();
334    if (mType instanceof PrimitiveEntry || mType instanceof SequenceEntry || mType instanceof TypedefEntry ||
335        mType instanceof StringEntry || !member.arrayInfo ().isEmpty ())
336    {
337      SymtabEntry mEntry = (SymtabEntry) ((InterfaceState) vMembers.elementAt (0)).entry;
338      ((JavaGenerator)member.generator ()).read (0, "    ", entryName + ".value", member, stream);
339    }
340    else if (mType instanceof ValueEntry || mType instanceof ValueBoxEntry)
341      stream.println ("    " + entryName + ".value = (" + Util.javaQualifiedName (mType) + ") ((org.omg.CORBA_2_3.portable.InputStream)istream).read_value (" + Util.helperName(mType, true) + ".get_instance ());"); // <d60929> // <d61056>
342    else
343      stream.println ("    " + entryName + ".value = " + Util.helperName (mType, true) + ".read (istream);"); // <d61056>
344  } // streamableRead
345
346  protected Hashtable  symbolTable = null;
347  protected ValueBoxEntry v = null;
348  protected PrintWriter stream = null;
349} // class ValueBoxGen
350