src/share/classes/com/sun/tools/javac/code/Symbol.java

Wed, 13 Apr 2011 11:35:43 -0700

author
jjh
date
Wed, 13 Apr 2011 11:35:43 -0700
changeset 972
694ff82ca68e
parent 904
4baab658f357
child 1005
68fde7f5863b
permissions
-rw-r--r--

7032975: API files in javax.annotation.processing need to be updated for references to JLS
7032972: API files in javax.tools need to updated for references to JVM Spec with editions/hyperlinks
7032978: API files in javax.tools need to be updated for references to JLS with editions/hyperlinks
Summary: Removed URLs and 'edition' references
Reviewed-by: jjg, darcy

duke@1 1 /*
jjg@815 2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.code;
duke@1 27
duke@1 28 import java.util.Set;
duke@1 29 import java.util.concurrent.Callable;
duke@1 30 import javax.lang.model.element.*;
duke@1 31 import javax.tools.JavaFileObject;
duke@1 32
duke@1 33 import com.sun.tools.javac.util.*;
duke@1 34 import com.sun.tools.javac.util.Name;
duke@1 35 import com.sun.tools.javac.code.Type.*;
duke@1 36 import com.sun.tools.javac.comp.Attr;
duke@1 37 import com.sun.tools.javac.comp.AttrContext;
duke@1 38 import com.sun.tools.javac.comp.Env;
duke@1 39 import com.sun.tools.javac.jvm.*;
duke@1 40 import com.sun.tools.javac.model.*;
duke@1 41 import com.sun.tools.javac.tree.JCTree;
duke@1 42
duke@1 43 import static com.sun.tools.javac.code.Flags.*;
duke@1 44 import static com.sun.tools.javac.code.Kinds.*;
duke@1 45 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 46
duke@1 47 /** Root class for Java symbols. It contains subclasses
duke@1 48 * for specific sorts of symbols, such as variables, methods and operators,
duke@1 49 * types, packages. Each subclass is represented as a static inner class
duke@1 50 * inside Symbol.
duke@1 51 *
jjg@581 52 * <p><b>This is NOT part of any supported API.
jjg@581 53 * If you write code that depends on this, you do so at your own risk.
duke@1 54 * This code and its internal interfaces are subject to change or
duke@1 55 * deletion without notice.</b>
duke@1 56 */
duke@1 57 public abstract class Symbol implements Element {
duke@1 58 // public Throwable debug = new Throwable();
duke@1 59
duke@1 60 /** The kind of this symbol.
duke@1 61 * @see Kinds
duke@1 62 */
duke@1 63 public int kind;
duke@1 64
duke@1 65 /** The flags of this symbol.
duke@1 66 */
duke@1 67 public long flags_field;
duke@1 68
duke@1 69 /** An accessor method for the flags of this symbol.
duke@1 70 * Flags of class symbols should be accessed through the accessor
duke@1 71 * method to make sure that the class symbol is loaded.
duke@1 72 */
duke@1 73 public long flags() { return flags_field; }
duke@1 74
duke@1 75 /** The attributes of this symbol.
duke@1 76 */
duke@1 77 public List<Attribute.Compound> attributes_field;
duke@1 78
duke@1 79 /** An accessor method for the attributes of this symbol.
duke@1 80 * Attributes of class symbols should be accessed through the accessor
duke@1 81 * method to make sure that the class symbol is loaded.
duke@1 82 */
duke@1 83 public List<Attribute.Compound> getAnnotationMirrors() {
jjg@816 84 return Assert.checkNonNull(attributes_field);
duke@1 85 }
duke@1 86
duke@1 87 /** Fetch a particular annotation from a symbol. */
duke@1 88 public Attribute.Compound attribute(Symbol anno) {
duke@1 89 for (Attribute.Compound a : getAnnotationMirrors())
duke@1 90 if (a.type.tsym == anno) return a;
duke@1 91 return null;
duke@1 92 }
duke@1 93
duke@1 94 /** The name of this symbol in Utf8 representation.
duke@1 95 */
duke@1 96 public Name name;
duke@1 97
duke@1 98 /** The type of this symbol.
duke@1 99 */
duke@1 100 public Type type;
duke@1 101
duke@1 102 /** The owner of this symbol.
duke@1 103 */
duke@1 104 public Symbol owner;
duke@1 105
duke@1 106 /** The completer of this symbol.
duke@1 107 */
duke@1 108 public Completer completer;
duke@1 109
duke@1 110 /** A cache for the type erasure of this symbol.
duke@1 111 */
duke@1 112 public Type erasure_field;
duke@1 113
duke@1 114 /** Construct a symbol with given kind, flags, name, type and owner.
duke@1 115 */
duke@1 116 public Symbol(int kind, long flags, Name name, Type type, Symbol owner) {
duke@1 117 this.kind = kind;
duke@1 118 this.flags_field = flags;
duke@1 119 this.type = type;
duke@1 120 this.owner = owner;
duke@1 121 this.completer = null;
duke@1 122 this.erasure_field = null;
duke@1 123 this.attributes_field = List.nil();
duke@1 124 this.name = name;
duke@1 125 }
duke@1 126
duke@1 127 /** Clone this symbol with new owner.
duke@1 128 * Legal only for fields and methods.
duke@1 129 */
duke@1 130 public Symbol clone(Symbol newOwner) {
duke@1 131 throw new AssertionError();
duke@1 132 }
duke@1 133
mcimadamore@121 134 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 135 return v.visitSymbol(this, p);
mcimadamore@121 136 }
mcimadamore@121 137
duke@1 138 /** The Java source which this symbol represents.
duke@1 139 * A description of this symbol; overrides Object.
duke@1 140 */
duke@1 141 public String toString() {
duke@1 142 return name.toString();
duke@1 143 }
duke@1 144
duke@1 145 /** A Java source description of the location of this symbol; used for
mcimadamore@80 146 * error reporting.
mcimadamore@80 147 *
mcimadamore@80 148 * @return null if the symbol is a package or a toplevel class defined in
mcimadamore@80 149 * the default package; otherwise, the owner symbol is returned
duke@1 150 */
mcimadamore@80 151 public Symbol location() {
jjg@509 152 if (owner.name == null || (owner.name.isEmpty() && owner.kind != PCK && owner.kind != TYP)) {
mcimadamore@80 153 return null;
duke@1 154 }
mcimadamore@80 155 return owner;
duke@1 156 }
duke@1 157
mcimadamore@80 158 public Symbol location(Type site, Types types) {
jjg@113 159 if (owner.name == null || owner.name.isEmpty()) {
duke@1 160 return location();
duke@1 161 }
duke@1 162 if (owner.type.tag == CLASS) {
duke@1 163 Type ownertype = types.asOuterSuper(site, owner);
mcimadamore@80 164 if (ownertype != null) return ownertype.tsym;
duke@1 165 }
mcimadamore@80 166 return owner;
duke@1 167 }
duke@1 168
duke@1 169 /** The symbol's erased type.
duke@1 170 */
duke@1 171 public Type erasure(Types types) {
duke@1 172 if (erasure_field == null)
duke@1 173 erasure_field = types.erasure(type);
duke@1 174 return erasure_field;
duke@1 175 }
duke@1 176
duke@1 177 /** The external type of a symbol. This is the symbol's erased type
duke@1 178 * except for constructors of inner classes which get the enclosing
duke@1 179 * instance class added as first argument.
duke@1 180 */
duke@1 181 public Type externalType(Types types) {
duke@1 182 Type t = erasure(types);
jjg@113 183 if (name == name.table.names.init && owner.hasOuterInstance()) {
duke@1 184 Type outerThisType = types.erasure(owner.type.getEnclosingType());
duke@1 185 return new MethodType(t.getParameterTypes().prepend(outerThisType),
duke@1 186 t.getReturnType(),
duke@1 187 t.getThrownTypes(),
duke@1 188 t.tsym);
duke@1 189 } else {
duke@1 190 return t;
duke@1 191 }
duke@1 192 }
duke@1 193
duke@1 194 public boolean isStatic() {
duke@1 195 return
duke@1 196 (flags() & STATIC) != 0 ||
duke@1 197 (owner.flags() & INTERFACE) != 0 && kind != MTH;
duke@1 198 }
duke@1 199
duke@1 200 public boolean isInterface() {
duke@1 201 return (flags() & INTERFACE) != 0;
duke@1 202 }
duke@1 203
mcimadamore@674 204 /** Recognize if this symbol was marked @PolymorphicSignature in the source. */
mcimadamore@674 205 public boolean isPolymorphicSignatureGeneric() {
mcimadamore@674 206 return (flags() & (POLYMORPHIC_SIGNATURE | HYPOTHETICAL)) == POLYMORPHIC_SIGNATURE;
mcimadamore@674 207 }
mcimadamore@674 208
mcimadamore@674 209 /** Recognize if this symbol was split from a @PolymorphicSignature symbol in the source. */
mcimadamore@674 210 public boolean isPolymorphicSignatureInstance() {
mcimadamore@674 211 return (flags() & (POLYMORPHIC_SIGNATURE | HYPOTHETICAL)) == (POLYMORPHIC_SIGNATURE | HYPOTHETICAL);
mcimadamore@674 212 }
mcimadamore@674 213
duke@1 214 /** Is this symbol declared (directly or indirectly) local
duke@1 215 * to a method or variable initializer?
duke@1 216 * Also includes fields of inner classes which are in
duke@1 217 * turn local to a method or variable initializer.
duke@1 218 */
duke@1 219 public boolean isLocal() {
duke@1 220 return
duke@1 221 (owner.kind & (VAR | MTH)) != 0 ||
duke@1 222 (owner.kind == TYP && owner.isLocal());
duke@1 223 }
duke@1 224
mcimadamore@537 225 /** Has this symbol an empty name? This includes anonymous
mcimadamore@537 226 * inner classses.
mcimadamore@537 227 */
mcimadamore@537 228 public boolean isAnonymous() {
mcimadamore@537 229 return name.isEmpty();
mcimadamore@537 230 }
mcimadamore@537 231
duke@1 232 /** Is this symbol a constructor?
duke@1 233 */
duke@1 234 public boolean isConstructor() {
jjg@113 235 return name == name.table.names.init;
duke@1 236 }
duke@1 237
duke@1 238 /** The fully qualified name of this symbol.
duke@1 239 * This is the same as the symbol's name except for class symbols,
duke@1 240 * which are handled separately.
duke@1 241 */
duke@1 242 public Name getQualifiedName() {
duke@1 243 return name;
duke@1 244 }
duke@1 245
duke@1 246 /** The fully qualified name of this symbol after converting to flat
duke@1 247 * representation. This is the same as the symbol's name except for
duke@1 248 * class symbols, which are handled separately.
duke@1 249 */
duke@1 250 public Name flatName() {
duke@1 251 return getQualifiedName();
duke@1 252 }
duke@1 253
duke@1 254 /** If this is a class or package, its members, otherwise null.
duke@1 255 */
duke@1 256 public Scope members() {
duke@1 257 return null;
duke@1 258 }
duke@1 259
duke@1 260 /** A class is an inner class if it it has an enclosing instance class.
duke@1 261 */
duke@1 262 public boolean isInner() {
duke@1 263 return type.getEnclosingType().tag == CLASS;
duke@1 264 }
duke@1 265
duke@1 266 /** An inner class has an outer instance if it is not an interface
duke@1 267 * it has an enclosing instance class which might be referenced from the class.
duke@1 268 * Nested classes can see instance members of their enclosing class.
duke@1 269 * Their constructors carry an additional this$n parameter, inserted
duke@1 270 * implicitly by the compiler.
duke@1 271 *
duke@1 272 * @see #isInner
duke@1 273 */
duke@1 274 public boolean hasOuterInstance() {
duke@1 275 return
duke@1 276 type.getEnclosingType().tag == CLASS && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
duke@1 277 }
duke@1 278
duke@1 279 /** The closest enclosing class of this symbol's declaration.
duke@1 280 */
duke@1 281 public ClassSymbol enclClass() {
duke@1 282 Symbol c = this;
duke@1 283 while (c != null &&
duke@1 284 ((c.kind & TYP) == 0 || c.type.tag != CLASS)) {
duke@1 285 c = c.owner;
duke@1 286 }
duke@1 287 return (ClassSymbol)c;
duke@1 288 }
duke@1 289
duke@1 290 /** The outermost class which indirectly owns this symbol.
duke@1 291 */
duke@1 292 public ClassSymbol outermostClass() {
duke@1 293 Symbol sym = this;
duke@1 294 Symbol prev = null;
duke@1 295 while (sym.kind != PCK) {
duke@1 296 prev = sym;
duke@1 297 sym = sym.owner;
duke@1 298 }
duke@1 299 return (ClassSymbol) prev;
duke@1 300 }
duke@1 301
duke@1 302 /** The package which indirectly owns this symbol.
duke@1 303 */
duke@1 304 public PackageSymbol packge() {
duke@1 305 Symbol sym = this;
duke@1 306 while (sym.kind != PCK) {
duke@1 307 sym = sym.owner;
duke@1 308 }
duke@1 309 return (PackageSymbol) sym;
duke@1 310 }
duke@1 311
duke@1 312 /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
duke@1 313 */
duke@1 314 public boolean isSubClass(Symbol base, Types types) {
duke@1 315 throw new AssertionError("isSubClass " + this);
duke@1 316 }
duke@1 317
duke@1 318 /** Fully check membership: hierarchy, protection, and hiding.
duke@1 319 * Does not exclude methods not inherited due to overriding.
duke@1 320 */
duke@1 321 public boolean isMemberOf(TypeSymbol clazz, Types types) {
duke@1 322 return
duke@1 323 owner == clazz ||
duke@1 324 clazz.isSubClass(owner, types) &&
duke@1 325 isInheritedIn(clazz, types) &&
duke@1 326 !hiddenIn((ClassSymbol)clazz, types);
duke@1 327 }
duke@1 328
duke@1 329 /** Is this symbol the same as or enclosed by the given class? */
duke@1 330 public boolean isEnclosedBy(ClassSymbol clazz) {
duke@1 331 for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
duke@1 332 if (sym == clazz) return true;
duke@1 333 return false;
duke@1 334 }
duke@1 335
duke@1 336 /** Check for hiding. Note that this doesn't handle multiple
duke@1 337 * (interface) inheritance. */
duke@1 338 private boolean hiddenIn(ClassSymbol clazz, Types types) {
duke@1 339 if (kind == MTH && (flags() & STATIC) == 0) return false;
duke@1 340 while (true) {
duke@1 341 if (owner == clazz) return false;
duke@1 342 Scope.Entry e = clazz.members().lookup(name);
duke@1 343 while (e.scope != null) {
duke@1 344 if (e.sym == this) return false;
duke@1 345 if (e.sym.kind == kind &&
duke@1 346 (kind != MTH ||
duke@1 347 (e.sym.flags() & STATIC) != 0 &&
duke@1 348 types.isSubSignature(e.sym.type, type)))
duke@1 349 return true;
duke@1 350 e = e.next();
duke@1 351 }
duke@1 352 Type superType = types.supertype(clazz.type);
duke@1 353 if (superType.tag != TypeTags.CLASS) return false;
duke@1 354 clazz = (ClassSymbol)superType.tsym;
duke@1 355 }
duke@1 356 }
duke@1 357
duke@1 358 /** Is this symbol inherited into a given class?
duke@1 359 * PRE: If symbol's owner is a interface,
duke@1 360 * it is already assumed that the interface is a superinterface
duke@1 361 * of given class.
duke@1 362 * @param clazz The class for which we want to establish membership.
duke@1 363 * This must be a subclass of the member's owner.
duke@1 364 */
duke@1 365 public boolean isInheritedIn(Symbol clazz, Types types) {
duke@1 366 switch ((int)(flags_field & Flags.AccessFlags)) {
duke@1 367 default: // error recovery
duke@1 368 case PUBLIC:
duke@1 369 return true;
duke@1 370 case PRIVATE:
duke@1 371 return this.owner == clazz;
duke@1 372 case PROTECTED:
duke@1 373 // we model interfaces as extending Object
duke@1 374 return (clazz.flags() & INTERFACE) == 0;
duke@1 375 case 0:
duke@1 376 PackageSymbol thisPackage = this.packge();
duke@1 377 for (Symbol sup = clazz;
duke@1 378 sup != null && sup != this.owner;
duke@1 379 sup = types.supertype(sup.type).tsym) {
mcimadamore@155 380 while (sup.type.tag == TYPEVAR)
mcimadamore@155 381 sup = sup.type.getUpperBound().tsym;
duke@1 382 if (sup.type.isErroneous())
duke@1 383 return true; // error recovery
duke@1 384 if ((sup.flags() & COMPOUND) != 0)
duke@1 385 continue;
duke@1 386 if (sup.packge() != thisPackage)
duke@1 387 return false;
duke@1 388 }
duke@1 389 return (clazz.flags() & INTERFACE) == 0;
duke@1 390 }
duke@1 391 }
duke@1 392
duke@1 393 /** The (variable or method) symbol seen as a member of given
duke@1 394 * class type`site' (this might change the symbol's type).
duke@1 395 * This is used exclusively for producing diagnostics.
duke@1 396 */
duke@1 397 public Symbol asMemberOf(Type site, Types types) {
duke@1 398 throw new AssertionError();
duke@1 399 }
duke@1 400
duke@1 401 /** Does this method symbol override `other' symbol, when both are seen as
duke@1 402 * members of class `origin'? It is assumed that _other is a member
duke@1 403 * of origin.
duke@1 404 *
duke@1 405 * It is assumed that both symbols have the same name. The static
duke@1 406 * modifier is ignored for this test.
duke@1 407 *
duke@1 408 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
duke@1 409 */
duke@1 410 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
duke@1 411 return false;
duke@1 412 }
duke@1 413
duke@1 414 /** Complete the elaboration of this symbol's definition.
duke@1 415 */
duke@1 416 public void complete() throws CompletionFailure {
duke@1 417 if (completer != null) {
duke@1 418 Completer c = completer;
duke@1 419 completer = null;
duke@1 420 c.complete(this);
duke@1 421 }
duke@1 422 }
duke@1 423
duke@1 424 /** True if the symbol represents an entity that exists.
duke@1 425 */
duke@1 426 public boolean exists() {
duke@1 427 return true;
duke@1 428 }
duke@1 429
duke@1 430 public Type asType() {
duke@1 431 return type;
duke@1 432 }
duke@1 433
duke@1 434 public Symbol getEnclosingElement() {
duke@1 435 return owner;
duke@1 436 }
duke@1 437
duke@1 438 public ElementKind getKind() {
duke@1 439 return ElementKind.OTHER; // most unkind
duke@1 440 }
duke@1 441
duke@1 442 public Set<Modifier> getModifiers() {
duke@1 443 return Flags.asModifierSet(flags());
duke@1 444 }
duke@1 445
duke@1 446 public Name getSimpleName() {
duke@1 447 return name;
duke@1 448 }
duke@1 449
duke@1 450 /**
duke@1 451 * @deprecated this method should never be used by javac internally.
duke@1 452 */
duke@1 453 @Deprecated
duke@1 454 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
duke@1 455 return JavacElements.getAnnotation(this, annoType);
duke@1 456 }
duke@1 457
duke@1 458 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
duke@1 459 public java.util.List<Symbol> getEnclosedElements() {
duke@1 460 return List.nil();
duke@1 461 }
duke@1 462
duke@1 463 public List<TypeSymbol> getTypeParameters() {
duke@1 464 ListBuffer<TypeSymbol> l = ListBuffer.lb();
duke@1 465 for (Type t : type.getTypeArguments()) {
duke@1 466 l.append(t.tsym);
duke@1 467 }
duke@1 468 return l.toList();
duke@1 469 }
duke@1 470
duke@1 471 public static class DelegatedSymbol extends Symbol {
duke@1 472 protected Symbol other;
duke@1 473 public DelegatedSymbol(Symbol other) {
duke@1 474 super(other.kind, other.flags_field, other.name, other.type, other.owner);
duke@1 475 this.other = other;
duke@1 476 }
duke@1 477 public String toString() { return other.toString(); }
mcimadamore@80 478 public Symbol location() { return other.location(); }
mcimadamore@80 479 public Symbol location(Type site, Types types) { return other.location(site, types); }
duke@1 480 public Type erasure(Types types) { return other.erasure(types); }
duke@1 481 public Type externalType(Types types) { return other.externalType(types); }
duke@1 482 public boolean isLocal() { return other.isLocal(); }
duke@1 483 public boolean isConstructor() { return other.isConstructor(); }
duke@1 484 public Name getQualifiedName() { return other.getQualifiedName(); }
duke@1 485 public Name flatName() { return other.flatName(); }
duke@1 486 public Scope members() { return other.members(); }
duke@1 487 public boolean isInner() { return other.isInner(); }
duke@1 488 public boolean hasOuterInstance() { return other.hasOuterInstance(); }
duke@1 489 public ClassSymbol enclClass() { return other.enclClass(); }
duke@1 490 public ClassSymbol outermostClass() { return other.outermostClass(); }
duke@1 491 public PackageSymbol packge() { return other.packge(); }
duke@1 492 public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
duke@1 493 public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
duke@1 494 public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
duke@1 495 public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
duke@1 496 public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
duke@1 497 public void complete() throws CompletionFailure { other.complete(); }
duke@1 498
duke@1 499 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 500 return other.accept(v, p);
duke@1 501 }
mcimadamore@121 502
mcimadamore@121 503 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 504 return v.visitSymbol(other, p);
mcimadamore@121 505 }
duke@1 506 }
duke@1 507
duke@1 508 /** A class for type symbols. Type variables are represented by instances
duke@1 509 * of this class, classes and packages by instances of subclasses.
duke@1 510 */
duke@1 511 public static class TypeSymbol
duke@1 512 extends Symbol implements TypeParameterElement {
duke@1 513 // Implements TypeParameterElement because type parameters don't
duke@1 514 // have their own TypeSymbol subclass.
duke@1 515 // TODO: type parameters should have their own TypeSymbol subclass
duke@1 516
duke@1 517 public TypeSymbol(long flags, Name name, Type type, Symbol owner) {
duke@1 518 super(TYP, flags, name, type, owner);
duke@1 519 }
duke@1 520
duke@1 521 /** form a fully qualified name from a name and an owner
duke@1 522 */
duke@1 523 static public Name formFullName(Name name, Symbol owner) {
duke@1 524 if (owner == null) return name;
duke@1 525 if (((owner.kind != ERR)) &&
duke@1 526 ((owner.kind & (VAR | MTH)) != 0
duke@1 527 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
duke@1 528 )) return name;
duke@1 529 Name prefix = owner.getQualifiedName();
jjg@113 530 if (prefix == null || prefix == prefix.table.names.empty)
duke@1 531 return name;
duke@1 532 else return prefix.append('.', name);
duke@1 533 }
duke@1 534
duke@1 535 /** form a fully qualified name from a name and an owner, after
duke@1 536 * converting to flat representation
duke@1 537 */
duke@1 538 static public Name formFlatName(Name name, Symbol owner) {
duke@1 539 if (owner == null ||
duke@1 540 (owner.kind & (VAR | MTH)) != 0
duke@1 541 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
duke@1 542 ) return name;
duke@1 543 char sep = owner.kind == TYP ? '$' : '.';
duke@1 544 Name prefix = owner.flatName();
jjg@113 545 if (prefix == null || prefix == prefix.table.names.empty)
duke@1 546 return name;
duke@1 547 else return prefix.append(sep, name);
duke@1 548 }
duke@1 549
duke@1 550 /**
duke@1 551 * A total ordering between type symbols that refines the
duke@1 552 * class inheritance graph.
duke@1 553 *
duke@1 554 * Typevariables always precede other kinds of symbols.
duke@1 555 */
duke@1 556 public final boolean precedes(TypeSymbol that, Types types) {
duke@1 557 if (this == that)
duke@1 558 return false;
duke@1 559 if (this.type.tag == that.type.tag) {
duke@1 560 if (this.type.tag == CLASS) {
duke@1 561 return
duke@1 562 types.rank(that.type) < types.rank(this.type) ||
duke@1 563 types.rank(that.type) == types.rank(this.type) &&
duke@1 564 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
duke@1 565 } else if (this.type.tag == TYPEVAR) {
duke@1 566 return types.isSubtype(this.type, that.type);
duke@1 567 }
duke@1 568 }
duke@1 569 return this.type.tag == TYPEVAR;
duke@1 570 }
duke@1 571
duke@1 572 // For type params; overridden in subclasses.
duke@1 573 public ElementKind getKind() {
duke@1 574 return ElementKind.TYPE_PARAMETER;
duke@1 575 }
duke@1 576
duke@1 577 public java.util.List<Symbol> getEnclosedElements() {
duke@1 578 List<Symbol> list = List.nil();
sundar@666 579 if (kind == TYP && type.tag == TYPEVAR) {
sundar@666 580 return list;
sundar@666 581 }
duke@1 582 for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
duke@1 583 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
duke@1 584 list = list.prepend(e.sym);
duke@1 585 }
duke@1 586 return list;
duke@1 587 }
duke@1 588
duke@1 589 // For type params.
duke@1 590 // Perhaps not needed if getEnclosingElement can be spec'ed
duke@1 591 // to do the same thing.
duke@1 592 // TODO: getGenericElement() might not be needed
duke@1 593 public Symbol getGenericElement() {
duke@1 594 return owner;
duke@1 595 }
duke@1 596
duke@1 597 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
jjg@816 598 Assert.check(type.tag == TYPEVAR); // else override will be invoked
duke@1 599 return v.visitTypeParameter(this, p);
duke@1 600 }
duke@1 601
mcimadamore@121 602 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 603 return v.visitTypeSymbol(this, p);
mcimadamore@121 604 }
mcimadamore@121 605
duke@1 606 public List<Type> getBounds() {
duke@1 607 TypeVar t = (TypeVar)type;
duke@1 608 Type bound = t.getUpperBound();
duke@1 609 if (!bound.isCompound())
duke@1 610 return List.of(bound);
duke@1 611 ClassType ct = (ClassType)bound;
duke@1 612 if (!ct.tsym.erasure_field.isInterface()) {
duke@1 613 return ct.interfaces_field.prepend(ct.supertype_field);
duke@1 614 } else {
duke@1 615 // No superclass was given in bounds.
duke@1 616 // In this case, supertype is Object, erasure is first interface.
duke@1 617 return ct.interfaces_field;
duke@1 618 }
duke@1 619 }
duke@1 620 }
duke@1 621
duke@1 622 /** A class for package symbols
duke@1 623 */
duke@1 624 public static class PackageSymbol extends TypeSymbol
duke@1 625 implements PackageElement {
duke@1 626
duke@1 627 public Scope members_field;
duke@1 628 public Name fullname;
duke@1 629 public ClassSymbol package_info; // see bug 6443073
duke@1 630
duke@1 631 public PackageSymbol(Name name, Type type, Symbol owner) {
duke@1 632 super(0, name, type, owner);
duke@1 633 this.kind = PCK;
duke@1 634 this.members_field = null;
duke@1 635 this.fullname = formFullName(name, owner);
duke@1 636 }
duke@1 637
duke@1 638 public PackageSymbol(Name name, Symbol owner) {
duke@1 639 this(name, null, owner);
duke@1 640 this.type = new PackageType(this);
duke@1 641 }
duke@1 642
duke@1 643 public String toString() {
duke@1 644 return fullname.toString();
duke@1 645 }
duke@1 646
duke@1 647 public Name getQualifiedName() {
duke@1 648 return fullname;
duke@1 649 }
duke@1 650
duke@1 651 public boolean isUnnamed() {
duke@1 652 return name.isEmpty() && owner != null;
duke@1 653 }
duke@1 654
duke@1 655 public Scope members() {
duke@1 656 if (completer != null) complete();
duke@1 657 return members_field;
duke@1 658 }
duke@1 659
duke@1 660 public long flags() {
duke@1 661 if (completer != null) complete();
duke@1 662 return flags_field;
duke@1 663 }
duke@1 664
duke@1 665 public List<Attribute.Compound> getAnnotationMirrors() {
duke@1 666 if (completer != null) complete();
jjg@483 667 if (package_info != null && package_info.completer != null) {
jjg@483 668 package_info.complete();
jjg@483 669 if (attributes_field.isEmpty())
jjg@483 670 attributes_field = package_info.attributes_field;
jjg@483 671 }
jjg@816 672 return Assert.checkNonNull(attributes_field);
duke@1 673 }
duke@1 674
duke@1 675 /** A package "exists" if a type or package that exists has
duke@1 676 * been seen within it.
duke@1 677 */
duke@1 678 public boolean exists() {
duke@1 679 return (flags_field & EXISTS) != 0;
duke@1 680 }
duke@1 681
duke@1 682 public ElementKind getKind() {
duke@1 683 return ElementKind.PACKAGE;
duke@1 684 }
duke@1 685
duke@1 686 public Symbol getEnclosingElement() {
duke@1 687 return null;
duke@1 688 }
duke@1 689
duke@1 690 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 691 return v.visitPackage(this, p);
duke@1 692 }
mcimadamore@121 693
mcimadamore@121 694 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 695 return v.visitPackageSymbol(this, p);
mcimadamore@121 696 }
duke@1 697 }
duke@1 698
duke@1 699 /** A class for class symbols
duke@1 700 */
duke@1 701 public static class ClassSymbol extends TypeSymbol implements TypeElement {
duke@1 702
duke@1 703 /** a scope for all class members; variables, methods and inner classes
duke@1 704 * type parameters are not part of this scope
duke@1 705 */
duke@1 706 public Scope members_field;
duke@1 707
duke@1 708 /** the fully qualified name of the class, i.e. pck.outer.inner.
duke@1 709 * null for anonymous classes
duke@1 710 */
duke@1 711 public Name fullname;
duke@1 712
duke@1 713 /** the fully qualified name of the class after converting to flat
duke@1 714 * representation, i.e. pck.outer$inner,
duke@1 715 * set externally for local and anonymous classes
duke@1 716 */
duke@1 717 public Name flatname;
duke@1 718
duke@1 719 /** the sourcefile where the class came from
duke@1 720 */
duke@1 721 public JavaFileObject sourcefile;
duke@1 722
duke@1 723 /** the classfile from where to load this class
duke@1 724 * this will have extension .class or .java
duke@1 725 */
duke@1 726 public JavaFileObject classfile;
duke@1 727
duke@1 728 /** the constant pool of the class
duke@1 729 */
duke@1 730 public Pool pool;
duke@1 731
mcimadamore@858 732 /** members closure cache (set by Types.membersClosure)
mcimadamore@858 733 */
mcimadamore@877 734 Scope.CompoundScope membersClosure;
mcimadamore@858 735
duke@1 736 public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
duke@1 737 super(flags, name, type, owner);
duke@1 738 this.members_field = null;
duke@1 739 this.fullname = formFullName(name, owner);
duke@1 740 this.flatname = formFlatName(name, owner);
duke@1 741 this.sourcefile = null;
duke@1 742 this.classfile = null;
duke@1 743 this.pool = null;
duke@1 744 }
duke@1 745
duke@1 746 public ClassSymbol(long flags, Name name, Symbol owner) {
duke@1 747 this(
duke@1 748 flags,
duke@1 749 name,
duke@1 750 new ClassType(Type.noType, null, null),
duke@1 751 owner);
duke@1 752 this.type.tsym = this;
duke@1 753 }
duke@1 754
duke@1 755 /** The Java source which this symbol represents.
duke@1 756 */
duke@1 757 public String toString() {
duke@1 758 return className();
duke@1 759 }
duke@1 760
duke@1 761 public long flags() {
duke@1 762 if (completer != null) complete();
duke@1 763 return flags_field;
duke@1 764 }
duke@1 765
duke@1 766 public Scope members() {
duke@1 767 if (completer != null) complete();
duke@1 768 return members_field;
duke@1 769 }
duke@1 770
duke@1 771 public List<Attribute.Compound> getAnnotationMirrors() {
duke@1 772 if (completer != null) complete();
jjg@816 773 return Assert.checkNonNull(attributes_field);
duke@1 774 }
duke@1 775
duke@1 776 public Type erasure(Types types) {
duke@1 777 if (erasure_field == null)
duke@1 778 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
duke@1 779 List.<Type>nil(), this);
duke@1 780 return erasure_field;
duke@1 781 }
duke@1 782
duke@1 783 public String className() {
jjg@113 784 if (name.isEmpty())
duke@1 785 return
duke@1 786 Log.getLocalizedString("anonymous.class", flatname);
duke@1 787 else
duke@1 788 return fullname.toString();
duke@1 789 }
duke@1 790
duke@1 791 public Name getQualifiedName() {
duke@1 792 return fullname;
duke@1 793 }
duke@1 794
duke@1 795 public Name flatName() {
duke@1 796 return flatname;
duke@1 797 }
duke@1 798
duke@1 799 public boolean isSubClass(Symbol base, Types types) {
duke@1 800 if (this == base) {
duke@1 801 return true;
duke@1 802 } else if ((base.flags() & INTERFACE) != 0) {
duke@1 803 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
duke@1 804 for (List<Type> is = types.interfaces(t);
duke@1 805 is.nonEmpty();
duke@1 806 is = is.tail)
duke@1 807 if (is.head.tsym.isSubClass(base, types)) return true;
duke@1 808 } else {
duke@1 809 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
duke@1 810 if (t.tsym == base) return true;
duke@1 811 }
duke@1 812 return false;
duke@1 813 }
duke@1 814
duke@1 815 /** Complete the elaboration of this symbol's definition.
duke@1 816 */
duke@1 817 public void complete() throws CompletionFailure {
duke@1 818 try {
duke@1 819 super.complete();
duke@1 820 } catch (CompletionFailure ex) {
duke@1 821 // quiet error recovery
duke@1 822 flags_field |= (PUBLIC|STATIC);
jjg@110 823 this.type = new ErrorType(this, Type.noType);
duke@1 824 throw ex;
duke@1 825 }
duke@1 826 }
duke@1 827
duke@1 828 public List<Type> getInterfaces() {
duke@1 829 complete();
duke@1 830 if (type instanceof ClassType) {
duke@1 831 ClassType t = (ClassType)type;
duke@1 832 if (t.interfaces_field == null) // FIXME: shouldn't be null
duke@1 833 t.interfaces_field = List.nil();
jjg@904 834 if (t.all_interfaces_field != null)
jjg@904 835 return Type.getModelTypes(t.all_interfaces_field);
duke@1 836 return t.interfaces_field;
duke@1 837 } else {
duke@1 838 return List.nil();
duke@1 839 }
duke@1 840 }
duke@1 841
duke@1 842 public Type getSuperclass() {
duke@1 843 complete();
duke@1 844 if (type instanceof ClassType) {
duke@1 845 ClassType t = (ClassType)type;
duke@1 846 if (t.supertype_field == null) // FIXME: shouldn't be null
duke@1 847 t.supertype_field = Type.noType;
duke@1 848 // An interface has no superclass; its supertype is Object.
duke@1 849 return t.isInterface()
duke@1 850 ? Type.noType
jjg@904 851 : t.supertype_field.getModelType();
duke@1 852 } else {
duke@1 853 return Type.noType;
duke@1 854 }
duke@1 855 }
duke@1 856
duke@1 857 public ElementKind getKind() {
duke@1 858 long flags = flags();
duke@1 859 if ((flags & ANNOTATION) != 0)
duke@1 860 return ElementKind.ANNOTATION_TYPE;
duke@1 861 else if ((flags & INTERFACE) != 0)
duke@1 862 return ElementKind.INTERFACE;
duke@1 863 else if ((flags & ENUM) != 0)
duke@1 864 return ElementKind.ENUM;
duke@1 865 else
duke@1 866 return ElementKind.CLASS;
duke@1 867 }
duke@1 868
duke@1 869 public NestingKind getNestingKind() {
duke@1 870 complete();
duke@1 871 if (owner.kind == PCK)
duke@1 872 return NestingKind.TOP_LEVEL;
duke@1 873 else if (name.isEmpty())
duke@1 874 return NestingKind.ANONYMOUS;
duke@1 875 else if (owner.kind == MTH)
duke@1 876 return NestingKind.LOCAL;
duke@1 877 else
duke@1 878 return NestingKind.MEMBER;
duke@1 879 }
duke@1 880
duke@1 881 /**
duke@1 882 * @deprecated this method should never be used by javac internally.
duke@1 883 */
duke@1 884 @Override @Deprecated
duke@1 885 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
duke@1 886 return JavacElements.getAnnotation(this, annoType);
duke@1 887 }
duke@1 888
duke@1 889 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 890 return v.visitType(this, p);
duke@1 891 }
mcimadamore@121 892
mcimadamore@121 893 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 894 return v.visitClassSymbol(this, p);
mcimadamore@121 895 }
duke@1 896 }
duke@1 897
duke@1 898
duke@1 899 /** A class for variable symbols
duke@1 900 */
duke@1 901 public static class VarSymbol extends Symbol implements VariableElement {
duke@1 902
duke@1 903 /** The variable's declaration position.
duke@1 904 */
duke@1 905 public int pos = Position.NOPOS;
duke@1 906
duke@1 907 /** The variable's address. Used for different purposes during
duke@1 908 * flow analysis, translation and code generation.
duke@1 909 * Flow analysis:
duke@1 910 * If this is a blank final or local variable, its sequence number.
duke@1 911 * Translation:
duke@1 912 * If this is a private field, its access number.
duke@1 913 * Code generation:
duke@1 914 * If this is a local variable, its logical slot number.
duke@1 915 */
duke@1 916 public int adr = -1;
duke@1 917
duke@1 918 /** Construct a variable symbol, given its flags, name, type and owner.
duke@1 919 */
duke@1 920 public VarSymbol(long flags, Name name, Type type, Symbol owner) {
duke@1 921 super(VAR, flags, name, type, owner);
duke@1 922 }
duke@1 923
duke@1 924 /** Clone this symbol with new owner.
duke@1 925 */
duke@1 926 public VarSymbol clone(Symbol newOwner) {
duke@1 927 VarSymbol v = new VarSymbol(flags_field, name, type, newOwner);
duke@1 928 v.pos = pos;
duke@1 929 v.adr = adr;
duke@1 930 v.data = data;
duke@1 931 // System.out.println("clone " + v + " in " + newOwner);//DEBUG
duke@1 932 return v;
duke@1 933 }
duke@1 934
duke@1 935 public String toString() {
duke@1 936 return name.toString();
duke@1 937 }
duke@1 938
duke@1 939 public Symbol asMemberOf(Type site, Types types) {
duke@1 940 return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
duke@1 941 }
duke@1 942
duke@1 943 public ElementKind getKind() {
duke@1 944 long flags = flags();
duke@1 945 if ((flags & PARAMETER) != 0) {
duke@1 946 if (isExceptionParameter())
duke@1 947 return ElementKind.EXCEPTION_PARAMETER;
duke@1 948 else
duke@1 949 return ElementKind.PARAMETER;
duke@1 950 } else if ((flags & ENUM) != 0) {
duke@1 951 return ElementKind.ENUM_CONSTANT;
duke@1 952 } else if (owner.kind == TYP || owner.kind == ERR) {
duke@1 953 return ElementKind.FIELD;
sundar@697 954 } else if (isResourceVariable()) {
sundar@697 955 return ElementKind.RESOURCE_VARIABLE;
duke@1 956 } else {
duke@1 957 return ElementKind.LOCAL_VARIABLE;
duke@1 958 }
duke@1 959 }
duke@1 960
duke@1 961 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 962 return v.visitVariable(this, p);
duke@1 963 }
duke@1 964
duke@1 965 public Object getConstantValue() { // Mirror API
duke@1 966 return Constants.decode(getConstValue(), type);
duke@1 967 }
duke@1 968
duke@1 969 public void setLazyConstValue(final Env<AttrContext> env,
duke@1 970 final Attr attr,
duke@1 971 final JCTree.JCExpression initializer)
duke@1 972 {
duke@1 973 setData(new Callable<Object>() {
duke@1 974 public Object call() {
jjg@841 975 return attr.attribLazyConstantValue(env, initializer, type);
duke@1 976 }
duke@1 977 });
duke@1 978 }
duke@1 979
duke@1 980 /**
duke@1 981 * The variable's constant value, if this is a constant.
duke@1 982 * Before the constant value is evaluated, it points to an
duke@1 983 * initalizer environment. If this is not a constant, it can
duke@1 984 * be used for other stuff.
duke@1 985 */
duke@1 986 private Object data;
duke@1 987
duke@1 988 public boolean isExceptionParameter() {
duke@1 989 return data == ElementKind.EXCEPTION_PARAMETER;
duke@1 990 }
duke@1 991
darcy@609 992 public boolean isResourceVariable() {
darcy@609 993 return data == ElementKind.RESOURCE_VARIABLE;
darcy@609 994 }
darcy@609 995
duke@1 996 public Object getConstValue() {
duke@1 997 // TODO: Consider if getConstValue and getConstantValue can be collapsed
darcy@609 998 if (data == ElementKind.EXCEPTION_PARAMETER ||
darcy@609 999 data == ElementKind.RESOURCE_VARIABLE) {
duke@1 1000 return null;
duke@1 1001 } else if (data instanceof Callable<?>) {
darcy@609 1002 // In this case, this is a final variable, with an as
duke@1 1003 // yet unevaluated initializer.
duke@1 1004 Callable<?> eval = (Callable<?>)data;
duke@1 1005 data = null; // to make sure we don't evaluate this twice.
duke@1 1006 try {
duke@1 1007 data = eval.call();
duke@1 1008 } catch (Exception ex) {
jjg@841 1009 ex.printStackTrace();
duke@1 1010 throw new AssertionError(ex);
duke@1 1011 }
duke@1 1012 }
duke@1 1013 return data;
duke@1 1014 }
duke@1 1015
duke@1 1016 public void setData(Object data) {
jjg@816 1017 Assert.check(!(data instanceof Env<?>), this);
duke@1 1018 this.data = data;
duke@1 1019 }
mcimadamore@121 1020
mcimadamore@121 1021 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 1022 return v.visitVarSymbol(this, p);
mcimadamore@121 1023 }
duke@1 1024 }
duke@1 1025
duke@1 1026 /** A class for method symbols.
duke@1 1027 */
duke@1 1028 public static class MethodSymbol extends Symbol implements ExecutableElement {
duke@1 1029
duke@1 1030 /** The code of the method. */
duke@1 1031 public Code code = null;
duke@1 1032
duke@1 1033 /** The parameters of the method. */
duke@1 1034 public List<VarSymbol> params = null;
duke@1 1035
duke@1 1036 /** The names of the parameters */
duke@1 1037 public List<Name> savedParameterNames;
duke@1 1038
duke@1 1039 /** For an attribute field accessor, its default value if any.
duke@1 1040 * The value is null if none appeared in the method
duke@1 1041 * declaration.
duke@1 1042 */
duke@1 1043 public Attribute defaultValue = null;
duke@1 1044
duke@1 1045 /** Construct a method symbol, given its flags, name, type and owner.
duke@1 1046 */
duke@1 1047 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
duke@1 1048 super(MTH, flags, name, type, owner);
jjg@816 1049 if (owner.type.tag == TYPEVAR) Assert.error(owner + "." + name);
duke@1 1050 }
duke@1 1051
duke@1 1052 /** Clone this symbol with new owner.
duke@1 1053 */
duke@1 1054 public MethodSymbol clone(Symbol newOwner) {
duke@1 1055 MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner);
duke@1 1056 m.code = code;
duke@1 1057 return m;
duke@1 1058 }
duke@1 1059
duke@1 1060 /** The Java source which this symbol represents.
duke@1 1061 */
duke@1 1062 public String toString() {
duke@1 1063 if ((flags() & BLOCK) != 0) {
duke@1 1064 return owner.name.toString();
duke@1 1065 } else {
jjg@113 1066 String s = (name == name.table.names.init)
duke@1 1067 ? owner.name.toString()
duke@1 1068 : name.toString();
duke@1 1069 if (type != null) {
duke@1 1070 if (type.tag == FORALL)
duke@1 1071 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
duke@1 1072 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
duke@1 1073 }
duke@1 1074 return s;
duke@1 1075 }
duke@1 1076 }
duke@1 1077
duke@1 1078 /** find a symbol that this (proxy method) symbol implements.
duke@1 1079 * @param c The class whose members are searched for
duke@1 1080 * implementations
duke@1 1081 */
duke@1 1082 public Symbol implemented(TypeSymbol c, Types types) {
duke@1 1083 Symbol impl = null;
duke@1 1084 for (List<Type> is = types.interfaces(c.type);
duke@1 1085 impl == null && is.nonEmpty();
duke@1 1086 is = is.tail) {
duke@1 1087 TypeSymbol i = is.head.tsym;
mcimadamore@780 1088 impl = implementedIn(i, types);
mcimadamore@780 1089 if (impl == null)
mcimadamore@780 1090 impl = implemented(i, types);
mcimadamore@780 1091 }
mcimadamore@780 1092 return impl;
mcimadamore@780 1093 }
mcimadamore@780 1094
mcimadamore@780 1095 public Symbol implementedIn(TypeSymbol c, Types types) {
mcimadamore@780 1096 Symbol impl = null;
mcimadamore@780 1097 for (Scope.Entry e = c.members().lookup(name);
mcimadamore@780 1098 impl == null && e.scope != null;
mcimadamore@780 1099 e = e.next()) {
mcimadamore@780 1100 if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
mcimadamore@780 1101 // FIXME: I suspect the following requires a
mcimadamore@780 1102 // subst() for a parametric return type.
mcimadamore@780 1103 types.isSameType(type.getReturnType(),
mcimadamore@780 1104 types.memberType(owner.type, e.sym).getReturnType())) {
mcimadamore@780 1105 impl = e.sym;
duke@1 1106 }
duke@1 1107 }
duke@1 1108 return impl;
duke@1 1109 }
duke@1 1110
duke@1 1111 /** Will the erasure of this method be considered by the VM to
duke@1 1112 * override the erasure of the other when seen from class `origin'?
duke@1 1113 */
duke@1 1114 public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
duke@1 1115 if (isConstructor() || _other.kind != MTH) return false;
duke@1 1116
duke@1 1117 if (this == _other) return true;
duke@1 1118 MethodSymbol other = (MethodSymbol)_other;
duke@1 1119
duke@1 1120 // check for a direct implementation
duke@1 1121 if (other.isOverridableIn((TypeSymbol)owner) &&
duke@1 1122 types.asSuper(owner.type, other.owner) != null &&
duke@1 1123 types.isSameType(erasure(types), other.erasure(types)))
duke@1 1124 return true;
duke@1 1125
duke@1 1126 // check for an inherited implementation
duke@1 1127 return
duke@1 1128 (flags() & ABSTRACT) == 0 &&
duke@1 1129 other.isOverridableIn(origin) &&
duke@1 1130 this.isMemberOf(origin, types) &&
duke@1 1131 types.isSameType(erasure(types), other.erasure(types));
duke@1 1132 }
duke@1 1133
duke@1 1134 /** The implementation of this (abstract) symbol in class origin,
duke@1 1135 * from the VM's point of view, null if method does not have an
duke@1 1136 * implementation in class.
duke@1 1137 * @param origin The class of which the implementation is a member.
duke@1 1138 */
duke@1 1139 public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
duke@1 1140 for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
duke@1 1141 for (Scope.Entry e = c.members().lookup(name);
duke@1 1142 e.scope != null;
duke@1 1143 e = e.next()) {
duke@1 1144 if (e.sym.kind == MTH &&
duke@1 1145 ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
duke@1 1146 return (MethodSymbol)e.sym;
duke@1 1147 }
duke@1 1148 }
duke@1 1149 return null;
duke@1 1150 }
duke@1 1151
duke@1 1152 /** Does this symbol override `other' symbol, when both are seen as
duke@1 1153 * members of class `origin'? It is assumed that _other is a member
duke@1 1154 * of origin.
duke@1 1155 *
duke@1 1156 * It is assumed that both symbols have the same name. The static
duke@1 1157 * modifier is ignored for this test.
duke@1 1158 *
duke@1 1159 * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
duke@1 1160 */
duke@1 1161 public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
duke@1 1162 if (isConstructor() || _other.kind != MTH) return false;
duke@1 1163
duke@1 1164 if (this == _other) return true;
duke@1 1165 MethodSymbol other = (MethodSymbol)_other;
duke@1 1166
duke@1 1167 // check for a direct implementation
duke@1 1168 if (other.isOverridableIn((TypeSymbol)owner) &&
duke@1 1169 types.asSuper(owner.type, other.owner) != null) {
duke@1 1170 Type mt = types.memberType(owner.type, this);
duke@1 1171 Type ot = types.memberType(owner.type, other);
duke@1 1172 if (types.isSubSignature(mt, ot)) {
duke@1 1173 if (!checkResult)
duke@1 1174 return true;
duke@1 1175 if (types.returnTypeSubstitutable(mt, ot))
duke@1 1176 return true;
duke@1 1177 }
duke@1 1178 }
duke@1 1179
duke@1 1180 // check for an inherited implementation
duke@1 1181 if ((flags() & ABSTRACT) != 0 ||
duke@1 1182 (other.flags() & ABSTRACT) == 0 ||
duke@1 1183 !other.isOverridableIn(origin) ||
duke@1 1184 !this.isMemberOf(origin, types))
duke@1 1185 return false;
duke@1 1186
duke@1 1187 // assert types.asSuper(origin.type, other.owner) != null;
duke@1 1188 Type mt = types.memberType(origin.type, this);
duke@1 1189 Type ot = types.memberType(origin.type, other);
duke@1 1190 return
duke@1 1191 types.isSubSignature(mt, ot) &&
duke@1 1192 (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings));
duke@1 1193 }
duke@1 1194
duke@1 1195 private boolean isOverridableIn(TypeSymbol origin) {
jjh@972 1196 // JLS 8.4.6.1
duke@1 1197 switch ((int)(flags_field & Flags.AccessFlags)) {
duke@1 1198 case Flags.PRIVATE:
duke@1 1199 return false;
duke@1 1200 case Flags.PUBLIC:
duke@1 1201 return true;
duke@1 1202 case Flags.PROTECTED:
duke@1 1203 return (origin.flags() & INTERFACE) == 0;
duke@1 1204 case 0:
duke@1 1205 // for package private: can only override in the same
duke@1 1206 // package
duke@1 1207 return
duke@1 1208 this.packge() == origin.packge() &&
duke@1 1209 (origin.flags() & INTERFACE) == 0;
duke@1 1210 default:
duke@1 1211 return false;
duke@1 1212 }
duke@1 1213 }
duke@1 1214
duke@1 1215 /** The implementation of this (abstract) symbol in class origin;
duke@1 1216 * null if none exists. Synthetic methods are not considered
duke@1 1217 * as possible implementations.
duke@1 1218 */
duke@1 1219 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@673 1220 return implementation(origin, types, checkResult, implementation_filter);
mcimadamore@673 1221 }
mcimadamore@673 1222 // where
mcimadamore@673 1223 private static final Filter<Symbol> implementation_filter = new Filter<Symbol>() {
mcimadamore@673 1224 public boolean accepts(Symbol s) {
mcimadamore@673 1225 return s.kind == Kinds.MTH &&
mcimadamore@673 1226 (s.flags() & SYNTHETIC) == 0;
mcimadamore@673 1227 }
mcimadamore@673 1228 };
mcimadamore@673 1229
mcimadamore@673 1230 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
mcimadamore@858 1231 MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
mcimadamore@341 1232 if (res != null)
mcimadamore@341 1233 return res;
duke@1 1234 // if origin is derived from a raw type, we might have missed
duke@1 1235 // an implementation because we do not know enough about instantiations.
duke@1 1236 // in this case continue with the supertype as origin.
duke@1 1237 if (types.isDerivedRaw(origin.type))
duke@1 1238 return implementation(types.supertype(origin.type).tsym, types, checkResult);
duke@1 1239 else
duke@1 1240 return null;
duke@1 1241 }
duke@1 1242
duke@1 1243 public List<VarSymbol> params() {
duke@1 1244 owner.complete();
duke@1 1245 if (params == null) {
jjg@428 1246 // If ClassReader.saveParameterNames has been set true, then
jjg@428 1247 // savedParameterNames will be set to a list of names that
jjg@428 1248 // matches the types in type.getParameterTypes(). If any names
jjg@428 1249 // were not found in the class file, those names in the list will
jjg@428 1250 // be set to the empty name.
jjg@428 1251 // If ClassReader.saveParameterNames has been set false, then
jjg@428 1252 // savedParameterNames will be null.
jjg@428 1253 List<Name> paramNames = savedParameterNames;
duke@1 1254 savedParameterNames = null;
jjg@428 1255 // discard the provided names if the list of names is the wrong size.
jjg@428 1256 if (paramNames == null || paramNames.size() != type.getParameterTypes().size())
jjg@428 1257 paramNames = List.nil();
duke@1 1258 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
jjg@428 1259 List<Name> remaining = paramNames;
jjg@428 1260 // assert: remaining and paramNames are both empty or both
jjg@428 1261 // have same cardinality as type.getParameterTypes()
jjg@428 1262 int i = 0;
duke@1 1263 for (Type t : type.getParameterTypes()) {
jjg@428 1264 Name paramName;
jjg@428 1265 if (remaining.isEmpty()) {
jjg@428 1266 // no names for any parameters available
jjg@428 1267 paramName = createArgName(i, paramNames);
jjg@428 1268 } else {
jjg@428 1269 paramName = remaining.head;
jjg@428 1270 remaining = remaining.tail;
jjg@428 1271 if (paramName.isEmpty()) {
jjg@428 1272 // no name for this specific parameter
jjg@428 1273 paramName = createArgName(i, paramNames);
jjg@428 1274 }
jjg@428 1275 }
jjg@428 1276 buf.append(new VarSymbol(PARAMETER, paramName, t, this));
jjg@428 1277 i++;
duke@1 1278 }
duke@1 1279 params = buf.toList();
duke@1 1280 }
duke@1 1281 return params;
duke@1 1282 }
duke@1 1283
jjg@428 1284 // Create a name for the argument at position 'index' that is not in
jjg@428 1285 // the exclude list. In normal use, either no names will have been
jjg@428 1286 // provided, in which case the exclude list is empty, or all the names
jjg@428 1287 // will have been provided, in which case this method will not be called.
jjg@428 1288 private Name createArgName(int index, List<Name> exclude) {
jjg@428 1289 String prefix = "arg";
jjg@428 1290 while (true) {
jjg@428 1291 Name argName = name.table.fromString(prefix + index);
jjg@428 1292 if (!exclude.contains(argName))
jjg@428 1293 return argName;
jjg@428 1294 prefix += "$";
jjg@428 1295 }
jjg@428 1296 }
jjg@428 1297
duke@1 1298 public Symbol asMemberOf(Type site, Types types) {
duke@1 1299 return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
duke@1 1300 }
duke@1 1301
duke@1 1302 public ElementKind getKind() {
jjg@113 1303 if (name == name.table.names.init)
duke@1 1304 return ElementKind.CONSTRUCTOR;
jjg@113 1305 else if (name == name.table.names.clinit)
duke@1 1306 return ElementKind.STATIC_INIT;
duke@1 1307 else
duke@1 1308 return ElementKind.METHOD;
duke@1 1309 }
duke@1 1310
duke@1 1311 public Attribute getDefaultValue() {
duke@1 1312 return defaultValue;
duke@1 1313 }
duke@1 1314
duke@1 1315 public List<VarSymbol> getParameters() {
duke@1 1316 return params();
duke@1 1317 }
duke@1 1318
duke@1 1319 public boolean isVarArgs() {
duke@1 1320 return (flags() & VARARGS) != 0;
duke@1 1321 }
duke@1 1322
duke@1 1323 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 1324 return v.visitExecutable(this, p);
duke@1 1325 }
duke@1 1326
mcimadamore@121 1327 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 1328 return v.visitMethodSymbol(this, p);
mcimadamore@121 1329 }
mcimadamore@121 1330
duke@1 1331 public Type getReturnType() {
duke@1 1332 return asType().getReturnType();
duke@1 1333 }
duke@1 1334
duke@1 1335 public List<Type> getThrownTypes() {
duke@1 1336 return asType().getThrownTypes();
duke@1 1337 }
duke@1 1338 }
duke@1 1339
duke@1 1340 /** A class for predefined operators.
duke@1 1341 */
duke@1 1342 public static class OperatorSymbol extends MethodSymbol {
duke@1 1343
duke@1 1344 public int opcode;
duke@1 1345
duke@1 1346 public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
duke@1 1347 super(PUBLIC | STATIC, name, type, owner);
duke@1 1348 this.opcode = opcode;
duke@1 1349 }
mcimadamore@121 1350
mcimadamore@121 1351 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
mcimadamore@121 1352 return v.visitOperatorSymbol(this, p);
mcimadamore@121 1353 }
duke@1 1354 }
duke@1 1355
duke@1 1356 /** Symbol completer interface.
duke@1 1357 */
duke@1 1358 public static interface Completer {
duke@1 1359 void complete(Symbol sym) throws CompletionFailure;
duke@1 1360 }
duke@1 1361
duke@1 1362 public static class CompletionFailure extends RuntimeException {
duke@1 1363 private static final long serialVersionUID = 0;
duke@1 1364 public Symbol sym;
duke@1 1365
jjg@12 1366 /** A diagnostic object describing the failure
jjg@12 1367 */
jjg@12 1368 public JCDiagnostic diag;
jjg@12 1369
duke@1 1370 /** A localized string describing the failure.
jjg@12 1371 * @deprecated Use {@code getDetail()} or {@code getMessage()}
duke@1 1372 */
jjg@12 1373 @Deprecated
duke@1 1374 public String errmsg;
duke@1 1375
duke@1 1376 public CompletionFailure(Symbol sym, String errmsg) {
duke@1 1377 this.sym = sym;
duke@1 1378 this.errmsg = errmsg;
duke@1 1379 // this.printStackTrace();//DEBUG
duke@1 1380 }
duke@1 1381
jjg@12 1382 public CompletionFailure(Symbol sym, JCDiagnostic diag) {
jjg@12 1383 this.sym = sym;
jjg@12 1384 this.diag = diag;
jjg@12 1385 // this.printStackTrace();//DEBUG
jjg@12 1386 }
jjg@12 1387
jjg@12 1388 public JCDiagnostic getDiagnostic() {
jjg@12 1389 return diag;
jjg@12 1390 }
jjg@12 1391
jjg@12 1392 @Override
duke@1 1393 public String getMessage() {
jjg@12 1394 if (diag != null)
jjg@12 1395 return diag.getMessage(null);
jjg@12 1396 else
jjg@12 1397 return errmsg;
jjg@12 1398 }
jjg@12 1399
jjg@12 1400 public Object getDetailValue() {
jjg@12 1401 return (diag != null ? diag : errmsg);
duke@1 1402 }
duke@1 1403
duke@1 1404 @Override
duke@1 1405 public CompletionFailure initCause(Throwable cause) {
duke@1 1406 super.initCause(cause);
duke@1 1407 return this;
duke@1 1408 }
duke@1 1409
duke@1 1410 }
mcimadamore@121 1411
mcimadamore@121 1412 /**
mcimadamore@121 1413 * A visitor for symbols. A visitor is used to implement operations
mcimadamore@121 1414 * (or relations) on symbols. Most common operations on types are
mcimadamore@121 1415 * binary relations and this interface is designed for binary
mcimadamore@121 1416 * relations, that is, operations on the form
mcimadamore@121 1417 * Symbol&nbsp;&times;&nbsp;P&nbsp;&rarr;&nbsp;R.
mcimadamore@121 1418 * <!-- In plain text: Type x P -> R -->
mcimadamore@121 1419 *
mcimadamore@121 1420 * @param <R> the return type of the operation implemented by this
mcimadamore@121 1421 * visitor; use Void if no return type is needed.
mcimadamore@121 1422 * @param <P> the type of the second argument (the first being the
mcimadamore@121 1423 * symbol itself) of the operation implemented by this visitor; use
mcimadamore@121 1424 * Void if a second argument is not needed.
mcimadamore@121 1425 */
mcimadamore@121 1426 public interface Visitor<R,P> {
mcimadamore@121 1427 R visitClassSymbol(ClassSymbol s, P arg);
mcimadamore@121 1428 R visitMethodSymbol(MethodSymbol s, P arg);
mcimadamore@121 1429 R visitPackageSymbol(PackageSymbol s, P arg);
mcimadamore@121 1430 R visitOperatorSymbol(OperatorSymbol s, P arg);
mcimadamore@121 1431 R visitVarSymbol(VarSymbol s, P arg);
mcimadamore@121 1432 R visitTypeSymbol(TypeSymbol s, P arg);
mcimadamore@121 1433 R visitSymbol(Symbol s, P arg);
mcimadamore@121 1434 }
duke@1 1435 }

mercurial