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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 2030
6a4286d4ceb1
parent 1410
9c913ea7e4a1
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset 146eabb0a016

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
msheppar@1052 449 //
msheppar@1052 450 // private transient boolean _instantiated = false;
msheppar@1052 451 //
aoqi@0 452 // private static Void checkPermission() {
aoqi@0 453 // SecurityManager sm = System.getSecurityManager();
aoqi@0 454 // if (sm != null) {
aoqi@0 455 // sm.checkPermission(new SerializablePermission(
aoqi@0 456 // "enableSubclassImplementation")); // testing
aoqi@0 457 // }
aoqi@0 458 // return null;
aoqi@0 459 // }
aoqi@0 460 //
aoqi@0 461 // private _XXXXX_Stub(Void ignore) {
aoqi@0 462 // }
aoqi@0 463 //
aoqi@0 464 // public _XXXXX_Stub() {
aoqi@0 465 // this(checkPermission());
msheppar@1052 466 // _instantiated = true;
msheppar@1052 467 // }
msheppar@1052 468 //
msheppar@1052 469 // private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
msheppar@1052 470 // checkPermission();
msheppar@1052 471 // s.defaultReadObject();
msheppar@1052 472 // _instantiated = true;
aoqi@0 473 // }
aoqi@0 474 //
aoqi@0 475 // where XXXXX is the name of the remote interface
aoqi@0 476
aoqi@0 477 p.pln();
msheppar@1052 478 p.plnI("private transient boolean _instantiated = false;");
msheppar@1052 479 p.pln();
msheppar@1052 480 p.pO();
aoqi@0 481 p.plnI("private static Void checkPermission() {");
aoqi@0 482 p.plnI("SecurityManager sm = System.getSecurityManager();");
aoqi@0 483 p.pln("if (sm != null) {");
aoqi@0 484 p.pI();
aoqi@0 485 p.plnI("sm.checkPermission(new SerializablePermission(");
aoqi@0 486 p.plnI("\"enableSubclassImplementation\"));");
aoqi@0 487 p.pO();
aoqi@0 488 p.pO();
aoqi@0 489 p.pOln("}");
aoqi@0 490 p.pln("return null;");
aoqi@0 491 p.pO();
aoqi@0 492 p.pOln("}");
aoqi@0 493 p.pln();
aoqi@0 494 p.pO();
aoqi@0 495
aoqi@0 496 p.pI();
msheppar@1052 497 p.plnI("private " + currentClass + "(Void ignore) { }");
aoqi@0 498 p.pln();
msheppar@1052 499 p.pO();
msheppar@1052 500
msheppar@1052 501 p.plnI("public " + currentClass + "() {");
aoqi@0 502 p.pln("this(checkPermission());");
msheppar@1052 503 p.pln("_instantiated = true;");
aoqi@0 504 p.pOln("}");
aoqi@0 505 p.pln();
msheppar@1052 506 p.plnI("private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {");
msheppar@1052 507 p.plnI("checkPermission();");
msheppar@1052 508 p.pO();
msheppar@1052 509 p.pln("s.defaultReadObject();");
msheppar@1052 510 p.pln("_instantiated = true;");
msheppar@1052 511 p.pOln("}");
msheppar@1052 512 p.pln();
msheppar@1052 513 //p.pO();
aoqi@0 514 }
aoqi@0 515
aoqi@0 516 if (!emitPermissionCheck) {
aoqi@0 517 p.pI();
aoqi@0 518 }
aoqi@0 519
aoqi@0 520 // Write the _ids() method...
aoqi@0 521
aoqi@0 522 p.plnI("public String[] _ids() { ");
aoqi@0 523 p.pln("return (String[]) _type_ids.clone();");
aoqi@0 524 p.pOln("}");
aoqi@0 525
aoqi@0 526 // Get all the methods and write each stub method...
aoqi@0 527
aoqi@0 528 CompoundType.Method[] remoteMethods = theType.getMethods();
aoqi@0 529 int methodCount = remoteMethods.length;
aoqi@0 530 if (methodCount > 0) {
aoqi@0 531 boolean writeHeader = true;
aoqi@0 532 for(int i = 0; i < methodCount; i++) {
aoqi@0 533 if (!remoteMethods[i].isConstructor()) {
aoqi@0 534 if (writeHeader) {
aoqi@0 535 writeHeader = false;
aoqi@0 536 }
aoqi@0 537 p.pln();
aoqi@0 538 writeStubMethod(p, remoteMethods[i], theType);
aoqi@0 539 }
aoqi@0 540 }
aoqi@0 541 }
aoqi@0 542
aoqi@0 543 // Write the cast array hack...
aoqi@0 544
aoqi@0 545 writeCastArray(p);
aoqi@0 546
aoqi@0 547 p.pOln("}"); // end stub class
aoqi@0 548 }
aoqi@0 549
aoqi@0 550 void addClassInUse(String qualifiedName) {
aoqi@0 551 String unqualifiedName = qualifiedName;
aoqi@0 552 String packageName = null;
aoqi@0 553 int index = qualifiedName.lastIndexOf('.');
aoqi@0 554 if (index > 0) {
aoqi@0 555 unqualifiedName = qualifiedName.substring(index+1);
aoqi@0 556 packageName = qualifiedName.substring(0,index);
aoqi@0 557 }
aoqi@0 558 addClassInUse(unqualifiedName,qualifiedName,packageName);
aoqi@0 559 }
aoqi@0 560
aoqi@0 561 void addClassInUse(Type type) {
aoqi@0 562 if (!type.isPrimitive()) {
aoqi@0 563 Identifier id = type.getIdentifier();
aoqi@0 564 String name = IDLNames.replace(id.getName().toString(),". ",".");
aoqi@0 565 String packageName = type.getPackageName();
aoqi@0 566 String qualifiedName;
aoqi@0 567 if (packageName != null) {
aoqi@0 568 qualifiedName = packageName+"."+name;
aoqi@0 569 } else {
aoqi@0 570 qualifiedName = name;
aoqi@0 571 }
aoqi@0 572 addClassInUse(name,qualifiedName,packageName);
aoqi@0 573 }
aoqi@0 574 }
aoqi@0 575
aoqi@0 576 void addClassInUse(Type[] types) {
aoqi@0 577 for (int i = 0; i < types.length; i++) {
aoqi@0 578 addClassInUse(types[i]);
aoqi@0 579 }
aoqi@0 580 }
aoqi@0 581
aoqi@0 582 void addStubInUse(Type type) {
aoqi@0 583 if (type.getIdentifier() != idCorbaObject &&
aoqi@0 584 type.isType(TYPE_CORBA_OBJECT)) {
aoqi@0 585 String stubName = getStubNameFor(type,false);
aoqi@0 586 String packageName = type.getPackageName();
aoqi@0 587 String fullName;
aoqi@0 588 if (packageName == null) {
aoqi@0 589 fullName = stubName;
aoqi@0 590 } else {
aoqi@0 591 fullName = packageName + "." + stubName;
aoqi@0 592 }
aoqi@0 593 addClassInUse(stubName,fullName,packageName);
aoqi@0 594 }
aoqi@0 595 if (type.isType(TYPE_REMOTE) ||
aoqi@0 596 type.isType(TYPE_JAVA_RMI_REMOTE)) {
aoqi@0 597 addClassInUse("javax.rmi.PortableRemoteObject");
aoqi@0 598 }
aoqi@0 599 }
aoqi@0 600
aoqi@0 601 String getStubNameFor(Type type, boolean qualified) {
aoqi@0 602 String stubName;
aoqi@0 603 String className;
aoqi@0 604 if (qualified) {
aoqi@0 605 className = type.getQualifiedName();
aoqi@0 606 } else {
aoqi@0 607 className = type.getName();
aoqi@0 608 }
aoqi@0 609 if (((CompoundType)type).isCORBAObject()) {
aoqi@0 610 stubName = Utility.idlStubName(className);
aoqi@0 611 } else {
aoqi@0 612 stubName = Utility.stubNameForCompiler(className);
aoqi@0 613 }
aoqi@0 614 return stubName;
aoqi@0 615 }
aoqi@0 616
aoqi@0 617 void addStubInUse(Type[] types) {
aoqi@0 618 for (int i = 0; i < types.length; i++) {
aoqi@0 619 addStubInUse(types[i]);
aoqi@0 620 }
aoqi@0 621 }
aoqi@0 622
aoqi@0 623 private static final String NO_IMPORT = new String();
aoqi@0 624
aoqi@0 625 void addClassInUse(String unqualifiedName, String qualifiedName, String packageName) {
aoqi@0 626
aoqi@0 627 // Have we already got an entry for this qualifiedName?
aoqi@0 628
aoqi@0 629 String currentName = (String)classesInUse.get(qualifiedName);
aoqi@0 630
aoqi@0 631 if (currentName == null) {
aoqi@0 632
aoqi@0 633 // No, never seen it before. Grab any existing import
aoqi@0 634 // name and then decide what to do...
aoqi@0 635
aoqi@0 636 String importName = (String) imports.get(unqualifiedName);
aoqi@0 637 String nameToUse = null;
aoqi@0 638
aoqi@0 639 if (packageName == null) {
aoqi@0 640
aoqi@0 641 // Default package, so doesn't matter which name to use...
aoqi@0 642
aoqi@0 643 nameToUse = unqualifiedName;
aoqi@0 644
aoqi@0 645 } else if (packageName.equals("java.lang")) {
aoqi@0 646
aoqi@0 647 // java.lang.*, so use unqualified name...
aoqi@0 648
aoqi@0 649 nameToUse = unqualifiedName;
aoqi@0 650
aoqi@0 651 // unless you want to be able to import things from the right place :--)
aoqi@0 652
aoqi@0 653 if(nameToUse.endsWith("_Stub")) nameToUse = Util.packagePrefix()+qualifiedName;
aoqi@0 654
aoqi@0 655 } else if (currentPackage != null && packageName.equals(currentPackage)) {
aoqi@0 656
aoqi@0 657 // Class in currentPackage, so use unqualified name...
aoqi@0 658
aoqi@0 659 nameToUse = unqualifiedName;
aoqi@0 660
aoqi@0 661 // Do we already have a previous import under this
aoqi@0 662 // unqualified name?
aoqi@0 663
aoqi@0 664 if (importName != null) {
aoqi@0 665
aoqi@0 666 // Yes, so we use qualifiedName...
aoqi@0 667
aoqi@0 668 nameToUse = qualifiedName;
aoqi@0 669
aoqi@0 670 }
aoqi@0 671
aoqi@0 672 } else if (importName != null) {
aoqi@0 673
aoqi@0 674 // It is in some package for which we normally
aoqi@0 675 // would import, but we have a previous import
aoqi@0 676 // under this unqualified name. We must use
aoqi@0 677 // the qualified name...
aoqi@0 678
aoqi@0 679 nameToUse = qualifiedName;
aoqi@0 680
aoqi@0 681 /*
aoqi@0 682 // Is the currentPackage the default package?
aoqi@0 683
aoqi@0 684 if (currentPackage == null) {
aoqi@0 685
aoqi@0 686 // Yes, so undo the import so that all
aoqi@0 687 // uses for this name will be qualified...
aoqi@0 688
aoqi@0 689 String old = (String)imports.remove(unqualifiedName);
aoqi@0 690 classesInUse.put(old,old);
aoqi@0 691 importCount--;
aoqi@0 692
aoqi@0 693 // Note that this name is in use but should
aoqi@0 694 // not be imported...
aoqi@0 695
aoqi@0 696 imports.put(nameToUse,NO_IMPORT);
aoqi@0 697 }
aoqi@0 698 */
aoqi@0 699 } else if (qualifiedName.equals("org.omg.CORBA.Object")) {
aoqi@0 700
aoqi@0 701 // Always qualify this quy to avoid confusion...
aoqi@0 702
aoqi@0 703 nameToUse = qualifiedName;
aoqi@0 704
aoqi@0 705 } else {
aoqi@0 706
aoqi@0 707 // Default to using unqualified name, and add
aoqi@0 708 // this guy to the imports...
aoqi@0 709
aoqi@0 710 // Check for nested class in which case we use
aoqi@0 711 // the fully qualified name instead of imports
aoqi@0 712 if (unqualifiedName.indexOf('.') != -1) {
aoqi@0 713 nameToUse = qualifiedName;
aoqi@0 714 } else {
aoqi@0 715 nameToUse = unqualifiedName;
aoqi@0 716 imports.put(unqualifiedName,qualifiedName);
aoqi@0 717 importCount++;
aoqi@0 718 }
aoqi@0 719 }
aoqi@0 720
aoqi@0 721 // Now add the name...
aoqi@0 722
aoqi@0 723 classesInUse.put(qualifiedName,nameToUse);
aoqi@0 724 }
aoqi@0 725 }
aoqi@0 726
aoqi@0 727 String getName(Type type) {
aoqi@0 728 if (type.isPrimitive()) {
aoqi@0 729 return type.getName() + type.getArrayBrackets();
aoqi@0 730 }
aoqi@0 731 Identifier id = type.getIdentifier();
aoqi@0 732 String name = IDLNames.replace(id.toString(),". ",".");
aoqi@0 733 return getName(name) + type.getArrayBrackets();
aoqi@0 734 }
aoqi@0 735
aoqi@0 736 // Added for Bug 4818753
aoqi@0 737 String getExceptionName(Type type) {
aoqi@0 738 Identifier id = type.getIdentifier();
aoqi@0 739 return IDLNames.replace(id.toString(),". ",".");
aoqi@0 740 }
aoqi@0 741
aoqi@0 742 String getName(String qualifiedName) {
aoqi@0 743 return (String)classesInUse.get(qualifiedName);
aoqi@0 744 }
aoqi@0 745
aoqi@0 746 String getName(Identifier id) {
aoqi@0 747 return getName(id.toString());
aoqi@0 748 }
aoqi@0 749
aoqi@0 750 String getStubName(Type type) {
aoqi@0 751 String stubName = getStubNameFor(type,true);
aoqi@0 752 return getName(stubName);
aoqi@0 753 }
aoqi@0 754
aoqi@0 755 void setStandardClassesInUse(CompoundType type,
aoqi@0 756 boolean stub) throws IOException {
aoqi@0 757
aoqi@0 758 // Reset our state...
aoqi@0 759
aoqi@0 760 currentPackage = type.getPackageName();
aoqi@0 761 imports.clear();
aoqi@0 762 classesInUse.clear();
aoqi@0 763 namesInUse.clear();
aoqi@0 764 importCount = 0;
aoqi@0 765 castArray = false;
aoqi@0 766
aoqi@0 767 // Add the top-level type...
aoqi@0 768
aoqi@0 769 addClassInUse(type);
aoqi@0 770
aoqi@0 771 // Set current class name...
aoqi@0 772
aoqi@0 773 if (stub) {
aoqi@0 774 currentClass = Utility.stubNameForCompiler(type.getName());
aoqi@0 775 } else {
aoqi@0 776 currentClass = Utility.tieNameForCompiler(type.getName());
aoqi@0 777 }
aoqi@0 778
aoqi@0 779 // Add current class...
aoqi@0 780
aoqi@0 781 if (currentPackage == null) {
aoqi@0 782 addClassInUse(currentClass,currentClass,currentPackage);
aoqi@0 783 } else {
aoqi@0 784 addClassInUse(currentClass,(currentPackage+"."+currentClass),currentPackage);
aoqi@0 785 }
aoqi@0 786
aoqi@0 787 // Add standard classes...
aoqi@0 788
aoqi@0 789 addClassInUse("javax.rmi.CORBA.Util");
aoqi@0 790 addClassInUse(idRemote.toString());
aoqi@0 791 addClassInUse(idRemoteException.toString());
aoqi@0 792 addClassInUse(idOutputStream.toString());
aoqi@0 793 addClassInUse(idInputStream.toString());
aoqi@0 794 addClassInUse(idSystemException.toString());
aoqi@0 795 addClassInUse(idJavaIoSerializable.toString());
aoqi@0 796 addClassInUse(idCorbaORB.toString());
aoqi@0 797 addClassInUse(idReplyHandler.toString());
aoqi@0 798
aoqi@0 799 // Add stub/tie specific imports...
aoqi@0 800
aoqi@0 801 if (stub) {
aoqi@0 802 addClassInUse(stubBaseClass);
aoqi@0 803 addClassInUse("java.rmi.UnexpectedException");
aoqi@0 804 addClassInUse(idRemarshalException.toString());
aoqi@0 805 addClassInUse(idApplicationException.toString());
aoqi@0 806 if (localStubs) {
aoqi@0 807 addClassInUse("org.omg.CORBA.portable.ServantObject");
aoqi@0 808 }
aoqi@0 809 } else {
aoqi@0 810 addClassInUse(type);
aoqi@0 811 addClassInUse(tieBaseClass);
aoqi@0 812 addClassInUse(idTieInterface.toString());
aoqi@0 813 addClassInUse(idBadMethodException.toString());
aoqi@0 814 addClassInUse(idPortableUnknownException.toString());
aoqi@0 815 addClassInUse(idJavaLangThrowable.toString());
aoqi@0 816 }
aoqi@0 817 }
aoqi@0 818
aoqi@0 819 void addClassesInUse(CompoundType type, RemoteType[] interfaces) {
aoqi@0 820
aoqi@0 821 // Walk all methods and add types in use...
aoqi@0 822
aoqi@0 823 CompoundType.Method[] methods = type.getMethods();
aoqi@0 824 for (int i = 0; i < methods.length; i++) {
aoqi@0 825 addClassInUse(methods[i].getReturnType());
aoqi@0 826 addStubInUse(methods[i].getReturnType());
aoqi@0 827 addClassInUse(methods[i].getArguments());
aoqi@0 828 addStubInUse(methods[i].getArguments());
aoqi@0 829 addClassInUse(methods[i].getExceptions());
aoqi@0 830 // bug 4473859: Also include narrower subtypes for use
aoqi@0 831 addClassInUse(methods[i].getImplExceptions());
aoqi@0 832 }
aoqi@0 833
aoqi@0 834 // If this is a stub, add all interfaces...
aoqi@0 835
aoqi@0 836 if (interfaces != null) {
aoqi@0 837 addClassInUse(interfaces);
aoqi@0 838 }
aoqi@0 839 }
aoqi@0 840
aoqi@0 841 void writePackageAndImports(IndentingWriter p) throws IOException {
aoqi@0 842
aoqi@0 843 // Write package declaration...
aoqi@0 844
aoqi@0 845 if (currentPackage != null) {
aoqi@0 846 p.pln("package " +
aoqi@0 847 Util.correctPackageName(
aoqi@0 848 currentPackage, false, standardPackage)
aoqi@0 849 + ";");
aoqi@0 850 p.pln();
aoqi@0 851 }
aoqi@0 852
aoqi@0 853 // Get imports into an array and sort them...
aoqi@0 854
aoqi@0 855 String[] names = new String[importCount];
aoqi@0 856 int index = 0;
aoqi@0 857 for (Enumeration e = imports.elements() ; e.hasMoreElements() ;) {
aoqi@0 858 String it = (String) e.nextElement();
aoqi@0 859 if (it != NO_IMPORT) {
aoqi@0 860 names[index++] = it;
aoqi@0 861 }
aoqi@0 862 }
aoqi@0 863
aoqi@0 864 Arrays.sort(names,new StringComparator());
aoqi@0 865
aoqi@0 866 // Now dump them out...
aoqi@0 867
aoqi@0 868 for (int i = 0; i < importCount; i++) {
aoqi@0 869 if(
aoqi@0 870 Util.isOffendingPackage(names[i])
aoqi@0 871 && names[i].endsWith("_Stub")
aoqi@0 872 && String.valueOf(names[i].charAt(names[i].lastIndexOf(".")+1)).equals("_")
aoqi@0 873 ){
aoqi@0 874 p.pln("import " + PackagePrefixChecker.packagePrefix()+names[i]+";");
aoqi@0 875 } else{
aoqi@0 876 p.pln("import " + names[i] + ";");
aoqi@0 877 }
aoqi@0 878 }
aoqi@0 879 p.pln();
aoqi@0 880
aoqi@0 881 // Include offending packages . . .
aoqi@0 882 if ( currentPackage!=null && Util.isOffendingPackage(currentPackage) ){
aoqi@0 883 p.pln("import " + currentPackage +".* ;");
aoqi@0 884 }
aoqi@0 885 p.pln();
aoqi@0 886
aoqi@0 887 }
aoqi@0 888
aoqi@0 889 boolean implementsRemote(CompoundType theType) {
aoqi@0 890 boolean result = theType.isType(TYPE_REMOTE) && !theType.isType(TYPE_ABSTRACT);
aoqi@0 891
aoqi@0 892 // If theType is not remote, look at all the interfaces
aoqi@0 893 // until we find one that is...
aoqi@0 894
aoqi@0 895 if (!result) {
aoqi@0 896 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 897 for (int i = 0; i < interfaces.length; i++) {
aoqi@0 898 result = implementsRemote(interfaces[i]);
aoqi@0 899 if (result) {
aoqi@0 900 break;
aoqi@0 901 }
aoqi@0 902 }
aoqi@0 903 }
aoqi@0 904
aoqi@0 905 return result;
aoqi@0 906 }
aoqi@0 907
aoqi@0 908 void writeStubMethod ( IndentingWriter p,
aoqi@0 909 CompoundType.Method method,
aoqi@0 910 CompoundType theType) throws IOException {
aoqi@0 911
aoqi@0 912 // Wtite the method declaration and opening brace...
aoqi@0 913 String methodName = method.getName();
aoqi@0 914 String methodIDLName = method.getIDLName();
aoqi@0 915
aoqi@0 916 Type paramTypes[] = method.getArguments();
aoqi@0 917 String paramNames[] = method.getArgumentNames();
aoqi@0 918 Type returnType = method.getReturnType();
aoqi@0 919 ValueType[] exceptions = getStubExceptions(method,false);
msheppar@1052 920 boolean hasIOException = false;
aoqi@0 921
aoqi@0 922 addNamesInUse(method);
aoqi@0 923 addNameInUse("_type_ids");
aoqi@0 924
aoqi@0 925 String objName = testUtil(getName(returnType), returnType);
aoqi@0 926 p.p("public " + objName + " " + methodName + "(");
aoqi@0 927 for(int i = 0; i < paramTypes.length; i++) {
aoqi@0 928 if (i > 0)
aoqi@0 929 p.p(", ");
aoqi@0 930 p.p(getName(paramTypes[i]) + " " + paramNames[i]);
aoqi@0 931 }
aoqi@0 932
aoqi@0 933 p.p(")");
aoqi@0 934 if (exceptions.length > 0) {
aoqi@0 935 p.p(" throws ");
aoqi@0 936 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 937 if (i > 0) {
aoqi@0 938 p.p(", ");
aoqi@0 939 }
aoqi@0 940 // Added for Bug 4818753
aoqi@0 941 p.p(getExceptionName(exceptions[i]));
aoqi@0 942 }
aoqi@0 943 }
aoqi@0 944
aoqi@0 945 p.plnI(" {");
aoqi@0 946
aoqi@0 947 // Now create the method body...
msheppar@1052 948 if (emitPermissionCheck) {
msheppar@1052 949 p.pln("if ((System.getSecurityManager() != null) && (!_instantiated)) {");
msheppar@1052 950 p.plnI(" throw new java.io.IOError(new java.io.IOException(\"InvalidObject \"));");
msheppar@1052 951 p.pOln("}");
msheppar@1052 952 p.pln();
msheppar@1052 953 }
msheppar@1052 954
aoqi@0 955
aoqi@0 956 if (localStubs) {
aoqi@0 957 writeLocalStubMethodBody(p,method,theType);
aoqi@0 958 } else {
aoqi@0 959 writeNonLocalStubMethodBody(p,method,theType);
aoqi@0 960 }
aoqi@0 961
aoqi@0 962 // Close out the method...
aoqi@0 963
aoqi@0 964 p.pOln("}");
aoqi@0 965 }
aoqi@0 966
aoqi@0 967
aoqi@0 968 void writeLocalStubMethodBody (IndentingWriter p,
aoqi@0 969 CompoundType.Method method,
aoqi@0 970 CompoundType theType) throws IOException {
aoqi@0 971
aoqi@0 972 String objName;
aoqi@0 973 String paramNames[] = method.getArgumentNames();
aoqi@0 974 Type returnType = method.getReturnType();
aoqi@0 975 ValueType[] exceptions = getStubExceptions(method,false);
aoqi@0 976 String methodName = method.getName();
aoqi@0 977 String methodIDLName = method.getIDLName();
aoqi@0 978
aoqi@0 979 p.plnI("if (!Util.isLocal(this)) {");
aoqi@0 980 writeNonLocalStubMethodBody(p,method,theType);
aoqi@0 981 p.pOlnI("} else {");
aoqi@0 982 String so = getVariableName("so");
aoqi@0 983
aoqi@0 984 p.pln("ServantObject "+so+" = _servant_preinvoke(\""+methodIDLName+"\","+getName(theType)+".class);");
aoqi@0 985 p.plnI("if ("+so+" == null) {");
aoqi@0 986 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 987 p.p("return ");
aoqi@0 988 }
aoqi@0 989 p.p(methodName+"(");
aoqi@0 990 for (int i = 0; i < paramNames.length; i++) {
aoqi@0 991 if (i > 0)
aoqi@0 992 p.p(", ");
aoqi@0 993 p.p(paramNames[i]);
aoqi@0 994 }
aoqi@0 995 p.pln(");");
aoqi@0 996 if (returnType.isType(TYPE_VOID)) {
aoqi@0 997 p.pln( "return ;" ) ;
aoqi@0 998 }
aoqi@0 999
aoqi@0 1000 p.pOln("}");
aoqi@0 1001 p.plnI("try {");
aoqi@0 1002
aoqi@0 1003 // Generate code to copy required arguments, and
aoqi@0 1004 // get back the names by which all arguments are known...
aoqi@0 1005
aoqi@0 1006 String[] argNames = writeCopyArguments(method,p);
aoqi@0 1007
aoqi@0 1008 // Now write the method...
aoqi@0 1009
aoqi@0 1010 boolean copyReturn = mustCopy(returnType);
aoqi@0 1011 String resultName = null;
aoqi@0 1012 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 1013 if (copyReturn) {
aoqi@0 1014 resultName = getVariableName("result");
aoqi@0 1015 objName = testUtil(getName(returnType), returnType);
aoqi@0 1016 p.p(objName+" "+resultName + " = ");
aoqi@0 1017 } else {
aoqi@0 1018 p.p("return ");
aoqi@0 1019 }
aoqi@0 1020 }
aoqi@0 1021 objName = testUtil(getName(theType), theType);
aoqi@0 1022 p.p("(("+objName+")"+so+".servant)."+methodName+"(");
aoqi@0 1023
aoqi@0 1024 for (int i = 0; i < argNames.length; i++) {
aoqi@0 1025 if (i > 0)
aoqi@0 1026 p.p(", ");
aoqi@0 1027 p.p(argNames[i]);
aoqi@0 1028 }
aoqi@0 1029
aoqi@0 1030 if (copyReturn) {
aoqi@0 1031 p.pln(");");
aoqi@0 1032 objName = testUtil(getName(returnType), returnType);
aoqi@0 1033 p.pln("return ("+objName+")Util.copyObject("+resultName+",_orb());");
aoqi@0 1034 } else {
aoqi@0 1035 p.pln(");");
aoqi@0 1036 }
aoqi@0 1037
aoqi@0 1038 String e1 = getVariableName("ex");
aoqi@0 1039 String e2 = getVariableName("exCopy");
aoqi@0 1040 p.pOlnI("} catch (Throwable "+e1+") {");
aoqi@0 1041
aoqi@0 1042 p.pln("Throwable "+e2+" = (Throwable)Util.copyObject("+e1+",_orb());");
aoqi@0 1043 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 1044 if (exceptions[i].getIdentifier() != idRemoteException &&
aoqi@0 1045 exceptions[i].isType(TYPE_VALUE)) {
aoqi@0 1046 // Added for Bug 4818753
aoqi@0 1047 p.plnI("if ("+e2+" instanceof "+getExceptionName(exceptions[i])+") {");
aoqi@0 1048 p.pln("throw ("+getExceptionName(exceptions[i])+")"+e2+";");
aoqi@0 1049 p.pOln("}");
aoqi@0 1050 }
aoqi@0 1051 }
aoqi@0 1052
aoqi@0 1053 p.pln("throw Util.wrapException("+e2+");");
aoqi@0 1054 p.pOlnI("} finally {");
aoqi@0 1055 p.pln("_servant_postinvoke("+so+");");
aoqi@0 1056 p.pOln("}");
aoqi@0 1057 p.pOln("}");
aoqi@0 1058 }
aoqi@0 1059
aoqi@0 1060
aoqi@0 1061 void writeNonLocalStubMethodBody ( IndentingWriter p,
aoqi@0 1062 CompoundType.Method method,
aoqi@0 1063 CompoundType theType) throws IOException {
aoqi@0 1064
aoqi@0 1065 String methodName = method.getName();
aoqi@0 1066 String methodIDLName = method.getIDLName();
aoqi@0 1067
aoqi@0 1068 Type paramTypes[] = method.getArguments();
aoqi@0 1069 String paramNames[] = method.getArgumentNames();
aoqi@0 1070 Type returnType = method.getReturnType();
aoqi@0 1071 ValueType[] exceptions = getStubExceptions(method,true);
aoqi@0 1072
aoqi@0 1073 String in = getVariableName("in");
aoqi@0 1074 String out = getVariableName("out");
aoqi@0 1075 String ex = getVariableName("ex");
aoqi@0 1076
aoqi@0 1077 // Decide if we need to use the new streams for
aoqi@0 1078 // any of the read calls...
aoqi@0 1079
aoqi@0 1080 boolean needNewReadStreamClass = false;
aoqi@0 1081 for (int i = 0; i < exceptions.length; i++) {
aoqi@0 1082 if (exceptions[i].getIdentifier() != idRemoteException &&
aoqi@0 1083 exceptions[i].isType(TYPE_VALUE) &&
aoqi@0 1084 needNewReadStreamClass(exceptions[i])) {
aoqi@0 1085 needNewReadStreamClass = true;
aoqi@0 1086 break;
aoqi@0 1087 }
aoqi@0 1088 }
aoqi@0 1089 if (!needNewReadStreamClass) {
aoqi@0 1090 for (int i = 0; i < paramTypes.length; i++) {
aoqi@0 1091 if (needNewReadStreamClass(paramTypes[i])) {
aoqi@0 1092 needNewReadStreamClass = true;
aoqi@0 1093 break;
aoqi@0 1094 }
aoqi@0 1095 }
aoqi@0 1096 }
aoqi@0 1097 if (!needNewReadStreamClass) {
aoqi@0 1098 needNewReadStreamClass = needNewReadStreamClass(returnType);
aoqi@0 1099 }
aoqi@0 1100
aoqi@0 1101 // Decide if we need to use the new streams for
aoqi@0 1102 // any of the write calls...
aoqi@0 1103
aoqi@0 1104 boolean needNewWriteStreamClass = false;
aoqi@0 1105 for (int i = 0; i < paramTypes.length; i++) {
aoqi@0 1106 if (needNewWriteStreamClass(paramTypes[i])) {
aoqi@0 1107 needNewWriteStreamClass = true;
aoqi@0 1108 break;
aoqi@0 1109 }
aoqi@0 1110 }
aoqi@0 1111
aoqi@0 1112 // Now write the method, inserting casts where needed...
aoqi@0 1113
aoqi@0 1114 p.plnI("try {");
aoqi@0 1115 if (needNewReadStreamClass) {
aoqi@0 1116 p.pln(idExtInputStream + " "+in+" = null;");
aoqi@0 1117 } else {
aoqi@0 1118 p.pln(idInputStream + " "+in+" = null;");
aoqi@0 1119 }
aoqi@0 1120 p.plnI("try {");
aoqi@0 1121
aoqi@0 1122 String argStream = "null";
aoqi@0 1123
aoqi@0 1124 if (needNewWriteStreamClass) {
aoqi@0 1125 p.plnI(idExtOutputStream + " "+out+" = ");
aoqi@0 1126 p.pln("(" + idExtOutputStream + ")");
aoqi@0 1127 p.pln("_request(\"" + methodIDLName + "\", true);");
aoqi@0 1128 p.pO();
aoqi@0 1129 } else {
aoqi@0 1130 p.pln("OutputStream "+out+" = _request(\"" + methodIDLName + "\", true);");
aoqi@0 1131 }
aoqi@0 1132
aoqi@0 1133 if (paramTypes.length > 0) {
aoqi@0 1134 writeMarshalArguments(p, out, paramTypes, paramNames);
aoqi@0 1135 p.pln();
aoqi@0 1136 }
aoqi@0 1137 argStream = out;
aoqi@0 1138
aoqi@0 1139 if (returnType.isType(TYPE_VOID)) {
aoqi@0 1140 p.pln("_invoke(" + argStream + ");" );
aoqi@0 1141 } else {
aoqi@0 1142 if (needNewReadStreamClass) {
aoqi@0 1143 p.plnI(in+" = (" + idExtInputStream + ")_invoke(" + argStream + ");");
aoqi@0 1144 p.pO();
aoqi@0 1145 } else {
aoqi@0 1146 p.pln(in+" = _invoke(" + argStream + ");");
aoqi@0 1147 }
aoqi@0 1148 p.p("return ");
aoqi@0 1149 writeUnmarshalArgument(p, in, returnType, null);
aoqi@0 1150 p.pln();
aoqi@0 1151 }
aoqi@0 1152
aoqi@0 1153 // Handle ApplicationException...
aoqi@0 1154
aoqi@0 1155 p.pOlnI("} catch ("+getName(idApplicationException)+" "+ex+") {");
aoqi@0 1156 if (needNewReadStreamClass) {
aoqi@0 1157 p.pln(in + " = (" + idExtInputStream + ") "+ex+".getInputStream();");
aoqi@0 1158 } else {
aoqi@0 1159 p.pln(in + " = "+ex+".getInputStream();");
aoqi@0 1160 }
aoqi@0 1161
aoqi@0 1162 boolean idRead = false;
aoqi@0 1163 boolean idAllocated = false;
aoqi@0 1164 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 1165 if (exceptions[i].getIdentifier() != idRemoteException) {
aoqi@0 1166
aoqi@0 1167 // Is this our special-case IDLEntity exception?
aoqi@0 1168
aoqi@0 1169 if (exceptions[i].isIDLEntityException() && !exceptions[i].isCORBAUserException()) {
aoqi@0 1170
aoqi@0 1171 // Yes.
aoqi@0 1172
aoqi@0 1173 if (!idAllocated && !idRead) {
aoqi@0 1174 p.pln("String $_id = "+ex+".getId();");
aoqi@0 1175 idAllocated = true;
aoqi@0 1176 }
aoqi@0 1177
aoqi@0 1178 String helperName = IDLNames.replace(exceptions[i].getQualifiedIDLName(false),"::",".");
aoqi@0 1179 helperName += "Helper";
aoqi@0 1180 p.plnI("if ($_id.equals("+helperName+".id())) {");
aoqi@0 1181 p.pln("throw "+helperName+".read("+in+");");
aoqi@0 1182
aoqi@0 1183 } else {
aoqi@0 1184
aoqi@0 1185 // No.
aoqi@0 1186
aoqi@0 1187 if (!idAllocated && !idRead) {
aoqi@0 1188 p.pln("String $_id = "+in+".read_string();");
aoqi@0 1189 idAllocated = true;
aoqi@0 1190 idRead = true;
aoqi@0 1191 } else if (idAllocated && !idRead) {
aoqi@0 1192 p.pln("$_id = "+in+".read_string();");
aoqi@0 1193 idRead = true;
aoqi@0 1194 }
aoqi@0 1195 p.plnI("if ($_id.equals(\""+getExceptionRepositoryID(exceptions[i])+"\")) {");
aoqi@0 1196 // Added for Bug 4818753
aoqi@0 1197 p.pln("throw ("+getExceptionName(exceptions[i])+") "+in+".read_value(" + getExceptionName(exceptions[i]) + ".class);");
aoqi@0 1198 }
aoqi@0 1199 p.pOln("}");
aoqi@0 1200 }
aoqi@0 1201 }
aoqi@0 1202 if (!idAllocated && !idRead) {
aoqi@0 1203 p.pln("String $_id = "+in+".read_string();");
aoqi@0 1204 idAllocated = true;
aoqi@0 1205 idRead = true;
aoqi@0 1206 } else if (idAllocated && !idRead) {
aoqi@0 1207 p.pln("$_id = "+in+".read_string();");
aoqi@0 1208 idRead = true;
aoqi@0 1209 }
aoqi@0 1210 p.pln("throw new UnexpectedException($_id);");
aoqi@0 1211
aoqi@0 1212 // Handle RemarshalException...
aoqi@0 1213
aoqi@0 1214 p.pOlnI("} catch ("+getName(idRemarshalException)+" "+ex+") {");
aoqi@0 1215 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 1216 p.p("return ");
aoqi@0 1217 }
aoqi@0 1218 p.p(methodName + "(");
aoqi@0 1219 for(int i = 0; i < paramTypes.length; i++) {
aoqi@0 1220 if (i > 0) {
aoqi@0 1221 p.p(",");
aoqi@0 1222 }
aoqi@0 1223 p.p(paramNames[i]);
aoqi@0 1224 }
aoqi@0 1225 p.pln(");");
aoqi@0 1226
aoqi@0 1227 // Ensure that we release the reply...
aoqi@0 1228
aoqi@0 1229 p.pOlnI("} finally {");
aoqi@0 1230 p.pln("_releaseReply("+in+");");
aoqi@0 1231
aoqi@0 1232 p.pOln("}");
aoqi@0 1233
aoqi@0 1234 // Handle SystemException...
aoqi@0 1235
aoqi@0 1236 p.pOlnI("} catch (SystemException "+ex+") {");
aoqi@0 1237 p.pln("throw Util.mapSystemException("+ex+");");
aoqi@0 1238 p.pOln("}");
aoqi@0 1239
aoqi@0 1240 // returnResult(p,returnType);
aoqi@0 1241 }
aoqi@0 1242
aoqi@0 1243 void allocateResult (IndentingWriter p,
aoqi@0 1244 Type returnType) throws IOException {
aoqi@0 1245 if (!returnType.isType(TYPE_VOID)) {
aoqi@0 1246 String objName = testUtil(getName(returnType), returnType);
aoqi@0 1247 p.p(objName + " result = ");
aoqi@0 1248 }
aoqi@0 1249 }
aoqi@0 1250
aoqi@0 1251 int getTypeCode(Type type) {
aoqi@0 1252
aoqi@0 1253 int typeCode = type.getTypeCode();
aoqi@0 1254
aoqi@0 1255 // Handle late-breaking special case for
aoqi@0 1256 // abstract IDL entities...
aoqi@0 1257
aoqi@0 1258 if ((type instanceof CompoundType) &&
aoqi@0 1259 ((CompoundType)type).isAbstractBase()) {
aoqi@0 1260 typeCode = TYPE_ABSTRACT;
aoqi@0 1261 }
aoqi@0 1262
aoqi@0 1263 return typeCode;
aoqi@0 1264 }
aoqi@0 1265
aoqi@0 1266
aoqi@0 1267 /**
aoqi@0 1268 * Write a snippet of Java code to marshal a value named "name" of
aoqi@0 1269 * type "type" to the java.io.ObjectOutput stream named "stream".
aoqi@0 1270 */
aoqi@0 1271 void writeMarshalArgument(IndentingWriter p,
aoqi@0 1272 String streamName,
aoqi@0 1273 Type type, String name) throws IOException {
aoqi@0 1274
aoqi@0 1275 int typeCode = getTypeCode(type);
aoqi@0 1276
aoqi@0 1277 switch (typeCode) {
aoqi@0 1278 case TYPE_BOOLEAN:
aoqi@0 1279 p.p(streamName + ".write_boolean(" + name + ");");
aoqi@0 1280 break;
aoqi@0 1281 case TYPE_BYTE:
aoqi@0 1282 p.p(streamName + ".write_octet(" + name + ");");
aoqi@0 1283 break;
aoqi@0 1284 case TYPE_CHAR:
aoqi@0 1285 p.p(streamName + ".write_wchar(" + name + ");");
aoqi@0 1286 break;
aoqi@0 1287 case TYPE_SHORT:
aoqi@0 1288 p.p(streamName + ".write_short(" + name + ");");
aoqi@0 1289 break;
aoqi@0 1290 case TYPE_INT:
aoqi@0 1291 p.p(streamName + ".write_long(" + name + ");");
aoqi@0 1292 break;
aoqi@0 1293 case TYPE_LONG:
aoqi@0 1294 p.p(streamName + ".write_longlong(" + name + ");");
aoqi@0 1295 break;
aoqi@0 1296 case TYPE_FLOAT:
aoqi@0 1297 p.p(streamName + ".write_float(" + name + ");");
aoqi@0 1298 break;
aoqi@0 1299 case TYPE_DOUBLE:
aoqi@0 1300 p.p(streamName + ".write_double(" + name + ");");
aoqi@0 1301 break;
aoqi@0 1302 case TYPE_STRING:
aoqi@0 1303 p.p(streamName + ".write_value(" + name + "," + getName(type) + ".class);");
aoqi@0 1304 break;
aoqi@0 1305 case TYPE_ANY:
aoqi@0 1306 p.p("Util.writeAny("+ streamName + "," + name + ");");
aoqi@0 1307 break;
aoqi@0 1308 case TYPE_CORBA_OBJECT:
aoqi@0 1309 p.p(streamName + ".write_Object(" + name + ");");
aoqi@0 1310 break;
aoqi@0 1311 case TYPE_REMOTE:
aoqi@0 1312 p.p("Util.writeRemoteObject("+ streamName + "," + name + ");");
aoqi@0 1313 break;
aoqi@0 1314 case TYPE_ABSTRACT:
aoqi@0 1315 p.p("Util.writeAbstractObject("+ streamName + "," + name + ");");
aoqi@0 1316 break;
aoqi@0 1317 case TYPE_NC_INTERFACE:
aoqi@0 1318 p.p(streamName + ".write_value((Serializable)" + name + "," + getName(type) + ".class);");
aoqi@0 1319 break;
aoqi@0 1320 case TYPE_VALUE:
aoqi@0 1321 p.p(streamName + ".write_value(" + name + "," + getName(type) + ".class);");
aoqi@0 1322 break;
aoqi@0 1323 case TYPE_IMPLEMENTATION:
aoqi@0 1324 p.p(streamName + ".write_value((Serializable)" + name + "," + getName(type) + ".class);");
aoqi@0 1325 break;
aoqi@0 1326 case TYPE_NC_CLASS:
aoqi@0 1327 p.p(streamName + ".write_value((Serializable)" + name + "," + getName(type) + ".class);");
aoqi@0 1328 break;
aoqi@0 1329 case TYPE_ARRAY:
aoqi@0 1330 castArray = true;
aoqi@0 1331 p.p(streamName + ".write_value(cast_array(" + name + ")," + getName(type) + ".class);");
aoqi@0 1332 break;
aoqi@0 1333 case TYPE_JAVA_RMI_REMOTE:
aoqi@0 1334 p.p("Util.writeRemoteObject("+ streamName + "," + name + ");");
aoqi@0 1335 break;
aoqi@0 1336 default:
aoqi@0 1337 throw new Error("unexpected type code: " + typeCode);
aoqi@0 1338 }
aoqi@0 1339 }
aoqi@0 1340
aoqi@0 1341 /**
aoqi@0 1342 * Write a snippet of Java code to unmarshal a value of type "type"
aoqi@0 1343 * from the java.io.ObjectInput stream named "stream" into a variable
aoqi@0 1344 * named "name" (if "name" is null, the value in unmarshalled and
aoqi@0 1345 * discarded).
aoqi@0 1346 */
aoqi@0 1347 void writeUnmarshalArgument(IndentingWriter p,
aoqi@0 1348 String streamName,
aoqi@0 1349 Type type,
aoqi@0 1350 String name) throws IOException {
aoqi@0 1351
aoqi@0 1352 int typeCode = getTypeCode(type);
aoqi@0 1353
aoqi@0 1354 if (name != null) {
aoqi@0 1355 p.p(name + " = ");
aoqi@0 1356 }
aoqi@0 1357
aoqi@0 1358 switch (typeCode) {
aoqi@0 1359 case TYPE_BOOLEAN:
aoqi@0 1360 p.p(streamName + ".read_boolean();");
aoqi@0 1361 break;
aoqi@0 1362 case TYPE_BYTE:
aoqi@0 1363 p.p(streamName + ".read_octet();");
aoqi@0 1364 break;
aoqi@0 1365 case TYPE_CHAR:
aoqi@0 1366 p.p(streamName + ".read_wchar();");
aoqi@0 1367 break;
aoqi@0 1368 case TYPE_SHORT:
aoqi@0 1369 p.p(streamName + ".read_short();");
aoqi@0 1370 break;
aoqi@0 1371 case TYPE_INT:
aoqi@0 1372 p.p(streamName + ".read_long();");
aoqi@0 1373 break;
aoqi@0 1374 case TYPE_LONG:
aoqi@0 1375 p.p(streamName + ".read_longlong();");
aoqi@0 1376 break;
aoqi@0 1377 case TYPE_FLOAT:
aoqi@0 1378 p.p(streamName + ".read_float();");
aoqi@0 1379 break;
aoqi@0 1380 case TYPE_DOUBLE:
aoqi@0 1381 p.p(streamName + ".read_double();");
aoqi@0 1382 break;
aoqi@0 1383 case TYPE_STRING:
aoqi@0 1384 p.p("(String) " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1385 break;
aoqi@0 1386 case TYPE_ANY:
aoqi@0 1387 if (type.getIdentifier() != idJavaLangObject) {
aoqi@0 1388 p.p("(" + getName(type) + ") ");
aoqi@0 1389 }
aoqi@0 1390 p.p("Util.readAny(" + streamName + ");");
aoqi@0 1391 break;
aoqi@0 1392 case TYPE_CORBA_OBJECT:
aoqi@0 1393 if (type.getIdentifier() == idCorbaObject) {
aoqi@0 1394 p.p("(" + getName(type) + ") " + streamName + ".read_Object();");
aoqi@0 1395 } else {
aoqi@0 1396 p.p("(" + getName(type) + ") " + streamName + ".read_Object(" + getStubName(type) + ".class);");
aoqi@0 1397 }
aoqi@0 1398 break;
aoqi@0 1399 case TYPE_REMOTE:
aoqi@0 1400 String objName = testUtil(getName(type), type);
aoqi@0 1401 p.p("(" + objName + ") " +
aoqi@0 1402 "PortableRemoteObject.narrow(" + streamName + ".read_Object(), " + objName + ".class);");
aoqi@0 1403 break;
aoqi@0 1404 case TYPE_ABSTRACT:
aoqi@0 1405 p.p("(" + getName(type) + ") " + streamName + ".read_abstract_interface();");
aoqi@0 1406 break;
aoqi@0 1407 case TYPE_NC_INTERFACE:
aoqi@0 1408 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1409 break;
aoqi@0 1410 case TYPE_VALUE:
aoqi@0 1411 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1412 break;
aoqi@0 1413 case TYPE_IMPLEMENTATION:
aoqi@0 1414 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1415 break;
aoqi@0 1416 case TYPE_NC_CLASS:
aoqi@0 1417 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1418 break;
aoqi@0 1419 case TYPE_ARRAY:
aoqi@0 1420 p.p("(" + getName(type) + ") " + streamName + ".read_value(" + getName(type) + ".class);");
aoqi@0 1421 break;
aoqi@0 1422 case TYPE_JAVA_RMI_REMOTE:
aoqi@0 1423 p.p("(" + getName(type) + ") " +
aoqi@0 1424 "PortableRemoteObject.narrow(" + streamName + ".read_Object(), " + getName(type) + ".class);");
aoqi@0 1425 // p.p("(" + getName(type) + ") " + streamName + ".read_Object(" + getStubName(type) + ".class);");
aoqi@0 1426 break;
aoqi@0 1427 default:
aoqi@0 1428 throw new Error("unexpected type code: " + typeCode);
aoqi@0 1429 }
aoqi@0 1430 }
aoqi@0 1431
aoqi@0 1432 /**
aoqi@0 1433 * Get a list of all the RepositoryIDs for interfaces
aoqi@0 1434 * implemented directly or indirectly by theType. In the
aoqi@0 1435 * case of an ImplementationType which implements 2 or
aoqi@0 1436 * more remote interfaces, this list will begin with the
aoqi@0 1437 * Identifier for the implementation (see section 5.9 in
aoqi@0 1438 * the Java -> IDL mapping). Ensures that the most derived
aoqi@0 1439 * type is first in the list because the IOR is generated
aoqi@0 1440 * using that entry in the _ids array.
aoqi@0 1441 */
aoqi@0 1442 String[] getAllRemoteRepIDs (CompoundType theType) {
aoqi@0 1443
aoqi@0 1444 String[] result;
aoqi@0 1445
aoqi@0 1446 // Collect up all the (inherited) remote interfaces
aoqi@0 1447 // (ignores all the 'special' interfaces: Remote,
aoqi@0 1448 // Serializable, Externalizable)...
aoqi@0 1449
aoqi@0 1450 Type[] types = collectAllRemoteInterfaces(theType);
aoqi@0 1451
aoqi@0 1452 int length = types.length;
aoqi@0 1453 boolean haveImpl = theType instanceof ImplementationType;
aoqi@0 1454 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 1455 int remoteCount = countRemote(interfaces,false);
aoqi@0 1456 int offset = 0;
aoqi@0 1457
aoqi@0 1458 // Do we have an implementation type that implements
aoqi@0 1459 // more than one remote interface?
aoqi@0 1460
aoqi@0 1461 if (haveImpl && remoteCount > 1) {
aoqi@0 1462
aoqi@0 1463 // Yes, so we need to insert it at the beginning...
aoqi@0 1464
aoqi@0 1465 result = new String[length + 1];
aoqi@0 1466 result[0] = getRepositoryID(theType);
aoqi@0 1467 offset = 1;
aoqi@0 1468
aoqi@0 1469 } else {
aoqi@0 1470
aoqi@0 1471 // No.
aoqi@0 1472
aoqi@0 1473 result = new String[length];
aoqi@0 1474
aoqi@0 1475 // Here we need to ensure that the most derived
aoqi@0 1476 // interface ends up being first in the list. If
aoqi@0 1477 // there is only one, we're done.
aoqi@0 1478
aoqi@0 1479 if (length > 1) {
aoqi@0 1480
aoqi@0 1481 // First, decide what the most derived type is...
aoqi@0 1482
aoqi@0 1483 String mostDerived = null;
aoqi@0 1484
aoqi@0 1485 if (haveImpl) {
aoqi@0 1486
aoqi@0 1487 // If we get here, we know that there is only one
aoqi@0 1488 // direct remote interface, so just find it...
aoqi@0 1489
aoqi@0 1490 for (int i = 0; i < interfaces.length; i++) {
aoqi@0 1491 if (interfaces[i].isType(TYPE_REMOTE)) {
aoqi@0 1492 mostDerived = interfaces[i].getRepositoryID();
aoqi@0 1493 break;
aoqi@0 1494 }
aoqi@0 1495 }
aoqi@0 1496 } else {
aoqi@0 1497
aoqi@0 1498 // If we get here we know that theType is a RemoteType
aoqi@0 1499 // so just use its id...
aoqi@0 1500
aoqi@0 1501 mostDerived = theType.getRepositoryID();
aoqi@0 1502 }
aoqi@0 1503
aoqi@0 1504 // Now search types list and make sure mostDerived is
aoqi@0 1505 // at index zero...
aoqi@0 1506
aoqi@0 1507 for (int i = 0; i < length; i++) {
aoqi@0 1508 if (types[i].getRepositoryID() == mostDerived) {
aoqi@0 1509
aoqi@0 1510 // Found it. Swap it if we need to...
aoqi@0 1511
aoqi@0 1512 if (i > 0) {
aoqi@0 1513 Type temp = types[0];
aoqi@0 1514 types[0] = types[i];
aoqi@0 1515 types[i] = temp;
aoqi@0 1516 }
aoqi@0 1517
aoqi@0 1518 break;
aoqi@0 1519 }
aoqi@0 1520 }
aoqi@0 1521 }
aoqi@0 1522 }
aoqi@0 1523
aoqi@0 1524 // Now copy contents of the types array...
aoqi@0 1525
aoqi@0 1526 for (int i = 0; i < types.length; i++) {
aoqi@0 1527 result[offset++] = getRepositoryID(types[i]);
aoqi@0 1528 }
aoqi@0 1529
aoqi@0 1530 // If we're supposed to, reverse the array. This
aoqi@0 1531 // is only done when the -testReverseIDs flag is
aoqi@0 1532 // passed, and that should ONLY be done for test
aoqi@0 1533 // cases. This is an undocumented feature.
aoqi@0 1534
aoqi@0 1535 if (reverseIDs) {
aoqi@0 1536 int start = 0;
aoqi@0 1537 int end = result.length -1;
aoqi@0 1538 while (start < end) {
aoqi@0 1539 String temp = result[start];
aoqi@0 1540 result[start++] = result[end];
aoqi@0 1541 result[end--] = temp;
aoqi@0 1542 }
aoqi@0 1543 }
aoqi@0 1544
aoqi@0 1545 return result;
aoqi@0 1546 }
aoqi@0 1547
aoqi@0 1548 /**
aoqi@0 1549 * Collect all the inherited remote interfaces.
aoqi@0 1550 */
aoqi@0 1551 Type[] collectAllRemoteInterfaces (CompoundType theType) {
aoqi@0 1552 Vector list = new Vector();
aoqi@0 1553
aoqi@0 1554 // Collect up all the Remote interfaces, and get an instance
aoqi@0 1555 // for java.rmi.Remote...
aoqi@0 1556
aoqi@0 1557 addRemoteInterfaces(list,theType);
aoqi@0 1558
aoqi@0 1559 // Create and return our results...
aoqi@0 1560
aoqi@0 1561 Type[] result = new Type[list.size()];
aoqi@0 1562 list.copyInto(result);
aoqi@0 1563
aoqi@0 1564 return result;
aoqi@0 1565 }
aoqi@0 1566
aoqi@0 1567 /**
aoqi@0 1568 * Add all the inherited remote interfaces to list.
aoqi@0 1569 */
aoqi@0 1570 void addRemoteInterfaces(Vector list, CompoundType theType) {
aoqi@0 1571
aoqi@0 1572 if (theType != null) {
aoqi@0 1573 if (theType.isInterface() && !list.contains(theType)) {
aoqi@0 1574 list.addElement(theType);
aoqi@0 1575 }
aoqi@0 1576
aoqi@0 1577 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 1578 for (int i = 0; i < interfaces.length; i++) {
aoqi@0 1579
aoqi@0 1580 if (interfaces[i].isType(TYPE_REMOTE)) {
aoqi@0 1581 addRemoteInterfaces(list,interfaces[i]);
aoqi@0 1582 }
aoqi@0 1583 }
aoqi@0 1584
aoqi@0 1585 addRemoteInterfaces(list,theType.getSuperclass());
aoqi@0 1586 }
aoqi@0 1587 }
aoqi@0 1588
aoqi@0 1589 /**
aoqi@0 1590 * Get a list of all the remote interfaces which this stub
aoqi@0 1591 * should declare.
aoqi@0 1592 */
aoqi@0 1593 RemoteType[] getDirectRemoteInterfaces (CompoundType theType) {
aoqi@0 1594
aoqi@0 1595 RemoteType[] result;
aoqi@0 1596 InterfaceType[] interfaces = theType.getInterfaces();
aoqi@0 1597
aoqi@0 1598 // First, get a list of all the interfaces...
aoqi@0 1599
aoqi@0 1600 InterfaceType[] list;
aoqi@0 1601
aoqi@0 1602 // Because we can be passed either an ImplementationType
aoqi@0 1603 // (which has interfaces) or a RemoteType (which is an
aoqi@0 1604 // interface and may have interfaces) we must handle each
aoqi@0 1605 // separately...
aoqi@0 1606
aoqi@0 1607 // Do we have an implementation type?
aoqi@0 1608
aoqi@0 1609 if (theType instanceof ImplementationType) {
aoqi@0 1610
aoqi@0 1611 // Yes, so list is exactly what this type
aoqi@0 1612 // implements and is correct already.
aoqi@0 1613
aoqi@0 1614 list = interfaces;
aoqi@0 1615
aoqi@0 1616 } else {
aoqi@0 1617
aoqi@0 1618 // No, so list is just theType...
aoqi@0 1619
aoqi@0 1620 list = new InterfaceType[1];
aoqi@0 1621 list[0] = (InterfaceType) theType;
aoqi@0 1622 }
aoqi@0 1623
aoqi@0 1624 // Ok, now count up the remote interfaces, allocate
aoqi@0 1625 // our result and fill it in...
aoqi@0 1626
aoqi@0 1627 int remoteCount = countRemote(list,false);
aoqi@0 1628
aoqi@0 1629 if (remoteCount == 0) {
aoqi@0 1630 throw new CompilerError("iiop.StubGenerator: No remote interfaces!");
aoqi@0 1631 }
aoqi@0 1632
aoqi@0 1633 result = new RemoteType[remoteCount];
aoqi@0 1634 int offset = 0;
aoqi@0 1635 for (int i = 0; i < list.length; i++) {
aoqi@0 1636 if (list[i].isType(TYPE_REMOTE)) {
aoqi@0 1637 result[offset++] = (RemoteType)list[i];
aoqi@0 1638 }
aoqi@0 1639 }
aoqi@0 1640
aoqi@0 1641 return result;
aoqi@0 1642 }
aoqi@0 1643
aoqi@0 1644 int countRemote (Type[] list, boolean includeAbstract) {
aoqi@0 1645 int remoteCount = 0;
aoqi@0 1646 for (int i = 0; i < list.length; i++) {
aoqi@0 1647 if (list[i].isType(TYPE_REMOTE) &&
aoqi@0 1648 (includeAbstract || !list[i].isType(TYPE_ABSTRACT))) {
aoqi@0 1649 remoteCount++;
aoqi@0 1650 }
aoqi@0 1651 }
aoqi@0 1652
aoqi@0 1653 return remoteCount;
aoqi@0 1654 }
aoqi@0 1655
aoqi@0 1656 void writeCastArray(IndentingWriter p) throws IOException {
aoqi@0 1657 if (castArray) {
aoqi@0 1658 p.pln();
aoqi@0 1659 p.pln("// This method is required as a work-around for");
aoqi@0 1660 p.pln("// a bug in the JDK 1.1.6 verifier.");
aoqi@0 1661 p.pln();
aoqi@0 1662 p.plnI("private "+getName(idJavaIoSerializable)+" cast_array(Object obj) {");
aoqi@0 1663 p.pln("return ("+getName(idJavaIoSerializable)+")obj;");
aoqi@0 1664 p.pOln("}");
aoqi@0 1665 }
aoqi@0 1666 }
aoqi@0 1667 void writeIds(IndentingWriter p, CompoundType theType, boolean isTie
aoqi@0 1668 ) throws IOException {
aoqi@0 1669 p.plnI("private static final String[] _type_ids = {");
aoqi@0 1670
aoqi@0 1671 String[] ids = getAllRemoteRepIDs(theType);
aoqi@0 1672
aoqi@0 1673 if (ids.length >0 ) {
aoqi@0 1674 for(int i = 0; i < ids.length; i++) {
aoqi@0 1675 if (i > 0)
aoqi@0 1676 p.pln(", ");
aoqi@0 1677 p.p("\"" + ids[i] + "\"");
aoqi@0 1678 }
aoqi@0 1679 } else {
aoqi@0 1680 // Must be an implementation which only implements Remote...
aoqi@0 1681 p.pln("\"\"");
aoqi@0 1682 }
aoqi@0 1683 String qname = theType.getQualifiedName() ;
aoqi@0 1684 boolean isTransactional = isTie && transactionalObjects.containsKey( qname ) ;
aoqi@0 1685 // Add TransactionalObject if needed.
aoqi@0 1686 if (isTransactional) {
aoqi@0 1687 // Have already written an id.
aoqi@0 1688 p.pln( ", " ) ;
aoqi@0 1689 p.pln( "\"IDL:omg.org/CosTransactions/TransactionalObject:1.0\"" ) ;
aoqi@0 1690 } else if (ids.length > 0) {
aoqi@0 1691 p.pln();
aoqi@0 1692 }
aoqi@0 1693 p.pOln("};");
aoqi@0 1694 }
aoqi@0 1695
aoqi@0 1696
aoqi@0 1697 /**
aoqi@0 1698 * Write the Tie for the remote class to a stream.
aoqi@0 1699 */
aoqi@0 1700 protected void writeTie(OutputType outputType,
aoqi@0 1701 IndentingWriter p) throws IOException
aoqi@0 1702 {
aoqi@0 1703 CompoundType theType = (CompoundType) outputType.getType();
aoqi@0 1704 RemoteType[] remoteInterfaces = null;
aoqi@0 1705
aoqi@0 1706 // Write comment...
aoqi@0 1707 p.pln("// Tie class generated by rmic, do not edit.");
aoqi@0 1708 p.pln("// Contents subject to change without notice.");
aoqi@0 1709 p.pln();
aoqi@0 1710
aoqi@0 1711 // Set our standard classes...
aoqi@0 1712 setStandardClassesInUse(theType,false);
aoqi@0 1713
aoqi@0 1714 // Add classes for this type...
aoqi@0 1715 addClassesInUse(theType,remoteInterfaces);
aoqi@0 1716
aoqi@0 1717 // Write package and import statements...
aoqi@0 1718 writePackageAndImports(p);
aoqi@0 1719
aoqi@0 1720 // Declare the tie class.
aoqi@0 1721 p.p("public class " + currentClass + " extends " +
aoqi@0 1722 getName(tieBaseClass) + " implements Tie");
aoqi@0 1723
aoqi@0 1724 // Add java.rmi.Remote if this type does not implement it.
aoqi@0 1725 // This allows stubs for Abstract interfaces to be treated
aoqi@0 1726 // uniformly...
aoqi@0 1727 if (!implementsRemote(theType)) {
aoqi@0 1728 p.pln(",");
aoqi@0 1729 p.p(getName("java.rmi.Remote"));
aoqi@0 1730 }
aoqi@0 1731
aoqi@0 1732 p.plnI(" {");
aoqi@0 1733
aoqi@0 1734 // Write data members...
aoqi@0 1735 p.pln();
aoqi@0 1736 p.pln("volatile private " + getName(theType) + " target = null;");
aoqi@0 1737 p.pln();
aoqi@0 1738
aoqi@0 1739 // Write the ids...
aoqi@0 1740 writeIds( p, theType, true ) ;
aoqi@0 1741
aoqi@0 1742 // Write setTarget method...
aoqi@0 1743 p.pln();
aoqi@0 1744 p.plnI("public void setTarget(Remote target) {");
aoqi@0 1745 p.pln("this.target = (" + getName(theType) + ") target;");
aoqi@0 1746 p.pOln("}");
aoqi@0 1747
aoqi@0 1748 // Write getTarget method...
aoqi@0 1749 p.pln();
aoqi@0 1750 p.plnI("public Remote getTarget() {");
aoqi@0 1751 p.pln("return target;");
aoqi@0 1752 p.pOln("}");
aoqi@0 1753
aoqi@0 1754 // Write thisObject method...
aoqi@0 1755 p.pln();
aoqi@0 1756 write_tie_thisObject_method(p,idCorbaObject);
aoqi@0 1757
aoqi@0 1758 // Write deactivate method...
aoqi@0 1759 p.pln();
aoqi@0 1760 write_tie_deactivate_method(p);
aoqi@0 1761
aoqi@0 1762 // Write get orb method...
aoqi@0 1763 p.pln();
aoqi@0 1764 p.plnI("public ORB orb() {");
aoqi@0 1765 p.pln("return _orb();");
aoqi@0 1766 p.pOln("}");
aoqi@0 1767
aoqi@0 1768 // Write set orb method...
aoqi@0 1769 p.pln();
aoqi@0 1770 write_tie_orb_method(p);
aoqi@0 1771
aoqi@0 1772 // Write the _ids() method...
aoqi@0 1773 p.pln();
aoqi@0 1774 write_tie__ids_method(p);
aoqi@0 1775
aoqi@0 1776 // Get all the methods...
aoqi@0 1777 CompoundType.Method[] remoteMethods = theType.getMethods();
aoqi@0 1778
aoqi@0 1779 // Register all the argument names used, plus our
aoqi@0 1780 // data member names...
aoqi@0 1781
aoqi@0 1782 addNamesInUse(remoteMethods);
aoqi@0 1783 addNameInUse("target");
aoqi@0 1784 addNameInUse("_type_ids");
aoqi@0 1785
aoqi@0 1786 // Write the _invoke method...
aoqi@0 1787 p.pln();
aoqi@0 1788
aoqi@0 1789 String in = getVariableName("in");
aoqi@0 1790 String _in = getVariableName("_in");
aoqi@0 1791 String ex = getVariableName("ex");
aoqi@0 1792 String method = getVariableName("method");
aoqi@0 1793 String reply = getVariableName("reply");
aoqi@0 1794
aoqi@0 1795 p.plnI("public OutputStream _invoke(String "+method+", InputStream "+_in+", " +
aoqi@0 1796 "ResponseHandler "+reply+") throws SystemException {");
aoqi@0 1797
aoqi@0 1798 if (remoteMethods.length > 0) {
aoqi@0 1799 p.plnI("try {");
aoqi@0 1800 p.pln(getName(theType) + " target = this.target;");
aoqi@0 1801 p.plnI("if (target == null) {");
aoqi@0 1802 p.pln("throw new java.io.IOException();");
aoqi@0 1803 p.pOln("}");
aoqi@0 1804 p.plnI(idExtInputStream + " "+in+" = ");
aoqi@0 1805 p.pln("(" + idExtInputStream + ") "+_in+";");
aoqi@0 1806 p.pO();
aoqi@0 1807
aoqi@0 1808 // See if we should use a hash table style
aoqi@0 1809 // comparison...
aoqi@0 1810
aoqi@0 1811 StaticStringsHash hash = getStringsHash(remoteMethods);
aoqi@0 1812
aoqi@0 1813 if (hash != null) {
aoqi@0 1814 p.plnI("switch ("+method+"."+hash.method+") {");
aoqi@0 1815 for (int i = 0; i < hash.buckets.length; i++) {
aoqi@0 1816 p.plnI("case "+hash.keys[i]+": ");
aoqi@0 1817 for (int j = 0; j < hash.buckets[i].length; j++) {
aoqi@0 1818 CompoundType.Method current = remoteMethods[hash.buckets[i][j]];
aoqi@0 1819 if (j > 0) {
aoqi@0 1820 p.pO("} else ");
aoqi@0 1821 }
aoqi@0 1822 p.plnI("if ("+method+".equals(\""+ current.getIDLName() +"\")) {");
aoqi@0 1823 writeTieMethod(p, theType,current);
aoqi@0 1824 }
aoqi@0 1825 p.pOln("}");
aoqi@0 1826 p.pO();
aoqi@0 1827 }
aoqi@0 1828 } else {
aoqi@0 1829 for(int i = 0; i < remoteMethods.length; i++) {
aoqi@0 1830 CompoundType.Method current = remoteMethods[i];
aoqi@0 1831 if (i > 0) {
aoqi@0 1832 p.pO("} else ");
aoqi@0 1833 }
aoqi@0 1834
aoqi@0 1835 p.plnI("if ("+method+".equals(\""+ current.getIDLName() +"\")) {");
aoqi@0 1836 writeTieMethod(p, theType, current);
aoqi@0 1837 }
aoqi@0 1838 }
aoqi@0 1839
aoqi@0 1840 if (hash != null) {
aoqi@0 1841 p.pI();
aoqi@0 1842 // p.plnI("default:");
aoqi@0 1843 } else {
aoqi@0 1844 // p.pOlnI("} else {");
aoqi@0 1845 }
aoqi@0 1846 // p.pln("throw new "+getName(idBadMethodException)+"();");
aoqi@0 1847
aoqi@0 1848 if (hash != null) {
aoqi@0 1849 p.pO();
aoqi@0 1850 }
aoqi@0 1851 p.pOln("}");
aoqi@0 1852 p.pln("throw new "+getName(idBadMethodException)+"();");
aoqi@0 1853
aoqi@0 1854 p.pOlnI("} catch ("+getName(idSystemException)+" "+ex+") {");
aoqi@0 1855 p.pln("throw "+ex+";");
aoqi@0 1856
aoqi@0 1857 p.pOlnI("} catch ("+getName(idJavaLangThrowable)+" "+ex+") {");
aoqi@0 1858 p.pln("throw new " + getName(idPortableUnknownException) + "("+ex+");");
aoqi@0 1859 p.pOln("}");
aoqi@0 1860 } else {
aoqi@0 1861 // No methods...
aoqi@0 1862
aoqi@0 1863 p.pln("throw new " + getName(idBadMethodException) + "();");
aoqi@0 1864 }
aoqi@0 1865
aoqi@0 1866 p.pOln("}"); // end invoke
aoqi@0 1867
aoqi@0 1868 // Write the cast array hack...
aoqi@0 1869
aoqi@0 1870 writeCastArray(p);
aoqi@0 1871
aoqi@0 1872 // End tie class...
aoqi@0 1873 p.pOln("}");
aoqi@0 1874 }
aoqi@0 1875 public void catchWrongPolicy(IndentingWriter p) throws IOException {
aoqi@0 1876 p.pln("");
aoqi@0 1877 }
aoqi@0 1878 public void catchServantNotActive(IndentingWriter p) throws IOException {
aoqi@0 1879 p.pln("");
aoqi@0 1880 }
aoqi@0 1881 public void catchObjectNotActive(IndentingWriter p) throws IOException {
aoqi@0 1882 p.pln("");
aoqi@0 1883 }
aoqi@0 1884
aoqi@0 1885 public void write_tie_thisObject_method(IndentingWriter p,
aoqi@0 1886 Identifier idCorbaObject)
aoqi@0 1887 throws IOException
aoqi@0 1888 {
aoqi@0 1889 if(POATie){
aoqi@0 1890 p.plnI("public " + idCorbaObject + " thisObject() {");
aoqi@0 1891 /*
aoqi@0 1892 p.pln("org.omg.CORBA.Object objref = null;");
aoqi@0 1893 p.pln("try{");
aoqi@0 1894 p.pln("objref = _poa().servant_to_reference(this);");
aoqi@0 1895 p.pln("}catch (org.omg.PortableServer.POAPackage.WrongPolicy exception){");
aoqi@0 1896 catchWrongPolicy(p);
aoqi@0 1897 p.pln("}catch (org.omg.PortableServer.POAPackage.ServantNotActive exception){");
aoqi@0 1898 catchServantNotActive(p);
aoqi@0 1899 p.pln("}");
aoqi@0 1900 p.pln("return objref;");
aoqi@0 1901 */
aoqi@0 1902 p.pln("return _this_object();");
aoqi@0 1903 p.pOln("}");
aoqi@0 1904 } else {
aoqi@0 1905 p.plnI("public " + idCorbaObject + " thisObject() {");
aoqi@0 1906 p.pln("return this;");
aoqi@0 1907 p.pOln("}");
aoqi@0 1908 }
aoqi@0 1909 }
aoqi@0 1910
aoqi@0 1911 public void write_tie_deactivate_method(IndentingWriter p)
aoqi@0 1912 throws IOException
aoqi@0 1913 {
aoqi@0 1914 if(POATie){
aoqi@0 1915 p.plnI("public void deactivate() {");
aoqi@0 1916 p.pln("try{");
aoqi@0 1917 p.pln("_poa().deactivate_object(_poa().servant_to_id(this));");
aoqi@0 1918 p.pln("}catch (org.omg.PortableServer.POAPackage.WrongPolicy exception){");
aoqi@0 1919 catchWrongPolicy(p);
aoqi@0 1920 p.pln("}catch (org.omg.PortableServer.POAPackage.ObjectNotActive exception){");
aoqi@0 1921 catchObjectNotActive(p);
aoqi@0 1922 p.pln("}catch (org.omg.PortableServer.POAPackage.ServantNotActive exception){");
aoqi@0 1923 catchServantNotActive(p);
aoqi@0 1924 p.pln("}");
aoqi@0 1925 p.pOln("}");
aoqi@0 1926 } else {
aoqi@0 1927 p.plnI("public void deactivate() {");
aoqi@0 1928 p.pln("_orb().disconnect(this);");
aoqi@0 1929 p.pln("_set_delegate(null);");
aoqi@0 1930 p.pln("target = null;");
aoqi@0 1931 p.pOln("}");
aoqi@0 1932 }
aoqi@0 1933 }
aoqi@0 1934
aoqi@0 1935 public void write_tie_orb_method(IndentingWriter p)
aoqi@0 1936 throws IOException
aoqi@0 1937 {
aoqi@0 1938 if(POATie){
aoqi@0 1939 p.plnI("public void orb(ORB orb) {");
aoqi@0 1940 /*
aoqi@0 1941 p.pln("try{");
aoqi@0 1942 p.pln("orb.connect(_poa().servant_to_reference(this));");
aoqi@0 1943 p.pln("}catch (org.omg.PortableServer.POAPackage.WrongPolicy exception){");
aoqi@0 1944 catchWrongPolicy(p);
aoqi@0 1945 p.pln("}catch (org.omg.PortableServer.POAPackage.ServantNotActive exception){");
aoqi@0 1946 catchServantNotActive(p);
aoqi@0 1947 p.pln("}");
aoqi@0 1948 */
aoqi@0 1949 p.pln("try {");
aoqi@0 1950 p.pln(" ((org.omg.CORBA_2_3.ORB)orb).set_delegate(this);");
aoqi@0 1951 p.pln("}");
aoqi@0 1952 p.pln("catch(ClassCastException e) {");
aoqi@0 1953 p.pln(" throw new org.omg.CORBA.BAD_PARAM");
aoqi@0 1954 p.pln(" (\"POA Servant requires an instance of org.omg.CORBA_2_3.ORB\");");
aoqi@0 1955 p.pln("}");
aoqi@0 1956 p.pOln("}");
aoqi@0 1957 } else {
aoqi@0 1958 p.plnI("public void orb(ORB orb) {");
aoqi@0 1959 p.pln("orb.connect(this);");
aoqi@0 1960 p.pOln("}");
aoqi@0 1961 }
aoqi@0 1962 }
aoqi@0 1963
aoqi@0 1964 public void write_tie__ids_method(IndentingWriter p)
aoqi@0 1965 throws IOException
aoqi@0 1966 {
aoqi@0 1967 if(POATie){
aoqi@0 1968 p.plnI("public String[] _all_interfaces(org.omg.PortableServer.POA poa, byte[] objectId){");
aoqi@0 1969 p.pln("return (String[]) _type_ids.clone();");
aoqi@0 1970 p.pOln("}");
aoqi@0 1971 } else {
aoqi@0 1972 p.plnI("public String[] _ids() { ");
aoqi@0 1973 p.pln("return (String[]) _type_ids.clone();");
aoqi@0 1974 p.pOln("}");
aoqi@0 1975 }
aoqi@0 1976 }
aoqi@0 1977
aoqi@0 1978
aoqi@0 1979 StaticStringsHash getStringsHash (CompoundType.Method[] methods) {
aoqi@0 1980 if (useHash && methods.length > 1) {
aoqi@0 1981 String[] methodNames = new String[methods.length];
aoqi@0 1982 for (int i = 0; i < methodNames.length; i++) {
aoqi@0 1983 methodNames[i] = methods[i].getIDLName();
aoqi@0 1984 }
aoqi@0 1985 return new StaticStringsHash(methodNames);
aoqi@0 1986 }
aoqi@0 1987 return null;
aoqi@0 1988 }
aoqi@0 1989
aoqi@0 1990 static boolean needNewReadStreamClass(Type type) {
aoqi@0 1991 if (type.isType(TYPE_ABSTRACT)) {
aoqi@0 1992 return true;
aoqi@0 1993 }
aoqi@0 1994 // Handle late-breaking special case for
aoqi@0 1995 // abstract IDL entities...
aoqi@0 1996 if ((type instanceof CompoundType) &&
aoqi@0 1997 ((CompoundType)type).isAbstractBase()) {
aoqi@0 1998 return true;
aoqi@0 1999 }
aoqi@0 2000 return needNewWriteStreamClass(type);
aoqi@0 2001 }
aoqi@0 2002
aoqi@0 2003 static boolean needNewWriteStreamClass(Type type) {
aoqi@0 2004 switch (type.getTypeCode()) {
aoqi@0 2005 case TYPE_VOID:
aoqi@0 2006 case TYPE_BOOLEAN:
aoqi@0 2007 case TYPE_BYTE:
aoqi@0 2008 case TYPE_CHAR:
aoqi@0 2009 case TYPE_SHORT:
aoqi@0 2010 case TYPE_INT:
aoqi@0 2011 case TYPE_LONG:
aoqi@0 2012 case TYPE_FLOAT:
aoqi@0 2013 case TYPE_DOUBLE: return false;
aoqi@0 2014
aoqi@0 2015 case TYPE_STRING: return true;
aoqi@0 2016 case TYPE_ANY: return false;
aoqi@0 2017 case TYPE_CORBA_OBJECT: return false;
aoqi@0 2018 case TYPE_REMOTE: return false;
aoqi@0 2019 case TYPE_ABSTRACT: return false;
aoqi@0 2020 case TYPE_NC_INTERFACE: return true;
aoqi@0 2021 case TYPE_VALUE: return true;
aoqi@0 2022 case TYPE_IMPLEMENTATION: return true;
aoqi@0 2023 case TYPE_NC_CLASS: return true;
aoqi@0 2024 case TYPE_ARRAY: return true;
aoqi@0 2025 case TYPE_JAVA_RMI_REMOTE: return false;
aoqi@0 2026
aoqi@0 2027 default: throw new Error("unexpected type code: " + type.getTypeCode());
aoqi@0 2028 }
aoqi@0 2029 }
aoqi@0 2030
aoqi@0 2031 /*
aoqi@0 2032 * Decide which arguments need to be copied and write
aoqi@0 2033 * the copy code. Returns an array of argument names to
aoqi@0 2034 * use to refer to either the copy or the original.
aoqi@0 2035 */
aoqi@0 2036 String[] writeCopyArguments(CompoundType.Method method,
aoqi@0 2037 IndentingWriter p) throws IOException {
aoqi@0 2038
aoqi@0 2039 Type[] args = method.getArguments();
aoqi@0 2040 String[] origNames = method.getArgumentNames();
aoqi@0 2041
aoqi@0 2042 // Copy the current parameter names to a result array...
aoqi@0 2043
aoqi@0 2044 String[] result = new String[origNames.length];
aoqi@0 2045 for (int i = 0; i < result.length; i++) {
aoqi@0 2046 result[i] = origNames[i];
aoqi@0 2047 }
aoqi@0 2048
aoqi@0 2049 // Decide which arguments must be copied, if any. If
aoqi@0 2050 // any of the arguments are types for which a 'real' copy
aoqi@0 2051 // will be done, rather than just an autoConnect, set
aoqi@0 2052 // realCopy = true. Note that abstract types may only
aoqi@0 2053 // need autoConnect, but we cannot know that at compile
aoqi@0 2054 // time...
aoqi@0 2055
aoqi@0 2056 boolean realCopy = false;
aoqi@0 2057 boolean[] copyArg = new boolean[args.length];
aoqi@0 2058 int copyCount = 0;
aoqi@0 2059 int firstCopiedArg = 0; // Only used in single copy case. It is only the first arg that
aoqi@0 2060 // needs copying IF copyCount == 1.
aoqi@0 2061
aoqi@0 2062 for (int i = 0; i < args.length; i++) {
aoqi@0 2063 if (mustCopy(args[i])) {
aoqi@0 2064 copyArg[i] = true;
aoqi@0 2065 copyCount++;
aoqi@0 2066 firstCopiedArg = i;
aoqi@0 2067 if (args[i].getTypeCode() != TYPE_REMOTE &&
aoqi@0 2068 args[i].getTypeCode() != TYPE_IMPLEMENTATION) {
aoqi@0 2069 realCopy = true;
aoqi@0 2070 }
aoqi@0 2071 } else {
aoqi@0 2072 copyArg[i] = false;
aoqi@0 2073 }
aoqi@0 2074 }
aoqi@0 2075
aoqi@0 2076 // Do we have any types which must be copied?
aoqi@0 2077 if (copyCount > 0) {
aoqi@0 2078 // Yes. Are we only doing the copy to ensure
aoqi@0 2079 // that autoConnect occurs?
aoqi@0 2080 if (realCopy) {
aoqi@0 2081 // Nope. We need to go back thru the list and
aoqi@0 2082 // mark any strings so that they will be copied
aoqi@0 2083 // to preserve any shared references...
aoqi@0 2084 for (int i = 0; i < args.length; i++) {
aoqi@0 2085 if (args[i].getTypeCode() == TYPE_STRING) {
aoqi@0 2086 copyArg[i] = true;
aoqi@0 2087 copyCount++;
aoqi@0 2088 }
aoqi@0 2089 }
aoqi@0 2090 }
aoqi@0 2091
aoqi@0 2092 // We're ready to generate code. Do we have more than
aoqi@0 2093 // one to copy?
aoqi@0 2094 if (copyCount > 1) {
aoqi@0 2095 // Generate a call to copyObjects...
aoqi@0 2096 String arrayName = getVariableName("copies");
aoqi@0 2097 p.p("Object[] " + arrayName + " = Util.copyObjects(new Object[]{");
aoqi@0 2098 boolean first = true;
aoqi@0 2099 for (int i = 0; i < args.length; i++) {
aoqi@0 2100 if (copyArg[i]) {
aoqi@0 2101 if (!first) {
aoqi@0 2102 p.p(",");
aoqi@0 2103 }
aoqi@0 2104 first = false;
aoqi@0 2105 p.p(origNames[i]);
aoqi@0 2106 }
aoqi@0 2107 }
aoqi@0 2108 p.pln("},_orb());");
aoqi@0 2109
aoqi@0 2110 // For each of the types which was copied, create
aoqi@0 2111 // a local temporary for it, updating the result
aoqi@0 2112 // array with the new local parameter name...
aoqi@0 2113 int copyIndex = 0 ;
aoqi@0 2114 for (int i = 0; i < args.length; i++) {
aoqi@0 2115 if (copyArg[i]) {
aoqi@0 2116 result[i] = getVariableName(result[i]+"Copy");
aoqi@0 2117 p.pln( getName(args[i]) + " " + result[i] + " = (" + getName(args[i]) + ") " +
aoqi@0 2118 arrayName + "[" + copyIndex++ +"];");
aoqi@0 2119 }
aoqi@0 2120 }
aoqi@0 2121 } else {
aoqi@0 2122 // Generate a call to copyObject, updating the result
aoqi@0 2123 // with the new local parameter name...
aoqi@0 2124 result[firstCopiedArg] = getVariableName(result[firstCopiedArg]+"Copy");
aoqi@0 2125 p.pln( getName(args[firstCopiedArg]) + " " + result[firstCopiedArg] + " = (" +
aoqi@0 2126 getName(args[firstCopiedArg]) + ") Util.copyObject(" +
aoqi@0 2127 origNames[firstCopiedArg] + ",_orb());");
aoqi@0 2128 }
aoqi@0 2129 }
aoqi@0 2130
aoqi@0 2131 return result;
aoqi@0 2132 }
aoqi@0 2133
aoqi@0 2134 static final String SINGLE_SLASH = "\\";
aoqi@0 2135 static final String DOUBLE_SLASH = SINGLE_SLASH + SINGLE_SLASH;
aoqi@0 2136
aoqi@0 2137 String getRepositoryID(Type type) {
aoqi@0 2138 return IDLNames.replace(type.getRepositoryID(), SINGLE_SLASH, DOUBLE_SLASH);
aoqi@0 2139 }
aoqi@0 2140
aoqi@0 2141 String getExceptionRepositoryID(Type type) {
aoqi@0 2142 ClassType theType = (ClassType) type;
aoqi@0 2143 return IDLNames.getIDLRepositoryID(theType.getQualifiedIDLExceptionName(false));
aoqi@0 2144 }
aoqi@0 2145
aoqi@0 2146 String getVariableName(String proposed) {
aoqi@0 2147 while (namesInUse.contains(proposed)) {
aoqi@0 2148 proposed = "$" + proposed;
aoqi@0 2149 }
aoqi@0 2150
aoqi@0 2151 return proposed;
aoqi@0 2152 }
aoqi@0 2153
aoqi@0 2154 void addNamesInUse(CompoundType.Method[] methods) {
aoqi@0 2155 for (int i = 0; i < methods.length; i++) {
aoqi@0 2156 addNamesInUse(methods[i]);
aoqi@0 2157 }
aoqi@0 2158 }
aoqi@0 2159
aoqi@0 2160 void addNamesInUse(CompoundType.Method method) {
aoqi@0 2161 String paramNames[] = method.getArgumentNames();
aoqi@0 2162 for (int i = 0; i < paramNames.length; i++) {
aoqi@0 2163 addNameInUse(paramNames[i]);
aoqi@0 2164 }
aoqi@0 2165 }
aoqi@0 2166
aoqi@0 2167 void addNameInUse(String name) {
aoqi@0 2168 namesInUse.add(name);
aoqi@0 2169 }
aoqi@0 2170
aoqi@0 2171 static boolean mustCopy(Type type) {
aoqi@0 2172 switch (type.getTypeCode()) {
aoqi@0 2173 case TYPE_VOID:
aoqi@0 2174 case TYPE_BOOLEAN:
aoqi@0 2175 case TYPE_BYTE:
aoqi@0 2176 case TYPE_CHAR:
aoqi@0 2177 case TYPE_SHORT:
aoqi@0 2178 case TYPE_INT:
aoqi@0 2179 case TYPE_LONG:
aoqi@0 2180 case TYPE_FLOAT:
aoqi@0 2181 case TYPE_DOUBLE:
aoqi@0 2182 case TYPE_STRING: return false;
aoqi@0 2183
aoqi@0 2184 case TYPE_ANY: return true;
aoqi@0 2185
aoqi@0 2186 case TYPE_CORBA_OBJECT: return false;
aoqi@0 2187
aoqi@0 2188 case TYPE_REMOTE:
aoqi@0 2189 case TYPE_ABSTRACT:
aoqi@0 2190 case TYPE_NC_INTERFACE:
aoqi@0 2191 case TYPE_VALUE:
aoqi@0 2192 case TYPE_IMPLEMENTATION:
aoqi@0 2193 case TYPE_NC_CLASS:
aoqi@0 2194 case TYPE_ARRAY:
aoqi@0 2195 case TYPE_JAVA_RMI_REMOTE: return true;
aoqi@0 2196
aoqi@0 2197 default: throw new Error("unexpected type code: " + type.getTypeCode());
aoqi@0 2198 }
aoqi@0 2199 }
aoqi@0 2200
aoqi@0 2201 ValueType[] getStubExceptions (CompoundType.Method method, boolean sort) {
aoqi@0 2202
aoqi@0 2203 ValueType[] list = method.getFilteredStubExceptions(method.getExceptions());
aoqi@0 2204
aoqi@0 2205 // Sort the list so that all org.omg.CORBA.UserException
aoqi@0 2206 // subtypes are at the beginning of the list. This ensures
aoqi@0 2207 // that the stub will not call read_string() before calling
aoqi@0 2208 // XXHelper.read().
aoqi@0 2209
aoqi@0 2210 if (sort) {
aoqi@0 2211 Arrays.sort(list,new UserExceptionComparator());
aoqi@0 2212 }
aoqi@0 2213
aoqi@0 2214 return list;
aoqi@0 2215 }
aoqi@0 2216
aoqi@0 2217 ValueType[] getTieExceptions (CompoundType.Method method) {
aoqi@0 2218 return method.getUniqueCatchList(method.getImplExceptions());
aoqi@0 2219 }
aoqi@0 2220
aoqi@0 2221 void writeTieMethod(IndentingWriter p, CompoundType type,
aoqi@0 2222 CompoundType.Method method) throws IOException {
aoqi@0 2223 String methodName = method.getName();
aoqi@0 2224 Type paramTypes[] = method.getArguments();
aoqi@0 2225 String paramNames[] = method.getArgumentNames();
aoqi@0 2226 Type returnType = method.getReturnType();
aoqi@0 2227 ValueType[] exceptions = getTieExceptions(method);
aoqi@0 2228 String in = getVariableName("in");
aoqi@0 2229 String ex = getVariableName("ex");
aoqi@0 2230 String out = getVariableName("out");
aoqi@0 2231 String reply = getVariableName("reply");
aoqi@0 2232
aoqi@0 2233 for (int i = 0; i < paramTypes.length; i++) {
aoqi@0 2234 p.p(getName(paramTypes[i])+" "+paramNames[i]+" = ");
aoqi@0 2235 writeUnmarshalArgument(p, in, paramTypes[i], null);
aoqi@0 2236 p.pln();
aoqi@0 2237 }
aoqi@0 2238
aoqi@0 2239 boolean handleExceptions = exceptions != null;
aoqi@0 2240 boolean doReturn = !returnType.isType(TYPE_VOID);
aoqi@0 2241
aoqi@0 2242 if (handleExceptions && doReturn) {
aoqi@0 2243 String objName = testUtil(getName(returnType), returnType);
aoqi@0 2244 p.pln(objName+" result;");
aoqi@0 2245 }
aoqi@0 2246
aoqi@0 2247 if (handleExceptions)
aoqi@0 2248 p.plnI("try {");
aoqi@0 2249
aoqi@0 2250 if (doReturn) {
aoqi@0 2251 if (handleExceptions) {
aoqi@0 2252 p.p("result = ");
aoqi@0 2253 } else {
aoqi@0 2254 p.p(getName(returnType)+" result = ");
aoqi@0 2255 }
aoqi@0 2256 }
aoqi@0 2257
aoqi@0 2258 p.p("target."+methodName+"(");
aoqi@0 2259 for(int i = 0; i < paramNames.length; i++) {
aoqi@0 2260 if (i > 0)
aoqi@0 2261 p.p(", ");
aoqi@0 2262 p.p(paramNames[i]);
aoqi@0 2263 }
aoqi@0 2264 p.pln(");");
aoqi@0 2265
aoqi@0 2266 if (handleExceptions) {
aoqi@0 2267 for(int i = 0; i < exceptions.length; i++) {
aoqi@0 2268 p.pOlnI("} catch ("+getName(exceptions[i])+" "+ex+") {");
aoqi@0 2269
aoqi@0 2270 // Is this our IDLEntity Exception special case?
aoqi@0 2271
aoqi@0 2272 if (exceptions[i].isIDLEntityException() && !exceptions[i].isCORBAUserException()) {
aoqi@0 2273
aoqi@0 2274 // Yes...
aoqi@0 2275
aoqi@0 2276 String helperName = IDLNames.replace(exceptions[i].getQualifiedIDLName(false),"::",".");
aoqi@0 2277 helperName += "Helper";
aoqi@0 2278 p.pln(idOutputStream+" "+out +" = "+reply+".createExceptionReply();");
aoqi@0 2279 p.pln(helperName+".write("+out+","+ex+");");
aoqi@0 2280
aoqi@0 2281 } else {
aoqi@0 2282
aoqi@0 2283 // No...
aoqi@0 2284
aoqi@0 2285 p.pln("String id = \"" + getExceptionRepositoryID(exceptions[i]) + "\";");
aoqi@0 2286 p.plnI(idExtOutputStream + " "+out+" = ");
aoqi@0 2287 p.pln("(" + idExtOutputStream + ") "+reply+".createExceptionReply();");
aoqi@0 2288 p.pOln(out+".write_string(id);");
aoqi@0 2289 p.pln(out+".write_value("+ex+"," + getName(exceptions[i]) + ".class);");
aoqi@0 2290 }
aoqi@0 2291
aoqi@0 2292 p.pln("return "+out+";");
aoqi@0 2293 }
aoqi@0 2294 p.pOln("}");
aoqi@0 2295 }
aoqi@0 2296
aoqi@0 2297 if (needNewWriteStreamClass(returnType)) {
aoqi@0 2298 p.plnI(idExtOutputStream + " "+out+" = ");
aoqi@0 2299 p.pln("(" + idExtOutputStream + ") "+reply+".createReply();");
aoqi@0 2300 p.pO();
aoqi@0 2301 } else {
aoqi@0 2302 p.pln("OutputStream "+out+" = "+reply+".createReply();");
aoqi@0 2303 }
aoqi@0 2304
aoqi@0 2305 if (doReturn) {
aoqi@0 2306 writeMarshalArgument(p, out, returnType, "result");
aoqi@0 2307 p.pln();
aoqi@0 2308 }
aoqi@0 2309
aoqi@0 2310 p.pln("return "+out+";");
aoqi@0 2311 }
aoqi@0 2312
aoqi@0 2313
aoqi@0 2314 /**
aoqi@0 2315 * Write Java statements to marshal a series of values in order as
aoqi@0 2316 * named in the "names" array, with types as specified in the "types"
aoqi@0 2317 * array", to the java.io.ObjectOutput stream named "stream".
aoqi@0 2318 */
aoqi@0 2319 void writeMarshalArguments(IndentingWriter p,
aoqi@0 2320 String streamName,
aoqi@0 2321 Type[] types, String[] names)
aoqi@0 2322 throws IOException
aoqi@0 2323 {
aoqi@0 2324 if (types.length != names.length) {
aoqi@0 2325 throw new Error("paramter type and name arrays different sizes");
aoqi@0 2326 }
aoqi@0 2327
aoqi@0 2328 for (int i = 0; i < types.length; i++) {
aoqi@0 2329 writeMarshalArgument(p, streamName, types[i], names[i]);
aoqi@0 2330 if (i != types.length -1) {
aoqi@0 2331 p.pln();
aoqi@0 2332 }
aoqi@0 2333 }
aoqi@0 2334 }
aoqi@0 2335
aoqi@0 2336 /**
aoqi@0 2337 * Added for IASRI 4987274. Remote classes named "Util" were
aoqi@0 2338 * getting confused with javax.rmi.CORBA.Util and the
aoqi@0 2339 * unqualifiedName "Util".
aoqi@0 2340 */
aoqi@0 2341 String testUtil(String objectName, Type ttype) {
aoqi@0 2342 if (objectName.equals("Util")) {
aoqi@0 2343 String correctedName = (String)ttype.getPackageName() + "." + objectName;
aoqi@0 2344 return correctedName;
aoqi@0 2345 } else {
aoqi@0 2346 return objectName;
aoqi@0 2347 }
aoqi@0 2348 }
aoqi@0 2349 }
aoqi@0 2350
aoqi@0 2351 class StringComparator implements java.util.Comparator {
aoqi@0 2352 public int compare(Object o1, Object o2) {
aoqi@0 2353 String s1 = (String)o1;
aoqi@0 2354 String s2 = (String)o2;
aoqi@0 2355 return s1.compareTo(s2);
aoqi@0 2356 }
aoqi@0 2357 }
aoqi@0 2358
aoqi@0 2359
aoqi@0 2360 class UserExceptionComparator implements java.util.Comparator {
aoqi@0 2361 public int compare(Object o1, Object o2) {
aoqi@0 2362 ValueType v1 = (ValueType)o1;
aoqi@0 2363 ValueType v2 = (ValueType)o2;
aoqi@0 2364 int result = 0;
aoqi@0 2365 if (isUserException(v1)) {
aoqi@0 2366 if (!isUserException(v2)) {
aoqi@0 2367 result = -1;
aoqi@0 2368 }
aoqi@0 2369 } else if (isUserException(v2)) {
aoqi@0 2370 if (!isUserException(v1)) {
aoqi@0 2371 result = 1;
aoqi@0 2372 }
aoqi@0 2373 }
aoqi@0 2374 return result;
aoqi@0 2375 }
aoqi@0 2376
aoqi@0 2377 final boolean isUserException(ValueType it) {
aoqi@0 2378 return it.isIDLEntityException() && !it.isCORBAUserException();
aoqi@0 2379 }
aoqi@0 2380 }

mercurial