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

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 537
9d9d08922405
child 581
f2fdd52e4e87
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1999, 2008, 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 API supported by Sun Microsystems.  If
    53  *  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         assert attributes_field != null;
    85         return attributes_field;
    86     }
    88     /** Fetch a particular annotation from a symbol. */
    89     public Attribute.Compound attribute(Symbol anno) {
    90         for (Attribute.Compound a : getAnnotationMirrors())
    91             if (a.type.tsym == anno) return a;
    92         return null;
    93     }
    95     /** The name of this symbol in Utf8 representation.
    96      */
    97     public Name name;
    99     /** The type of this symbol.
   100      */
   101     public Type type;
   103     /** The type annotations targeted to a tree directly owned by this symbol
   104      */
   105     // type annotations are stored here for two purposes:
   106     //  - convenient location to store annotations for generation after erasure
   107     //  - a private interface for accessing type annotations parsed from
   108     //    classfiles
   109     //  the field is populated for the following declaration only
   110     //  class, field, variable and type parameters
   111     //
   112     public List<Attribute.TypeCompound> typeAnnotations;
   114     /** The owner of this symbol.
   115      */
   116     public Symbol owner;
   118     /** The completer of this symbol.
   119      */
   120     public Completer completer;
   122     /** A cache for the type erasure of this symbol.
   123      */
   124     public Type erasure_field;
   126     /** Construct a symbol with given kind, flags, name, type and owner.
   127      */
   128     public Symbol(int kind, long flags, Name name, Type type, Symbol owner) {
   129         this.kind = kind;
   130         this.flags_field = flags;
   131         this.type = type;
   132         this.owner = owner;
   133         this.completer = null;
   134         this.erasure_field = null;
   135         this.attributes_field = List.nil();
   136         this.typeAnnotations = List.nil();
   137         this.name = name;
   138     }
   140     /** Clone this symbol with new owner.
   141      *  Legal only for fields and methods.
   142      */
   143     public Symbol clone(Symbol newOwner) {
   144         throw new AssertionError();
   145     }
   147     public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
   148         return v.visitSymbol(this, p);
   149     }
   151     /** The Java source which this symbol represents.
   152      *  A description of this symbol; overrides Object.
   153      */
   154     public String toString() {
   155         return name.toString();
   156     }
   158     /** A Java source description of the location of this symbol; used for
   159      *  error reporting.
   160      *
   161      * @return null if the symbol is a package or a toplevel class defined in
   162      * the default package; otherwise, the owner symbol is returned
   163      */
   164     public Symbol location() {
   165         if (owner.name == null || (owner.name.isEmpty() && owner.kind != PCK && owner.kind != TYP)) {
   166             return null;
   167         }
   168         return owner;
   169     }
   171     public Symbol location(Type site, Types types) {
   172         if (owner.name == null || owner.name.isEmpty()) {
   173             return location();
   174         }
   175         if (owner.type.tag == CLASS) {
   176             Type ownertype = types.asOuterSuper(site, owner);
   177             if (ownertype != null) return ownertype.tsym;
   178         }
   179         return owner;
   180     }
   182     /** The symbol's erased type.
   183      */
   184     public Type erasure(Types types) {
   185         if (erasure_field == null)
   186             erasure_field = types.erasure(type);
   187         return erasure_field;
   188     }
   190     /** The external type of a symbol. This is the symbol's erased type
   191      *  except for constructors of inner classes which get the enclosing
   192      *  instance class added as first argument.
   193      */
   194     public Type externalType(Types types) {
   195         Type t = erasure(types);
   196         if (name == name.table.names.init && owner.hasOuterInstance()) {
   197             Type outerThisType = types.erasure(owner.type.getEnclosingType());
   198             return new MethodType(t.getParameterTypes().prepend(outerThisType),
   199                                   t.getReturnType(),
   200                                   t.getThrownTypes(),
   201                                   t.tsym);
   202         } else {
   203             return t;
   204         }
   205     }
   207     public boolean isStatic() {
   208         return
   209             (flags() & STATIC) != 0 ||
   210             (owner.flags() & INTERFACE) != 0 && kind != MTH;
   211     }
   213     public boolean isInterface() {
   214         return (flags() & INTERFACE) != 0;
   215     }
   217     /** Is this symbol declared (directly or indirectly) local
   218      *  to a method or variable initializer?
   219      *  Also includes fields of inner classes which are in
   220      *  turn local to a method or variable initializer.
   221      */
   222     public boolean isLocal() {
   223         return
   224             (owner.kind & (VAR | MTH)) != 0 ||
   225             (owner.kind == TYP && owner.isLocal());
   226     }
   228     /** Has this symbol an empty name? This includes anonymous
   229      *  inner classses.
   230      */
   231     public boolean isAnonymous() {
   232         return name.isEmpty();
   233     }
   235     /** Is this symbol a constructor?
   236      */
   237     public boolean isConstructor() {
   238         return name == name.table.names.init;
   239     }
   241     /** The fully qualified name of this symbol.
   242      *  This is the same as the symbol's name except for class symbols,
   243      *  which are handled separately.
   244      */
   245     public Name getQualifiedName() {
   246         return name;
   247     }
   249     /** The fully qualified name of this symbol after converting to flat
   250      *  representation. This is the same as the symbol's name except for
   251      *  class symbols, which are handled separately.
   252      */
   253     public Name flatName() {
   254         return getQualifiedName();
   255     }
   257     /** If this is a class or package, its members, otherwise null.
   258      */
   259     public Scope members() {
   260         return null;
   261     }
   263     /** A class is an inner class if it it has an enclosing instance class.
   264      */
   265     public boolean isInner() {
   266         return type.getEnclosingType().tag == CLASS;
   267     }
   269     /** An inner class has an outer instance if it is not an interface
   270      *  it has an enclosing instance class which might be referenced from the class.
   271      *  Nested classes can see instance members of their enclosing class.
   272      *  Their constructors carry an additional this$n parameter, inserted
   273      *  implicitly by the compiler.
   274      *
   275      *  @see #isInner
   276      */
   277     public boolean hasOuterInstance() {
   278         return
   279             type.getEnclosingType().tag == CLASS && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
   280     }
   282     /** The closest enclosing class of this symbol's declaration.
   283      */
   284     public ClassSymbol enclClass() {
   285         Symbol c = this;
   286         while (c != null &&
   287                ((c.kind & TYP) == 0 || c.type.tag != CLASS)) {
   288             c = c.owner;
   289         }
   290         return (ClassSymbol)c;
   291     }
   293     /** The outermost class which indirectly owns this symbol.
   294      */
   295     public ClassSymbol outermostClass() {
   296         Symbol sym = this;
   297         Symbol prev = null;
   298         while (sym.kind != PCK) {
   299             prev = sym;
   300             sym = sym.owner;
   301         }
   302         return (ClassSymbol) prev;
   303     }
   305     /** The package which indirectly owns this symbol.
   306      */
   307     public PackageSymbol packge() {
   308         Symbol sym = this;
   309         while (sym.kind != PCK) {
   310             sym = sym.owner;
   311         }
   312         return (PackageSymbol) sym;
   313     }
   315     /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
   316      */
   317     public boolean isSubClass(Symbol base, Types types) {
   318         throw new AssertionError("isSubClass " + this);
   319     }
   321     /** Fully check membership: hierarchy, protection, and hiding.
   322      *  Does not exclude methods not inherited due to overriding.
   323      */
   324     public boolean isMemberOf(TypeSymbol clazz, Types types) {
   325         return
   326             owner == clazz ||
   327             clazz.isSubClass(owner, types) &&
   328             isInheritedIn(clazz, types) &&
   329             !hiddenIn((ClassSymbol)clazz, types);
   330     }
   332     /** Is this symbol the same as or enclosed by the given class? */
   333     public boolean isEnclosedBy(ClassSymbol clazz) {
   334         for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
   335             if (sym == clazz) return true;
   336         return false;
   337     }
   339     /** Check for hiding.  Note that this doesn't handle multiple
   340      *  (interface) inheritance. */
   341     private boolean hiddenIn(ClassSymbol clazz, Types types) {
   342         if (kind == MTH && (flags() & STATIC) == 0) return false;
   343         while (true) {
   344             if (owner == clazz) return false;
   345             Scope.Entry e = clazz.members().lookup(name);
   346             while (e.scope != null) {
   347                 if (e.sym == this) return false;
   348                 if (e.sym.kind == kind &&
   349                     (kind != MTH ||
   350                      (e.sym.flags() & STATIC) != 0 &&
   351                      types.isSubSignature(e.sym.type, type)))
   352                     return true;
   353                 e = e.next();
   354             }
   355             Type superType = types.supertype(clazz.type);
   356             if (superType.tag != TypeTags.CLASS) return false;
   357             clazz = (ClassSymbol)superType.tsym;
   358         }
   359     }
   361     /** Is this symbol inherited into a given class?
   362      *  PRE: If symbol's owner is a interface,
   363      *       it is already assumed that the interface is a superinterface
   364      *       of given class.
   365      *  @param clazz  The class for which we want to establish membership.
   366      *                This must be a subclass of the member's owner.
   367      */
   368     public boolean isInheritedIn(Symbol clazz, Types types) {
   369         switch ((int)(flags_field & Flags.AccessFlags)) {
   370         default: // error recovery
   371         case PUBLIC:
   372             return true;
   373         case PRIVATE:
   374             return this.owner == clazz;
   375         case PROTECTED:
   376             // we model interfaces as extending Object
   377             return (clazz.flags() & INTERFACE) == 0;
   378         case 0:
   379             PackageSymbol thisPackage = this.packge();
   380             for (Symbol sup = clazz;
   381                  sup != null && sup != this.owner;
   382                  sup = types.supertype(sup.type).tsym) {
   383                 while (sup.type.tag == TYPEVAR)
   384                     sup = sup.type.getUpperBound().tsym;
   385                 if (sup.type.isErroneous())
   386                     return true; // error recovery
   387                 if ((sup.flags() & COMPOUND) != 0)
   388                     continue;
   389                 if (sup.packge() != thisPackage)
   390                     return false;
   391             }
   392             return (clazz.flags() & INTERFACE) == 0;
   393         }
   394     }
   396     /** The (variable or method) symbol seen as a member of given
   397      *  class type`site' (this might change the symbol's type).
   398      *  This is used exclusively for producing diagnostics.
   399      */
   400     public Symbol asMemberOf(Type site, Types types) {
   401         throw new AssertionError();
   402     }
   404     /** Does this method symbol override `other' symbol, when both are seen as
   405      *  members of class `origin'?  It is assumed that _other is a member
   406      *  of origin.
   407      *
   408      *  It is assumed that both symbols have the same name.  The static
   409      *  modifier is ignored for this test.
   410      *
   411      *  See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
   412      */
   413     public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
   414         return false;
   415     }
   417     /** Complete the elaboration of this symbol's definition.
   418      */
   419     public void complete() throws CompletionFailure {
   420         if (completer != null) {
   421             Completer c = completer;
   422             completer = null;
   423             c.complete(this);
   424         }
   425     }
   427     /** True if the symbol represents an entity that exists.
   428      */
   429     public boolean exists() {
   430         return true;
   431     }
   433     public Type asType() {
   434         return type;
   435     }
   437     public Symbol getEnclosingElement() {
   438         return owner;
   439     }
   441     public ElementKind getKind() {
   442         return ElementKind.OTHER;       // most unkind
   443     }
   445     public Set<Modifier> getModifiers() {
   446         return Flags.asModifierSet(flags());
   447     }
   449     public Name getSimpleName() {
   450         return name;
   451     }
   453     /**
   454      * @deprecated this method should never be used by javac internally.
   455      */
   456     @Deprecated
   457     public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
   458         return JavacElements.getAnnotation(this, annoType);
   459     }
   461     // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
   462     public java.util.List<Symbol> getEnclosedElements() {
   463         return List.nil();
   464     }
   466     public List<TypeSymbol> getTypeParameters() {
   467         ListBuffer<TypeSymbol> l = ListBuffer.lb();
   468         for (Type t : type.getTypeArguments()) {
   469             l.append(t.tsym);
   470         }
   471         return l.toList();
   472     }
   474     public static class DelegatedSymbol extends Symbol {
   475         protected Symbol other;
   476         public DelegatedSymbol(Symbol other) {
   477             super(other.kind, other.flags_field, other.name, other.type, other.owner);
   478             this.other = other;
   479         }
   480         public String toString() { return other.toString(); }
   481         public Symbol location() { return other.location(); }
   482         public Symbol location(Type site, Types types) { return other.location(site, types); }
   483         public Type erasure(Types types) { return other.erasure(types); }
   484         public Type externalType(Types types) { return other.externalType(types); }
   485         public boolean isLocal() { return other.isLocal(); }
   486         public boolean isConstructor() { return other.isConstructor(); }
   487         public Name getQualifiedName() { return other.getQualifiedName(); }
   488         public Name flatName() { return other.flatName(); }
   489         public Scope members() { return other.members(); }
   490         public boolean isInner() { return other.isInner(); }
   491         public boolean hasOuterInstance() { return other.hasOuterInstance(); }
   492         public ClassSymbol enclClass() { return other.enclClass(); }
   493         public ClassSymbol outermostClass() { return other.outermostClass(); }
   494         public PackageSymbol packge() { return other.packge(); }
   495         public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
   496         public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
   497         public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
   498         public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
   499         public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
   500         public void complete() throws CompletionFailure { other.complete(); }
   502         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
   503             return other.accept(v, p);
   504         }
   506         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
   507             return v.visitSymbol(other, p);
   508         }
   509     }
   511     /** A class for type symbols. Type variables are represented by instances
   512      *  of this class, classes and packages by instances of subclasses.
   513      */
   514     public static class TypeSymbol
   515             extends Symbol implements TypeParameterElement {
   516         // Implements TypeParameterElement because type parameters don't
   517         // have their own TypeSymbol subclass.
   518         // TODO: type parameters should have their own TypeSymbol subclass
   520         public TypeSymbol(long flags, Name name, Type type, Symbol owner) {
   521             super(TYP, flags, name, type, owner);
   522         }
   524         /** form a fully qualified name from a name and an owner
   525          */
   526         static public Name formFullName(Name name, Symbol owner) {
   527             if (owner == null) return name;
   528             if (((owner.kind != ERR)) &&
   529                 ((owner.kind & (VAR | MTH)) != 0
   530                  || (owner.kind == TYP && owner.type.tag == TYPEVAR)
   531                  )) return name;
   532             Name prefix = owner.getQualifiedName();
   533             if (prefix == null || prefix == prefix.table.names.empty)
   534                 return name;
   535             else return prefix.append('.', name);
   536         }
   538         /** form a fully qualified name from a name and an owner, after
   539          *  converting to flat representation
   540          */
   541         static public Name formFlatName(Name name, Symbol owner) {
   542             if (owner == null ||
   543                 (owner.kind & (VAR | MTH)) != 0
   544                 || (owner.kind == TYP && owner.type.tag == TYPEVAR)
   545                 ) return name;
   546             char sep = owner.kind == TYP ? '$' : '.';
   547             Name prefix = owner.flatName();
   548             if (prefix == null || prefix == prefix.table.names.empty)
   549                 return name;
   550             else return prefix.append(sep, name);
   551         }
   553         /**
   554          * A total ordering between type symbols that refines the
   555          * class inheritance graph.
   556          *
   557          * Typevariables always precede other kinds of symbols.
   558          */
   559         public final boolean precedes(TypeSymbol that, Types types) {
   560             if (this == that)
   561                 return false;
   562             if (this.type.tag == that.type.tag) {
   563                 if (this.type.tag == CLASS) {
   564                     return
   565                         types.rank(that.type) < types.rank(this.type) ||
   566                         types.rank(that.type) == types.rank(this.type) &&
   567                         that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
   568                 } else if (this.type.tag == TYPEVAR) {
   569                     return types.isSubtype(this.type, that.type);
   570                 }
   571             }
   572             return this.type.tag == TYPEVAR;
   573         }
   575         // For type params; overridden in subclasses.
   576         public ElementKind getKind() {
   577             return ElementKind.TYPE_PARAMETER;
   578         }
   580         public java.util.List<Symbol> getEnclosedElements() {
   581             List<Symbol> list = List.nil();
   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 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             assert attributes_field != null;
   673             return attributes_field;
   674         }
   676         /** A package "exists" if a type or package that exists has
   677          *  been seen within it.
   678          */
   679         public boolean exists() {
   680             return (flags_field & EXISTS) != 0;
   681         }
   683         public ElementKind getKind() {
   684             return ElementKind.PACKAGE;
   685         }
   687         public Symbol getEnclosingElement() {
   688             return null;
   689         }
   691         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
   692             return v.visitPackage(this, p);
   693         }
   695         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
   696             return v.visitPackageSymbol(this, p);
   697         }
   698     }
   700     /** A class for class symbols
   701      */
   702     public static class ClassSymbol extends TypeSymbol implements TypeElement {
   704         /** a scope for all class members; variables, methods and inner classes
   705          *  type parameters are not part of this scope
   706          */
   707         public Scope members_field;
   709         /** the fully qualified name of the class, i.e. pck.outer.inner.
   710          *  null for anonymous classes
   711          */
   712         public Name fullname;
   714         /** the fully qualified name of the class after converting to flat
   715          *  representation, i.e. pck.outer$inner,
   716          *  set externally for local and anonymous classes
   717          */
   718         public Name flatname;
   720         /** the sourcefile where the class came from
   721          */
   722         public JavaFileObject sourcefile;
   724         /** the classfile from where to load this class
   725          *  this will have extension .class or .java
   726          */
   727         public JavaFileObject classfile;
   729         /** the constant pool of the class
   730          */
   731         public Pool pool;
   733         public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
   734             super(flags, name, type, owner);
   735             this.members_field = null;
   736             this.fullname = formFullName(name, owner);
   737             this.flatname = formFlatName(name, owner);
   738             this.sourcefile = null;
   739             this.classfile = null;
   740             this.pool = null;
   741         }
   743         public ClassSymbol(long flags, Name name, Symbol owner) {
   744             this(
   745                 flags,
   746                 name,
   747                 new ClassType(Type.noType, null, null),
   748                 owner);
   749             this.type.tsym = this;
   750         }
   752         /** The Java source which this symbol represents.
   753          */
   754         public String toString() {
   755             return className();
   756         }
   758         public long flags() {
   759             if (completer != null) complete();
   760             return flags_field;
   761         }
   763         public Scope members() {
   764             if (completer != null) complete();
   765             return members_field;
   766         }
   768         public List<Attribute.Compound> getAnnotationMirrors() {
   769             if (completer != null) complete();
   770             assert attributes_field != null;
   771             return attributes_field;
   772         }
   774         public Type erasure(Types types) {
   775             if (erasure_field == null)
   776                 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
   777                                               List.<Type>nil(), this);
   778             return erasure_field;
   779         }
   781         public String className() {
   782             if (name.isEmpty())
   783                 return
   784                     Log.getLocalizedString("anonymous.class", flatname);
   785             else
   786                 return fullname.toString();
   787         }
   789         public Name getQualifiedName() {
   790             return fullname;
   791         }
   793         public Name flatName() {
   794             return flatname;
   795         }
   797         public boolean isSubClass(Symbol base, Types types) {
   798             if (this == base) {
   799                 return true;
   800             } else if ((base.flags() & INTERFACE) != 0) {
   801                 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
   802                     for (List<Type> is = types.interfaces(t);
   803                          is.nonEmpty();
   804                          is = is.tail)
   805                         if (is.head.tsym.isSubClass(base, types)) return true;
   806             } else {
   807                 for (Type t = type; t.tag == CLASS; t = types.supertype(t))
   808                     if (t.tsym == base) return true;
   809             }
   810             return false;
   811         }
   813         /** Complete the elaboration of this symbol's definition.
   814          */
   815         public void complete() throws CompletionFailure {
   816             try {
   817                 super.complete();
   818             } catch (CompletionFailure ex) {
   819                 // quiet error recovery
   820                 flags_field |= (PUBLIC|STATIC);
   821                 this.type = new ErrorType(this, Type.noType);
   822                 throw ex;
   823             }
   824         }
   826         public List<Type> getInterfaces() {
   827             complete();
   828             if (type instanceof ClassType) {
   829                 ClassType t = (ClassType)type;
   830                 if (t.interfaces_field == null) // FIXME: shouldn't be null
   831                     t.interfaces_field = List.nil();
   832                 return t.interfaces_field;
   833             } else {
   834                 return List.nil();
   835             }
   836         }
   838         public Type getSuperclass() {
   839             complete();
   840             if (type instanceof ClassType) {
   841                 ClassType t = (ClassType)type;
   842                 if (t.supertype_field == null) // FIXME: shouldn't be null
   843                     t.supertype_field = Type.noType;
   844                 // An interface has no superclass; its supertype is Object.
   845                 return t.isInterface()
   846                     ? Type.noType
   847                     : t.supertype_field;
   848             } else {
   849                 return Type.noType;
   850             }
   851         }
   853         public ElementKind getKind() {
   854             long flags = flags();
   855             if ((flags & ANNOTATION) != 0)
   856                 return ElementKind.ANNOTATION_TYPE;
   857             else if ((flags & INTERFACE) != 0)
   858                 return ElementKind.INTERFACE;
   859             else if ((flags & ENUM) != 0)
   860                 return ElementKind.ENUM;
   861             else
   862                 return ElementKind.CLASS;
   863         }
   865         public NestingKind getNestingKind() {
   866             complete();
   867             if (owner.kind == PCK)
   868                 return NestingKind.TOP_LEVEL;
   869             else if (name.isEmpty())
   870                 return NestingKind.ANONYMOUS;
   871             else if (owner.kind == MTH)
   872                 return NestingKind.LOCAL;
   873             else
   874                 return NestingKind.MEMBER;
   875         }
   877         /**
   878          * @deprecated this method should never be used by javac internally.
   879          */
   880         @Override @Deprecated
   881         public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
   882             return JavacElements.getAnnotation(this, annoType);
   883         }
   885         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
   886             return v.visitType(this, p);
   887         }
   889         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
   890             return v.visitClassSymbol(this, p);
   891         }
   892     }
   895     /** A class for variable symbols
   896      */
   897     public static class VarSymbol extends Symbol implements VariableElement {
   899         /** The variable's declaration position.
   900          */
   901         public int pos = Position.NOPOS;
   903         /** The variable's address. Used for different purposes during
   904          *  flow analysis, translation and code generation.
   905          *  Flow analysis:
   906          *    If this is a blank final or local variable, its sequence number.
   907          *  Translation:
   908          *    If this is a private field, its access number.
   909          *  Code generation:
   910          *    If this is a local variable, its logical slot number.
   911          */
   912         public int adr = -1;
   914         /** Construct a variable symbol, given its flags, name, type and owner.
   915          */
   916         public VarSymbol(long flags, Name name, Type type, Symbol owner) {
   917             super(VAR, flags, name, type, owner);
   918         }
   920         /** Clone this symbol with new owner.
   921          */
   922         public VarSymbol clone(Symbol newOwner) {
   923             VarSymbol v = new VarSymbol(flags_field, name, type, newOwner);
   924             v.pos = pos;
   925             v.adr = adr;
   926             v.data = data;
   927 //          System.out.println("clone " + v + " in " + newOwner);//DEBUG
   928             return v;
   929         }
   931         public String toString() {
   932             return name.toString();
   933         }
   935         public Symbol asMemberOf(Type site, Types types) {
   936             return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
   937         }
   939         public ElementKind getKind() {
   940             long flags = flags();
   941             if ((flags & PARAMETER) != 0) {
   942                 if (isExceptionParameter())
   943                     return ElementKind.EXCEPTION_PARAMETER;
   944                 else
   945                     return ElementKind.PARAMETER;
   946             } else if ((flags & ENUM) != 0) {
   947                 return ElementKind.ENUM_CONSTANT;
   948             } else if (owner.kind == TYP || owner.kind == ERR) {
   949                 return ElementKind.FIELD;
   950             } else {
   951                 return ElementKind.LOCAL_VARIABLE;
   952             }
   953         }
   955         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
   956             return v.visitVariable(this, p);
   957         }
   959         public Object getConstantValue() { // Mirror API
   960             return Constants.decode(getConstValue(), type);
   961         }
   963         public void setLazyConstValue(final Env<AttrContext> env,
   964                                       final Log log,
   965                                       final Attr attr,
   966                                       final JCTree.JCExpression initializer)
   967         {
   968             setData(new Callable<Object>() {
   969                 public Object call() {
   970                     JavaFileObject source = log.useSource(env.toplevel.sourcefile);
   971                     try {
   972                         Type itype = attr.attribExpr(initializer, env, type);
   973                         if (itype.constValue() != null)
   974                             return attr.coerce(itype, type).constValue();
   975                         else
   976                             return null;
   977                     } finally {
   978                         log.useSource(source);
   979                     }
   980                 }
   981             });
   982         }
   984         /**
   985          * The variable's constant value, if this is a constant.
   986          * Before the constant value is evaluated, it points to an
   987          * initalizer environment.  If this is not a constant, it can
   988          * be used for other stuff.
   989          */
   990         private Object data;
   992         public boolean isExceptionParameter() {
   993             return data == ElementKind.EXCEPTION_PARAMETER;
   994         }
   996         public Object getConstValue() {
   997             // TODO: Consider if getConstValue and getConstantValue can be collapsed
   998             if (data == ElementKind.EXCEPTION_PARAMETER) {
   999                 return null;
  1000             } else if (data instanceof Callable<?>) {
  1001                 // In this case, this is final a variable, with an as
  1002                 // yet unevaluated initializer.
  1003                 Callable<?> eval = (Callable<?>)data;
  1004                 data = null; // to make sure we don't evaluate this twice.
  1005                 try {
  1006                     data = eval.call();
  1007                 } catch (Exception ex) {
  1008                     throw new AssertionError(ex);
  1011             return data;
  1014         public void setData(Object data) {
  1015             assert !(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             assert owner.type.tag != TYPEVAR : 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                 for (Scope.Entry e = i.members().lookup(name);
  1087                      impl == null && e.scope != null;
  1088                      e = e.next()) {
  1089                     if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
  1090                         // FIXME: I suspect the following requires a
  1091                         // subst() for a parametric return type.
  1092                         types.isSameType(type.getReturnType(),
  1093                                          types.memberType(owner.type, e.sym).getReturnType())) {
  1094                         impl = e.sym;
  1096                     if (impl == null)
  1097                         impl = implemented(i, types);
  1100             return impl;
  1103         /** Will the erasure of this method be considered by the VM to
  1104          *  override the erasure of the other when seen from class `origin'?
  1105          */
  1106         public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
  1107             if (isConstructor() || _other.kind != MTH) return false;
  1109             if (this == _other) return true;
  1110             MethodSymbol other = (MethodSymbol)_other;
  1112             // check for a direct implementation
  1113             if (other.isOverridableIn((TypeSymbol)owner) &&
  1114                 types.asSuper(owner.type, other.owner) != null &&
  1115                 types.isSameType(erasure(types), other.erasure(types)))
  1116                 return true;
  1118             // check for an inherited implementation
  1119             return
  1120                 (flags() & ABSTRACT) == 0 &&
  1121                 other.isOverridableIn(origin) &&
  1122                 this.isMemberOf(origin, types) &&
  1123                 types.isSameType(erasure(types), other.erasure(types));
  1126         /** The implementation of this (abstract) symbol in class origin,
  1127          *  from the VM's point of view, null if method does not have an
  1128          *  implementation in class.
  1129          *  @param origin   The class of which the implementation is a member.
  1130          */
  1131         public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
  1132             for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
  1133                 for (Scope.Entry e = c.members().lookup(name);
  1134                      e.scope != null;
  1135                      e = e.next()) {
  1136                     if (e.sym.kind == MTH &&
  1137                         ((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
  1138                         return (MethodSymbol)e.sym;
  1141             return null;
  1144         /** Does this symbol override `other' symbol, when both are seen as
  1145          *  members of class `origin'?  It is assumed that _other is a member
  1146          *  of origin.
  1148          *  It is assumed that both symbols have the same name.  The static
  1149          *  modifier is ignored for this test.
  1151          *  See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
  1152          */
  1153         public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
  1154             if (isConstructor() || _other.kind != MTH) return false;
  1156             if (this == _other) return true;
  1157             MethodSymbol other = (MethodSymbol)_other;
  1159             // check for a direct implementation
  1160             if (other.isOverridableIn((TypeSymbol)owner) &&
  1161                 types.asSuper(owner.type, other.owner) != null) {
  1162                 Type mt = types.memberType(owner.type, this);
  1163                 Type ot = types.memberType(owner.type, other);
  1164                 if (types.isSubSignature(mt, ot)) {
  1165                     if (!checkResult)
  1166                         return true;
  1167                     if (types.returnTypeSubstitutable(mt, ot))
  1168                         return true;
  1172             // check for an inherited implementation
  1173             if ((flags() & ABSTRACT) != 0 ||
  1174                 (other.flags() & ABSTRACT) == 0 ||
  1175                 !other.isOverridableIn(origin) ||
  1176                 !this.isMemberOf(origin, types))
  1177                 return false;
  1179             // assert types.asSuper(origin.type, other.owner) != null;
  1180             Type mt = types.memberType(origin.type, this);
  1181             Type ot = types.memberType(origin.type, other);
  1182             return
  1183                 types.isSubSignature(mt, ot) &&
  1184                 (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings));
  1187         private boolean isOverridableIn(TypeSymbol origin) {
  1188             // JLS3 8.4.6.1
  1189             switch ((int)(flags_field & Flags.AccessFlags)) {
  1190             case Flags.PRIVATE:
  1191                 return false;
  1192             case Flags.PUBLIC:
  1193                 return true;
  1194             case Flags.PROTECTED:
  1195                 return (origin.flags() & INTERFACE) == 0;
  1196             case 0:
  1197                 // for package private: can only override in the same
  1198                 // package
  1199                 return
  1200                     this.packge() == origin.packge() &&
  1201                     (origin.flags() & INTERFACE) == 0;
  1202             default:
  1203                 return false;
  1207         /** The implementation of this (abstract) symbol in class origin;
  1208          *  null if none exists. Synthetic methods are not considered
  1209          *  as possible implementations.
  1210          */
  1211         public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
  1212             MethodSymbol res = types.implementation(this, origin, types, checkResult);
  1213             if (res != null)
  1214                 return res;
  1215             // if origin is derived from a raw type, we might have missed
  1216             // an implementation because we do not know enough about instantiations.
  1217             // in this case continue with the supertype as origin.
  1218             if (types.isDerivedRaw(origin.type))
  1219                 return implementation(types.supertype(origin.type).tsym, types, checkResult);
  1220             else
  1221                 return null;
  1224         public List<VarSymbol> params() {
  1225             owner.complete();
  1226             if (params == null) {
  1227                 // If ClassReader.saveParameterNames has been set true, then
  1228                 // savedParameterNames will be set to a list of names that
  1229                 // matches the types in type.getParameterTypes().  If any names
  1230                 // were not found in the class file, those names in the list will
  1231                 // be set to the empty name.
  1232                 // If ClassReader.saveParameterNames has been set false, then
  1233                 // savedParameterNames will be null.
  1234                 List<Name> paramNames = savedParameterNames;
  1235                 savedParameterNames = null;
  1236                 // discard the provided names if the list of names is the wrong size.
  1237                 if (paramNames == null || paramNames.size() != type.getParameterTypes().size())
  1238                     paramNames = List.nil();
  1239                 ListBuffer<VarSymbol> buf = new ListBuffer<VarSymbol>();
  1240                 List<Name> remaining = paramNames;
  1241                 // assert: remaining and paramNames are both empty or both
  1242                 // have same cardinality as type.getParameterTypes()
  1243                 int i = 0;
  1244                 for (Type t : type.getParameterTypes()) {
  1245                     Name paramName;
  1246                     if (remaining.isEmpty()) {
  1247                         // no names for any parameters available
  1248                         paramName = createArgName(i, paramNames);
  1249                     } else {
  1250                         paramName = remaining.head;
  1251                         remaining = remaining.tail;
  1252                         if (paramName.isEmpty()) {
  1253                             // no name for this specific parameter
  1254                             paramName = createArgName(i, paramNames);
  1257                     buf.append(new VarSymbol(PARAMETER, paramName, t, this));
  1258                     i++;
  1260                 params = buf.toList();
  1262             return params;
  1265         // Create a name for the argument at position 'index' that is not in
  1266         // the exclude list. In normal use, either no names will have been
  1267         // provided, in which case the exclude list is empty, or all the names
  1268         // will have been provided, in which case this method will not be called.
  1269         private Name createArgName(int index, List<Name> exclude) {
  1270             String prefix = "arg";
  1271             while (true) {
  1272                 Name argName = name.table.fromString(prefix + index);
  1273                 if (!exclude.contains(argName))
  1274                     return argName;
  1275                 prefix += "$";
  1279         public Symbol asMemberOf(Type site, Types types) {
  1280             return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
  1283         public ElementKind getKind() {
  1284             if (name == name.table.names.init)
  1285                 return ElementKind.CONSTRUCTOR;
  1286             else if (name == name.table.names.clinit)
  1287                 return ElementKind.STATIC_INIT;
  1288             else
  1289                 return ElementKind.METHOD;
  1292         public Attribute getDefaultValue() {
  1293             return defaultValue;
  1296         public List<VarSymbol> getParameters() {
  1297             return params();
  1300         public boolean isVarArgs() {
  1301             return (flags() & VARARGS) != 0;
  1304         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
  1305             return v.visitExecutable(this, p);
  1308         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
  1309             return v.visitMethodSymbol(this, p);
  1312         public Type getReturnType() {
  1313             return asType().getReturnType();
  1316         public List<Type> getThrownTypes() {
  1317             return asType().getThrownTypes();
  1321     /** A class for predefined operators.
  1322      */
  1323     public static class OperatorSymbol extends MethodSymbol {
  1325         public int opcode;
  1327         public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
  1328             super(PUBLIC | STATIC, name, type, owner);
  1329             this.opcode = opcode;
  1332         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
  1333             return v.visitOperatorSymbol(this, p);
  1337     /** Symbol completer interface.
  1338      */
  1339     public static interface Completer {
  1340         void complete(Symbol sym) throws CompletionFailure;
  1343     public static class CompletionFailure extends RuntimeException {
  1344         private static final long serialVersionUID = 0;
  1345         public Symbol sym;
  1347         /** A diagnostic object describing the failure
  1348          */
  1349         public JCDiagnostic diag;
  1351         /** A localized string describing the failure.
  1352          * @deprecated Use {@code getDetail()} or {@code getMessage()}
  1353          */
  1354         @Deprecated
  1355         public String errmsg;
  1357         public CompletionFailure(Symbol sym, String errmsg) {
  1358             this.sym = sym;
  1359             this.errmsg = errmsg;
  1360 //          this.printStackTrace();//DEBUG
  1363         public CompletionFailure(Symbol sym, JCDiagnostic diag) {
  1364             this.sym = sym;
  1365             this.diag = diag;
  1366 //          this.printStackTrace();//DEBUG
  1369         public JCDiagnostic getDiagnostic() {
  1370             return diag;
  1373         @Override
  1374         public String getMessage() {
  1375             if (diag != null)
  1376                 return diag.getMessage(null);
  1377             else
  1378                 return errmsg;
  1381         public Object getDetailValue() {
  1382             return (diag != null ? diag : errmsg);
  1385         @Override
  1386         public CompletionFailure initCause(Throwable cause) {
  1387             super.initCause(cause);
  1388             return this;
  1393     /**
  1394      * A visitor for symbols.  A visitor is used to implement operations
  1395      * (or relations) on symbols.  Most common operations on types are
  1396      * binary relations and this interface is designed for binary
  1397      * relations, that is, operations on the form
  1398      * Symbol&nbsp;&times;&nbsp;P&nbsp;&rarr;&nbsp;R.
  1399      * <!-- In plain text: Type x P -> R -->
  1401      * @param <R> the return type of the operation implemented by this
  1402      * visitor; use Void if no return type is needed.
  1403      * @param <P> the type of the second argument (the first being the
  1404      * symbol itself) of the operation implemented by this visitor; use
  1405      * Void if a second argument is not needed.
  1406      */
  1407     public interface Visitor<R,P> {
  1408         R visitClassSymbol(ClassSymbol s, P arg);
  1409         R visitMethodSymbol(MethodSymbol s, P arg);
  1410         R visitPackageSymbol(PackageSymbol s, P arg);
  1411         R visitOperatorSymbol(OperatorSymbol s, P arg);
  1412         R visitVarSymbol(VarSymbol s, P arg);
  1413         R visitTypeSymbol(TypeSymbol s, P arg);
  1414         R visitSymbol(Symbol s, P arg);

mercurial