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

Thu, 24 May 2018 16:41:12 +0800

author
aoqi
date
Thu, 24 May 2018 16:41:12 +0800
changeset 1410
9c913ea7e4a1
parent 748
6845b95cba6b
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2007, 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.util.Vector;
aoqi@0 36 import java.util.Hashtable;
aoqi@0 37 import java.util.Enumeration;
aoqi@0 38 import java.io.IOException;
aoqi@0 39 import sun.tools.java.ClassDefinition;
aoqi@0 40 import sun.tools.java.ClassDeclaration;
aoqi@0 41 import sun.tools.java.Identifier;
aoqi@0 42 import sun.tools.java.ClassNotFound;
aoqi@0 43 import sun.tools.java.CompilerError;
aoqi@0 44 import sun.rmi.rmic.IndentingWriter;
aoqi@0 45 import java.util.HashSet;
aoqi@0 46 import com.sun.corba.se.impl.util.RepositoryId;
aoqi@0 47 import sun.rmi.rmic.Names;
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Type is an abstract base class for a family of types which provide
aoqi@0 51 * conformance checking and name mapping as defined in the "Java to IDL
aoqi@0 52 * Mapping" OMG specification. The family is composed of the following
aoqi@0 53 * fixed set of types:
aoqi@0 54 * <pre>
aoqi@0 55 *
aoqi@0 56 * +- RemoteType <-- AbstractType
aoqi@0 57 * |
aoqi@0 58 * +- InterfaceType <-+- SpecialInterfaceType
aoqi@0 59 * +- PrimitiveType | |
aoqi@0 60 * | | +- NCInterfaceType
aoqi@0 61 * Type <-+- CompoundType <-|
aoqi@0 62 * | | +- ValueType
aoqi@0 63 * +- ArrayType | |
aoqi@0 64 * +- ClassType <-----+- ImplementationType
aoqi@0 65 * |
aoqi@0 66 * +- SpecialClassType
aoqi@0 67 * |
aoqi@0 68 * +- NCClassType
aoqi@0 69 *
aoqi@0 70 * </pre>
aoqi@0 71 * PrimitiveType represents a primitive or a void type.
aoqi@0 72 * <p>
aoqi@0 73 * CompoundType is an abstract base representing any non-special class
aoqi@0 74 * or interface type.
aoqi@0 75 * <p>
aoqi@0 76 * InterfaceType is an abstract base representing any non-special
aoqi@0 77 * interface type.
aoqi@0 78 * <p>
aoqi@0 79 * RemoteType represents any non-special interface which inherits
aoqi@0 80 * from java.rmi.Remote.
aoqi@0 81 * <p>
aoqi@0 82 * AbstractType represents any non-special interface which does not
aoqi@0 83 * inherit from java.rmi.Remote, for which all methods throw RemoteException.
aoqi@0 84 * <p>
aoqi@0 85 * SpecialInterfaceType represents any one of the following types:
aoqi@0 86 * <pre>
aoqi@0 87 * java.rmi.Remote
aoqi@0 88 * java.io.Serializable
aoqi@0 89 * java.io.Externalizable
aoqi@0 90 * </pre>
aoqi@0 91 * all of which are treated as special cases.
aoqi@0 92 * <p>
aoqi@0 93 * NCInterfaceType represents any non-special, non-conforming interface.
aoqi@0 94 * <p>
aoqi@0 95 * ClassType is an abstract base representing any non-special class
aoqi@0 96 * type.
aoqi@0 97 * <p>
aoqi@0 98 * ValueType represents any non-special class which does inherit from
aoqi@0 99 * java.io.Serializable and does not inherit from java.rmi.Remote.
aoqi@0 100 * <p>
aoqi@0 101 * ImplementationType represents any non-special class which implements
aoqi@0 102 * one or more interfaces which inherit from java.rmi.Remote.
aoqi@0 103 * <p>
aoqi@0 104 * SpecialClassType represents any one of the following types:
aoqi@0 105 * <pre>
aoqi@0 106 * java.lang.Object
aoqi@0 107 * java.lang.String
aoqi@0 108 * org.omg.CORBA.Object
aoqi@0 109 * </pre>
aoqi@0 110 * all of which are treated as special cases. For all but CORBA.Object,
aoqi@0 111 * the type must match exactly. For CORBA.Object, the type must either be
aoqi@0 112 * CORBA.Object or inherit from it.
aoqi@0 113 * <p>
aoqi@0 114 * NCClassType represents any non-special, non-conforming class.
aoqi@0 115 * <p>
aoqi@0 116 * ArrayType is a wrapper for any of the other types. The getElementType()
aoqi@0 117 * method can be used to get the array element type. The getArrayDimension()
aoqi@0 118 * method can be used to get the array dimension.
aoqi@0 119 * <p>
aoqi@0 120 * <i><strong>NOTE:</strong> None of these types is multi-thread-safe</i>
aoqi@0 121 * @author Bryan Atsatt
aoqi@0 122 */
aoqi@0 123 public abstract class Type implements sun.rmi.rmic.iiop.Constants, ContextElement, Cloneable {
aoqi@0 124
aoqi@0 125 private int typeCode;
aoqi@0 126 private int fullTypeCode;
aoqi@0 127 private Identifier id;
aoqi@0 128
aoqi@0 129 private String name;
aoqi@0 130 private String packageName;
aoqi@0 131 private String qualifiedName;
aoqi@0 132
aoqi@0 133 private String idlName;
aoqi@0 134 private String[] idlModuleNames;
aoqi@0 135 private String qualifiedIDLName;
aoqi@0 136
aoqi@0 137 private String repositoryID;
aoqi@0 138 private Class ourClass;
aoqi@0 139
aoqi@0 140 private int status = STATUS_PENDING;
aoqi@0 141
aoqi@0 142 protected BatchEnvironment env; // Easy access for subclasses.
aoqi@0 143 protected ContextStack stack; // Easy access for subclasses.
aoqi@0 144
aoqi@0 145 protected boolean destroyed = false;
aoqi@0 146
aoqi@0 147 //_____________________________________________________________________
aoqi@0 148 // Public Interfaces
aoqi@0 149 //_____________________________________________________________________
aoqi@0 150
aoqi@0 151 /**
aoqi@0 152 * Return the unqualified name for this type (e.g. com.acme.Dynamite would
aoqi@0 153 * return "Dynamite").
aoqi@0 154 */
aoqi@0 155 public String getName() {
aoqi@0 156 return name;
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 /**
aoqi@0 160 * Return the package of this type (e.g. com.acme.Dynamite would
aoqi@0 161 * return "com.acme"). Will return null if default package or
aoqi@0 162 * if this type is a primitive.
aoqi@0 163 */
aoqi@0 164 public String getPackageName() {
aoqi@0 165 return packageName;
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 /**
aoqi@0 169 * Return the fully qualified name of this type (e.g. com.acme.Dynamite
aoqi@0 170 * would return "com.acme.Dynamite")
aoqi@0 171 */
aoqi@0 172 public String getQualifiedName() {
aoqi@0 173 return qualifiedName;
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 /**
aoqi@0 177 * Return signature for this type (e.g. com.acme.Dynamite
aoqi@0 178 * would return "com.acme.Dynamite", byte = "B")
aoqi@0 179 */
aoqi@0 180 public abstract String getSignature();
aoqi@0 181
aoqi@0 182 /**
aoqi@0 183 * IDL_Naming
aoqi@0 184 * Return the unqualified IDL name of this type (e.g. com.acme.Dynamite would
aoqi@0 185 * return "Dynamite").
aoqi@0 186 */
aoqi@0 187 public String getIDLName() {
aoqi@0 188 return idlName;
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 /**
aoqi@0 192 * IDL_Naming
aoqi@0 193 * Return the IDL module name for this type (e.g. com.acme.Dynamite would return
aoqi@0 194 * a three element array of {"com","acme"). May be a zero length array if
aoqi@0 195 * there is no module name.
aoqi@0 196 */
aoqi@0 197 public String[] getIDLModuleNames() {
aoqi@0 198 return idlModuleNames;
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 /**
aoqi@0 202 * IDL_Naming
aoqi@0 203 * Return the fully qualified IDL name for this type (e.g. com.acme.Dynamite would
aoqi@0 204 * return "com::acme::Dynamite").
aoqi@0 205 * @param global If true, prepends "::".
aoqi@0 206 */
aoqi@0 207 public String getQualifiedIDLName(boolean global) {
aoqi@0 208 if (global && getIDLModuleNames().length > 0) {
aoqi@0 209 return IDL_NAME_SEPARATOR + qualifiedIDLName;
aoqi@0 210 } else {
aoqi@0 211 return qualifiedIDLName;
aoqi@0 212 }
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 /**
aoqi@0 216 * Return the identifier for this type. May be qualified.
aoqi@0 217 */
aoqi@0 218 public Identifier getIdentifier() {
aoqi@0 219 return id;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 /**
aoqi@0 223 * Return the repository ID for this type.
aoqi@0 224 */
aoqi@0 225 public String getRepositoryID() {
aoqi@0 226 return repositoryID;
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 /**
aoqi@0 230 * Return the repository ID for this "boxed" type.
aoqi@0 231 */
aoqi@0 232 public String getBoxedRepositoryID() {
aoqi@0 233 return RepositoryId.createForJavaType(ourClass);
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 /**
aoqi@0 237 * Return the Class for this type.
aoqi@0 238 */
aoqi@0 239 public Class getClassInstance() {
aoqi@0 240 if (ourClass == null) {
aoqi@0 241 initClass();
aoqi@0 242 }
aoqi@0 243 return ourClass;
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 /**
aoqi@0 247 * Return the status of this type.
aoqi@0 248 */
aoqi@0 249 public int getStatus() {
aoqi@0 250 return status;
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 /**
aoqi@0 254 * Set the status of this type.
aoqi@0 255 */
aoqi@0 256 public void setStatus(int status) {
aoqi@0 257 this.status = status;
aoqi@0 258 }
aoqi@0 259
aoqi@0 260 /**
aoqi@0 261 * Return the compiler environment for this type.
aoqi@0 262 */
aoqi@0 263 public BatchEnvironment getEnv() {
aoqi@0 264 return env;
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 /**
aoqi@0 268 * Get type code, without modifiers. Type codes are defined in sun.rmi.rmic.iiop.Constants.
aoqi@0 269 */
aoqi@0 270 public int getTypeCode() {
aoqi@0 271 return typeCode;
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 /**
aoqi@0 275 * Get type code, with modifiers. Type codes are defined in sun.rmi.rmic.iiop.Constants.
aoqi@0 276 */
aoqi@0 277 public int getFullTypeCode() {
aoqi@0 278 return fullTypeCode;
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 /**
aoqi@0 282 * Get type code modifiers. Type codes are defined in sun.rmi.rmic.iiop.Constants.
aoqi@0 283 */
aoqi@0 284 public int getTypeCodeModifiers() {
aoqi@0 285 return fullTypeCode & TM_MASK;
aoqi@0 286 }
aoqi@0 287
aoqi@0 288 /**
aoqi@0 289 * Check for a certain type. Type codes are defined in sun.rmi.rmic.iiop.Constants.
aoqi@0 290 * Returns true if all of the bits in typeCodeMask are present in the full type code
aoqi@0 291 * of this object.
aoqi@0 292 */
aoqi@0 293 public boolean isType(int typeCodeMask) {
aoqi@0 294 return (fullTypeCode & typeCodeMask) == typeCodeMask;
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 /**
aoqi@0 298 * Like isType(), but returns true if <em>any</em> of the bits in typeCodeMask are
aoqi@0 299 * present in the full type code of this object.
aoqi@0 300 */
aoqi@0 301 public boolean typeMatches(int typeCodeMask) {
aoqi@0 302 return (fullTypeCode & typeCodeMask) > 0;
aoqi@0 303 }
aoqi@0 304
aoqi@0 305
aoqi@0 306 /**
aoqi@0 307 * Return the fullTypeCode. If an array, returns the
aoqi@0 308 * type code from the element type.
aoqi@0 309 */
aoqi@0 310 public int getRootTypeCode() {
aoqi@0 311 if (isArray()) {
aoqi@0 312 return getElementType().getFullTypeCode();
aoqi@0 313 } else {
aoqi@0 314 return fullTypeCode;
aoqi@0 315 }
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 /**
aoqi@0 319 * Return true if this type is-a InterfaceType.
aoqi@0 320 */
aoqi@0 321 public boolean isInterface() {
aoqi@0 322 return (fullTypeCode & TM_INTERFACE) == TM_INTERFACE;
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 /**
aoqi@0 326 * Return true if this type is-a ClassType.
aoqi@0 327 */
aoqi@0 328 public boolean isClass() {
aoqi@0 329 return (fullTypeCode & TM_CLASS) == TM_CLASS;
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 /**
aoqi@0 333 * Return true if this type is-a inner class or interface.
aoqi@0 334 */
aoqi@0 335 public boolean isInner() {
aoqi@0 336 return (fullTypeCode & TM_INNER) == TM_INNER;
aoqi@0 337 }
aoqi@0 338
aoqi@0 339
aoqi@0 340 /**
aoqi@0 341 * Return true if this type is-a SpecialInterfaceType.
aoqi@0 342 */
aoqi@0 343 public boolean isSpecialInterface() {
aoqi@0 344 return (fullTypeCode & TM_SPECIAL_INTERFACE) == TM_SPECIAL_INTERFACE;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 /**
aoqi@0 348 * Return true if this type is-a SpecialClassType.
aoqi@0 349 */
aoqi@0 350 public boolean isSpecialClass() {
aoqi@0 351 return (fullTypeCode & TM_SPECIAL_CLASS) == TM_SPECIAL_CLASS;
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 /**
aoqi@0 355 * Return true if this type is-a CompoundType.
aoqi@0 356 */
aoqi@0 357 public boolean isCompound() {
aoqi@0 358 return (fullTypeCode & TM_COMPOUND) == TM_COMPOUND;
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 /**
aoqi@0 362 * Return true if this type is-a PrimitiveType.
aoqi@0 363 */
aoqi@0 364 public boolean isPrimitive() {
aoqi@0 365 return (fullTypeCode & TM_PRIMITIVE) == TM_PRIMITIVE;
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 /**
aoqi@0 369 * Return true if this type is-a ArrayType.
aoqi@0 370 */
aoqi@0 371 public boolean isArray() {
aoqi@0 372 return (fullTypeCode & TYPE_ARRAY) == TYPE_ARRAY;
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 /**
aoqi@0 376 * Return true if this type is a conforming type.
aoqi@0 377 */
aoqi@0 378 public boolean isConforming() {
aoqi@0 379 return (fullTypeCode & TM_NON_CONFORMING) == TM_NON_CONFORMING;
aoqi@0 380 }
aoqi@0 381
aoqi@0 382 /**
aoqi@0 383 * Return a string representation of this type.
aoqi@0 384 */
aoqi@0 385 public String toString () {
aoqi@0 386 return getQualifiedName();
aoqi@0 387 }
aoqi@0 388
aoqi@0 389 /**
aoqi@0 390 * Get element type. Returns null if not an array.
aoqi@0 391 */
aoqi@0 392 public Type getElementType () {
aoqi@0 393 return null;
aoqi@0 394 }
aoqi@0 395
aoqi@0 396 /**
aoqi@0 397 * Get array dimension. Returns zero if not an array.
aoqi@0 398 */
aoqi@0 399 public int getArrayDimension () {
aoqi@0 400 return 0;
aoqi@0 401 }
aoqi@0 402
aoqi@0 403 /**
aoqi@0 404 * Get brackets string. Returns "" if not an array.
aoqi@0 405 */
aoqi@0 406 public String getArrayBrackets () {
aoqi@0 407 return "";
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 /**
aoqi@0 411 * Equality check based on the string representation.
aoqi@0 412 */
aoqi@0 413 public boolean equals(Object obj) {
aoqi@0 414
aoqi@0 415 String us = toString();
aoqi@0 416 String them = ((Type)obj).toString();
aoqi@0 417 return us.equals(them);
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 /**
aoqi@0 421 * Collect all the matching types referenced directly or indirectly
aoqi@0 422 * by this type, including itself.
aoqi@0 423 * @param typeCodeFilter The typeCode to use as a filter.
aoqi@0 424 */
aoqi@0 425 public Type[] collectMatching (int typeCodeFilter) {
aoqi@0 426 return collectMatching(typeCodeFilter,new HashSet(env.allTypes.size()));
aoqi@0 427 }
aoqi@0 428
aoqi@0 429 /**
aoqi@0 430 * Collect all the matching types referenced directly or indirectly
aoqi@0 431 * by this type, including itself.
aoqi@0 432 * @param typeCodeFilter The typeCode to use as a filter.
aoqi@0 433 * @param alreadyChecked Contains types which have previously been checked
aoqi@0 434 * and will be ignored. Updated during collection.
aoqi@0 435 */
aoqi@0 436 public Type[] collectMatching (int typeCodeFilter, HashSet alreadyChecked) {
aoqi@0 437 Vector matching = new Vector();
aoqi@0 438
aoqi@0 439 // Fill up the list...
aoqi@0 440
aoqi@0 441 addTypes(typeCodeFilter,alreadyChecked,matching);
aoqi@0 442
aoqi@0 443 // Copy vector contents to array and return it...
aoqi@0 444
aoqi@0 445 Type[] result = new Type[matching.size()];
aoqi@0 446 matching.copyInto(result);
aoqi@0 447
aoqi@0 448 return result;
aoqi@0 449 }
aoqi@0 450
aoqi@0 451 /**
aoqi@0 452 * Return a string describing this type.
aoqi@0 453 */
aoqi@0 454 public abstract String getTypeDescription ();
aoqi@0 455
aoqi@0 456 /**
aoqi@0 457 * Return the name of this type. For arrays, will include "[]" if useIDLNames == false.
aoqi@0 458 * @param useQualifiedNames If true, print qualified names; otherwise, print unqualified names.
aoqi@0 459 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 460 * @param globalIDLNames If true and useIDLNames true, prepends "::".
aoqi@0 461 */
aoqi@0 462 public String getTypeName ( boolean useQualifiedNames,
aoqi@0 463 boolean useIDLNames,
aoqi@0 464 boolean globalIDLNames) {
aoqi@0 465 if (useIDLNames) {
aoqi@0 466 if (useQualifiedNames) {
aoqi@0 467 return getQualifiedIDLName(globalIDLNames);
aoqi@0 468 } else {
aoqi@0 469 return getIDLName();
aoqi@0 470 }
aoqi@0 471 } else {
aoqi@0 472 if (useQualifiedNames) {
aoqi@0 473 return getQualifiedName();
aoqi@0 474 } else {
aoqi@0 475 return getName();
aoqi@0 476 }
aoqi@0 477 }
aoqi@0 478 }
aoqi@0 479
aoqi@0 480 /**
aoqi@0 481 * Print all types referenced directly or indirectly by this type which
aoqi@0 482 * match the filter.
aoqi@0 483 * @param writer The stream to print to.
aoqi@0 484 * @param typeCodeFilter The type codes to print.
aoqi@0 485 * @param useQualifiedNames If true, print qualified names; otherwise, print unqualified names.
aoqi@0 486 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 487 * @param globalIDLNames If true and useIDLNames true, prepends "::".
aoqi@0 488 */
aoqi@0 489 public void print ( IndentingWriter writer,
aoqi@0 490 int typeCodeFilter,
aoqi@0 491 boolean useQualifiedNames,
aoqi@0 492 boolean useIDLNames,
aoqi@0 493 boolean globalIDLNames) throws IOException {
aoqi@0 494
aoqi@0 495 Type[] theTypes = collectMatching(typeCodeFilter);
aoqi@0 496 print(writer,theTypes,useQualifiedNames,useIDLNames,globalIDLNames);
aoqi@0 497 }
aoqi@0 498
aoqi@0 499 /**
aoqi@0 500 * Print an array of types.
aoqi@0 501 * @param writer The stream to print to.
aoqi@0 502 * @param theTypes The types to print.
aoqi@0 503 * @param useQualifiedNames If true, print qualified names; otherwise, print unqualified names.
aoqi@0 504 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 505 * @param globalIDLNames If true and useIDLNames true, prepends "::".
aoqi@0 506 */
aoqi@0 507 public static void print ( IndentingWriter writer,
aoqi@0 508 Type[] theTypes,
aoqi@0 509 boolean useQualifiedNames,
aoqi@0 510 boolean useIDLNames,
aoqi@0 511 boolean globalIDLNames) throws IOException {
aoqi@0 512
aoqi@0 513 for (int i = 0; i < theTypes.length; i++) {
aoqi@0 514 theTypes[i].println(writer,useQualifiedNames,useIDLNames,globalIDLNames);
aoqi@0 515 }
aoqi@0 516 }
aoqi@0 517
aoqi@0 518
aoqi@0 519 /**
aoqi@0 520 * Print this type.
aoqi@0 521 * @param writer The stream to print to.
aoqi@0 522 * @param useQualifiedNames If true, print qualified names; otherwise, print unqualified names.
aoqi@0 523 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 524 * @param globalIDLNames If true and useIDLNames true, prepends "::".
aoqi@0 525 */
aoqi@0 526 public void print ( IndentingWriter writer,
aoqi@0 527 boolean useQualifiedNames,
aoqi@0 528 boolean useIDLNames,
aoqi@0 529 boolean globalIDLNames) throws IOException {
aoqi@0 530 printTypeName(writer,useQualifiedNames,useIDLNames,globalIDLNames);
aoqi@0 531 }
aoqi@0 532
aoqi@0 533 /**
aoqi@0 534 * Print this type, followed by a newline.
aoqi@0 535 * @param writer The stream to print to.
aoqi@0 536 * @param useQualifiedNames If true, print qualified names; otherwise, print unqualified names.
aoqi@0 537 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 538 * @param globalIDLNames If true and useIDLNames true, prepends "::".
aoqi@0 539 */
aoqi@0 540 public void println ( IndentingWriter writer,
aoqi@0 541 boolean useQualifiedNames,
aoqi@0 542 boolean useIDLNames,
aoqi@0 543 boolean globalIDLNames) throws IOException {
aoqi@0 544
aoqi@0 545 print(writer,useQualifiedNames,useIDLNames,globalIDLNames);
aoqi@0 546 writer.pln();
aoqi@0 547 }
aoqi@0 548
aoqi@0 549
aoqi@0 550
aoqi@0 551 /**
aoqi@0 552 * Print the name of this type.
aoqi@0 553 * @param writer The stream to print to.
aoqi@0 554 * @param useQualifiedNames If true, print qualified names; otherwise, print unqualified names.
aoqi@0 555 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 556 * @param globalIDLNames If true and useIDLNames true, prepends "::".
aoqi@0 557 */
aoqi@0 558 public void printTypeName ( IndentingWriter writer,
aoqi@0 559 boolean useQualifiedNames,
aoqi@0 560 boolean useIDLNames,
aoqi@0 561 boolean globalIDLNames) throws IOException {
aoqi@0 562
aoqi@0 563 writer.p(getTypeName(useQualifiedNames,useIDLNames,globalIDLNames));
aoqi@0 564 }
aoqi@0 565
aoqi@0 566 /**
aoqi@0 567 * Return context element name.
aoqi@0 568 */
aoqi@0 569 public String getElementName() {
aoqi@0 570 return getQualifiedName();
aoqi@0 571 }
aoqi@0 572
aoqi@0 573 //_____________________________________________________________________
aoqi@0 574 // Subclass Interfaces
aoqi@0 575 //_____________________________________________________________________
aoqi@0 576
aoqi@0 577 /**
aoqi@0 578 * Print the "opening" of the package or module of this type.
aoqi@0 579 * @param writer The stream to print to.
aoqi@0 580 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 581 */
aoqi@0 582 protected void printPackageOpen ( IndentingWriter writer,
aoqi@0 583 boolean useIDLNames) throws IOException {
aoqi@0 584
aoqi@0 585 if (useIDLNames) {
aoqi@0 586 String[] moduleNames = getIDLModuleNames();
aoqi@0 587 for (int i = 0; i < moduleNames.length; i++ ) {
aoqi@0 588 writer.plnI("module " + moduleNames[i] + " {");
aoqi@0 589 }
aoqi@0 590 } else {
aoqi@0 591 String packageName = getPackageName();
aoqi@0 592 if (packageName != null) {
aoqi@0 593 writer.pln("package " + packageName + ";");
aoqi@0 594 }
aoqi@0 595 }
aoqi@0 596 }
aoqi@0 597
aoqi@0 598 /**
aoqi@0 599 * Get a type out of the table.
aoqi@0 600 */
aoqi@0 601 protected static Type getType (sun.tools.java.Type key, ContextStack stack) {
aoqi@0 602 return getType(key.toString(),stack);
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 /**
aoqi@0 606 * Get a type out of the table.
aoqi@0 607 */
aoqi@0 608 protected static Type getType (String key, ContextStack stack) {
aoqi@0 609 Type result = (Type) stack.getEnv().allTypes.get(key);
aoqi@0 610
aoqi@0 611 if (result != null) {
aoqi@0 612 stack.traceExistingType(result);
aoqi@0 613 }
aoqi@0 614
aoqi@0 615 return result;
aoqi@0 616 }
aoqi@0 617
aoqi@0 618 /**
aoqi@0 619 * Remove a type from the table.
aoqi@0 620 */
aoqi@0 621 protected static void removeType (String key, ContextStack stack) {
aoqi@0 622 Type value = (Type) stack.getEnv().allTypes.remove(key);
aoqi@0 623 stack.getEnv().invalidTypes.put(value,key);
aoqi@0 624 }
aoqi@0 625
aoqi@0 626 /**
aoqi@0 627 * Remove a type from the table.
aoqi@0 628 */
aoqi@0 629 protected static void removeType (sun.tools.java.Type key, ContextStack stack) {
aoqi@0 630 String theKey = key.toString();
aoqi@0 631 Type old = (Type) stack.getEnv().allTypes.remove(theKey);
aoqi@0 632 putInvalidType(old,theKey,stack);
aoqi@0 633 }
aoqi@0 634
aoqi@0 635 /**
aoqi@0 636 * Put a type into the table.
aoqi@0 637 */
aoqi@0 638 protected static void putType (sun.tools.java.Type key, Type value, ContextStack stack) {
aoqi@0 639 stack.getEnv().allTypes.put(key.toString(),value);
aoqi@0 640 }
aoqi@0 641
aoqi@0 642 /**
aoqi@0 643 * Put a type into the table.
aoqi@0 644 */
aoqi@0 645 protected static void putType (String key, Type value, ContextStack stack) {
aoqi@0 646 stack.getEnv().allTypes.put(key,value);
aoqi@0 647 }
aoqi@0 648
aoqi@0 649 /**
aoqi@0 650 * Put an invalid type into the.
aoqi@0 651 */
aoqi@0 652 protected static void putInvalidType (Type key, String value, ContextStack stack) {
aoqi@0 653 stack.getEnv().invalidTypes.put(key,value);
aoqi@0 654 }
aoqi@0 655
aoqi@0 656
aoqi@0 657 /**
aoqi@0 658 * Remove all invalid types...
aoqi@0 659 */
aoqi@0 660 public void removeInvalidTypes () {
aoqi@0 661 if (env.invalidTypes.size() > 0) {
aoqi@0 662 env.invalidTypes.clear();
aoqi@0 663 }
aoqi@0 664 }
aoqi@0 665
aoqi@0 666 /**
aoqi@0 667 * Walk all types and tell them to update invalid types...
aoqi@0 668 */
aoqi@0 669 protected static void updateAllInvalidTypes (ContextStack stack) {
aoqi@0 670 BatchEnvironment env = stack.getEnv();
aoqi@0 671 if (env.invalidTypes.size() > 0) {
aoqi@0 672
aoqi@0 673 // Walk all types and swap invalid...
aoqi@0 674
aoqi@0 675 for (Enumeration e = env.allTypes.elements() ; e.hasMoreElements() ;) {
aoqi@0 676 Type it = (Type) e.nextElement();
aoqi@0 677 it.swapInvalidTypes();
aoqi@0 678 }
aoqi@0 679
aoqi@0 680 // Delete all invalidTypes...
aoqi@0 681
aoqi@0 682 env.invalidTypes.clear();
aoqi@0 683 }
aoqi@0 684 }
aoqi@0 685
aoqi@0 686 /**
aoqi@0 687 * Return count of previously parsed types.
aoqi@0 688 */
aoqi@0 689 protected int countTypes () {
aoqi@0 690 return env.allTypes.size();
aoqi@0 691 }
aoqi@0 692
aoqi@0 693 /**
aoqi@0 694 * Reset types removes all previously parsed types.
aoqi@0 695 */
aoqi@0 696 void resetTypes () {
aoqi@0 697 env.reset();
aoqi@0 698 }
aoqi@0 699
aoqi@0 700 /**
aoqi@0 701 * Release all resources.
aoqi@0 702 */
aoqi@0 703 protected void destroy () {
aoqi@0 704 if (!destroyed) {
aoqi@0 705 id = null;
aoqi@0 706 name = null;
aoqi@0 707 packageName = null;
aoqi@0 708 qualifiedName = null;
aoqi@0 709 idlName = null;
aoqi@0 710 idlModuleNames = null;
aoqi@0 711 qualifiedIDLName = null;
aoqi@0 712 repositoryID = null;
aoqi@0 713 ourClass = null;
aoqi@0 714 env = null;
aoqi@0 715 stack = null;
aoqi@0 716 destroyed = true;
aoqi@0 717 }
aoqi@0 718 }
aoqi@0 719
aoqi@0 720 /**
aoqi@0 721 * Convert all invalid types to valid ones.
aoqi@0 722 */
aoqi@0 723 protected void swapInvalidTypes () {
aoqi@0 724 }
aoqi@0 725
aoqi@0 726 /**
aoqi@0 727 * Convert an invalid type to a valid one.
aoqi@0 728 */
aoqi@0 729 protected Type getValidType (Type invalidType) {
aoqi@0 730 if (invalidType.getStatus() == STATUS_VALID) {
aoqi@0 731 return invalidType;
aoqi@0 732 }
aoqi@0 733
aoqi@0 734 String key = (String)env.invalidTypes.get(invalidType);
aoqi@0 735 Type result = null;
aoqi@0 736 if (key != null) {
aoqi@0 737 result = (Type) env.allTypes.get(key);
aoqi@0 738 }
aoqi@0 739
aoqi@0 740 if (result == null) {
aoqi@0 741 throw new Error("Failed to find valid type to swap for " + invalidType + " mis-identified as " + invalidType.getTypeDescription());
aoqi@0 742 }
aoqi@0 743 //System.out.println("Swapped " + result + " from " + invalidType.getTypeDescription()
aoqi@0 744 // + " to " + result.getTypeDescription());
aoqi@0 745 //ContextStack.dumpCallStack();
aoqi@0 746 return result;
aoqi@0 747 }
aoqi@0 748
aoqi@0 749 /**
aoqi@0 750 * Print the "closing" of the package or module of this type.
aoqi@0 751 * @param writer The stream to print to.
aoqi@0 752 * @param useIDLNames If true, print IDL names; otherwise, print java names.
aoqi@0 753 */
aoqi@0 754 protected void printPackageClose ( IndentingWriter writer,
aoqi@0 755 boolean useIDLNames) throws IOException {
aoqi@0 756 if (useIDLNames) {
aoqi@0 757 String[] moduleNames = getIDLModuleNames();
aoqi@0 758 for (int i = 0; i < moduleNames.length; i++ ) {
aoqi@0 759 writer.pOln("};");
aoqi@0 760 }
aoqi@0 761 }
aoqi@0 762 }
aoqi@0 763
aoqi@0 764 /**
aoqi@0 765 * Create a Type instance for the given type. Requires that
aoqi@0 766 * setName(Identifier) be called afterward.
aoqi@0 767 */
aoqi@0 768 protected Type(ContextStack stack, int fullTypeCode) {
aoqi@0 769 this.env = stack.getEnv();
aoqi@0 770 this.stack = stack;
aoqi@0 771 this.fullTypeCode = fullTypeCode;
aoqi@0 772 typeCode = fullTypeCode & TYPE_MASK;
aoqi@0 773 }
aoqi@0 774
aoqi@0 775 /**
aoqi@0 776 * Set type codes. May only be called during initialization.
aoqi@0 777 */
aoqi@0 778 protected void setTypeCode(int fullTypeCode) {
aoqi@0 779 this.fullTypeCode = fullTypeCode;
aoqi@0 780 typeCode = fullTypeCode & TYPE_MASK;
aoqi@0 781 }
aoqi@0 782
aoqi@0 783 /**
aoqi@0 784 * Set name and package. May only be called during initialization.
aoqi@0 785 */
aoqi@0 786 protected void setNames(Identifier id, String[] idlModuleNames, String idlName) {
aoqi@0 787
aoqi@0 788 this.id = id;
aoqi@0 789 name = Names.mangleClass(id).getName().toString();
aoqi@0 790 packageName = null;
aoqi@0 791
aoqi@0 792 if (id.isQualified()) {
aoqi@0 793 packageName = id.getQualifier().toString();
aoqi@0 794 qualifiedName = packageName + NAME_SEPARATOR + name;
aoqi@0 795 } else {
aoqi@0 796 qualifiedName = name;
aoqi@0 797 }
aoqi@0 798
aoqi@0 799 setIDLNames(idlModuleNames,idlName);
aoqi@0 800 }
aoqi@0 801
aoqi@0 802
aoqi@0 803 /**
aoqi@0 804 * Set IDL name. May only be called during initialization.
aoqi@0 805 */
aoqi@0 806 protected void setIDLNames(String[] idlModuleNames, String idlName) {
aoqi@0 807 this.idlName = idlName;
aoqi@0 808
aoqi@0 809 if (idlModuleNames != null) {
aoqi@0 810 this.idlModuleNames = idlModuleNames;
aoqi@0 811 } else {
aoqi@0 812 this.idlModuleNames = new String[0];
aoqi@0 813 }
aoqi@0 814 qualifiedIDLName = IDLNames.getQualifiedName(idlModuleNames,idlName);
aoqi@0 815 }
aoqi@0 816
aoqi@0 817 /**
aoqi@0 818 * Report a ClassNotFoundException thru the compiler environment.
aoqi@0 819 */
aoqi@0 820 protected static void classNotFound(ContextStack stack,
aoqi@0 821 ClassNotFound e) {
aoqi@0 822 classNotFound(false,stack,e);
aoqi@0 823 }
aoqi@0 824
aoqi@0 825 /**
aoqi@0 826 * Report a ClassNotFoundException thru the compiler environment.
aoqi@0 827 */
aoqi@0 828 protected static void classNotFound(boolean quiet,
aoqi@0 829 ContextStack stack,
aoqi@0 830 ClassNotFound e) {
aoqi@0 831 if (!quiet) stack.getEnv().error(0, "rmic.class.not.found", e.name);
aoqi@0 832 stack.traceCallStack();
aoqi@0 833 }
aoqi@0 834
aoqi@0 835 /**
aoqi@0 836 * Report a constraint failure thru the compiler environment.
aoqi@0 837 * @param constraintNum Used to generate a key of the form
aoqi@0 838 "rmic.iiop.constraint.N", which must identify a message
aoqi@0 839 in the "rmic.properties" file.
aoqi@0 840 * @param quiet True if should not cause failure or message.
aoqi@0 841 * @param stack The context stack.
aoqi@0 842 * @param arg0 An object to substitute for {0} in the message.
aoqi@0 843 * @param arg1 An object to substitute for {1} in the message.
aoqi@0 844 * @param arg2 An object to substitute for {2} in the message.
aoqi@0 845 * @return false.
aoqi@0 846 */
aoqi@0 847 protected static boolean failedConstraint(int constraintNum,
aoqi@0 848 boolean quiet,
aoqi@0 849 ContextStack stack,
aoqi@0 850 Object arg0, Object arg1, Object arg2) {
aoqi@0 851 String message = "rmic.iiop.constraint." + constraintNum;
aoqi@0 852
aoqi@0 853 if (!quiet) {
aoqi@0 854 stack.getEnv().error(0,message,
aoqi@0 855 (arg0 != null ? arg0.toString() : null),
aoqi@0 856 (arg1 != null ? arg1.toString() : null),
aoqi@0 857 (arg2 != null ? arg2.toString() : null));
aoqi@0 858 } else {
aoqi@0 859 String error = stack.getEnv().errorString(message,arg0,arg1,arg2);
aoqi@0 860 stack.traceln(error);
aoqi@0 861 }
aoqi@0 862
aoqi@0 863 return false;
aoqi@0 864 }
aoqi@0 865
aoqi@0 866 /**
aoqi@0 867 * Report a constraint failure thru the compiler environment.
aoqi@0 868 * @param constraintNum Used to generate a key of the form
aoqi@0 869 "rmic.iiop.constraint.N", which must identify a message
aoqi@0 870 in the "rmic.properties" file.
aoqi@0 871 * @param quiet True if should not cause failure or message.
aoqi@0 872 * @param stack The context stack.
aoqi@0 873 * @param arg0 An object to substitute for {0} in the message.
aoqi@0 874 * @param arg1 An object to substitute for {1} in the message.
aoqi@0 875 * @return false.
aoqi@0 876 */
aoqi@0 877 protected static boolean failedConstraint(int constraintNum,
aoqi@0 878 boolean quiet,
aoqi@0 879 ContextStack stack,
aoqi@0 880 Object arg0, Object arg1) {
aoqi@0 881 return failedConstraint(constraintNum,quiet,stack,arg0,arg1,null);
aoqi@0 882 }
aoqi@0 883
aoqi@0 884
aoqi@0 885 /**
aoqi@0 886 * Report a constraint failure thru the compiler environment.
aoqi@0 887 * @param constraintNum Used to generate a key of the form
aoqi@0 888 "rmic.iiop.constraint.N", which must identify a message
aoqi@0 889 in the "rmic.properties" file.
aoqi@0 890 * @param quiet True if should not cause failure or message.
aoqi@0 891 * @param stack The context stack.
aoqi@0 892 * @param arg0 An object to substitute for {0} in the message.
aoqi@0 893 * @return false.
aoqi@0 894 */
aoqi@0 895 protected static boolean failedConstraint(int constraintNum,
aoqi@0 896 boolean quiet,
aoqi@0 897 ContextStack stack,
aoqi@0 898 Object arg0) {
aoqi@0 899 return failedConstraint(constraintNum,quiet,stack,arg0,null,null);
aoqi@0 900 }
aoqi@0 901
aoqi@0 902 /**
aoqi@0 903 * Report a constraint failure thru the compiler environment.
aoqi@0 904 * @param quiet True if should not cause failure or message.
aoqi@0 905 * @param stack The context stack.
aoqi@0 906 * @param constraintNum Used to generate a key of the form
aoqi@0 907 "rmic.iiop.constraint.N", which must identify a message
aoqi@0 908 in the "rmic.properties" file.
aoqi@0 909 * @return false.
aoqi@0 910 */
aoqi@0 911 protected static boolean failedConstraint(int constraintNum,
aoqi@0 912 boolean quiet,
aoqi@0 913 ContextStack stack) {
aoqi@0 914 return failedConstraint(constraintNum,quiet,stack,null,null,null);
aoqi@0 915 }
aoqi@0 916
aoqi@0 917 /**
aoqi@0 918 * Cloning is supported by returning a shallow copy of this object.
aoqi@0 919 */
aoqi@0 920 protected Object clone() {
aoqi@0 921 try {
aoqi@0 922 return super.clone();
aoqi@0 923 } catch (CloneNotSupportedException e) {
aoqi@0 924 throw new Error("clone failed");
aoqi@0 925 }
aoqi@0 926 }
aoqi@0 927
aoqi@0 928 /*
aoqi@0 929 * Add matching types to list. Return true if this type has not
aoqi@0 930 * been previously checked, false otherwise.
aoqi@0 931 */
aoqi@0 932 protected boolean addTypes (int typeCodeFilter,
aoqi@0 933 HashSet checked,
aoqi@0 934 Vector matching) {
aoqi@0 935
aoqi@0 936 boolean result;
aoqi@0 937
aoqi@0 938 // Have we already checked this type?
aoqi@0 939
aoqi@0 940 if (checked.contains(this)) {
aoqi@0 941
aoqi@0 942 // Yes, so return false.
aoqi@0 943
aoqi@0 944 result = false;
aoqi@0 945
aoqi@0 946 } else {
aoqi@0 947
aoqi@0 948 // Nope, so add it...
aoqi@0 949
aoqi@0 950 checked.add(this);
aoqi@0 951
aoqi@0 952 // Do we match the filter?
aoqi@0 953
aoqi@0 954 if (typeMatches(typeCodeFilter)) {
aoqi@0 955
aoqi@0 956 // Yep. so add it and set result to true...
aoqi@0 957
aoqi@0 958 matching.addElement(this);
aoqi@0 959 }
aoqi@0 960
aoqi@0 961 // Return true.
aoqi@0 962
aoqi@0 963 result = true;
aoqi@0 964 }
aoqi@0 965
aoqi@0 966 return result;
aoqi@0 967 }
aoqi@0 968
aoqi@0 969 /*
aoqi@0 970 * Load a Class instance. Return null if fail.
aoqi@0 971 */
aoqi@0 972 protected abstract Class loadClass();
aoqi@0 973
aoqi@0 974 private boolean initClass() {
aoqi@0 975 if (ourClass == null) {
aoqi@0 976 ourClass = loadClass();
aoqi@0 977 if (ourClass == null) {
aoqi@0 978 failedConstraint(27,false,stack,getQualifiedName());
aoqi@0 979 return false;
aoqi@0 980 }
aoqi@0 981 }
aoqi@0 982 return true;
aoqi@0 983 }
aoqi@0 984
aoqi@0 985 /*
aoqi@0 986 * Set the clz and repositoryID fields. Reports error
aoqi@0 987 * and returns false if fails, returns true if succeeds.
aoqi@0 988 */
aoqi@0 989 protected boolean setRepositoryID() {
aoqi@0 990
aoqi@0 991 // First, load the class...
aoqi@0 992
aoqi@0 993 if (!initClass()) {
aoqi@0 994 return false;
aoqi@0 995 }
aoqi@0 996
aoqi@0 997 // Now make the repositoryID and return success...
aoqi@0 998
aoqi@0 999 repositoryID = RepositoryId.createForAnyType(ourClass);
aoqi@0 1000 return true;
aoqi@0 1001 }
aoqi@0 1002
aoqi@0 1003
aoqi@0 1004 //_____________________________________________________________________
aoqi@0 1005 // Internal Interfaces
aoqi@0 1006 //_____________________________________________________________________
aoqi@0 1007
aoqi@0 1008 private Type () {} // Disallowed.
aoqi@0 1009 }

mercurial