Holder.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// -D61056   <klr> Use Util.helperName
40
41import java.io.PrintWriter;
42
43import com.sun.tools.corba.se.idl.GenFileStream;
44import com.sun.tools.corba.se.idl.SymtabEntry;
45import com.sun.tools.corba.se.idl.ValueBoxEntry;
46import com.sun.tools.corba.se.idl.InterfaceState;
47import com.sun.tools.corba.se.idl.TypedefEntry;
48import com.sun.tools.corba.se.idl.StringEntry;
49import com.sun.tools.corba.se.idl.PrimitiveEntry;
50
51/**
52 *
53 **/
54public class Holder implements AuxGen
55{
56  /**
57   * Public zero-argument constructor.
58   **/
59  public Holder ()
60  {
61  } // ctor
62
63  /**
64   * Generate the holder class. Provides general algorithm for
65   * auxiliary binding generation:
66   * 1.) Initialize symbol table and symbol table entry members,
67   *     common to all generators.
68   * 2.) Initialize members unique to this generator.
69   * 3.) Open print stream
70   * 4.) Write class heading (package, prologue, source comment, class
71   *     statement, open curly
72   * 5.) Write class body (member data and methods)
73   * 6.) Write class closing (close curly)
74   * 7.) Close the print stream
75   **/
76  public void generate (java.util.Hashtable symbolTable, com.sun.tools.corba.se.idl.SymtabEntry entry)
77  {
78    this.symbolTable = symbolTable;
79    this.entry       = entry;
80    init ();
81
82    openStream ();
83    if (stream == null)
84      return;
85    writeHeading ();
86    writeBody ();
87    writeClosing ();
88    closeStream ();
89  } // generate
90
91  /**
92   * Initialize variables unique to this generator.
93   **/
94  protected void init ()
95  {
96    holderClass = entry.name () + "Holder";
97    helperClass = Util.helperName (entry, true); // <d61056>
98    if (entry instanceof ValueBoxEntry)
99    {
100      ValueBoxEntry v = (ValueBoxEntry) entry;
101      TypedefEntry member = ((InterfaceState) v.state ().elementAt (0)).entry;
102      SymtabEntry mType =  member.type ();
103      holderType = Util.javaName (mType);
104    }
105    else
106      holderType = Util.javaName (entry);
107  } // init
108
109  /**
110   * Open the print stream for subsequent output.
111   **/
112  protected void openStream ()
113  {
114    stream = Util.stream (entry, "Holder.java");
115  } // openStream
116
117  /**
118   * Generate the heading, including the package, imports,
119   * source comment, class statement, and left curly.
120   **/
121  protected void writeHeading ()
122  {
123    Util.writePackage (stream, entry, Util.HolderFile);
124    Util.writeProlog (stream, stream.name ());
125    if (entry.comment () != null)
126      entry.comment ().generate ("", stream);
127    stream.println ("public final class " + holderClass + " implements org.omg.CORBA.portable.Streamable");
128    stream.println ('{');
129  } // writeHeading
130
131  /**
132   * Generate members of this class.
133   **/
134  protected void writeBody ()
135  {
136    if (entry instanceof ValueBoxEntry)
137      stream.println ("  public " + holderType + " value;");
138    else
139      Util.writeInitializer ("  public ", "value", "", entry, stream);
140    stream.println ();
141    writeCtors ();
142    writeRead ();
143    writeWrite ();
144    writeType ();
145  } // writeBody
146
147  /**
148   * Generate the closing statements.
149   **/
150  protected void writeClosing ()
151  {
152    stream.println ('}');
153  } // writeClosing
154
155  /**
156   * Write the stream to file by closing the print stream.
157   **/
158  protected void closeStream ()
159  {
160    stream.close ();
161  } // closeStream
162
163  /**
164   * Generate the constructors.
165   **/
166  protected void writeCtors ()
167  {
168    stream.println ("  public " + holderClass + " ()");
169    stream.println ("  {");
170    stream.println ("  }");
171    stream.println ();
172    stream.println ("  public " + holderClass + " (" + holderType + " initialValue)");
173    stream.println ("  {");
174    stream.println ("    value = initialValue;");
175    stream.println ("  }");
176    stream.println ();
177  } // writeCtors
178
179  /**
180   * Generate the _read method.
181   **/
182  protected void writeRead ()
183  {
184    stream.println ("  public void _read (org.omg.CORBA.portable.InputStream i)");
185    stream.println ("  {");
186    if (entry instanceof ValueBoxEntry)
187    {
188      TypedefEntry member = ((InterfaceState) ((ValueBoxEntry) entry).state ().elementAt (0)).entry;
189      SymtabEntry mType = member.type ();
190      if (mType instanceof StringEntry)
191        stream.println ("    value = i.read_string ();");
192
193      else if (mType instanceof PrimitiveEntry)
194        stream.println ("    value = " + helperClass + ".read (i).value;");
195
196      else
197        stream.println ("    value = " + helperClass + ".read (i);");
198    }
199    else
200      stream.println ("    value = " + helperClass + ".read (i);");
201    stream.println ("  }");
202    stream.println ();
203  } // writeRead
204
205  /**
206   * Generate the _write method.
207   **/
208  protected void writeWrite ()
209  {
210    stream.println ("  public void _write (org.omg.CORBA.portable.OutputStream o)");
211    stream.println ("  {");
212    if (entry instanceof ValueBoxEntry)
213    {
214      TypedefEntry member = ((InterfaceState) ((ValueBoxEntry) entry).state ().elementAt (0)).entry;
215      SymtabEntry mType = member.type ();
216      if (mType instanceof StringEntry)
217        stream.println ("    o.write_string (value);");
218
219      else if (mType instanceof PrimitiveEntry)
220      {
221        String name = entry.name ();
222        stream.println ("    " + name + " vb = new " + name + " (value);");
223        stream.println ("    " + helperClass + ".write (o, vb);");
224      }
225
226      else
227        stream.println ("    " + helperClass + ".write (o, value);");
228    }
229    else
230      stream.println ("    " + helperClass + ".write (o, value);");
231    stream.println ("  }");
232    stream.println ();
233  } // writeWrite
234
235  /**
236   * Generate the _type method.
237   **/
238  protected void writeType ()
239  {
240    stream.println ("  public org.omg.CORBA.TypeCode _type ()");
241    stream.println ("  {");
242    stream.println ("    return " + helperClass + ".type ();");
243    stream.println ("  }");
244    stream.println ();
245  } // writeType
246
247  protected java.util.Hashtable     symbolTable;
248  protected com.sun.tools.corba.se.idl.SymtabEntry entry;
249  protected GenFileStream           stream;
250
251  // Unique to this generator
252  protected String holderClass;
253  protected String helperClass;
254  protected String holderType;
255} // class Holder
256