src/share/classes/com/sun/tools/corba/se/idl/toJavaPortable/Stub.java

changeset 0
7ef37b2cdcad
child 748
6845b95cba6b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/corba/se/idl/toJavaPortable/Stub.java	Wed Apr 27 01:21:28 2016 +0800
     1.3 @@ -0,0 +1,380 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +/*
    1.29 + * COMPONENT_NAME: idl.toJava
    1.30 + *
    1.31 + * ORIGINS: 27
    1.32 + *
    1.33 + * Licensed Materials - Property of IBM
    1.34 + * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
    1.35 + * RMI-IIOP v1.0
    1.36 + *
    1.37 + */
    1.38 +
    1.39 +package com.sun.tools.corba.se.idl.toJavaPortable;
    1.40 +
    1.41 +// NOTES:
    1.42 +
    1.43 +import java.io.File;
    1.44 +import java.io.PrintWriter;
    1.45 +import java.util.Enumeration;
    1.46 +import java.util.Hashtable;
    1.47 +import java.util.Vector;
    1.48 +
    1.49 +import com.sun.tools.corba.se.idl.AttributeEntry;
    1.50 +import com.sun.tools.corba.se.idl.GenFileStream;
    1.51 +import com.sun.tools.corba.se.idl.InterfaceEntry;
    1.52 +import com.sun.tools.corba.se.idl.MethodEntry;
    1.53 +import com.sun.tools.corba.se.idl.SymtabEntry;
    1.54 +
    1.55 +/**
    1.56 + *
    1.57 + **/
    1.58 +public class Stub implements AuxGen
    1.59 +{
    1.60 +  /**
    1.61 +   * Public zero-argument constructor.
    1.62 +   **/
    1.63 +  public Stub ()
    1.64 +  {
    1.65 +  } // ctor
    1.66 +
    1.67 +  /**
    1.68 +   *
    1.69 +   **/
    1.70 +  public void generate (Hashtable symbolTable, SymtabEntry entry)
    1.71 +  {
    1.72 +    this.symbolTable = symbolTable;
    1.73 +    this.i           = (InterfaceEntry)entry;
    1.74 +    this.localStub   = i.isLocalServant();
    1.75 +    this.isAbstract  = i.isAbstract( );
    1.76 +    init ();
    1.77 +
    1.78 +    openStream ();
    1.79 +    if (stream == null)
    1.80 +      return;
    1.81 +    writeHeading ();
    1.82 +    writeBody ();
    1.83 +    writeClosing ();
    1.84 +    closeStream ();
    1.85 +  } // generate
    1.86 +
    1.87 +  /**
    1.88 +   * Initialize unique members of this generator.
    1.89 +   **/
    1.90 +  protected void init ()
    1.91 +  {
    1.92 +    classSuffix = "Stub";
    1.93 +  } // init
    1.94 +
    1.95 +  /**
    1.96 +   *
    1.97 +   **/
    1.98 +  protected void openStream ()
    1.99 +  {
   1.100 +    String name = '_' + i.name () + classSuffix;
   1.101 +    String pkg = Util.containerFullName (i.container ());
   1.102 +    if (pkg != null && !pkg.equals (""))
   1.103 +    {
   1.104 +      Util.mkdir (pkg);
   1.105 +      name = pkg + '/' + name;
   1.106 +    }
   1.107 +    stream = Util.getStream (name.replace ('/', File.separatorChar) + ".java", i);
   1.108 +  } // openStream
   1.109 +
   1.110 +  /**
   1.111 +   *
   1.112 +   **/
   1.113 +  protected void writeHeading ()
   1.114 +  {
   1.115 +    Util.writePackage (stream, i, Util.StubFile);
   1.116 +    Util.writeProlog (stream, ((GenFileStream)stream).name ());
   1.117 +
   1.118 +    // Transfer interface comment to target <31jul1997>.
   1.119 +    if (i.comment () != null)
   1.120 +      i.comment ().generate ("", stream);
   1.121 +
   1.122 +    writeClassDeclaration ();
   1.123 +    stream.println ('{');
   1.124 +  } // writeHeading
   1.125 +
   1.126 +  /**
   1.127 +   *
   1.128 +   **/
   1.129 +  protected void writeClassDeclaration ()
   1.130 +  {
   1.131 +    stream.print ("public class _" + i.name () + classSuffix + " extends org.omg.CORBA.portable.ObjectImpl");
   1.132 +    stream.println (" implements " + Util.javaName (i));
   1.133 +  } // writeClassDeclaration
   1.134 +
   1.135 +  /**
   1.136 +   * Steps done within writeBody include:
   1.137 +   * 1.)  makeCtors ();
   1.138 +   * 2.)  buildMethodList ();
   1.139 +   * 3.)  makeMethods ();
   1.140 +   * 4.)  makeCORBAObjectMethods ()
   1.141 +   **/
   1.142 +  protected void writeBody ()
   1.143 +  {
   1.144 +    writeCtors ();
   1.145 +    buildMethodList ();
   1.146 +    writeMethods ();
   1.147 +    writeCORBAObjectMethods ();
   1.148 +    writeSerializationMethods ();
   1.149 +  } // writeBody
   1.150 +
   1.151 +  /**
   1.152 +   *
   1.153 +   **/
   1.154 +  protected void writeClosing ()
   1.155 +  {
   1.156 +    stream.println ("} // class _" + i.name () + classSuffix);
   1.157 +  } // writeClosing
   1.158 +
   1.159 +  /**
   1.160 +   *
   1.161 +   **/
   1.162 +  protected void closeStream ()
   1.163 +  {
   1.164 +    stream.close ();
   1.165 +  } // closeStream
   1.166 +
   1.167 +  /**
   1.168 +   *
   1.169 +   **/
   1.170 +  protected void writeCtors ()
   1.171 +  {
   1.172 +    String name = i.name ();
   1.173 +
   1.174 +    /***  the constructors are not generated as per ptc/00-01-08.pdf
   1.175 +     *    since these are non-standard APIs, and same can be accomplished
   1.176 +     *    programatically, we need to comment this out, in order to
   1.177 +     *    be able to generate standard stubs
   1.178 +     */
   1.179 +
   1.180 +    /*************
   1.181 +    stream.println ("  // Constructors");
   1.182 +    stream.println ("  // NOTE:  If the default constructor is used, the");
   1.183 +    stream.println ("  //        object is useless until _set_delegate (...)");
   1.184 +    stream.println ("  //        is called.");
   1.185 +    stream.println ("  public _" + name + classSuffix + " ()");
   1.186 +    stream.println ("  {");
   1.187 +    stream.println ("    super ();");
   1.188 +    stream.println ("  }");
   1.189 +    stream.println ();
   1.190 +    stream.println ("  public _" + name + classSuffix + " (org.omg.CORBA.portable.Delegate delegate)");
   1.191 +    stream.println ("  {");
   1.192 +    stream.println ("    super ();");
   1.193 +    stream.println ("    _set_delegate (delegate);");
   1.194 +    stream.println ("  }");
   1.195 +    ***************/
   1.196 +    // This is confusing since we have localOptimization flag as well.
   1.197 +    // We have left this code because JCK team filed a P1 bug for changing
   1.198 +    // _opsClass to $opsClass. Will clean it up in Tiger
   1.199 +    // _REVISIT_ (Hemanth 03/05/2002)
   1.200 +    if (localStub) {
   1.201 +        stream.println ("  final public static java.lang.Class _opsClass = " +
   1.202 +            name + "Operations.class;");
   1.203 +        stream.println ();
   1.204 +    }
   1.205 +    stream.println ();
   1.206 +  } // writeCtors
   1.207 +
   1.208 +  /**
   1.209 +   * Build a list of all of the methods, keeping out duplicates.
   1.210 +   **/
   1.211 +  protected void buildMethodList ()
   1.212 +  {
   1.213 +    // Start from scratch
   1.214 +    methodList = new Vector ();
   1.215 +
   1.216 +    buildMethodList (i);
   1.217 +  } // buildMethodList
   1.218 +
   1.219 +  /**
   1.220 +   *
   1.221 +   **/
   1.222 +  private void buildMethodList (InterfaceEntry entry)
   1.223 +  {
   1.224 +    // Add the local methods
   1.225 +    Enumeration locals = entry.methods ().elements ();
   1.226 +    while (locals.hasMoreElements ())
   1.227 +      addMethod ((MethodEntry)locals.nextElement ());
   1.228 +
   1.229 +    // Add the inherited methods
   1.230 +    Enumeration parents = entry.derivedFrom ().elements ();
   1.231 +    while (parents.hasMoreElements ())
   1.232 +    {
   1.233 +      InterfaceEntry parent = (InterfaceEntry)parents.nextElement ();
   1.234 +      if (!parent.name ().equals ("Object"))
   1.235 +        buildMethodList (parent);
   1.236 +    }
   1.237 +  } // buildMethodList
   1.238 +
   1.239 +  /**
   1.240 +   *
   1.241 +   **/
   1.242 +  private void addMethod (MethodEntry method)
   1.243 +  {
   1.244 +    if (!methodList.contains (method))
   1.245 +      methodList.addElement (method);
   1.246 +  } // addMethod
   1.247 +
   1.248 +  /**
   1.249 +   *
   1.250 +   **/
   1.251 +  protected void writeMethods ()
   1.252 +  {
   1.253 +    // Count the methods, attributes which are not readonly are
   1.254 +    // counted as 2 methods.
   1.255 +    int count = methodList.size ();
   1.256 +    Enumeration e = methodList.elements ();
   1.257 +    while (e.hasMoreElements ())
   1.258 +    {
   1.259 +      Object method = e.nextElement ();
   1.260 +      if (method instanceof AttributeEntry && !((AttributeEntry)method).readOnly ())
   1.261 +        ++count;
   1.262 +    }
   1.263 +
   1.264 +    if( (((Arguments)Compile.compiler.arguments).LocalOptimization )
   1.265 +      && !isAbstract )
   1.266 +    {
   1.267 +        stream.println( "    final public static java.lang.Class _opsClass =" );
   1.268 +        stream.println( "        " + this.i.name() + "Operations.class;" );
   1.269 +    }
   1.270 +
   1.271 +    // Write the methods
   1.272 +    int realI = 0;
   1.273 +    for (int i = 0; i < methodList.size (); ++i)
   1.274 +    {
   1.275 +      MethodEntry method = (MethodEntry)methodList.elementAt (i);
   1.276 +      if (!localStub) {
   1.277 +      ((MethodGen)method.generator ()).stub (this.i.name(), isAbstract, symbolTable, method, stream, realI);
   1.278 +      } else {
   1.279 +      ((MethodGen)method.generator ()).localstub (symbolTable, method, stream, realI, this.i);
   1.280 +      }
   1.281 +      if (method instanceof AttributeEntry && !((AttributeEntry)method).readOnly ())
   1.282 +        realI += 2;
   1.283 +      else
   1.284 +        ++realI;
   1.285 +    }
   1.286 +  } // writeMethods
   1.287 +
   1.288 +  /**
   1.289 +   *
   1.290 +   **/
   1.291 +  private void buildIDList (InterfaceEntry entry, Vector list)
   1.292 +  {
   1.293 +    if (!entry.fullName ().equals ("org/omg/CORBA/Object"))
   1.294 +    {
   1.295 +      String id = Util.stripLeadingUnderscoresFromID (entry.repositoryID ().ID ());
   1.296 +      if (!list.contains (id))
   1.297 +        list.addElement (id);
   1.298 +      Enumeration e = entry.derivedFrom ().elements ();
   1.299 +      while (e.hasMoreElements ())
   1.300 +        buildIDList ((InterfaceEntry)e.nextElement (), list);
   1.301 +    }
   1.302 +  } // buildIDList
   1.303 +
   1.304 +  /**
   1.305 +   *
   1.306 +   **/
   1.307 +  private void writeIDs ()
   1.308 +  {
   1.309 +    Vector list = new Vector ();
   1.310 +    buildIDList (i, list);
   1.311 +    Enumeration e = list.elements ();
   1.312 +    boolean first = true;
   1.313 +    while (e.hasMoreElements ())
   1.314 +    {
   1.315 +      if (first)
   1.316 +        first = false;
   1.317 +      else
   1.318 +        stream.println (", ");
   1.319 +      stream.print ("    \"" + (String)e.nextElement () + '"');
   1.320 +    }
   1.321 +  } // writeIDs
   1.322 +
   1.323 +  /**
   1.324 +   *
   1.325 +   **/
   1.326 +  protected void writeCORBAObjectMethods ()
   1.327 +  {
   1.328 +    stream.println ("  // Type-specific CORBA::Object operations");
   1.329 +    stream.println ("  private static String[] __ids = {");
   1.330 +    writeIDs ();
   1.331 +    stream.println ("};");
   1.332 +    stream.println ();
   1.333 +    stream.println ("  public String[] _ids ()");
   1.334 +    stream.println ("  {");
   1.335 +    stream.println ("    return (String[])__ids.clone ();");
   1.336 +    stream.println ("  }");
   1.337 +    stream.println ();
   1.338 +  } // writeCORBAObjectMethods
   1.339 +
   1.340 +  /**
   1.341 +   *
   1.342 +   **/
   1.343 +  protected void writeSerializationMethods ()
   1.344 +  {
   1.345 +    stream.println ("  private void readObject (java.io.ObjectInputStream s) throws java.io.IOException");
   1.346 +    stream.println ("  {");
   1.347 +    stream.println ("     String str = s.readUTF ();");
   1.348 +    stream.println ("     String[] args = null;");
   1.349 +    stream.println ("     java.util.Properties props = null;");
   1.350 +    stream.println ("     org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);");
   1.351 +    stream.println ("   try {");
   1.352 +    stream.println ("     org.omg.CORBA.Object obj = orb.string_to_object (str);");
   1.353 +    stream.println ("     org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl) obj)._get_delegate ();");
   1.354 +    stream.println ("     _set_delegate (delegate);");
   1.355 +    stream.println ("   } finally {");
   1.356 +    stream.println ("     orb.destroy() ;");
   1.357 +    stream.println ("   }");
   1.358 +    stream.println ("  }");
   1.359 +    stream.println ();
   1.360 +    stream.println ("  private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException");
   1.361 +    stream.println ("  {");
   1.362 +    stream.println ("     String[] args = null;");
   1.363 +    stream.println ("     java.util.Properties props = null;");
   1.364 +    stream.println ("     org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);");
   1.365 +    stream.println ("   try {");
   1.366 +    stream.println ("     String str = orb.object_to_string (this);");
   1.367 +    stream.println ("     s.writeUTF (str);");
   1.368 +    stream.println ("   } finally {");
   1.369 +    stream.println ("     orb.destroy() ;");
   1.370 +    stream.println ("   }");
   1.371 +    stream.println ("  }");
   1.372 +  }
   1.373 +
   1.374 +  protected Hashtable      symbolTable = null;
   1.375 +  protected InterfaceEntry i           = null;
   1.376 +  protected PrintWriter    stream      = null;
   1.377 +
   1.378 +  // Unique to this generator
   1.379 +  protected Vector         methodList  = null;
   1.380 +  protected String         classSuffix = "";
   1.381 +  protected boolean        localStub = false;
   1.382 +  private   boolean        isAbstract = false;
   1.383 +} // class Stub

mercurial