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

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2717
11743872bfc9
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

merge

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

mercurial