src/share/classes/sun/rmi/rmic/iiop/StubGenerator.java

Wed, 27 Apr 2016 01:21:28 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:21:28 +0800
changeset 0
7ef37b2cdcad
child 748
6845b95cba6b
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/corba/
changeset: 765:f46df0af2ca8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 /*
aoqi@0 27 * Licensed Materials - Property of IBM
aoqi@0 28 * RMI-IIOP v1.0
aoqi@0 29 * Copyright IBM Corp. 1998 1999 All Rights Reserved
aoqi@0 30 *
aoqi@0 31 */
aoqi@0 32
aoqi@0 33 package sun.rmi.rmic.iiop;
aoqi@0 34
aoqi@0 35 import java.io.File;
aoqi@0 36 import java.io.IOException;
aoqi@0 37 import java.io.SerializablePermission;
aoqi@0 38 import java.security.AccessController;
aoqi@0 39 import java.security.PrivilegedAction;
aoqi@0 40 import java.util.Vector;
aoqi@0 41 import java.util.Hashtable;
aoqi@0 42 import java.util.Enumeration;
aoqi@0 43 import sun.tools.java.Identifier;
aoqi@0 44 import sun.tools.java.ClassNotFound;
aoqi@0 45 import sun.tools.java.ClassDefinition;
aoqi@0 46 import sun.tools.java.ClassDeclaration;
aoqi@0 47 import sun.tools.java.CompilerError;
aoqi@0 48 import sun.rmi.rmic.IndentingWriter;
aoqi@0 49 import java.util.HashSet;
aoqi@0 50 import java.util.Arrays;
aoqi@0 51 import com.sun.corba.se.impl.util.Utility;
aoqi@0 52 import com.sun.corba.se.impl.util.PackagePrefixChecker;
aoqi@0 53 import sun.rmi.rmic.Main;
aoqi@0 54
aoqi@0 55
aoqi@0 56 /**
aoqi@0 57 * An IIOP stub/tie generator for rmic.
aoqi@0 58 *
aoqi@0 59 * @author Bryan Atsatt
aoqi@0 60 * @author Anil Vijendran
aoqi@0 61 * @author M. Mortazavi
aoqi@0 62 */
aoqi@0 63
aoqi@0 64 public class StubGenerator extends sun.rmi.rmic.iiop.Generator {
aoqi@0 65
aoqi@0 66 private static final String DEFAULT_STUB_CLASS = "javax.rmi.CORBA.Stub";
aoqi@0 67 private static final String DEFAULT_TIE_CLASS = "org.omg.CORBA_2_3.portable.ObjectImpl";
aoqi@0 68 private static final String DEFAULT_POA_TIE_CLASS = "org.omg.PortableServer.Servant";
aoqi@0 69
aoqi@0 70 protected boolean reverseIDs = false;
aoqi@0 71 protected boolean localStubs = true;
aoqi@0 72 protected boolean standardPackage = false;
aoqi@0 73 protected boolean useHash = true;
aoqi@0 74 protected String stubBaseClass = DEFAULT_STUB_CLASS;
aoqi@0 75 protected String tieBaseClass = DEFAULT_TIE_CLASS;
aoqi@0 76 protected HashSet namesInUse = new HashSet();
aoqi@0 77 protected Hashtable classesInUse = new Hashtable();
aoqi@0 78 protected Hashtable imports = new Hashtable();
aoqi@0 79 protected int importCount = 0;
aoqi@0 80 protected String currentPackage = null;
aoqi@0 81 protected String currentClass = null;
aoqi@0 82 protected boolean castArray = false;
aoqi@0 83 protected Hashtable transactionalObjects = new Hashtable() ;
aoqi@0 84 protected boolean POATie = false ;
aoqi@0 85 protected boolean emitPermissionCheck = false;
aoqi@0 86
aoqi@0 87 /**
aoqi@0 88 * Default constructor for Main to use.
aoqi@0 89 */
aoqi@0 90 public StubGenerator() {
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 /**
aoqi@0 94 * Overridden in order to set the standardPackage flag.
aoqi@0 95 */
aoqi@0 96 public void generate(
aoqi@0 97 sun.rmi.rmic.BatchEnvironment env,
aoqi@0 98 ClassDefinition cdef, File destDir) {
aoqi@0 99 ((sun.rmi.rmic.iiop.BatchEnvironment)env).
aoqi@0 100 setStandardPackage(standardPackage);
aoqi@0 101 super.generate(env, cdef, destDir);
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 /**
aoqi@0 105 * Return true if a new instance should be created for each
aoqi@0 106 * class on the command line. Subclasses which return true
aoqi@0 107 * should override newInstance() to return an appropriately
aoqi@0 108 * constructed instance.
aoqi@0 109 */
aoqi@0 110 protected boolean requireNewInstance() {
aoqi@0 111 return false;
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 /**
aoqi@0 115 * Return true if non-conforming types should be parsed.
aoqi@0 116 * @param stack The context stack.
aoqi@0 117 */
aoqi@0 118 protected boolean parseNonConforming(ContextStack stack) {
aoqi@0 119
aoqi@0 120 // We let the environment setting decide so that
aoqi@0 121 // another generator (e.g. IDLGenerator) can change
aoqi@0 122 // it and we will just go with the flow...
aoqi@0 123
aoqi@0 124 return stack.getEnv().getParseNonConforming();
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 /**
aoqi@0 128 * Create and return a top-level type.
aoqi@0 129 * @param cdef The top-level class definition.
aoqi@0 130 * @param stack The context stack.
aoqi@0 131 * @return The compound type or null if is non-conforming.
aoqi@0 132 */
aoqi@0 133 protected CompoundType getTopType(ClassDefinition cdef, ContextStack stack) {
aoqi@0 134
aoqi@0 135 CompoundType result = null;
aoqi@0 136
aoqi@0 137 // Do we have an interface?
aoqi@0 138
aoqi@0 139 if (cdef.isInterface()) {
aoqi@0 140
aoqi@0 141 // Yes, so first try Abstract...
aoqi@0 142
aoqi@0 143 result = AbstractType.forAbstract(cdef,stack,true);
aoqi@0 144
aoqi@0 145 if (result == null) {
aoqi@0 146
aoqi@0 147 // Then try Remote...
aoqi@0 148
aoqi@0 149 result = RemoteType.forRemote(cdef,stack,false);
aoqi@0 150 }
aoqi@0 151 } else {
aoqi@0 152
aoqi@0 153 // Not an interface, so try Implementation...
aoqi@0 154
aoqi@0 155 result = ImplementationType.forImplementation(cdef,stack,false);
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 return result;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 /**
aoqi@0 162 * Examine and consume command line arguments.
aoqi@0 163 * @param argv The command line arguments. Ignore null
aoqi@0 164 * and unknown arguments. Set each consumed argument to null.
aoqi@0 165 * @param error Report any errors using the main.error() methods.
aoqi@0 166 * @return true if no errors, false otherwise.
aoqi@0 167 */
aoqi@0 168 public boolean parseArgs(String argv[], Main main) {
aoqi@0 169 Object marker = new Object() ;
aoqi@0 170
aoqi@0 171 // Reset any cached options...
aoqi@0 172
aoqi@0 173 reverseIDs = false;
aoqi@0 174 localStubs = true;
aoqi@0 175 useHash = true;
aoqi@0 176 stubBaseClass = DEFAULT_STUB_CLASS;
aoqi@0 177 // tieBaseClass = DEFAULT_TIE_CLASS;
aoqi@0 178 transactionalObjects = new Hashtable() ;
aoqi@0 179
aoqi@0 180 // Parse options...
aoqi@0 181
aoqi@0 182 boolean result = super.parseArgs(argv,main);
aoqi@0 183 if (result) {
aoqi@0 184 for (int i = 0; i < argv.length; i++) {
aoqi@0 185 if (argv[i] != null) {
aoqi@0 186 String arg = argv[i].toLowerCase();
aoqi@0 187 if (arg.equals("-iiop")) {
aoqi@0 188 argv[i] = null;
aoqi@0 189 } else if (arg.equals("-xreverseids")) {
aoqi@0 190 reverseIDs = true;
aoqi@0 191 argv[i] = null;
aoqi@0 192 } else if (arg.equals("-nolocalstubs")) {
aoqi@0 193 localStubs = false;
aoqi@0 194 argv[i] = null;
aoqi@0 195 } else if (arg.equals("-xnohash")) {
aoqi@0 196 useHash = false;
aoqi@0 197 argv[i] = null;
aoqi@0 198 } else if (argv[i].equals("-standardPackage")) {
aoqi@0 199 standardPackage = true;
aoqi@0 200 argv[i] = null;
aoqi@0 201 } else if (argv[i].equals("-emitPermissionCheck")) {
aoqi@0 202 emitPermissionCheck = true;
aoqi@0 203 argv[i] = null;
aoqi@0 204 } else if (arg.equals("-xstubbase")) {
aoqi@0 205 argv[i] = null;
aoqi@0 206 if (++i < argv.length && argv[i] != null && !argv[i].startsWith("-")) {
aoqi@0 207 stubBaseClass = argv[i];
aoqi@0 208 argv[i] = null;
aoqi@0 209 } else {
aoqi@0 210 main.error("rmic.option.requires.argument", "-Xstubbase");
aoqi@0 211 result = false;
aoqi@0 212 }
aoqi@0 213 } else if (arg.equals("-xtiebase")) {
aoqi@0 214 argv[i] = null;
aoqi@0 215 if (++i < argv.length && argv[i] != null && !argv[i].startsWith("-")) {
aoqi@0 216 tieBaseClass = argv[i];
aoqi@0 217 argv[i] = null;
aoqi@0 218 } else {
aoqi@0 219 main.error("rmic.option.requires.argument", "-Xtiebase");
aoqi@0 220 result = false;
aoqi@0 221 }
aoqi@0 222 } else if (arg.equals("-transactional" )) {
aoqi@0 223 // Scan for the next non-flag argument.
aoqi@0 224 // Assume that it is a class name and add it
aoqi@0 225 // to the list of transactional classes.
aoqi@0 226 for ( int ctr=i+1; ctr<argv.length; ctr++ ) {
aoqi@0 227 if (argv[ctr].charAt(1) != '-') {
aoqi@0 228 transactionalObjects.put( argv[ctr], marker ) ;
aoqi@0 229 break ;
aoqi@0 230 }
aoqi@0 231 }
aoqi@0 232 argv[i] = null;
aoqi@0 233 } else if (arg.equals( "-poa" )) {
aoqi@0 234 POATie = true ;
aoqi@0 235 argv[i] = null;
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239 }
aoqi@0 240 if(POATie){
aoqi@0 241 tieBaseClass = DEFAULT_POA_TIE_CLASS;
aoqi@0 242 } else {
aoqi@0 243 tieBaseClass = DEFAULT_TIE_CLASS;
aoqi@0 244 }
aoqi@0 245 return result;
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 /**
aoqi@0 249 * Return an array containing all the file names and types that need to be
aoqi@0 250 * generated for the given top-level type. The file names must NOT have an
aoqi@0 251 * extension (e.g. ".java").
aoqi@0 252 * @param topType The type returned by getTopType().
aoqi@0 253 * @param alreadyChecked A set of Types which have already been checked.
aoqi@0 254 * Intended to be passed to topType.collectMatching(filter,alreadyChecked).
aoqi@0 255 */
aoqi@0 256 protected OutputType[] getOutputTypesFor(CompoundType topType,
aoqi@0 257 HashSet alreadyChecked) {
aoqi@0 258
aoqi@0 259 // We want to generate stubs for all remote and implementation types,
aoqi@0 260 // so collect them up.
aoqi@0 261 //
aoqi@0 262 // We use the form of collectMatching which allows us to pass in a set of
aoqi@0 263 // types which have previously been checked. By doing so, we ensure that if
aoqi@0 264 // the command line contains Hello and HelloImpl, we will only generate
aoqi@0 265 // output for Hello once...
aoqi@0 266
aoqi@0 267 int filter = TYPE_REMOTE | TYPE_IMPLEMENTATION;
aoqi@0 268 Type[] genTypes = topType.collectMatching(filter,alreadyChecked);
aoqi@0 269 int count = genTypes.length;
aoqi@0 270 Vector list = new Vector(count+5);
aoqi@0 271 BatchEnvironment theEnv = topType.getEnv();
aoqi@0 272
aoqi@0 273 // Now walk all types...
aoqi@0 274
aoqi@0 275 for (int i = 0; i < genTypes.length; i++) {
aoqi@0 276
aoqi@0 277 Type type = genTypes[i];
aoqi@0 278 String typeName = type.getName();
aoqi@0 279 boolean createStub = true;
aoqi@0 280
aoqi@0 281 // Is it an implementation type?
aoqi@0 282
aoqi@0 283 if (type instanceof ImplementationType) {
aoqi@0 284
aoqi@0 285 // Yes, so add a tie for it...
aoqi@0 286
aoqi@0 287 list.addElement(new OutputType(Utility.tieNameForCompiler(typeName), type));
aoqi@0 288
aoqi@0 289 // Does it have more than 1 remote interface? If so, we
aoqi@0 290 // want to create a stub for it...
aoqi@0 291
aoqi@0 292 int remoteInterfaceCount = 0;
aoqi@0 293 InterfaceType[] interfaces = ((CompoundType)type).getInterfaces();
aoqi@0 294 for (int j = 0; j < interfaces.length; j++) {
aoqi@0 295 if (interfaces[j].isType(TYPE_REMOTE) &&
aoqi@0 296 !interfaces[j].isType(TYPE_ABSTRACT)) {
aoqi@0 297 remoteInterfaceCount++;
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 if (remoteInterfaceCount <= 1) {
aoqi@0 302
aoqi@0 303 // No, so do not create a stub for this type...
aoqi@0 304
aoqi@0 305 createStub = false;
aoqi@0 306 }
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 // Is it an abstract interface type?
aoqi@0 310
aoqi@0 311 if (type instanceof AbstractType) {
aoqi@0 312
aoqi@0 313 // Do not create a stub for this type...
aoqi@0 314
aoqi@0 315 createStub = false; // d11141
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 if (createStub) {
aoqi@0 319
aoqi@0 320 // Add a stub for the type...
aoqi@0 321
aoqi@0 322 list.addElement(new OutputType(Utility.stubNameForCompiler(typeName), type));
aoqi@0 323 }
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 // Copy list into array..
aoqi@0 327
aoqi@0 328 OutputType[] outputTypes = new OutputType[list.size()];
aoqi@0 329 list.copyInto(outputTypes);
aoqi@0 330 return outputTypes;
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 /**
aoqi@0 334 * Return the file name extension for the given file name (e.g. ".java").
aoqi@0 335 * All files generated with the ".java" extension will be compiled. To
aoqi@0 336 * change this behavior for ".java" files, override the compileJavaSourceFile
aoqi@0 337 * method to return false.
aoqi@0 338 * @param outputType One of the items returned by getOutputTypesFor(...)
aoqi@0 339 */
aoqi@0 340 protected String getFileNameExtensionFor(OutputType outputType) {
aoqi@0 341 return SOURCE_FILE_EXTENSION;
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 /**
aoqi@0 345 * Write the output for the given OutputFileName into the output stream.
aoqi@0 346 * @param name One of the items returned by getOutputTypesFor(...)
aoqi@0 347 * @param alreadyChecked A set of Types which have already been checked.
aoqi@0 348 * Intended to be passed to Type.collectMatching(filter,alreadyChecked).
aoqi@0 349 * @param writer The output stream.
aoqi@0 350 */
aoqi@0 351 protected void writeOutputFor( OutputType outputType,
aoqi@0 352 HashSet alreadyChecked,
aoqi@0 353 IndentingWriter writer) throws IOException {
aoqi@0 354
aoqi@0 355 String fileName = outputType.getName();
aoqi@0 356 CompoundType theType = (CompoundType) outputType.getType();
aoqi@0 357
aoqi@0 358 // Are we doing a Stub or Tie?
aoqi@0 359
aoqi@0 360 if (fileName.endsWith(Utility.RMI_STUB_SUFFIX)) {
aoqi@0 361
aoqi@0 362 // Stub.
aoqi@0 363
aoqi@0 364 writeStub(outputType,writer);
aoqi@0 365
aoqi@0 366 } else {
aoqi@0 367
aoqi@0 368 // Tie
aoqi@0 369
aoqi@0 370 writeTie(outputType,writer);
aoqi@0 371 }
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 /**
aoqi@0 375 * Write a stub for the specified type.
aoqi@0 376 */
aoqi@0 377 protected void writeStub(OutputType outputType,
aoqi@0 378 IndentingWriter p) throws IOException {
aoqi@0 379
aoqi@0 380 CompoundType theType = (CompoundType) outputType.getType();
aoqi@0 381 RemoteType[] remoteInterfaces = getDirectRemoteInterfaces(theType);
aoqi@0 382
aoqi@0 383 // Write comment.
aoqi@0 384
aoqi@0 385 p.pln("// Stub class generated by rmic, do not edit.");
aoqi@0 386 p.pln("// Contents subject to change without notice.");
aoqi@0 387 p.pln();
aoqi@0 388
aoqi@0 389 // Set our standard classes...
aoqi@0 390
aoqi@0 391 setStandardClassesInUse(theType,true);
aoqi@0 392
aoqi@0 393 // Add classes for this type...
aoqi@0 394
aoqi@0 395 addClassesInUse(theType,remoteInterfaces);
aoqi@0 396
aoqi@0 397 // Write package and import statements...
aoqi@0 398
aoqi@0 399 writePackageAndImports(p);
aoqi@0 400
aoqi@0 401 // generate
aoqi@0 402 // import java.security.AccessController;
aoqi@0 403 // import java.security.PrivilegedAction;
aoqi@0 404 // import java.io.SerializablePermission;
aoqi@0 405 if (emitPermissionCheck) {
aoqi@0 406 p.pln("import java.security.AccessController;");
aoqi@0 407 p.pln("import java.security.PrivilegedAction;");
aoqi@0 408 p.pln("import java.io.SerializablePermission;");
aoqi@0 409 p.pln();
aoqi@0 410 p.pln();
aoqi@0 411 }
aoqi@0 412
aoqi@0 413 // Declare the stub class; implement all remote interfaces.
aoqi@0 414
aoqi@0 415 p.p("public class " + currentClass);
aoqi@0 416
aoqi@0 417 p.p(" extends " + getName(stubBaseClass));
aoqi@0 418 p.p(" implements ");
aoqi@0 419 if (remoteInterfaces.length > 0) {
aoqi@0 420 for(int i = 0; i < remoteInterfaces.length; i++) {
aoqi@0 421 if (i > 0) {
aoqi@0 422 p.pln(",");
aoqi@0 423 }
aoqi@0 424 String objName = testUtil(getName(remoteInterfaces[i]), theType);
aoqi@0 425 p.p(objName);
aoqi@0 426 }
aoqi@0 427 }
aoqi@0 428
aoqi@0 429 // Add java.rmi.Remote if this type does not implement it.
aoqi@0 430 // This allows stubs for Abstract interfaces to be treated
aoqi@0 431 // uniformly...
aoqi@0 432
aoqi@0 433 if (!implementsRemote(theType)) {
aoqi@0 434 p.pln(",");
aoqi@0 435 p.p(getName("java.rmi.Remote"));
aoqi@0 436 }
aoqi@0 437
aoqi@0 438 p.plnI(" {");
aoqi@0 439 p.pln();
aoqi@0 440
aoqi@0 441 // Write the ids...
aoqi@0 442
aoqi@0 443 writeIds( p, theType, false );
aoqi@0 444 p.pln();
aoqi@0 445
aoqi@0 446 if (emitPermissionCheck) {
aoqi@0 447
aoqi@0 448 // produce the following generated code for example
aoqi@0 449 // private static Void checkPermission() {
aoqi@0 450 // SecurityManager sm = System.getSecurityManager();
aoqi@0 451 // if (sm != null) {
aoqi@0 452 // sm.checkPermission(new SerializablePermission(
aoqi@0 453 // "enableSubclassImplementation")); // testing
aoqi@0 454 // }
aoqi@0 455 // return null;
aoqi@0 456 // }
aoqi@0 457 //
aoqi@0 458 // private _XXXXX_Stub(Void ignore) {
aoqi@0 459 // }
aoqi@0 460 //
aoqi@0 461 // public _XXXXX_Stub() {
aoqi@0 462 // this(checkPermission());
aoqi@0 463 // }
aoqi@0 464 //
aoqi@0 465 // where XXXXX is the name of the remote interface
aoqi@0 466
aoqi@0 467 p.pln();
aoqi@0 468 p.plnI("private static Void checkPermission() {");
aoqi@0 469 p.plnI("SecurityManager sm = System.getSecurityManager();");
aoqi@0 470 p.pln("if (sm != null) {");
aoqi@0 471 p.pI();
aoqi@0 472 p.plnI("sm.checkPermission(new SerializablePermission(");
aoqi@0 473 p.plnI("\"enableSubclassImplementation\"));");
aoqi@0 474 p.pO();
aoqi@0 475 p.pO();
aoqi@0 476 p.pOln("}");
aoqi@0 477 p.pln("return null;");
aoqi@0 478 p.pO();
aoqi@0 479 p.pOln("}");
aoqi@0 480 p.pln();
aoqi@0 481 p.pO();
aoqi@0 482
aoqi@0 483 p.pI();
aoqi@0 484 p.pln("private " + currentClass + "(Void ignore) { }");
aoqi@0 485 p.pln();
aoqi@0 486
aoqi@0 487 p.plnI("public " + currentClass + "() { ");
aoqi@0 488 p.pln("this(checkPermission());");
aoqi@0 489 p.pOln("}");
aoqi@0 490 p.pln();
aoqi@0 491 }
aoqi@0 492
aoqi@0 493 if (!emitPermissionCheck) {
aoqi@0 494 p.pI();
aoqi@0 495 }
aoqi@0 496
aoqi@0 497 // Write the _ids() method...
aoqi@0 498
aoqi@0 499 p.plnI("public String[] _ids() { ");
aoqi@0 500 p.pln("return (String[]) _type_ids.clone();");
aoqi@0 501 p.pOln("}");
aoqi@0 502
aoqi@0 503 // Get all the methods and write each stub method...
aoqi@0 504
aoqi@0 505 CompoundType.Method[] remoteMethods = theType.getMethods();
aoqi@0 506 int methodCount = remoteMethods.length;
aoqi@0 507 if (methodCount > 0) {
aoqi@0 508 boolean writeHeader = true;
aoqi@0 509 for(int i = 0; i < methodCount; i++) {
aoqi@0 510 if (!remoteMethods[i].isConstructor()) {
aoqi@0 511 if (writeHeader) {
aoqi@0 512 writeHeader = false;
aoqi@0 513 }
aoqi@0 514 p.pln();
aoqi@0 515 writeStubMethod(p, remoteMethods[i], theType);
aoqi@0 516 }
aoqi@0 517 }
aoqi@0 518 }
aoqi@0 519
aoqi@0 520 // Write the cast array hack...
aoqi@0 521
aoqi@0 522 writeCastArray(p);
aoqi@0 523
aoqi@0 524 p.pOln("}"); // end stub class
aoqi@0 525 }
aoqi@0 526
aoqi@0 527 void addClassInUse(String qualifiedName) {
aoqi@0 528 String unqualifiedName = qualifiedName;
aoqi@0 529 String packageName = null;
aoqi@0 530 int index = qualifiedName.lastIndexOf('.');
aoqi@0 531 if (index > 0) {
aoqi@0 532 unqualifiedName = qualifiedName.substring(index+1);
aoqi@0 533 packageName = qualifiedName.substring(0,index);
aoqi@0 534 }
aoqi@0 535 addClassInUse(unqualifiedName,qualifiedName,packageName);
aoqi@0 536 }
aoqi@0 537
aoqi@0 538 void addClassInUse(Type type) {
aoqi@0 539 if (!type.isPrimitive()) {
aoqi@0 540 Identifier id = type.getIdentifier();
aoqi@0 541 String name = IDLNames.replace(id.getName().toString(),". ",".");
aoqi@0 542 String packageName = type.getPackageName();
aoqi@0 543 String qualifiedName;
aoqi@0 544 if (packageName != null) {
aoqi@0 545 qualifiedName = packageName+"."+name;
aoqi@0 546 } else {
aoqi@0 547 qualifiedName = name;
aoqi@0 548 }
aoqi@0 549 addClassInUse(name,qualifiedName,packageName);
aoqi@0 550 }
aoqi@0 551 }
aoqi@0 552
aoqi@0 553 void addClassInUse(Type[] types) {
aoqi@0 554 for (int i = 0; i < types.length; i++) {
aoqi@0 555 addClassInUse(types[i]);
aoqi@0 556 }
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 void addStubInUse(Type type) {
aoqi@0 560 if (type.getIdentifier() != idCorbaObject &&
aoqi@0 561 type.isType(TYPE_CORBA_OBJECT)) {
aoqi@0 562 String stubName = getStubNameFor(type,false);
aoqi@0 563 String packageName = type.getPackageName();
aoqi@0 564 String fullName;
aoqi@0 565 if (packageName == null) {
aoqi@0 566 fullName = stubName;
aoqi@0 567 } else {
aoqi@0 568 fullName = packageName + "." + stubName;
aoqi@0 569 }
aoqi@0 570 addClassInUse(stubName,fullName,packageName);
aoqi@0 571 }
aoqi@0 572 if (type.isType(TYPE_REMOTE) ||
aoqi@0 573 type.isType(TYPE_JAVA_RMI_REMOTE)) {
aoqi@0 574 addClassInUse("javax.rmi.PortableRemoteObject");
aoqi@0 575 }
aoqi@0 576 }
aoqi@0 577
aoqi@0 578 String getStubNameFor(Type type, boolean qualified) {
aoqi@0 579 String stubName;
aoqi@0 580 String className;
aoqi@0 581 if (qualified) {
aoqi@0 582 className = type.getQualifiedName();
aoqi@0 583 } else {
aoqi@0 584 className = type.getName();
aoqi@0 585 }
aoqi@0 586 if (((CompoundType)type).isCORBAObject()) {
aoqi@0 587 stubName = Utility.idlStubName(className);
aoqi@0 588 } else {
aoqi@0 589 stubName = Utility.stubNameForCompiler(className);
aoqi@0 590 }
aoqi@0 591 return stubName;
aoqi@0 592 }
aoqi@0 593
aoqi@0 594 void addStubInUse(Type[] types) {
aoqi@0 595 for (int i = 0; i < types.length; i++) {
aoqi@0 596 addStubInUse(types[i]);
aoqi@0 597 }
aoqi@0 598 }
aoqi@0 599
aoqi@0 600 private static final String NO_IMPORT = new String();
aoqi@0 601
aoqi@0 602 void addClassInUse(String unqualifiedName, String qualifiedName, String packageName) {
aoqi@0 603
aoqi@0 604 // Have we already got an entry for this qualifiedName?
aoqi@0 605
aoqi@0 606 String currentName = (String)classesInUse.get(qualifiedName);
aoqi@0 607
aoqi@0 608 if (currentName == null) {
aoqi@0 609
aoqi@0 610 // No, never seen it before. Grab any existing import
aoqi@0 611 // name and then decide what to do...
aoqi@0 612
aoqi@0 613 String importName = (String) imports.get(unqualifiedName);
aoqi@0 614 String nameToUse = null;
aoqi@0 615
aoqi@0 616 if (packageName == null) {
aoqi@0 617
aoqi@0 618 // Default package, so doesn't matter which name to use...
aoqi@0 619
aoqi@0 620 nameToUse = unqualifiedName;
aoqi@0 621
aoqi@0 622 } else if (packageName.equals("java.lang")) {
aoqi@0 623
aoqi@0 624 // java.lang.*, so use unqualified name...
aoqi@0 625
aoqi@0 626 nameToUse = unqualifiedName;
aoqi@0 627
aoqi@0 628 // unless you want to be able to import things from the right place :--)
aoqi@0 629
aoqi@0 630 if(nameToUse.endsWith("_Stub")) nameToUse = Util.packagePrefix()+qualifiedName;
aoqi@0 631
aoqi@0 632 } else if (currentPackage != null && packageName.equals(currentPackage)) {
aoqi@0 633
aoqi@0 634 // Class in currentPackage, so use unqualified name...
aoqi@0 635
aoqi@0 636 nameToUse = unqualifiedName;
aoqi@0 637
aoqi@0 638 // Do we already have a previous import under this
aoqi@0 639 // unqualified name?
aoqi@0 640
aoqi@0 641 if (importName != null) {
aoqi@0 642
aoqi@0 643 // Yes, so we use qualifiedName...
aoqi@0 644
aoqi@0 645 nameToUse = qualifiedName;
aoqi@0 646
aoqi@0 647 }
aoqi@0 648
aoqi@0 649 } else if (importName != null) {
aoqi@0 650
aoqi@0 651 // It is in some package for which we normally
aoqi@0 652 // would import, but we have a previous import
aoqi@0 653 // under this unqualified name. We must use
aoqi@0 654 // the qualified name...
aoqi@0 655
aoqi@0 656 nameToUse = qualifiedName;
aoqi@0 657
aoqi@0 658 /*
aoqi@0 659 // Is the currentPackage the default package?
aoqi@0 660
aoqi@0 661 if (currentPackage == null) {
aoqi@0 662
aoqi@0 663 // Yes, so undo the import so that all
aoqi@0 664 // uses for this name will be qualified...
aoqi@0 665
aoqi@0 666 String old = (String)imports.remove(unqualifiedName);
aoqi@0 667 classesInUse.put(old,old);
aoqi@0 668 importCount--;
aoqi@0 669
aoqi@0 670 // Note that this name is in use but should
aoqi@0 671 // not be imported...
aoqi@0 672
aoqi@0 673 imports.put(nameToUse,NO_IMPORT);
aoqi@0 674 }
aoqi@0 675 */
aoqi@0 676 } else if (qualifiedName.equals("org.omg.CORBA.Object")) {
aoqi@0 677
aoqi@0 678 // Always qualify this quy to avoid confusion...
aoqi@0 679
aoqi@0 680 nameToUse = qualifiedName;
aoqi@0 681
aoqi@0 682 } else {
aoqi@0 683
aoqi@0 684 // Default to using unqualified name, and add
aoqi@0 685 // this guy to the imports...
aoqi@0 686
aoqi@0 687 // Check for nested class in which case we use
aoqi@0 688 // the fully qualified name instead of imports
aoqi@0 689 if (unqualifiedName.indexOf('.') != -1) {
aoqi@0 690 nameToUse = qualifiedName;
aoqi@0 691 } else {
aoqi@0 692 nameToUse = unqualifiedName;
aoqi@0 693 imports.put(unqualifiedName,qualifiedName);
aoqi@0 694 importCount++;
aoqi@0 695 }
aoqi@0 696 }
aoqi@0 697
aoqi@0 698 // Now add the name...
aoqi@0 699
aoqi@0 700 classesInUse.put(qualifiedName,nameToUse);
aoqi@0 701 }
aoqi@0 702 }
aoqi@0 703
aoqi@0 704 String getName(Type type) {
aoqi@0 705 if (type.isPrimitive()) {
aoqi@0 706 return type.getName() + type.getArrayBrackets();
aoqi@0 707 }
aoqi@0 708 Identifier id = type.getIdentifier();
aoqi@0 709 String name = IDLNames.replace(id.toString(),". ",".");
aoqi@0 710 return getName(name) + type.getArrayBrackets();
aoqi@0 711 }
aoqi@0 712
aoqi@0 713 // Added for Bug 4818753
aoqi@0 714 String getExceptionName(Type type) {
aoqi@0 715 Identifier id = type.getIdentifier();
aoqi@0 716 return IDLNames.replace(id.toString(),". ",".");
aoqi@0 717 }
aoqi@0 718
aoqi@0 719 String getName(String qualifiedName) {
aoqi@0 720 return (String)classesInUse.get(qualifiedName);
aoqi@0 721 }
aoqi@0 722
aoqi@0 723 String getName(Identifier id) {
aoqi@0 724 return getName(id.toString());
aoqi@0 725 }
aoqi@0 726
aoqi@0 727 String getStubName(Type type) {
aoqi@0 728 String stubName = getStubNameFor(type,true);
aoqi@0 729 return getName(stubName);
aoqi@0 730 }
aoqi@0 731
aoqi@0 732 void setStandardClassesInUse(CompoundType type,
aoqi@0 733 boolean stub) throws IOException {
aoqi@0 734
aoqi@0 735 // Reset our state...
aoqi@0 736
aoqi@0 737 currentPackage = type.getPackageName();
aoqi@0 738 imports.clear();
aoqi@0 739 classesInUse.clear();
aoqi@0 740 namesInUse.clear();
aoqi@0 741 importCount = 0;
aoqi@0 742 castArray = false;
aoqi@0 743
aoqi@0 744 // Add the top-level type...
aoqi@0 745
aoqi@0 746 addClassInUse(type);
aoqi@0 747
aoqi@0 748 // Set current class name...
aoqi@0 749
aoqi@0 750 if (stub) {
aoqi@0 751 currentClass = Utility.stubNameForCompiler(type.getName());
aoqi@0 752 } else {
aoqi@0 753 currentClass = Utility.tieNameForCompiler(type.getName());
aoqi@0 754 }
aoqi@0 755
aoqi@0 756 // Add current class...
aoqi@0 757
aoqi@0 758 if (currentPackage == null) {
aoqi@0 759 addClassInUse(currentClass,currentClass,currentPackage);
aoqi@0 760 } else {
aoqi@0 761 addClassInUse(currentClass,(currentPackage+"."+currentClass),currentPackage);
aoqi@0 762 }
aoqi@0 763
aoqi@0 764 // Add standard classes...
aoqi@0 765
aoqi@0 766 addClassInUse("javax.rmi.CORBA.Util");
aoqi@0 767 addClassInUse(idRemote.toString());
aoqi@0 768 addClassInUse(idRemoteException.toString());
aoqi@0 769 addClassInUse(idOutputStream.toString());
aoqi@0 770 addClassInUse(idInputStream.toString());
aoqi@0 771 addClassInUse(idSystemException.toString());
aoqi@0 772 addClassInUse(idJavaIoSerializable.toString());
aoqi@0 773 addClassInUse(idCorbaORB.toString());
aoqi@0 774 addClassInUse(idReplyHandler.toString());
aoqi@0 775
aoqi@0 776 // Add stub/tie specific imports...
aoqi@0 777
aoqi@0 778 if (stub) {
aoqi@0 779 addClassInUse(stubBaseClass);
aoqi@0 780 addClassInUse("java.rmi.UnexpectedException");
aoqi@0 781 addClassInUse(idRemarshalException.toString());
aoqi@0 782 addClassInUse(idApplicationException.toString());
aoqi@0 783 if (localStubs) {
aoqi@0 784 addClassInUse("org.omg.CORBA.portable.ServantObject");
aoqi@0 785 }
aoqi@0 786 } else {
aoqi@0 787 addClassInUse(type);
aoqi@0 788 addClassInUse(tieBaseClass);
aoqi@0 789 addClassInUse(idTieInterface.toString());
aoqi@0 790 addClassInUse(idBadMethodException.toString());
aoqi@0 791 addClassInUse(idPortableUnknownException.toString());
aoqi@0 792 addClassInUse(idJavaLangThrowable.toString());
aoqi@0 793 }
aoqi@0 794 }
aoqi@0 795
aoqi@0 796 void addClassesInUse(CompoundType type, RemoteType[] interfaces) {
aoqi@0 797
aoqi@0 798 // Walk all methods and add types in use...
aoqi@0 799
aoqi@0 800 CompoundType.Method[] methods = type.getMethods();
aoqi@0 801 for (int i = 0; i < methods.length; i++) {
aoqi@0 802 addClassInUse(methods[i].getReturnType());
aoqi@0 803 addStubInUse(methods[i].getReturnType());
aoqi@0 804 addClassInUse(methods[i].getArguments());
aoqi@0 805 addStubInUse(methods[i].getArguments());
aoqi@0 806 addClassInUse(methods[i].getExceptions());
aoqi@0 807 // bug 4473859: Also include narrower subtypes for use
aoqi@0 808 addClassInUse(methods[i].getImplExceptions());
aoqi@0 809 }
aoqi@0 810
aoqi@0 811 // If this is a stub, add all interfaces...
aoqi@0 812
aoqi@0 813 if (interfaces != null) {
aoqi@0 814 addClassInUse(interfaces);
aoqi@0 815 }
aoqi@0 816 }
aoqi@0 817
aoqi@0 818 void writePackageAndImports(IndentingWriter p) throws IOException {
aoqi@0 819
aoqi@0 820 // Write package declaration...
aoqi@0 821
aoqi@0 822 if (currentPackage != null) {
aoqi@0 823 p.pln("package " +
aoqi@0 824 Util.correctPackageName(
aoqi@0 825 currentPackage, false, standardPackage)
aoqi@0 826 + ";");
aoqi@0 827 p.pln();
aoqi@0 828 }
aoqi@0 829
aoqi@0 830 // Get imports into an array and sort them...
aoqi@0 831
aoqi@0 832 String[] names = new String[importCount];
aoqi@0 833 int index = 0;
aoqi@0 834 for (Enumeration e = imports.elements() ; e.hasMoreElements() ;) {
aoqi@0 835 String it = (String) e.nextElement();
aoqi@0 836 if (it != NO_IMPORT) {
aoqi@0 837 names[index++] = it;
aoqi@0 838 }
aoqi@0 839 }
aoqi@0 840
aoqi@0 841 Arrays.sort(names,new StringComparator());
aoqi@0 842
aoqi@0 843 // Now dump them out...
aoqi@0 844
aoqi@0 845 for (int i = 0; i < importCount; i++) {
aoqi@0 846 if(
aoqi@0 847 Util.isOffendingPackage(names[i])
aoqi@0 848 && names[i].endsWith("_Stub")
aoqi@0 849 && String.valueOf(names[i].charAt(names[i].lastIndexOf(".")+1)).equals("_")
aoqi@0 850 ){
aoqi@0 851 p.pln("import " + PackagePrefixChecker.packagePrefix()+names[i]+";");
aoqi@0 852 } else{
aoqi@0 853 p.pln("import " + names[i] + ";");
aoqi@0 854 }
aoqi@0 855 }
aoqi@0 856 p.pln();
aoqi@0 857
aoqi@0 858 // Include offending packages . . .
aoqi@0 859 if ( currentPackage!=null && Util.isOffendingPackage(currentPackage) ){
aoqi@0 860 p.pln("import " + currentPackage +".* ;");
aoqi@0 861 }
aoqi@0 862 p.pln();
aoqi@0 863
aoqi@0 864 }
aoqi@0 865
aoqi@0 866 boolean implementsRemote(CompoundType theType) {
aoqi@0 867 boolean result = theType.isType(TYPE_REMOTE) && !theType.isType(TYPE_ABSTRACT);
aoqi@0 868
aoqi@0 869 // If theType is not remote, look at all the interfaces
aoqi@0 870 // until we find one that is...
aoqi@0 871
aoqi@0 872 if (!result) {
aoqi@0 873 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 874 for (int i = 0; i < interfaces.length; i++) {
aoqi@0 875 result = implementsRemote(interfaces[i]);
aoqi@0 876 if (result) {
aoqi@0 877 break;
aoqi@0 878 }
aoqi@0 879 }
aoqi@0 880 }
aoqi@0 881
aoqi@0 882 return result;
aoqi@0 883 }
aoqi@0 884
aoqi@0 885 void writeStubMethod ( IndentingWriter p,
aoqi@0 886 CompoundType.Method method,
aoqi@0 887 CompoundType theType) throws IOException {
aoqi@0 888
aoqi@0 889 // Wtite the method declaration and opening brace...
aoqi@0 890 String methodName = method.getName();
aoqi@0 891 String methodIDLName = method.getIDLName();
aoqi@0 892
aoqi@0 893 Type paramTypes[] = method.getArguments();
aoqi@0 894 String paramNames[] = method.getArgumentNames();
aoqi@0 895 Type returnType = method.getReturnType();
aoqi@0 896 ValueType[] exceptions = getStubExceptions(method,false);
aoqi@0 897
aoqi@0 898 addNamesInUse(method);
aoqi@0 899 addNameInUse("_type_ids");
aoqi@0 900
aoqi@0 901 String objName = testUtil(getName(returnType), returnType);
aoqi@0 902 p.p("public " + objName + " " + methodName + "(");
aoqi@0 903 for(int i = 0; i < paramTypes.length; i++) {
aoqi@0 904 if (i > 0)
aoqi@0 905 p.p(", ");
aoqi@0 906 p.p(getName(paramTypes[i]) + " " + paramNames[i]);
aoqi@0 907 }
aoqi@0 908
aoqi@0 909 p.p(")");
aoqi@0 910 if (exceptions.length > 0) {
aoqi@0 911 p.p(" throws ");
aoqi@0 912 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 913 if (i > 0) {
aoqi@0 914 p.p(", ");
aoqi@0 915 }
aoqi@0 916 // Added for Bug 4818753
aoqi@0 917 p.p(getExceptionName(exceptions[i]));
aoqi@0 918 }
aoqi@0 919 }
aoqi@0 920
aoqi@0 921 p.plnI(" {");
aoqi@0 922
aoqi@0 923 // Now create the method body...
aoqi@0 924
aoqi@0 925 if (localStubs) {
aoqi@0 926 writeLocalStubMethodBody(p,method,theType);
aoqi@0 927 } else {
aoqi@0 928 writeNonLocalStubMethodBody(p,method,theType);
aoqi@0 929 }
aoqi@0 930
aoqi@0 931 // Close out the method...
aoqi@0 932
aoqi@0 933 p.pOln("}");
aoqi@0 934 }
aoqi@0 935
aoqi@0 936
aoqi@0 937 void writeLocalStubMethodBody (IndentingWriter p,
aoqi@0 938 CompoundType.Method method,
aoqi@0 939 CompoundType theType) throws IOException {
aoqi@0 940
aoqi@0 941 String objName;
aoqi@0 942 String paramNames[] = method.getArgumentNames();
aoqi@0 943 Type returnType = method.getReturnType();
aoqi@0 944 ValueType[] exceptions = getStubExceptions(method,false);
aoqi@0 945 String methodName = method.getName();
aoqi@0 946 String methodIDLName = method.getIDLName();
aoqi@0 947
aoqi@0 948 p.plnI("if (!Util.isLocal(this)) {");
aoqi@0 949 writeNonLocalStubMethodBody(p,method,theType);
aoqi@0 950 p.pOlnI("} else {");
aoqi@0 951 String so = getVariableName("so");
aoqi@0 952
aoqi@0 953 p.pln("ServantObject "+so+" = _servant_preinvoke(\""+methodIDLName+"\","+getName(theType)+".class);");
aoqi@0 954 p.plnI("if ("+so+" == null) {");
aoqi@0 955 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 956 p.p("return ");
aoqi@0 957 }
aoqi@0 958 p.p(methodName+"(");
aoqi@0 959 for (int i = 0; i < paramNames.length; i++) {
aoqi@0 960 if (i > 0)
aoqi@0 961 p.p(", ");
aoqi@0 962 p.p(paramNames[i]);
aoqi@0 963 }
aoqi@0 964 p.pln(");");
aoqi@0 965 if (returnType.isType(TYPE_VOID)) {
aoqi@0 966 p.pln( "return ;" ) ;
aoqi@0 967 }
aoqi@0 968
aoqi@0 969 p.pOln("}");
aoqi@0 970 p.plnI("try {");
aoqi@0 971
aoqi@0 972 // Generate code to copy required arguments, and
aoqi@0 973 // get back the names by which all arguments are known...
aoqi@0 974
aoqi@0 975 String[] argNames = writeCopyArguments(method,p);
aoqi@0 976
aoqi@0 977 // Now write the method...
aoqi@0 978
aoqi@0 979 boolean copyReturn = mustCopy(returnType);
aoqi@0 980 String resultName = null;
aoqi@0 981 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 982 if (copyReturn) {
aoqi@0 983 resultName = getVariableName("result");
aoqi@0 984 objName = testUtil(getName(returnType), returnType);
aoqi@0 985 p.p(objName+" "+resultName + " = ");
aoqi@0 986 } else {
aoqi@0 987 p.p("return ");
aoqi@0 988 }
aoqi@0 989 }
aoqi@0 990 objName = testUtil(getName(theType), theType);
aoqi@0 991 p.p("(("+objName+")"+so+".servant)."+methodName+"(");
aoqi@0 992
aoqi@0 993 for (int i = 0; i < argNames.length; i++) {
aoqi@0 994 if (i > 0)
aoqi@0 995 p.p(", ");
aoqi@0 996 p.p(argNames[i]);
aoqi@0 997 }
aoqi@0 998
aoqi@0 999 if (copyReturn) {
aoqi@0 1000 p.pln(");");
aoqi@0 1001 objName = testUtil(getName(returnType), returnType);
aoqi@0 1002 p.pln("return ("+objName+")Util.copyObject("+resultName+",_orb());");
aoqi@0 1003 } else {
aoqi@0 1004 p.pln(");");
aoqi@0 1005 }
aoqi@0 1006
aoqi@0 1007 String e1 = getVariableName("ex");
aoqi@0 1008 String e2 = getVariableName("exCopy");
aoqi@0 1009 p.pOlnI("} catch (Throwable "+e1+") {");
aoqi@0 1010
aoqi@0 1011 p.pln("Throwable "+e2+" = (Throwable)Util.copyObject("+e1+",_orb());");
aoqi@0 1012 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 1013 if (exceptions[i].getIdentifier() != idRemoteException &&
aoqi@0 1014 exceptions[i].isType(TYPE_VALUE)) {
aoqi@0 1015 // Added for Bug 4818753
aoqi@0 1016 p.plnI("if ("+e2+" instanceof "+getExceptionName(exceptions[i])+") {");
aoqi@0 1017 p.pln("throw ("+getExceptionName(exceptions[i])+")"+e2+";");
aoqi@0 1018 p.pOln("}");
aoqi@0 1019 }
aoqi@0 1020 }
aoqi@0 1021
aoqi@0 1022 p.pln("throw Util.wrapException("+e2+");");
aoqi@0 1023 p.pOlnI("} finally {");
aoqi@0 1024 p.pln("_servant_postinvoke("+so+");");
aoqi@0 1025 p.pOln("}");
aoqi@0 1026 p.pOln("}");
aoqi@0 1027 }
aoqi@0 1028
aoqi@0 1029
aoqi@0 1030 void writeNonLocalStubMethodBody ( IndentingWriter p,
aoqi@0 1031 CompoundType.Method method,
aoqi@0 1032 CompoundType theType) throws IOException {
aoqi@0 1033
aoqi@0 1034 String methodName = method.getName();
aoqi@0 1035 String methodIDLName = method.getIDLName();
aoqi@0 1036
aoqi@0 1037 Type paramTypes[] = method.getArguments();
aoqi@0 1038 String paramNames[] = method.getArgumentNames();
aoqi@0 1039 Type returnType = method.getReturnType();
aoqi@0 1040 ValueType[] exceptions = getStubExceptions(method,true);
aoqi@0 1041
aoqi@0 1042 String in = getVariableName("in");
aoqi@0 1043 String out = getVariableName("out");
aoqi@0 1044 String ex = getVariableName("ex");
aoqi@0 1045
aoqi@0 1046 // Decide if we need to use the new streams for
aoqi@0 1047 // any of the read calls...
aoqi@0 1048
aoqi@0 1049 boolean needNewReadStreamClass = false;
aoqi@0 1050 for (int i = 0; i < exceptions.length; i++) {
aoqi@0 1051 if (exceptions[i].getIdentifier() != idRemoteException &&
aoqi@0 1052 exceptions[i].isType(TYPE_VALUE) &&
aoqi@0 1053 needNewReadStreamClass(exceptions[i])) {
aoqi@0 1054 needNewReadStreamClass = true;
aoqi@0 1055 break;
aoqi@0 1056 }
aoqi@0 1057 }
aoqi@0 1058 if (!needNewReadStreamClass) {
aoqi@0 1059 for (int i = 0; i < paramTypes.length; i++) {
aoqi@0 1060 if (needNewReadStreamClass(paramTypes[i])) {
aoqi@0 1061 needNewReadStreamClass = true;
aoqi@0 1062 break;
aoqi@0 1063 }
aoqi@0 1064 }
aoqi@0 1065 }
aoqi@0 1066 if (!needNewReadStreamClass) {
aoqi@0 1067 needNewReadStreamClass = needNewReadStreamClass(returnType);
aoqi@0 1068 }
aoqi@0 1069
aoqi@0 1070 // Decide if we need to use the new streams for
aoqi@0 1071 // any of the write calls...
aoqi@0 1072
aoqi@0 1073 boolean needNewWriteStreamClass = false;
aoqi@0 1074 for (int i = 0; i < paramTypes.length; i++) {
aoqi@0 1075 if (needNewWriteStreamClass(paramTypes[i])) {
aoqi@0 1076 needNewWriteStreamClass = true;
aoqi@0 1077 break;
aoqi@0 1078 }
aoqi@0 1079 }
aoqi@0 1080
aoqi@0 1081 // Now write the method, inserting casts where needed...
aoqi@0 1082
aoqi@0 1083 p.plnI("try {");
aoqi@0 1084 if (needNewReadStreamClass) {
aoqi@0 1085 p.pln(idExtInputStream + " "+in+" = null;");
aoqi@0 1086 } else {
aoqi@0 1087 p.pln(idInputStream + " "+in+" = null;");
aoqi@0 1088 }
aoqi@0 1089 p.plnI("try {");
aoqi@0 1090
aoqi@0 1091 String argStream = "null";
aoqi@0 1092
aoqi@0 1093 if (needNewWriteStreamClass) {
aoqi@0 1094 p.plnI(idExtOutputStream + " "+out+" = ");
aoqi@0 1095 p.pln("(" + idExtOutputStream + ")");
aoqi@0 1096 p.pln("_request(\"" + methodIDLName + "\", true);");
aoqi@0 1097 p.pO();
aoqi@0 1098 } else {
aoqi@0 1099 p.pln("OutputStream "+out+" = _request(\"" + methodIDLName + "\", true);");
aoqi@0 1100 }
aoqi@0 1101
aoqi@0 1102 if (paramTypes.length > 0) {
aoqi@0 1103 writeMarshalArguments(p, out, paramTypes, paramNames);
aoqi@0 1104 p.pln();
aoqi@0 1105 }
aoqi@0 1106 argStream = out;
aoqi@0 1107
aoqi@0 1108 if (returnType.isType(TYPE_VOID)) {
aoqi@0 1109 p.pln("_invoke(" + argStream + ");" );
aoqi@0 1110 } else {
aoqi@0 1111 if (needNewReadStreamClass) {
aoqi@0 1112 p.plnI(in+" = (" + idExtInputStream + ")_invoke(" + argStream + ");");
aoqi@0 1113 p.pO();
aoqi@0 1114 } else {
aoqi@0 1115 p.pln(in+" = _invoke(" + argStream + ");");
aoqi@0 1116 }
aoqi@0 1117 p.p("return ");
aoqi@0 1118 writeUnmarshalArgument(p, in, returnType, null);
aoqi@0 1119 p.pln();
aoqi@0 1120 }
aoqi@0 1121
aoqi@0 1122 // Handle ApplicationException...
aoqi@0 1123
aoqi@0 1124 p.pOlnI("} catch ("+getName(idApplicationException)+" "+ex+") {");
aoqi@0 1125 if (needNewReadStreamClass) {
aoqi@0 1126 p.pln(in + " = (" + idExtInputStream + ") "+ex+".getInputStream();");
aoqi@0 1127 } else {
aoqi@0 1128 p.pln(in + " = "+ex+".getInputStream();");
aoqi@0 1129 }
aoqi@0 1130
aoqi@0 1131 boolean idRead = false;
aoqi@0 1132 boolean idAllocated = false;
aoqi@0 1133 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 1134 if (exceptions[i].getIdentifier() != idRemoteException) {
aoqi@0 1135
aoqi@0 1136 // Is this our special-case IDLEntity exception?
aoqi@0 1137
aoqi@0 1138 if (exceptions[i].isIDLEntityException() && !exceptions[i].isCORBAUserException()) {
aoqi@0 1139
aoqi@0 1140 // Yes.
aoqi@0 1141
aoqi@0 1142 if (!idAllocated && !idRead) {
aoqi@0 1143 p.pln("String $_id = "+ex+".getId();");
aoqi@0 1144 idAllocated = true;
aoqi@0 1145 }
aoqi@0 1146
aoqi@0 1147 String helperName = IDLNames.replace(exceptions[i].getQualifiedIDLName(false),"::",".");
aoqi@0 1148 helperName += "Helper";
aoqi@0 1149 p.plnI("if ($_id.equals("+helperName+".id())) {");
aoqi@0 1150 p.pln("throw "+helperName+".read("+in+");");
aoqi@0 1151
aoqi@0 1152 } else {
aoqi@0 1153
aoqi@0 1154 // No.
aoqi@0 1155
aoqi@0 1156 if (!idAllocated && !idRead) {
aoqi@0 1157 p.pln("String $_id = "+in+".read_string();");
aoqi@0 1158 idAllocated = true;
aoqi@0 1159 idRead = true;
aoqi@0 1160 } else if (idAllocated && !idRead) {
aoqi@0 1161 p.pln("$_id = "+in+".read_string();");
aoqi@0 1162 idRead = true;
aoqi@0 1163 }
aoqi@0 1164 p.plnI("if ($_id.equals(\""+getExceptionRepositoryID(exceptions[i])+"\")) {");
aoqi@0 1165 // Added for Bug 4818753
aoqi@0 1166 p.pln("throw ("+getExceptionName(exceptions[i])+") "+in+".read_value(" + getExceptionName(exceptions[i]) + ".class);");
aoqi@0 1167 }
aoqi@0 1168 p.pOln("}");
aoqi@0 1169 }
aoqi@0 1170 }
aoqi@0 1171 if (!idAllocated && !idRead) {
aoqi@0 1172 p.pln("String $_id = "+in+".read_string();");
aoqi@0 1173 idAllocated = true;
aoqi@0 1174 idRead = true;
aoqi@0 1175 } else if (idAllocated && !idRead) {
aoqi@0 1176 p.pln("$_id = "+in+".read_string();");
aoqi@0 1177 idRead = true;
aoqi@0 1178 }
aoqi@0 1179 p.pln("throw new UnexpectedException($_id);");
aoqi@0 1180
aoqi@0 1181 // Handle RemarshalException...
aoqi@0 1182
aoqi@0 1183 p.pOlnI("} catch ("+getName(idRemarshalException)+" "+ex+") {");
aoqi@0 1184 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 1185 p.p("return ");
aoqi@0 1186 }
aoqi@0 1187 p.p(methodName + "(");
aoqi@0 1188 for(int i = 0; i < paramTypes.length; i++) {
aoqi@0 1189 if (i > 0) {
aoqi@0 1190 p.p(",");
aoqi@0 1191 }
aoqi@0 1192 p.p(paramNames[i]);
aoqi@0 1193 }
aoqi@0 1194 p.pln(");");
aoqi@0 1195
aoqi@0 1196 // Ensure that we release the reply...
aoqi@0 1197
aoqi@0 1198 p.pOlnI("} finally {");
aoqi@0 1199 p.pln("_releaseReply("+in+");");
aoqi@0 1200
aoqi@0 1201 p.pOln("}");
aoqi@0 1202
aoqi@0 1203 // Handle SystemException...
aoqi@0 1204
aoqi@0 1205 p.pOlnI("} catch (SystemException "+ex+") {");
aoqi@0 1206 p.pln("throw Util.mapSystemException("+ex+");");
aoqi@0 1207 p.pOln("}");
aoqi@0 1208
aoqi@0 1209 // returnResult(p,returnType);
aoqi@0 1210 }
aoqi@0 1211
aoqi@0 1212 void allocateResult (IndentingWriter p,
aoqi@0 1213 Type returnType) throws IOException {
aoqi@0 1214 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 1215 String objName = testUtil(getName(returnType), returnType);
aoqi@0 1216 p.p(objName + " result = ");
aoqi@0 1217 }
aoqi@0 1218 }
aoqi@0 1219
aoqi@0 1220 int getTypeCode(Type type) {
aoqi@0 1221
aoqi@0 1222 int typeCode = type.getTypeCode();
aoqi@0 1223
aoqi@0 1224 // Handle late-breaking special case for
aoqi@0 1225 // abstract IDL entities...
aoqi@0 1226
aoqi@0 1227 if ((type instanceof CompoundType) &&
aoqi@0 1228 ((CompoundType)type).isAbstractBase()) {
aoqi@0 1229 typeCode = TYPE_ABSTRACT;
aoqi@0 1230 }
aoqi@0 1231
aoqi@0 1232 return typeCode;
aoqi@0 1233 }
aoqi@0 1234
aoqi@0 1235
aoqi@0 1236 /**
aoqi@0 1237 * Write a snippet of Java code to marshal a value named "name" of
aoqi@0 1238 * type "type" to the java.io.ObjectOutput stream named "stream".
aoqi@0 1239 */
aoqi@0 1240 void writeMarshalArgument(IndentingWriter p,
aoqi@0 1241 String streamName,
aoqi@0 1242 Type type, String name) throws IOException {
aoqi@0 1243
aoqi@0 1244 int typeCode = getTypeCode(type);
aoqi@0 1245
aoqi@0 1246 switch (typeCode) {
aoqi@0 1247 case TYPE_BOOLEAN:
aoqi@0 1248 p.p(streamName + ".write_boolean(" + name + ");");
aoqi@0 1249 break;
aoqi@0 1250 case TYPE_BYTE:
aoqi@0 1251 p.p(streamName + ".write_octet(" + name + ");");
aoqi@0 1252 break;
aoqi@0 1253 case TYPE_CHAR:
aoqi@0 1254 p.p(streamName + ".write_wchar(" + name + ");");
aoqi@0 1255 break;
aoqi@0 1256 case TYPE_SHORT:
aoqi@0 1257 p.p(streamName + ".write_short(" + name + ");");
aoqi@0 1258 break;
aoqi@0 1259 case TYPE_INT:
aoqi@0 1260 p.p(streamName + ".write_long(" + name + ");");
aoqi@0 1261 break;
aoqi@0 1262 case TYPE_LONG:
aoqi@0 1263 p.p(streamName + ".write_longlong(" + name + ");");
aoqi@0 1264 break;
aoqi@0 1265 case TYPE_FLOAT:
aoqi@0 1266 p.p(streamName + ".write_float(" + name + ");");
aoqi@0 1267 break;
aoqi@0 1268 case TYPE_DOUBLE:
aoqi@0 1269 p.p(streamName + ".write_double(" + name + ");");
aoqi@0 1270 break;
aoqi@0 1271 case TYPE_STRING:
aoqi@0 1272 p.p(streamName + ".write_value(" + name + "," + getName(type) + ".class);");
aoqi@0 1273 break;
aoqi@0 1274 case TYPE_ANY:
aoqi@0 1275 p.p("Util.writeAny("+ streamName + "," + name + ");");
aoqi@0 1276 break;
aoqi@0 1277 case TYPE_CORBA_OBJECT:
aoqi@0 1278 p.p(streamName + ".write_Object(" + name + ");");
aoqi@0 1279 break;
aoqi@0 1280 case TYPE_REMOTE:
aoqi@0 1281 p.p("Util.writeRemoteObject("+ streamName + "," + name + ");");
aoqi@0 1282 break;
aoqi@0 1283 case TYPE_ABSTRACT:
aoqi@0 1284 p.p("Util.writeAbstractObject("+ streamName + "," + name + ");");
aoqi@0 1285 break;
aoqi@0 1286 case TYPE_NC_INTERFACE:
aoqi@0 1287 p.p(streamName + ".write_value((Serializable)" + name + "," + getName(type) + ".class);");
aoqi@0 1288 break;
aoqi@0 1289 case TYPE_VALUE:
aoqi@0 1290 p.p(streamName + ".write_value(" + name + "," + getName(type) + ".class);");
aoqi@0 1291 break;
aoqi@0 1292 case TYPE_IMPLEMENTATION:
aoqi@0 1293 p.p(streamName + ".write_value((Serializable)" + name + "," + getName(type) + ".class);");
aoqi@0 1294 break;
aoqi@0 1295 case TYPE_NC_CLASS:
aoqi@0 1296 p.p(streamName + ".write_value((Serializable)" + name + "," + getName(type) + ".class);");
aoqi@0 1297 break;
aoqi@0 1298 case TYPE_ARRAY:
aoqi@0 1299 castArray = true;
aoqi@0 1300 p.p(streamName + ".write_value(cast_array(" + name + ")," + getName(type) + ".class);");
aoqi@0 1301 break;
aoqi@0 1302 case TYPE_JAVA_RMI_REMOTE:
aoqi@0 1303 p.p("Util.writeRemoteObject("+ streamName + "," + name + ");");
aoqi@0 1304 break;
aoqi@0 1305 default:
aoqi@0 1306 throw new Error("unexpected type code: " + typeCode);
aoqi@0 1307 }
aoqi@0 1308 }
aoqi@0 1309
aoqi@0 1310 /**
aoqi@0 1311 * Write a snippet of Java code to unmarshal a value of type "type"
aoqi@0 1312 * from the java.io.ObjectInput stream named "stream" into a variable
aoqi@0 1313 * named "name" (if "name" is null, the value in unmarshalled and
aoqi@0 1314 * discarded).
aoqi@0 1315 */
aoqi@0 1316 void writeUnmarshalArgument(IndentingWriter p,
aoqi@0 1317 String streamName,
aoqi@0 1318 Type type,
aoqi@0 1319 String name) throws IOException {
aoqi@0 1320
aoqi@0 1321 int typeCode = getTypeCode(type);
aoqi@0 1322
aoqi@0 1323 if (name != null) {
aoqi@0 1324 p.p(name + " = ");
aoqi@0 1325 }
aoqi@0 1326
aoqi@0 1327 switch (typeCode) {
aoqi@0 1328 case TYPE_BOOLEAN:
aoqi@0 1329 p.p(streamName + ".read_boolean();");
aoqi@0 1330 break;
aoqi@0 1331 case TYPE_BYTE:
aoqi@0 1332 p.p(streamName + ".read_octet();");
aoqi@0 1333 break;
aoqi@0 1334 case TYPE_CHAR:
aoqi@0 1335 p.p(streamName + ".read_wchar();");
aoqi@0 1336 break;
aoqi@0 1337 case TYPE_SHORT:
aoqi@0 1338 p.p(streamName + ".read_short();");
aoqi@0 1339 break;
aoqi@0 1340 case TYPE_INT:
aoqi@0 1341 p.p(streamName + ".read_long();");
aoqi@0 1342 break;
aoqi@0 1343 case TYPE_LONG:
aoqi@0 1344 p.p(streamName + ".read_longlong();");
aoqi@0 1345 break;
aoqi@0 1346 case TYPE_FLOAT:
aoqi@0 1347 p.p(streamName + ".read_float();");
aoqi@0 1348 break;
aoqi@0 1349 case TYPE_DOUBLE:
aoqi@0 1350 p.p(streamName + ".read_double();");
aoqi@0 1351 break;
aoqi@0 1352 case TYPE_STRING:
aoqi@0 1353 p.p("(String) " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1354 break;
aoqi@0 1355 case TYPE_ANY:
aoqi@0 1356 if (type.getIdentifier() != idJavaLangObject) {
aoqi@0 1357 p.p("(" + getName(type) + ") ");
aoqi@0 1358 }
aoqi@0 1359 p.p("Util.readAny(" + streamName + ");");
aoqi@0 1360 break;
aoqi@0 1361 case TYPE_CORBA_OBJECT:
aoqi@0 1362 if (type.getIdentifier() == idCorbaObject) {
aoqi@0 1363 p.p("(" + getName(type) + ") " + streamName + ".read_Object();");
aoqi@0 1364 } else {
aoqi@0 1365 p.p("(" + getName(type) + ") " + streamName + ".read_Object(" + getStubName(type) + ".class);");
aoqi@0 1366 }
aoqi@0 1367 break;
aoqi@0 1368 case TYPE_REMOTE:
aoqi@0 1369 String objName = testUtil(getName(type), type);
aoqi@0 1370 p.p("(" + objName + ") " +
aoqi@0 1371 "PortableRemoteObject.narrow(" + streamName + ".read_Object(), " + objName + ".class);");
aoqi@0 1372 break;
aoqi@0 1373 case TYPE_ABSTRACT:
aoqi@0 1374 p.p("(" + getName(type) + ") " + streamName + ".read_abstract_interface();");
aoqi@0 1375 break;
aoqi@0 1376 case TYPE_NC_INTERFACE:
aoqi@0 1377 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1378 break;
aoqi@0 1379 case TYPE_VALUE:
aoqi@0 1380 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1381 break;
aoqi@0 1382 case TYPE_IMPLEMENTATION:
aoqi@0 1383 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1384 break;
aoqi@0 1385 case TYPE_NC_CLASS:
aoqi@0 1386 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1387 break;
aoqi@0 1388 case TYPE_ARRAY:
aoqi@0 1389 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1390 break;
aoqi@0 1391 case TYPE_JAVA_RMI_REMOTE:
aoqi@0 1392 p.p("(" + getName(type) + ") " +
aoqi@0 1393 "PortableRemoteObject.narrow(" + streamName + ".read_Object(), " + getName(type) + ".class);");
aoqi@0 1394 // p.p("(" + getName(type) + ") " + streamName + ".read_Object(" + getStubName(type) + ".class);");
aoqi@0 1395 break;
aoqi@0 1396 default:
aoqi@0 1397 throw new Error("unexpected type code: " + typeCode);
aoqi@0 1398 }
aoqi@0 1399 }
aoqi@0 1400
aoqi@0 1401 /**
aoqi@0 1402 * Get a list of all the RepositoryIDs for interfaces
aoqi@0 1403 * implemented directly or indirectly by theType. In the
aoqi@0 1404 * case of an ImplementationType which implements 2 or
aoqi@0 1405 * more remote interfaces, this list will begin with the
aoqi@0 1406 * Identifier for the implementation (see section 5.9 in
aoqi@0 1407 * the Java -> IDL mapping). Ensures that the most derived
aoqi@0 1408 * type is first in the list because the IOR is generated
aoqi@0 1409 * using that entry in the _ids array.
aoqi@0 1410 */
aoqi@0 1411 String[] getAllRemoteRepIDs (CompoundType theType) {
aoqi@0 1412
aoqi@0 1413 String[] result;
aoqi@0 1414
aoqi@0 1415 // Collect up all the (inherited) remote interfaces
aoqi@0 1416 // (ignores all the 'special' interfaces: Remote,
aoqi@0 1417 // Serializable, Externalizable)...
aoqi@0 1418
aoqi@0 1419 Type[] types = collectAllRemoteInterfaces(theType);
aoqi@0 1420
aoqi@0 1421 int length = types.length;
aoqi@0 1422 boolean haveImpl = theType instanceof ImplementationType;
aoqi@0 1423 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 1424 int remoteCount = countRemote(interfaces,false);
aoqi@0 1425 int offset = 0;
aoqi@0 1426
aoqi@0 1427 // Do we have an implementation type that implements
aoqi@0 1428 // more than one remote interface?
aoqi@0 1429
aoqi@0 1430 if (haveImpl && remoteCount > 1) {
aoqi@0 1431
aoqi@0 1432 // Yes, so we need to insert it at the beginning...
aoqi@0 1433
aoqi@0 1434 result = new String[length + 1];
aoqi@0 1435 result[0] = getRepositoryID(theType);
aoqi@0 1436 offset = 1;
aoqi@0 1437
aoqi@0 1438 } else {
aoqi@0 1439
aoqi@0 1440 // No.
aoqi@0 1441
aoqi@0 1442 result = new String[length];
aoqi@0 1443
aoqi@0 1444 // Here we need to ensure that the most derived
aoqi@0 1445 // interface ends up being first in the list. If
aoqi@0 1446 // there is only one, we're done.
aoqi@0 1447
aoqi@0 1448 if (length > 1) {
aoqi@0 1449
aoqi@0 1450 // First, decide what the most derived type is...
aoqi@0 1451
aoqi@0 1452 String mostDerived = null;
aoqi@0 1453
aoqi@0 1454 if (haveImpl) {
aoqi@0 1455
aoqi@0 1456 // If we get here, we know that there is only one
aoqi@0 1457 // direct remote interface, so just find it...
aoqi@0 1458
aoqi@0 1459 for (int i = 0; i < interfaces.length; i++) {
aoqi@0 1460 if (interfaces[i].isType(TYPE_REMOTE)) {
aoqi@0 1461 mostDerived = interfaces[i].getRepositoryID();
aoqi@0 1462 break;
aoqi@0 1463 }
aoqi@0 1464 }
aoqi@0 1465 } else {
aoqi@0 1466
aoqi@0 1467 // If we get here we know that theType is a RemoteType
aoqi@0 1468 // so just use its id...
aoqi@0 1469
aoqi@0 1470 mostDerived = theType.getRepositoryID();
aoqi@0 1471 }
aoqi@0 1472
aoqi@0 1473 // Now search types list and make sure mostDerived is
aoqi@0 1474 // at index zero...
aoqi@0 1475
aoqi@0 1476 for (int i = 0; i < length; i++) {
aoqi@0 1477 if (types[i].getRepositoryID() == mostDerived) {
aoqi@0 1478
aoqi@0 1479 // Found it. Swap it if we need to...
aoqi@0 1480
aoqi@0 1481 if (i > 0) {
aoqi@0 1482 Type temp = types[0];
aoqi@0 1483 types[0] = types[i];
aoqi@0 1484 types[i] = temp;
aoqi@0 1485 }
aoqi@0 1486
aoqi@0 1487 break;
aoqi@0 1488 }
aoqi@0 1489 }
aoqi@0 1490 }
aoqi@0 1491 }
aoqi@0 1492
aoqi@0 1493 // Now copy contents of the types array...
aoqi@0 1494
aoqi@0 1495 for (int i = 0; i < types.length; i++) {
aoqi@0 1496 result[offset++] = getRepositoryID(types[i]);
aoqi@0 1497 }
aoqi@0 1498
aoqi@0 1499 // If we're supposed to, reverse the array. This
aoqi@0 1500 // is only done when the -testReverseIDs flag is
aoqi@0 1501 // passed, and that should ONLY be done for test
aoqi@0 1502 // cases. This is an undocumented feature.
aoqi@0 1503
aoqi@0 1504 if (reverseIDs) {
aoqi@0 1505 int start = 0;
aoqi@0 1506 int end = result.length -1;
aoqi@0 1507 while (start < end) {
aoqi@0 1508 String temp = result[start];
aoqi@0 1509 result[start++] = result[end];
aoqi@0 1510 result[end--] = temp;
aoqi@0 1511 }
aoqi@0 1512 }
aoqi@0 1513
aoqi@0 1514 return result;
aoqi@0 1515 }
aoqi@0 1516
aoqi@0 1517 /**
aoqi@0 1518 * Collect all the inherited remote interfaces.
aoqi@0 1519 */
aoqi@0 1520 Type[] collectAllRemoteInterfaces (CompoundType theType) {
aoqi@0 1521 Vector list = new Vector();
aoqi@0 1522
aoqi@0 1523 // Collect up all the Remote interfaces, and get an instance
aoqi@0 1524 // for java.rmi.Remote...
aoqi@0 1525
aoqi@0 1526 addRemoteInterfaces(list,theType);
aoqi@0 1527
aoqi@0 1528 // Create and return our results...
aoqi@0 1529
aoqi@0 1530 Type[] result = new Type[list.size()];
aoqi@0 1531 list.copyInto(result);
aoqi@0 1532
aoqi@0 1533 return result;
aoqi@0 1534 }
aoqi@0 1535
aoqi@0 1536 /**
aoqi@0 1537 * Add all the inherited remote interfaces to list.
aoqi@0 1538 */
aoqi@0 1539 void addRemoteInterfaces(Vector list, CompoundType theType) {
aoqi@0 1540
aoqi@0 1541 if (theType != null) {
aoqi@0 1542 if (theType.isInterface() && !list.contains(theType)) {
aoqi@0 1543 list.addElement(theType);
aoqi@0 1544 }
aoqi@0 1545
aoqi@0 1546 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 1547 for (int i = 0; i < interfaces.length; i++) {
aoqi@0 1548
aoqi@0 1549 if (interfaces[i].isType(TYPE_REMOTE)) {
aoqi@0 1550 addRemoteInterfaces(list,interfaces[i]);
aoqi@0 1551 }
aoqi@0 1552 }
aoqi@0 1553
aoqi@0 1554 addRemoteInterfaces(list,theType.getSuperclass());
aoqi@0 1555 }
aoqi@0 1556 }
aoqi@0 1557
aoqi@0 1558 /**
aoqi@0 1559 * Get a list of all the remote interfaces which this stub
aoqi@0 1560 * should declare.
aoqi@0 1561 */
aoqi@0 1562 RemoteType[] getDirectRemoteInterfaces (CompoundType theType) {
aoqi@0 1563
aoqi@0 1564 RemoteType[] result;
aoqi@0 1565 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 1566
aoqi@0 1567 // First, get a list of all the interfaces...
aoqi@0 1568
aoqi@0 1569 InterfaceType[] list;
aoqi@0 1570
aoqi@0 1571 // Because we can be passed either an ImplementationType
aoqi@0 1572 // (which has interfaces) or a RemoteType (which is an
aoqi@0 1573 // interface and may have interfaces) we must handle each
aoqi@0 1574 // separately...
aoqi@0 1575
aoqi@0 1576 // Do we have an implementation type?
aoqi@0 1577
aoqi@0 1578 if (theType instanceof ImplementationType) {
aoqi@0 1579
aoqi@0 1580 // Yes, so list is exactly what this type
aoqi@0 1581 // implements and is correct already.
aoqi@0 1582
aoqi@0 1583 list = interfaces;
aoqi@0 1584
aoqi@0 1585 } else {
aoqi@0 1586
aoqi@0 1587 // No, so list is just theType...
aoqi@0 1588
aoqi@0 1589 list = new InterfaceType[1];
aoqi@0 1590 list[0] = (InterfaceType) theType;
aoqi@0 1591 }
aoqi@0 1592
aoqi@0 1593 // Ok, now count up the remote interfaces, allocate
aoqi@0 1594 // our result and fill it in...
aoqi@0 1595
aoqi@0 1596 int remoteCount = countRemote(list,false);
aoqi@0 1597
aoqi@0 1598 if (remoteCount == 0) {
aoqi@0 1599 throw new CompilerError("iiop.StubGenerator: No remote interfaces!");
aoqi@0 1600 }
aoqi@0 1601
aoqi@0 1602 result = new RemoteType[remoteCount];
aoqi@0 1603 int offset = 0;
aoqi@0 1604 for (int i = 0; i < list.length; i++) {
aoqi@0 1605 if (list[i].isType(TYPE_REMOTE)) {
aoqi@0 1606 result[offset++] = (RemoteType)list[i];
aoqi@0 1607 }
aoqi@0 1608 }
aoqi@0 1609
aoqi@0 1610 return result;
aoqi@0 1611 }
aoqi@0 1612
aoqi@0 1613 int countRemote (Type[] list, boolean includeAbstract) {
aoqi@0 1614 int remoteCount = 0;
aoqi@0 1615 for (int i = 0; i < list.length; i++) {
aoqi@0 1616 if (list[i].isType(TYPE_REMOTE) &&
aoqi@0 1617 (includeAbstract || !list[i].isType(TYPE_ABSTRACT))) {
aoqi@0 1618 remoteCount++;
aoqi@0 1619 }
aoqi@0 1620 }
aoqi@0 1621
aoqi@0 1622 return remoteCount;
aoqi@0 1623 }
aoqi@0 1624
aoqi@0 1625 void writeCastArray(IndentingWriter p) throws IOException {
aoqi@0 1626 if (castArray) {
aoqi@0 1627 p.pln();
aoqi@0 1628 p.pln("// This method is required as a work-around for");
aoqi@0 1629 p.pln("// a bug in the JDK 1.1.6 verifier.");
aoqi@0 1630 p.pln();
aoqi@0 1631 p.plnI("private "+getName(idJavaIoSerializable)+" cast_array(Object obj) {");
aoqi@0 1632 p.pln("return ("+getName(idJavaIoSerializable)+")obj;");
aoqi@0 1633 p.pOln("}");
aoqi@0 1634 }
aoqi@0 1635 }
aoqi@0 1636 void writeIds(IndentingWriter p, CompoundType theType, boolean isTie
aoqi@0 1637 ) throws IOException {
aoqi@0 1638 p.plnI("private static final String[] _type_ids = {");
aoqi@0 1639
aoqi@0 1640 String[] ids = getAllRemoteRepIDs(theType);
aoqi@0 1641
aoqi@0 1642 if (ids.length >0 ) {
aoqi@0 1643 for(int i = 0; i < ids.length; i++) {
aoqi@0 1644 if (i > 0)
aoqi@0 1645 p.pln(", ");
aoqi@0 1646 p.p("\"" + ids[i] + "\"");
aoqi@0 1647 }
aoqi@0 1648 } else {
aoqi@0 1649 // Must be an implementation which only implements Remote...
aoqi@0 1650 p.pln("\"\"");
aoqi@0 1651 }
aoqi@0 1652 String qname = theType.getQualifiedName() ;
aoqi@0 1653 boolean isTransactional = isTie && transactionalObjects.containsKey( qname ) ;
aoqi@0 1654 // Add TransactionalObject if needed.
aoqi@0 1655 if (isTransactional) {
aoqi@0 1656 // Have already written an id.
aoqi@0 1657 p.pln( ", " ) ;
aoqi@0 1658 p.pln( "\"IDL:omg.org/CosTransactions/TransactionalObject:1.0\"" ) ;
aoqi@0 1659 } else if (ids.length > 0) {
aoqi@0 1660 p.pln();
aoqi@0 1661 }
aoqi@0 1662 p.pOln("};");
aoqi@0 1663 }
aoqi@0 1664
aoqi@0 1665
aoqi@0 1666 /**
aoqi@0 1667 * Write the Tie for the remote class to a stream.
aoqi@0 1668 */
aoqi@0 1669 protected void writeTie(OutputType outputType,
aoqi@0 1670 IndentingWriter p) throws IOException
aoqi@0 1671 {
aoqi@0 1672 CompoundType theType = (CompoundType) outputType.getType();
aoqi@0 1673 RemoteType[] remoteInterfaces = null;
aoqi@0 1674
aoqi@0 1675 // Write comment...
aoqi@0 1676 p.pln("// Tie class generated by rmic, do not edit.");
aoqi@0 1677 p.pln("// Contents subject to change without notice.");
aoqi@0 1678 p.pln();
aoqi@0 1679
aoqi@0 1680 // Set our standard classes...
aoqi@0 1681 setStandardClassesInUse(theType,false);
aoqi@0 1682
aoqi@0 1683 // Add classes for this type...
aoqi@0 1684 addClassesInUse(theType,remoteInterfaces);
aoqi@0 1685
aoqi@0 1686 // Write package and import statements...
aoqi@0 1687 writePackageAndImports(p);
aoqi@0 1688
aoqi@0 1689 // Declare the tie class.
aoqi@0 1690 p.p("public class " + currentClass + " extends " +
aoqi@0 1691 getName(tieBaseClass) + " implements Tie");
aoqi@0 1692
aoqi@0 1693 // Add java.rmi.Remote if this type does not implement it.
aoqi@0 1694 // This allows stubs for Abstract interfaces to be treated
aoqi@0 1695 // uniformly...
aoqi@0 1696 if (!implementsRemote(theType)) {
aoqi@0 1697 p.pln(",");
aoqi@0 1698 p.p(getName("java.rmi.Remote"));
aoqi@0 1699 }
aoqi@0 1700
aoqi@0 1701 p.plnI(" {");
aoqi@0 1702
aoqi@0 1703 // Write data members...
aoqi@0 1704 p.pln();
aoqi@0 1705 p.pln("volatile private " + getName(theType) + " target = null;");
aoqi@0 1706 p.pln();
aoqi@0 1707
aoqi@0 1708 // Write the ids...
aoqi@0 1709 writeIds( p, theType, true ) ;
aoqi@0 1710
aoqi@0 1711 // Write setTarget method...
aoqi@0 1712 p.pln();
aoqi@0 1713 p.plnI("public void setTarget(Remote target) {");
aoqi@0 1714 p.pln("this.target = (" + getName(theType) + ") target;");
aoqi@0 1715 p.pOln("}");
aoqi@0 1716
aoqi@0 1717 // Write getTarget method...
aoqi@0 1718 p.pln();
aoqi@0 1719 p.plnI("public Remote getTarget() {");
aoqi@0 1720 p.pln("return target;");
aoqi@0 1721 p.pOln("}");
aoqi@0 1722
aoqi@0 1723 // Write thisObject method...
aoqi@0 1724 p.pln();
aoqi@0 1725 write_tie_thisObject_method(p,idCorbaObject);
aoqi@0 1726
aoqi@0 1727 // Write deactivate method...
aoqi@0 1728 p.pln();
aoqi@0 1729 write_tie_deactivate_method(p);
aoqi@0 1730
aoqi@0 1731 // Write get orb method...
aoqi@0 1732 p.pln();
aoqi@0 1733 p.plnI("public ORB orb() {");
aoqi@0 1734 p.pln("return _orb();");
aoqi@0 1735 p.pOln("}");
aoqi@0 1736
aoqi@0 1737 // Write set orb method...
aoqi@0 1738 p.pln();
aoqi@0 1739 write_tie_orb_method(p);
aoqi@0 1740
aoqi@0 1741 // Write the _ids() method...
aoqi@0 1742 p.pln();
aoqi@0 1743 write_tie__ids_method(p);
aoqi@0 1744
aoqi@0 1745 // Get all the methods...
aoqi@0 1746 CompoundType.Method[] remoteMethods = theType.getMethods();
aoqi@0 1747
aoqi@0 1748 // Register all the argument names used, plus our
aoqi@0 1749 // data member names...
aoqi@0 1750
aoqi@0 1751 addNamesInUse(remoteMethods);
aoqi@0 1752 addNameInUse("target");
aoqi@0 1753 addNameInUse("_type_ids");
aoqi@0 1754
aoqi@0 1755 // Write the _invoke method...
aoqi@0 1756 p.pln();
aoqi@0 1757
aoqi@0 1758 String in = getVariableName("in");
aoqi@0 1759 String _in = getVariableName("_in");
aoqi@0 1760 String ex = getVariableName("ex");
aoqi@0 1761 String method = getVariableName("method");
aoqi@0 1762 String reply = getVariableName("reply");
aoqi@0 1763
aoqi@0 1764 p.plnI("public OutputStream _invoke(String "+method+", InputStream "+_in+", " +
aoqi@0 1765 "ResponseHandler "+reply+") throws SystemException {");
aoqi@0 1766
aoqi@0 1767 if (remoteMethods.length > 0) {
aoqi@0 1768 p.plnI("try {");
aoqi@0 1769 p.pln(getName(theType) + " target = this.target;");
aoqi@0 1770 p.plnI("if (target == null) {");
aoqi@0 1771 p.pln("throw new java.io.IOException();");
aoqi@0 1772 p.pOln("}");
aoqi@0 1773 p.plnI(idExtInputStream + " "+in+" = ");
aoqi@0 1774 p.pln("(" + idExtInputStream + ") "+_in+";");
aoqi@0 1775 p.pO();
aoqi@0 1776
aoqi@0 1777 // See if we should use a hash table style
aoqi@0 1778 // comparison...
aoqi@0 1779
aoqi@0 1780 StaticStringsHash hash = getStringsHash(remoteMethods);
aoqi@0 1781
aoqi@0 1782 if (hash != null) {
aoqi@0 1783 p.plnI("switch ("+method+"."+hash.method+") {");
aoqi@0 1784 for (int i = 0; i < hash.buckets.length; i++) {
aoqi@0 1785 p.plnI("case "+hash.keys[i]+": ");
aoqi@0 1786 for (int j = 0; j < hash.buckets[i].length; j++) {
aoqi@0 1787 CompoundType.Method current = remoteMethods[hash.buckets[i][j]];
aoqi@0 1788 if (j > 0) {
aoqi@0 1789 p.pO("} else ");
aoqi@0 1790 }
aoqi@0 1791 p.plnI("if ("+method+".equals(\""+ current.getIDLName() +"\")) {");
aoqi@0 1792 writeTieMethod(p, theType,current);
aoqi@0 1793 }
aoqi@0 1794 p.pOln("}");
aoqi@0 1795 p.pO();
aoqi@0 1796 }
aoqi@0 1797 } else {
aoqi@0 1798 for(int i = 0; i < remoteMethods.length; i++) {
aoqi@0 1799 CompoundType.Method current = remoteMethods[i];
aoqi@0 1800 if (i > 0) {
aoqi@0 1801 p.pO("} else ");
aoqi@0 1802 }
aoqi@0 1803
aoqi@0 1804 p.plnI("if ("+method+".equals(\""+ current.getIDLName() +"\")) {");
aoqi@0 1805 writeTieMethod(p, theType, current);
aoqi@0 1806 }
aoqi@0 1807 }
aoqi@0 1808
aoqi@0 1809 if (hash != null) {
aoqi@0 1810 p.pI();
aoqi@0 1811 // p.plnI("default:");
aoqi@0 1812 } else {
aoqi@0 1813 // p.pOlnI("} else {");
aoqi@0 1814 }
aoqi@0 1815 // p.pln("throw new "+getName(idBadMethodException)+"();");
aoqi@0 1816
aoqi@0 1817 if (hash != null) {
aoqi@0 1818 p.pO();
aoqi@0 1819 }
aoqi@0 1820 p.pOln("}");
aoqi@0 1821 p.pln("throw new "+getName(idBadMethodException)+"();");
aoqi@0 1822
aoqi@0 1823 p.pOlnI("} catch ("+getName(idSystemException)+" "+ex+") {");
aoqi@0 1824 p.pln("throw "+ex+";");
aoqi@0 1825
aoqi@0 1826 p.pOlnI("} catch ("+getName(idJavaLangThrowable)+" "+ex+") {");
aoqi@0 1827 p.pln("throw new " + getName(idPortableUnknownException) + "("+ex+");");
aoqi@0 1828 p.pOln("}");
aoqi@0 1829 } else {
aoqi@0 1830 // No methods...
aoqi@0 1831
aoqi@0 1832 p.pln("throw new " + getName(idBadMethodException) + "();");
aoqi@0 1833 }
aoqi@0 1834
aoqi@0 1835 p.pOln("}"); // end invoke
aoqi@0 1836
aoqi@0 1837 // Write the cast array hack...
aoqi@0 1838
aoqi@0 1839 writeCastArray(p);
aoqi@0 1840
aoqi@0 1841 // End tie class...
aoqi@0 1842 p.pOln("}");
aoqi@0 1843 }
aoqi@0 1844 public void catchWrongPolicy(IndentingWriter p) throws IOException {
aoqi@0 1845 p.pln("");
aoqi@0 1846 }
aoqi@0 1847 public void catchServantNotActive(IndentingWriter p) throws IOException {
aoqi@0 1848 p.pln("");
aoqi@0 1849 }
aoqi@0 1850 public void catchObjectNotActive(IndentingWriter p) throws IOException {
aoqi@0 1851 p.pln("");
aoqi@0 1852 }
aoqi@0 1853
aoqi@0 1854 public void write_tie_thisObject_method(IndentingWriter p,
aoqi@0 1855 Identifier idCorbaObject)
aoqi@0 1856 throws IOException
aoqi@0 1857 {
aoqi@0 1858 if(POATie){
aoqi@0 1859 p.plnI("public " + idCorbaObject + " thisObject() {");
aoqi@0 1860 /*
aoqi@0 1861 p.pln("org.omg.CORBA.Object objref = null;");
aoqi@0 1862 p.pln("try{");
aoqi@0 1863 p.pln("objref = _poa().servant_to_reference(this);");
aoqi@0 1864 p.pln("}catch (org.omg.PortableServer.POAPackage.WrongPolicy exception){");
aoqi@0 1865 catchWrongPolicy(p);
aoqi@0 1866 p.pln("}catch (org.omg.PortableServer.POAPackage.ServantNotActive exception){");
aoqi@0 1867 catchServantNotActive(p);
aoqi@0 1868 p.pln("}");
aoqi@0 1869 p.pln("return objref;");
aoqi@0 1870 */
aoqi@0 1871 p.pln("return _this_object();");
aoqi@0 1872 p.pOln("}");
aoqi@0 1873 } else {
aoqi@0 1874 p.plnI("public " + idCorbaObject + " thisObject() {");
aoqi@0 1875 p.pln("return this;");
aoqi@0 1876 p.pOln("}");
aoqi@0 1877 }
aoqi@0 1878 }
aoqi@0 1879
aoqi@0 1880 public void write_tie_deactivate_method(IndentingWriter p)
aoqi@0 1881 throws IOException
aoqi@0 1882 {
aoqi@0 1883 if(POATie){
aoqi@0 1884 p.plnI("public void deactivate() {");
aoqi@0 1885 p.pln("try{");
aoqi@0 1886 p.pln("_poa().deactivate_object(_poa().servant_to_id(this));");
aoqi@0 1887 p.pln("}catch (org.omg.PortableServer.POAPackage.WrongPolicy exception){");
aoqi@0 1888 catchWrongPolicy(p);
aoqi@0 1889 p.pln("}catch (org.omg.PortableServer.POAPackage.ObjectNotActive exception){");
aoqi@0 1890 catchObjectNotActive(p);
aoqi@0 1891 p.pln("}catch (org.omg.PortableServer.POAPackage.ServantNotActive exception){");
aoqi@0 1892 catchServantNotActive(p);
aoqi@0 1893 p.pln("}");
aoqi@0 1894 p.pOln("}");
aoqi@0 1895 } else {
aoqi@0 1896 p.plnI("public void deactivate() {");
aoqi@0 1897 p.pln("_orb().disconnect(this);");
aoqi@0 1898 p.pln("_set_delegate(null);");
aoqi@0 1899 p.pln("target = null;");
aoqi@0 1900 p.pOln("}");
aoqi@0 1901 }
aoqi@0 1902 }
aoqi@0 1903
aoqi@0 1904 public void write_tie_orb_method(IndentingWriter p)
aoqi@0 1905 throws IOException
aoqi@0 1906 {
aoqi@0 1907 if(POATie){
aoqi@0 1908 p.plnI("public void orb(ORB orb) {");
aoqi@0 1909 /*
aoqi@0 1910 p.pln("try{");
aoqi@0 1911 p.pln("orb.connect(_poa().servant_to_reference(this));");
aoqi@0 1912 p.pln("}catch (org.omg.PortableServer.POAPackage.WrongPolicy exception){");
aoqi@0 1913 catchWrongPolicy(p);
aoqi@0 1914 p.pln("}catch (org.omg.PortableServer.POAPackage.ServantNotActive exception){");
aoqi@0 1915 catchServantNotActive(p);
aoqi@0 1916 p.pln("}");
aoqi@0 1917 */
aoqi@0 1918 p.pln("try {");
aoqi@0 1919 p.pln(" ((org.omg.CORBA_2_3.ORB)orb).set_delegate(this);");
aoqi@0 1920 p.pln("}");
aoqi@0 1921 p.pln("catch(ClassCastException e) {");
aoqi@0 1922 p.pln(" throw new org.omg.CORBA.BAD_PARAM");
aoqi@0 1923 p.pln(" (\"POA Servant requires an instance of org.omg.CORBA_2_3.ORB\");");
aoqi@0 1924 p.pln("}");
aoqi@0 1925 p.pOln("}");
aoqi@0 1926 } else {
aoqi@0 1927 p.plnI("public void orb(ORB orb) {");
aoqi@0 1928 p.pln("orb.connect(this);");
aoqi@0 1929 p.pOln("}");
aoqi@0 1930 }
aoqi@0 1931 }
aoqi@0 1932
aoqi@0 1933 public void write_tie__ids_method(IndentingWriter p)
aoqi@0 1934 throws IOException
aoqi@0 1935 {
aoqi@0 1936 if(POATie){
aoqi@0 1937 p.plnI("public String[] _all_interfaces(org.omg.PortableServer.POA poa, byte[] objectId){");
aoqi@0 1938 p.pln("return (String[]) _type_ids.clone();");
aoqi@0 1939 p.pOln("}");
aoqi@0 1940 } else {
aoqi@0 1941 p.plnI("public String[] _ids() { ");
aoqi@0 1942 p.pln("return (String[]) _type_ids.clone();");
aoqi@0 1943 p.pOln("}");
aoqi@0 1944 }
aoqi@0 1945 }
aoqi@0 1946
aoqi@0 1947
aoqi@0 1948 StaticStringsHash getStringsHash (CompoundType.Method[] methods) {
aoqi@0 1949 if (useHash && methods.length > 1) {
aoqi@0 1950 String[] methodNames = new String[methods.length];
aoqi@0 1951 for (int i = 0; i < methodNames.length; i++) {
aoqi@0 1952 methodNames[i] = methods[i].getIDLName();
aoqi@0 1953 }
aoqi@0 1954 return new StaticStringsHash(methodNames);
aoqi@0 1955 }
aoqi@0 1956 return null;
aoqi@0 1957 }
aoqi@0 1958
aoqi@0 1959 static boolean needNewReadStreamClass(Type type) {
aoqi@0 1960 if (type.isType(TYPE_ABSTRACT)) {
aoqi@0 1961 return true;
aoqi@0 1962 }
aoqi@0 1963 // Handle late-breaking special case for
aoqi@0 1964 // abstract IDL entities...
aoqi@0 1965 if ((type instanceof CompoundType) &&
aoqi@0 1966 ((CompoundType)type).isAbstractBase()) {
aoqi@0 1967 return true;
aoqi@0 1968 }
aoqi@0 1969 return needNewWriteStreamClass(type);
aoqi@0 1970 }
aoqi@0 1971
aoqi@0 1972 static boolean needNewWriteStreamClass(Type type) {
aoqi@0 1973 switch (type.getTypeCode()) {
aoqi@0 1974 case TYPE_VOID:
aoqi@0 1975 case TYPE_BOOLEAN:
aoqi@0 1976 case TYPE_BYTE:
aoqi@0 1977 case TYPE_CHAR:
aoqi@0 1978 case TYPE_SHORT:
aoqi@0 1979 case TYPE_INT:
aoqi@0 1980 case TYPE_LONG:
aoqi@0 1981 case TYPE_FLOAT:
aoqi@0 1982 case TYPE_DOUBLE: return false;
aoqi@0 1983
aoqi@0 1984 case TYPE_STRING: return true;
aoqi@0 1985 case TYPE_ANY: return false;
aoqi@0 1986 case TYPE_CORBA_OBJECT: return false;
aoqi@0 1987 case TYPE_REMOTE: return false;
aoqi@0 1988 case TYPE_ABSTRACT: return false;
aoqi@0 1989 case TYPE_NC_INTERFACE: return true;
aoqi@0 1990 case TYPE_VALUE: return true;
aoqi@0 1991 case TYPE_IMPLEMENTATION: return true;
aoqi@0 1992 case TYPE_NC_CLASS: return true;
aoqi@0 1993 case TYPE_ARRAY: return true;
aoqi@0 1994 case TYPE_JAVA_RMI_REMOTE: return false;
aoqi@0 1995
aoqi@0 1996 default: throw new Error("unexpected type code: " + type.getTypeCode());
aoqi@0 1997 }
aoqi@0 1998 }
aoqi@0 1999
aoqi@0 2000 /*
aoqi@0 2001 * Decide which arguments need to be copied and write
aoqi@0 2002 * the copy code. Returns an array of argument names to
aoqi@0 2003 * use to refer to either the copy or the original.
aoqi@0 2004 */
aoqi@0 2005 String[] writeCopyArguments(CompoundType.Method method,
aoqi@0 2006 IndentingWriter p) throws IOException {
aoqi@0 2007
aoqi@0 2008 Type[] args = method.getArguments();
aoqi@0 2009 String[] origNames = method.getArgumentNames();
aoqi@0 2010
aoqi@0 2011 // Copy the current parameter names to a result array...
aoqi@0 2012
aoqi@0 2013 String[] result = new String[origNames.length];
aoqi@0 2014 for (int i = 0; i < result.length; i++) {
aoqi@0 2015 result[i] = origNames[i];
aoqi@0 2016 }
aoqi@0 2017
aoqi@0 2018 // Decide which arguments must be copied, if any. If
aoqi@0 2019 // any of the arguments are types for which a 'real' copy
aoqi@0 2020 // will be done, rather than just an autoConnect, set
aoqi@0 2021 // realCopy = true. Note that abstract types may only
aoqi@0 2022 // need autoConnect, but we cannot know that at compile
aoqi@0 2023 // time...
aoqi@0 2024
aoqi@0 2025 boolean realCopy = false;
aoqi@0 2026 boolean[] copyArg = new boolean[args.length];
aoqi@0 2027 int copyCount = 0;
aoqi@0 2028 int firstCopiedArg = 0; // Only used in single copy case. It is only the first arg that
aoqi@0 2029 // needs copying IF copyCount == 1.
aoqi@0 2030
aoqi@0 2031 for (int i = 0; i < args.length; i++) {
aoqi@0 2032 if (mustCopy(args[i])) {
aoqi@0 2033 copyArg[i] = true;
aoqi@0 2034 copyCount++;
aoqi@0 2035 firstCopiedArg = i;
aoqi@0 2036 if (args[i].getTypeCode() != TYPE_REMOTE &&
aoqi@0 2037 args[i].getTypeCode() != TYPE_IMPLEMENTATION) {
aoqi@0 2038 realCopy = true;
aoqi@0 2039 }
aoqi@0 2040 } else {
aoqi@0 2041 copyArg[i] = false;
aoqi@0 2042 }
aoqi@0 2043 }
aoqi@0 2044
aoqi@0 2045 // Do we have any types which must be copied?
aoqi@0 2046 if (copyCount > 0) {
aoqi@0 2047 // Yes. Are we only doing the copy to ensure
aoqi@0 2048 // that autoConnect occurs?
aoqi@0 2049 if (realCopy) {
aoqi@0 2050 // Nope. We need to go back thru the list and
aoqi@0 2051 // mark any strings so that they will be copied
aoqi@0 2052 // to preserve any shared references...
aoqi@0 2053 for (int i = 0; i < args.length; i++) {
aoqi@0 2054 if (args[i].getTypeCode() == TYPE_STRING) {
aoqi@0 2055 copyArg[i] = true;
aoqi@0 2056 copyCount++;
aoqi@0 2057 }
aoqi@0 2058 }
aoqi@0 2059 }
aoqi@0 2060
aoqi@0 2061 // We're ready to generate code. Do we have more than
aoqi@0 2062 // one to copy?
aoqi@0 2063 if (copyCount > 1) {
aoqi@0 2064 // Generate a call to copyObjects...
aoqi@0 2065 String arrayName = getVariableName("copies");
aoqi@0 2066 p.p("Object[] " + arrayName + " = Util.copyObjects(new Object[]{");
aoqi@0 2067 boolean first = true;
aoqi@0 2068 for (int i = 0; i < args.length; i++) {
aoqi@0 2069 if (copyArg[i]) {
aoqi@0 2070 if (!first) {
aoqi@0 2071 p.p(",");
aoqi@0 2072 }
aoqi@0 2073 first = false;
aoqi@0 2074 p.p(origNames[i]);
aoqi@0 2075 }
aoqi@0 2076 }
aoqi@0 2077 p.pln("},_orb());");
aoqi@0 2078
aoqi@0 2079 // For each of the types which was copied, create
aoqi@0 2080 // a local temporary for it, updating the result
aoqi@0 2081 // array with the new local parameter name...
aoqi@0 2082 int copyIndex = 0 ;
aoqi@0 2083 for (int i = 0; i < args.length; i++) {
aoqi@0 2084 if (copyArg[i]) {
aoqi@0 2085 result[i] = getVariableName(result[i]+"Copy");
aoqi@0 2086 p.pln( getName(args[i]) + " " + result[i] + " = (" + getName(args[i]) + ") " +
aoqi@0 2087 arrayName + "[" + copyIndex++ +"];");
aoqi@0 2088 }
aoqi@0 2089 }
aoqi@0 2090 } else {
aoqi@0 2091 // Generate a call to copyObject, updating the result
aoqi@0 2092 // with the new local parameter name...
aoqi@0 2093 result[firstCopiedArg] = getVariableName(result[firstCopiedArg]+"Copy");
aoqi@0 2094 p.pln( getName(args[firstCopiedArg]) + " " + result[firstCopiedArg] + " = (" +
aoqi@0 2095 getName(args[firstCopiedArg]) + ") Util.copyObject(" +
aoqi@0 2096 origNames[firstCopiedArg] + ",_orb());");
aoqi@0 2097 }
aoqi@0 2098 }
aoqi@0 2099
aoqi@0 2100 return result;
aoqi@0 2101 }
aoqi@0 2102
aoqi@0 2103 static final String SINGLE_SLASH = "\\";
aoqi@0 2104 static final String DOUBLE_SLASH = SINGLE_SLASH + SINGLE_SLASH;
aoqi@0 2105
aoqi@0 2106 String getRepositoryID(Type type) {
aoqi@0 2107 return IDLNames.replace(type.getRepositoryID(), SINGLE_SLASH, DOUBLE_SLASH);
aoqi@0 2108 }
aoqi@0 2109
aoqi@0 2110 String getExceptionRepositoryID(Type type) {
aoqi@0 2111 ClassType theType = (ClassType) type;
aoqi@0 2112 return IDLNames.getIDLRepositoryID(theType.getQualifiedIDLExceptionName(false));
aoqi@0 2113 }
aoqi@0 2114
aoqi@0 2115 String getVariableName(String proposed) {
aoqi@0 2116 while (namesInUse.contains(proposed)) {
aoqi@0 2117 proposed = "$" + proposed;
aoqi@0 2118 }
aoqi@0 2119
aoqi@0 2120 return proposed;
aoqi@0 2121 }
aoqi@0 2122
aoqi@0 2123 void addNamesInUse(CompoundType.Method[] methods) {
aoqi@0 2124 for (int i = 0; i < methods.length; i++) {
aoqi@0 2125 addNamesInUse(methods[i]);
aoqi@0 2126 }
aoqi@0 2127 }
aoqi@0 2128
aoqi@0 2129 void addNamesInUse(CompoundType.Method method) {
aoqi@0 2130 String paramNames[] = method.getArgumentNames();
aoqi@0 2131 for (int i = 0; i < paramNames.length; i++) {
aoqi@0 2132 addNameInUse(paramNames[i]);
aoqi@0 2133 }
aoqi@0 2134 }
aoqi@0 2135
aoqi@0 2136 void addNameInUse(String name) {
aoqi@0 2137 namesInUse.add(name);
aoqi@0 2138 }
aoqi@0 2139
aoqi@0 2140 static boolean mustCopy(Type type) {
aoqi@0 2141 switch (type.getTypeCode()) {
aoqi@0 2142 case TYPE_VOID:
aoqi@0 2143 case TYPE_BOOLEAN:
aoqi@0 2144 case TYPE_BYTE:
aoqi@0 2145 case TYPE_CHAR:
aoqi@0 2146 case TYPE_SHORT:
aoqi@0 2147 case TYPE_INT:
aoqi@0 2148 case TYPE_LONG:
aoqi@0 2149 case TYPE_FLOAT:
aoqi@0 2150 case TYPE_DOUBLE:
aoqi@0 2151 case TYPE_STRING: return false;
aoqi@0 2152
aoqi@0 2153 case TYPE_ANY: return true;
aoqi@0 2154
aoqi@0 2155 case TYPE_CORBA_OBJECT: return false;
aoqi@0 2156
aoqi@0 2157 case TYPE_REMOTE:
aoqi@0 2158 case TYPE_ABSTRACT:
aoqi@0 2159 case TYPE_NC_INTERFACE:
aoqi@0 2160 case TYPE_VALUE:
aoqi@0 2161 case TYPE_IMPLEMENTATION:
aoqi@0 2162 case TYPE_NC_CLASS:
aoqi@0 2163 case TYPE_ARRAY:
aoqi@0 2164 case TYPE_JAVA_RMI_REMOTE: return true;
aoqi@0 2165
aoqi@0 2166 default: throw new Error("unexpected type code: " + type.getTypeCode());
aoqi@0 2167 }
aoqi@0 2168 }
aoqi@0 2169
aoqi@0 2170 ValueType[] getStubExceptions (CompoundType.Method method, boolean sort) {
aoqi@0 2171
aoqi@0 2172 ValueType[] list = method.getFilteredStubExceptions(method.getExceptions());
aoqi@0 2173
aoqi@0 2174 // Sort the list so that all org.omg.CORBA.UserException
aoqi@0 2175 // subtypes are at the beginning of the list. This ensures
aoqi@0 2176 // that the stub will not call read_string() before calling
aoqi@0 2177 // XXHelper.read().
aoqi@0 2178
aoqi@0 2179 if (sort) {
aoqi@0 2180 Arrays.sort(list,new UserExceptionComparator());
aoqi@0 2181 }
aoqi@0 2182
aoqi@0 2183 return list;
aoqi@0 2184 }
aoqi@0 2185
aoqi@0 2186 ValueType[] getTieExceptions (CompoundType.Method method) {
aoqi@0 2187 return method.getUniqueCatchList(method.getImplExceptions());
aoqi@0 2188 }
aoqi@0 2189
aoqi@0 2190 void writeTieMethod(IndentingWriter p, CompoundType type,
aoqi@0 2191 CompoundType.Method method) throws IOException {
aoqi@0 2192 String methodName = method.getName();
aoqi@0 2193 Type paramTypes[] = method.getArguments();
aoqi@0 2194 String paramNames[] = method.getArgumentNames();
aoqi@0 2195 Type returnType = method.getReturnType();
aoqi@0 2196 ValueType[] exceptions = getTieExceptions(method);
aoqi@0 2197 String in = getVariableName("in");
aoqi@0 2198 String ex = getVariableName("ex");
aoqi@0 2199 String out = getVariableName("out");
aoqi@0 2200 String reply = getVariableName("reply");
aoqi@0 2201
aoqi@0 2202 for (int i = 0; i < paramTypes.length; i++) {
aoqi@0 2203 p.p(getName(paramTypes[i])+" "+paramNames[i]+" = ");
aoqi@0 2204 writeUnmarshalArgument(p, in, paramTypes[i], null);
aoqi@0 2205 p.pln();
aoqi@0 2206 }
aoqi@0 2207
aoqi@0 2208 boolean handleExceptions = exceptions != null;
aoqi@0 2209 boolean doReturn = !returnType.isType(TYPE_VOID);
aoqi@0 2210
aoqi@0 2211 if (handleExceptions && doReturn) {
aoqi@0 2212 String objName = testUtil(getName(returnType), returnType);
aoqi@0 2213 p.pln(objName+" result;");
aoqi@0 2214 }
aoqi@0 2215
aoqi@0 2216 if (handleExceptions)
aoqi@0 2217 p.plnI("try {");
aoqi@0 2218
aoqi@0 2219 if (doReturn) {
aoqi@0 2220 if (handleExceptions) {
aoqi@0 2221 p.p("result = ");
aoqi@0 2222 } else {
aoqi@0 2223 p.p(getName(returnType)+" result = ");
aoqi@0 2224 }
aoqi@0 2225 }
aoqi@0 2226
aoqi@0 2227 p.p("target."+methodName+"(");
aoqi@0 2228 for(int i = 0; i < paramNames.length; i++) {
aoqi@0 2229 if (i > 0)
aoqi@0 2230 p.p(", ");
aoqi@0 2231 p.p(paramNames[i]);
aoqi@0 2232 }
aoqi@0 2233 p.pln(");");
aoqi@0 2234
aoqi@0 2235 if (handleExceptions) {
aoqi@0 2236 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 2237 p.pOlnI("} catch ("+getName(exceptions[i])+" "+ex+") {");
aoqi@0 2238
aoqi@0 2239 // Is this our IDLEntity Exception special case?
aoqi@0 2240
aoqi@0 2241 if (exceptions[i].isIDLEntityException() && !exceptions[i].isCORBAUserException()) {
aoqi@0 2242
aoqi@0 2243 // Yes...
aoqi@0 2244
aoqi@0 2245 String helperName = IDLNames.replace(exceptions[i].getQualifiedIDLName(false),"::",".");
aoqi@0 2246 helperName += "Helper";
aoqi@0 2247 p.pln(idOutputStream+" "+out +" = "+reply+".createExceptionReply();");
aoqi@0 2248 p.pln(helperName+".write("+out+","+ex+");");
aoqi@0 2249
aoqi@0 2250 } else {
aoqi@0 2251
aoqi@0 2252 // No...
aoqi@0 2253
aoqi@0 2254 p.pln("String id = \"" + getExceptionRepositoryID(exceptions[i]) + "\";");
aoqi@0 2255 p.plnI(idExtOutputStream + " "+out+" = ");
aoqi@0 2256 p.pln("(" + idExtOutputStream + ") "+reply+".createExceptionReply();");
aoqi@0 2257 p.pOln(out+".write_string(id);");
aoqi@0 2258 p.pln(out+".write_value("+ex+"," + getName(exceptions[i]) + ".class);");
aoqi@0 2259 }
aoqi@0 2260
aoqi@0 2261 p.pln("return "+out+";");
aoqi@0 2262 }
aoqi@0 2263 p.pOln("}");
aoqi@0 2264 }
aoqi@0 2265
aoqi@0 2266 if (needNewWriteStreamClass(returnType)) {
aoqi@0 2267 p.plnI(idExtOutputStream + " "+out+" = ");
aoqi@0 2268 p.pln("(" + idExtOutputStream + ") "+reply+".createReply();");
aoqi@0 2269 p.pO();
aoqi@0 2270 } else {
aoqi@0 2271 p.pln("OutputStream "+out+" = "+reply+".createReply();");
aoqi@0 2272 }
aoqi@0 2273
aoqi@0 2274 if (doReturn) {
aoqi@0 2275 writeMarshalArgument(p, out, returnType, "result");
aoqi@0 2276 p.pln();
aoqi@0 2277 }
aoqi@0 2278
aoqi@0 2279 p.pln("return "+out+";");
aoqi@0 2280 }
aoqi@0 2281
aoqi@0 2282
aoqi@0 2283 /**
aoqi@0 2284 * Write Java statements to marshal a series of values in order as
aoqi@0 2285 * named in the "names" array, with types as specified in the "types"
aoqi@0 2286 * array", to the java.io.ObjectOutput stream named "stream".
aoqi@0 2287 */
aoqi@0 2288 void writeMarshalArguments(IndentingWriter p,
aoqi@0 2289 String streamName,
aoqi@0 2290 Type[] types, String[] names)
aoqi@0 2291 throws IOException
aoqi@0 2292 {
aoqi@0 2293 if (types.length != names.length) {
aoqi@0 2294 throw new Error("paramter type and name arrays different sizes");
aoqi@0 2295 }
aoqi@0 2296
aoqi@0 2297 for (int i = 0; i < types.length; i++) {
aoqi@0 2298 writeMarshalArgument(p, streamName, types[i], names[i]);
aoqi@0 2299 if (i != types.length -1) {
aoqi@0 2300 p.pln();
aoqi@0 2301 }
aoqi@0 2302 }
aoqi@0 2303 }
aoqi@0 2304
aoqi@0 2305 /**
aoqi@0 2306 * Added for IASRI 4987274. Remote classes named "Util" were
aoqi@0 2307 * getting confused with javax.rmi.CORBA.Util and the
aoqi@0 2308 * unqualifiedName "Util".
aoqi@0 2309 */
aoqi@0 2310 String testUtil(String objectName, Type ttype) {
aoqi@0 2311 if (objectName.equals("Util")) {
aoqi@0 2312 String correctedName = (String)ttype.getPackageName() + "." + objectName;
aoqi@0 2313 return correctedName;
aoqi@0 2314 } else {
aoqi@0 2315 return objectName;
aoqi@0 2316 }
aoqi@0 2317 }
aoqi@0 2318 }
aoqi@0 2319
aoqi@0 2320 class StringComparator implements java.util.Comparator {
aoqi@0 2321 public int compare(Object o1, Object o2) {
aoqi@0 2322 String s1 = (String)o1;
aoqi@0 2323 String s2 = (String)o2;
aoqi@0 2324 return s1.compareTo(s2);
aoqi@0 2325 }
aoqi@0 2326 }
aoqi@0 2327
aoqi@0 2328
aoqi@0 2329 class UserExceptionComparator implements java.util.Comparator {
aoqi@0 2330 public int compare(Object o1, Object o2) {
aoqi@0 2331 ValueType v1 = (ValueType)o1;
aoqi@0 2332 ValueType v2 = (ValueType)o2;
aoqi@0 2333 int result = 0;
aoqi@0 2334 if (isUserException(v1)) {
aoqi@0 2335 if (!isUserException(v2)) {
aoqi@0 2336 result = -1;
aoqi@0 2337 }
aoqi@0 2338 } else if (isUserException(v2)) {
aoqi@0 2339 if (!isUserException(v1)) {
aoqi@0 2340 result = 1;
aoqi@0 2341 }
aoqi@0 2342 }
aoqi@0 2343 return result;
aoqi@0 2344 }
aoqi@0 2345
aoqi@0 2346 final boolean isUserException(ValueType it) {
aoqi@0 2347 return it.isIDLEntityException() && !it.isCORBAUserException();
aoqi@0 2348 }
aoqi@0 2349 }

mercurial