Stub.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 1999, 2010, 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 40import java.io.File; 41import java.io.PrintWriter; 42import java.util.Enumeration; 43import java.util.Hashtable; 44import java.util.Vector; 45 46import com.sun.tools.corba.se.idl.AttributeEntry; 47import com.sun.tools.corba.se.idl.GenFileStream; 48import com.sun.tools.corba.se.idl.InterfaceEntry; 49import com.sun.tools.corba.se.idl.MethodEntry; 50import com.sun.tools.corba.se.idl.SymtabEntry; 51 52/** 53 * 54 **/ 55public class Stub implements AuxGen 56{ 57 /** 58 * Public zero-argument constructor. 59 **/ 60 public Stub () 61 { 62 } // ctor 63 64 /** 65 * 66 **/ 67 public void generate (Hashtable symbolTable, SymtabEntry entry) 68 { 69 this.symbolTable = symbolTable; 70 this.i = (InterfaceEntry)entry; 71 this.localStub = i.isLocalServant(); 72 this.isAbstract = i.isAbstract( ); 73 init (); 74 75 openStream (); 76 if (stream == null) 77 return; 78 writeHeading (); 79 writeBody (); 80 writeClosing (); 81 closeStream (); 82 } // generate 83 84 /** 85 * Initialize unique members of this generator. 86 **/ 87 protected void init () 88 { 89 classSuffix = "Stub"; 90 } // init 91 92 /** 93 * 94 **/ 95 protected void openStream () 96 { 97 String name = '_' + i.name () + classSuffix; 98 String pkg = Util.containerFullName (i.container ()); 99 if (pkg != null && !pkg.equals ("")) 100 { 101 Util.mkdir (pkg); 102 name = pkg + '/' + name; 103 } 104 stream = Util.getStream (name.replace ('/', File.separatorChar) + ".java", i); 105 } // openStream 106 107 /** 108 * 109 **/ 110 protected void writeHeading () 111 { 112 Util.writePackage (stream, i, Util.StubFile); 113 Util.writeProlog (stream, ((GenFileStream)stream).name ()); 114 115 // Transfer interface comment to target <31jul1997>. 116 if (i.comment () != null) 117 i.comment ().generate ("", stream); 118 119 writeClassDeclaration (); 120 stream.println ('{'); 121 } // writeHeading 122 123 /** 124 * 125 **/ 126 protected void writeClassDeclaration () 127 { 128 stream.print ("public class _" + i.name () + classSuffix + " extends org.omg.CORBA.portable.ObjectImpl"); 129 stream.println (" implements " + Util.javaName (i)); 130 } // writeClassDeclaration 131 132 /** 133 * Steps done within writeBody include: 134 * 1.) makeCtors (); 135 * 2.) buildMethodList (); 136 * 3.) makeMethods (); 137 * 4.) makeCORBAObjectMethods () 138 **/ 139 protected void writeBody () 140 { 141 writeCtors (); 142 buildMethodList (); 143 writeMethods (); 144 writeCORBAObjectMethods (); 145 writeSerializationMethods (); 146 } // writeBody 147 148 /** 149 * 150 **/ 151 protected void writeClosing () 152 { 153 stream.println ("} // class _" + i.name () + classSuffix); 154 } // writeClosing 155 156 /** 157 * 158 **/ 159 protected void closeStream () 160 { 161 stream.close (); 162 } // closeStream 163 164 /** 165 * 166 **/ 167 protected void writeCtors () 168 { 169 String name = i.name (); 170 171 /*** the constructors are not generated as per ptc/00-01-08.pdf 172 * since these are non-standard APIs, and same can be accomplished 173 * programatically, we need to comment this out, in order to 174 * be able to generate standard stubs 175 */ 176 177 /************* 178 stream.println (" // Constructors"); 179 stream.println (" // NOTE: If the default constructor is used, the"); 180 stream.println (" // object is useless until _set_delegate (...)"); 181 stream.println (" // is called."); 182 stream.println (" public _" + name + classSuffix + " ()"); 183 stream.println (" {"); 184 stream.println (" super ();"); 185 stream.println (" }"); 186 stream.println (); 187 stream.println (" public _" + name + classSuffix + " (org.omg.CORBA.portable.Delegate delegate)"); 188 stream.println (" {"); 189 stream.println (" super ();"); 190 stream.println (" _set_delegate (delegate);"); 191 stream.println (" }"); 192 ***************/ 193 // This is confusing since we have localOptimization flag as well. 194 // We have left this code because JCK team filed a P1 bug for changing 195 // _opsClass to $opsClass. Will clean it up in Tiger 196 // _REVISIT_ (Hemanth 03/05/2002) 197 if (localStub) { 198 stream.println (" final public static java.lang.Class _opsClass = " + 199 name + "Operations.class;"); 200 stream.println (); 201 } 202 stream.println (); 203 } // writeCtors 204 205 /** 206 * Build a list of all of the methods, keeping out duplicates. 207 **/ 208 protected void buildMethodList () 209 { 210 // Start from scratch 211 methodList = new Vector (); 212 213 buildMethodList (i); 214 } // buildMethodList 215 216 /** 217 * 218 **/ 219 private void buildMethodList (InterfaceEntry entry) 220 { 221 // Add the local methods 222 Enumeration locals = entry.methods ().elements (); 223 while (locals.hasMoreElements ()) 224 addMethod ((MethodEntry)locals.nextElement ()); 225 226 // Add the inherited methods 227 Enumeration parents = entry.derivedFrom ().elements (); 228 while (parents.hasMoreElements ()) 229 { 230 InterfaceEntry parent = (InterfaceEntry)parents.nextElement (); 231 if (!parent.name ().equals ("Object")) 232 buildMethodList (parent); 233 } 234 } // buildMethodList 235 236 /** 237 * 238 **/ 239 private void addMethod (MethodEntry method) 240 { 241 if (!methodList.contains (method)) 242 methodList.addElement (method); 243 } // addMethod 244 245 /** 246 * 247 **/ 248 protected void writeMethods () 249 { 250 // Count the methods, attributes which are not readonly are 251 // counted as 2 methods. 252 int count = methodList.size (); 253 Enumeration e = methodList.elements (); 254 while (e.hasMoreElements ()) 255 { 256 Object method = e.nextElement (); 257 if (method instanceof AttributeEntry && !((AttributeEntry)method).readOnly ()) 258 ++count; 259 } 260 261 if( (((Arguments)Compile.compiler.arguments).LocalOptimization ) 262 && !isAbstract ) 263 { 264 stream.println( " final public static java.lang.Class _opsClass =" ); 265 stream.println( " " + this.i.name() + "Operations.class;" ); 266 } 267 268 // Write the methods 269 int realI = 0; 270 for (int i = 0; i < methodList.size (); ++i) 271 { 272 MethodEntry method = (MethodEntry)methodList.elementAt (i); 273 if (!localStub) { 274 ((MethodGen)method.generator ()).stub (this.i.name(), isAbstract, symbolTable, method, stream, realI); 275 } else { 276 ((MethodGen)method.generator ()).localstub (symbolTable, method, stream, realI, this.i); 277 } 278 if (method instanceof AttributeEntry && !((AttributeEntry)method).readOnly ()) 279 realI += 2; 280 else 281 ++realI; 282 } 283 } // writeMethods 284 285 /** 286 * 287 **/ 288 private void buildIDList (InterfaceEntry entry, Vector list) 289 { 290 if (!entry.fullName ().equals ("org/omg/CORBA/Object")) 291 { 292 String id = Util.stripLeadingUnderscoresFromID (entry.repositoryID ().ID ()); 293 if (!list.contains (id)) 294 list.addElement (id); 295 Enumeration e = entry.derivedFrom ().elements (); 296 while (e.hasMoreElements ()) 297 buildIDList ((InterfaceEntry)e.nextElement (), list); 298 } 299 } // buildIDList 300 301 /** 302 * 303 **/ 304 private void writeIDs () 305 { 306 Vector list = new Vector (); 307 buildIDList (i, list); 308 Enumeration e = list.elements (); 309 boolean first = true; 310 while (e.hasMoreElements ()) 311 { 312 if (first) 313 first = false; 314 else 315 stream.println (", "); 316 stream.print (" \"" + (String)e.nextElement () + '"'); 317 } 318 } // writeIDs 319 320 /** 321 * 322 **/ 323 protected void writeCORBAObjectMethods () 324 { 325 stream.println (" // Type-specific CORBA::Object operations"); 326 stream.println (" private static String[] __ids = {"); 327 writeIDs (); 328 stream.println ("};"); 329 stream.println (); 330 stream.println (" public String[] _ids ()"); 331 stream.println (" {"); 332 stream.println (" return (String[])__ids.clone ();"); 333 stream.println (" }"); 334 stream.println (); 335 } // writeCORBAObjectMethods 336 337 /** 338 * 339 **/ 340 protected void writeSerializationMethods () 341 { 342 stream.println (" private void readObject (java.io.ObjectInputStream s) throws java.io.IOException"); 343 stream.println (" {"); 344 stream.println (" String str = s.readUTF ();"); 345 stream.println (" String[] args = null;"); 346 stream.println (" java.util.Properties props = null;"); 347 stream.println (" org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);"); 348 stream.println (" try {"); 349 stream.println (" org.omg.CORBA.Object obj = orb.string_to_object (str);"); 350 stream.println (" org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl) obj)._get_delegate ();"); 351 stream.println (" _set_delegate (delegate);"); 352 stream.println (" } finally {"); 353 stream.println (" orb.destroy() ;"); 354 stream.println (" }"); 355 stream.println (" }"); 356 stream.println (); 357 stream.println (" private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException"); 358 stream.println (" {"); 359 stream.println (" String[] args = null;"); 360 stream.println (" java.util.Properties props = null;"); 361 stream.println (" org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);"); 362 stream.println (" try {"); 363 stream.println (" String str = orb.object_to_string (this);"); 364 stream.println (" s.writeUTF (str);"); 365 stream.println (" } finally {"); 366 stream.println (" orb.destroy() ;"); 367 stream.println (" }"); 368 stream.println (" }"); 369 } 370 371 protected Hashtable symbolTable = null; 372 protected InterfaceEntry i = null; 373 protected PrintWriter stream = null; 374 375 // Unique to this generator 376 protected Vector methodList = null; 377 protected String classSuffix = ""; 378 protected boolean localStub = false; 379 private boolean isAbstract = false; 380} // class Stub 381