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