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

Mon, 14 Jan 2013 19:52:36 +0100

author
jfranck
date
Mon, 14 Jan 2013 19:52:36 +0100
changeset 1491
9f42a06a49c0
parent 1473
31780dd06ec7
child 1513
cf84b07a82db
permissions
-rw-r--r--

7193719: Support repeating annotations in javax.lang.model
Reviewed-by: jjg

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

mercurial