1 /*
   2  * Copyright (c) 1999, 2001, 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 
  36 package com.sun.tools.corba.se.idl.toJavaPortable;
  37 
  38 // NOTES:
  39 // -D62023   <klr> New file to implement CORBA 2.4 RTF
  40 // -D62794   <klr> Fix problem with no-arg create functions
  41 
  42 import java.io.PrintWriter;
  43 
  44 import java.util.Enumeration;
  45 import java.util.Vector;
  46 import java.util.Hashtable;
  47 
  48 import com.sun.tools.corba.se.idl.GenFileStream;
  49 import com.sun.tools.corba.se.idl.InterfaceEntry;
  50 import com.sun.tools.corba.se.idl.MethodEntry;
  51 import com.sun.tools.corba.se.idl.ParameterEntry;
  52 import com.sun.tools.corba.se.idl.SymtabEntry;
  53 import com.sun.tools.corba.se.idl.ValueEntry;
  54 import com.sun.tools.corba.se.idl.ValueBoxEntry;
  55 import com.sun.tools.corba.se.idl.TypedefEntry;
  56 import com.sun.tools.corba.se.idl.InterfaceState;
  57 import com.sun.tools.corba.se.idl.PrimitiveEntry;
  58 import com.sun.tools.corba.se.idl.StructEntry;
  59 
  60 /**
  61  *
  62  **/
  63 public class MethodGen24 extends MethodGen
  64 {
  65   /**
  66    * Public zero-argument constructor.
  67    **/
  68   public MethodGen24 ()
  69   {
  70   } // ctor
  71 
  72   /**
  73    * Print the parameter list for the factory method.
  74    * @param m The method to list parameters for
  75    * @param listTypes If try, declare the parms, otherwise just list them
  76    * @param stream The PrintWriter to print on
  77    */
  78   protected void writeParmList (MethodEntry m, boolean listTypes, PrintWriter stream) {
  79     boolean firstTime = true;
  80     Enumeration e = m.parameters ().elements ();
  81     while (e.hasMoreElements ())
  82     {
  83       if (firstTime)
  84         firstTime = false;
  85       else
  86         stream.print (", ");
  87       ParameterEntry parm = (ParameterEntry)e.nextElement ();
  88       if (listTypes) {
  89         writeParmType (parm.type (), parm.passType ());
  90         stream.print (' ');
  91       }
  92       // Print parm name
  93       stream.print (parm.name ());
  94       // end of parameter list
  95     }
  96   }
  97 
  98   /**
  99    * d62023 - write the methodEntry for a valuetype factory method into
 100    *          the Value Helper class. Contents from email from Simon,
 101    *          4/25/99.
 102    **/
 103   protected void helperFactoryMethod (Hashtable symbolTable, MethodEntry m, SymtabEntry t, PrintWriter stream)
 104   {
 105     this.symbolTable = symbolTable;
 106     this.m = m;
 107     this.stream = stream;
 108     String initializerName = m.name ();
 109     String typeName = Util.javaName (t);
 110     String factoryName = typeName + "ValueFactory";
 111 
 112     // Step 1. Print factory method decl up to parms.
 113     stream.print  ("  public static " + typeName + " " + initializerName +
 114             " (org.omg.CORBA.ORB $orb");
 115     if (!m.parameters ().isEmpty ())
 116       stream.print (", "); // <d62794>
 117 
 118     // Step 2. Print the declaration parameter list.
 119     writeParmList (m, true, stream);
 120 
 121     // Step 3. Print the body of the factory method
 122     stream.println (")");
 123     stream.println ("  {");
 124     stream.println ("    try {");
 125     stream.println ("      " + factoryName + " $factory = (" + factoryName + ")");
 126     stream.println ("          ((org.omg.CORBA_2_3.ORB) $orb).lookup_value_factory(id());");
 127     stream.print   ("      return $factory." + initializerName + " (");
 128     writeParmList (m, false, stream);
 129     stream.println (");");
 130     stream.println ("    } catch (ClassCastException $ex) {");
 131     stream.println ("      throw new org.omg.CORBA.BAD_PARAM ();");
 132     stream.println ("    }");
 133     stream.println ("  }");
 134     stream.println ();
 135   } // helperFactoryMethod
 136 
 137   /**
 138    * d62023 - write an abstract method definition
 139    **/
 140   protected void abstractMethod (Hashtable symbolTable, MethodEntry m, PrintWriter stream)
 141   {
 142     this.symbolTable = symbolTable;
 143     this.m           = m;
 144     this.stream      = stream;
 145     if (m.comment () != null)
 146       m.comment ().generate ("  ", stream);
 147     stream.print ("  ");
 148     stream.print ("public abstract ");
 149     writeMethodSignature ();
 150     stream.println (";");
 151     stream.println ();
 152   } // abstractMethod
 153 
 154   /**
 155    * d62023   - write a default factory method implementation for the
 156    *            {@code <value>DefaultFactory}. m is a methodEntry for a factory
 157    *            method contained in a non-abstract ValueEntry.
 158    **/
 159   protected void defaultFactoryMethod (Hashtable symbolTable, MethodEntry m, PrintWriter stream)
 160   {
 161     this.symbolTable = symbolTable;
 162     this.m           = m;
 163     this.stream      = stream;
 164     String typeName = m.container (). name ();
 165     stream.println ();
 166     if (m.comment () != null)
 167       m.comment ().generate ("  ", stream);
 168     stream.print   ("  public " + typeName + " " + m.name () + " (");
 169     writeParmList  (m, true, stream);
 170     stream.println (")");
 171     stream.println ("  {");
 172     stream.print   ("    return new " + typeName + "Impl (");
 173     writeParmList (m, false, stream);
 174     stream.println (");");
 175     stream.println ("  }");
 176   } // defaultFactoryMethod
 177 
 178   /**
 179    * d62023 - remove all valueInitializer junk
 180    **/
 181   protected void writeMethodSignature ()
 182   {
 183     // Step 0.  Print the return type and name.
 184     // A return type of null indicates the "void" return type. If m is a
 185     // Valuetype factory method, it has a null return type,
 186     if (m.type () == null)
 187     {
 188         // if factory method, result type is container
 189         if (isValueInitializer ())
 190             stream.print (m.container ().name ());
 191         else
 192             stream.print ("void");
 193     }
 194     else
 195     {
 196       stream.print (Util.javaName (m.type ()));
 197     }
 198     stream.print (' ' + m.name () + " (");
 199 
 200     // Step 1.  Print the parameter list.
 201     boolean firstTime = true;
 202     Enumeration e = m.parameters ().elements ();
 203     while (e.hasMoreElements ())
 204     {
 205       if (firstTime)
 206         firstTime = false;
 207       else
 208         stream.print (", ");
 209       ParameterEntry parm = (ParameterEntry)e.nextElement ();
 210 
 211       writeParmType (parm.type (), parm.passType ());
 212 
 213       // Print parm name
 214       stream.print (' ' + parm.name ());
 215     }
 216 
 217     // Step 2.  Add the context parameter if necessary.
 218     if (m.contexts ().size () > 0)
 219     {
 220       if (!firstTime)
 221         stream.print (", ");
 222       stream.print ("org.omg.CORBA.Context $context");
 223     }
 224 
 225     // Step 3.  Print the throws clause (if necessary).
 226     if (m.exceptions ().size () > 0)
 227     {
 228       stream.print (") throws ");
 229       e = m.exceptions ().elements ();
 230       firstTime = true;
 231       while (e.hasMoreElements ())
 232       {
 233         if (firstTime)
 234           firstTime = false;
 235         else
 236           stream.print (", ");
 237         stream.print (Util.javaName ((SymtabEntry)e.nextElement ()));
 238       }
 239     }
 240     else
 241       stream.print (')');
 242   } // writeMethodSignature
 243 
 244   /**
 245    * d62023 - delete method templates for valuetypes
 246    **/
 247   protected void interfaceMethod (Hashtable symbolTable, MethodEntry m, PrintWriter stream)
 248   {
 249     this.symbolTable = symbolTable;
 250     this.m           = m;
 251     this.stream      = stream;
 252     if (m.comment () != null)
 253       m.comment ().generate ("  ", stream);
 254     stream.print ("  ");
 255     writeMethodSignature ();
 256     stream.println (";");
 257   } // interfaceMethod
 258 }