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

Tue, 13 Jan 2009 13:28:20 +0000

author
mcimadamore
date
Tue, 13 Jan 2009 13:28:20 +0000
changeset 185
d57378c34fdb
parent 155
4d2d8b6459e1
child 308
03944ee4fac4
permissions
-rw-r--r--

6665356: Cast not allowed when both qualifying type and inner class are parameterized
Summary: Fixed parser and cats conversion in order to allow cast between generic inner classes
Reviewed-by: jjg

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

mercurial