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

Mon, 07 Feb 2011 18:10:13 +0000

author
mcimadamore
date
Mon, 07 Feb 2011 18:10:13 +0000
changeset 858
96d4226bdd60
parent 841
df371fd16386
child 877
351027202f60
permissions
-rw-r--r--

7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
Summary: override clash algorithm is not implemented correctly
Reviewed-by: jjg

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

mercurial