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

Tue, 18 Mar 2014 22:12:46 +0000

author
vromero
date
Tue, 18 Mar 2014 22:12:46 +0000
changeset 2434
7de1481c6cd8
parent 2427
a3ad6e2ede44
child 2525
2eb010b6cb22
child 2717
11743872bfc9
permissions
-rw-r--r--

8036007: javac crashes when encountering an unresolvable interface
Reviewed-by: vromero, jlahoda
Contributed-by: paul.govereau@oracle.com

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

mercurial