duke@1: /* jfranck@1491: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.code; duke@1: duke@1: import java.util.Set; duke@1: import java.util.concurrent.Callable; jjg@1357: duke@1: import javax.lang.model.element.*; duke@1: import javax.tools.JavaFileObject; duke@1: duke@1: import com.sun.tools.javac.code.Type.*; duke@1: import com.sun.tools.javac.comp.Attr; duke@1: import com.sun.tools.javac.comp.AttrContext; duke@1: import com.sun.tools.javac.comp.Env; duke@1: import com.sun.tools.javac.jvm.*; duke@1: import com.sun.tools.javac.model.*; duke@1: import com.sun.tools.javac.tree.JCTree; jjg@1357: import com.sun.tools.javac.util.*; jjg@1357: import com.sun.tools.javac.util.Name; duke@1: import static com.sun.tools.javac.code.Flags.*; duke@1: import static com.sun.tools.javac.code.Kinds.*; jjg@1374: import static com.sun.tools.javac.code.TypeTag.CLASS; jjg@1374: import static com.sun.tools.javac.code.TypeTag.FORALL; jjg@1374: import static com.sun.tools.javac.code.TypeTag.TYPEVAR; duke@1: duke@1: /** Root class for Java symbols. It contains subclasses duke@1: * for specific sorts of symbols, such as variables, methods and operators, duke@1: * types, packages. Each subclass is represented as a static inner class duke@1: * inside Symbol. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public abstract class Symbol implements Element { duke@1: // public Throwable debug = new Throwable(); duke@1: duke@1: /** The kind of this symbol. duke@1: * @see Kinds duke@1: */ duke@1: public int kind; duke@1: duke@1: /** The flags of this symbol. duke@1: */ duke@1: public long flags_field; duke@1: duke@1: /** An accessor method for the flags of this symbol. duke@1: * Flags of class symbols should be accessed through the accessor duke@1: * method to make sure that the class symbol is loaded. duke@1: */ duke@1: public long flags() { return flags_field; } duke@1: jfranck@1313: /** The attributes of this symbol are contained in this jfranck@1313: * Annotations. The Annotations instance is NOT immutable. duke@1: */ jfranck@1313: public final Annotations annotations = new Annotations(this); duke@1: duke@1: /** An accessor method for the attributes of this symbol. duke@1: * Attributes of class symbols should be accessed through the accessor duke@1: * method to make sure that the class symbol is loaded. duke@1: */ jfranck@1464: public List getRawAttributes() { jjg@1521: return annotations.getDeclarationAttributes(); jjg@1521: } jjg@1521: jjg@1521: /** An accessor method for the type attributes of this symbol. jjg@1521: * Attributes of class symbols should be accessed through the accessor jjg@1521: * method to make sure that the class symbol is loaded. jjg@1521: */ jjg@1521: public List getRawTypeAttributes() { jjg@1521: return annotations.getTypeAttributes(); duke@1: } duke@1: duke@1: /** Fetch a particular annotation from a symbol. */ duke@1: public Attribute.Compound attribute(Symbol anno) { jfranck@1464: for (Attribute.Compound a : getRawAttributes()) { duke@1: if (a.type.tsym == anno) return a; jfranck@1313: } duke@1: return null; duke@1: } duke@1: duke@1: /** The name of this symbol in Utf8 representation. duke@1: */ duke@1: public Name name; duke@1: duke@1: /** The type of this symbol. duke@1: */ duke@1: public Type type; duke@1: duke@1: /** The owner of this symbol. duke@1: */ duke@1: public Symbol owner; duke@1: duke@1: /** The completer of this symbol. duke@1: */ duke@1: public Completer completer; duke@1: duke@1: /** A cache for the type erasure of this symbol. duke@1: */ duke@1: public Type erasure_field; duke@1: duke@1: /** Construct a symbol with given kind, flags, name, type and owner. duke@1: */ duke@1: public Symbol(int kind, long flags, Name name, Type type, Symbol owner) { duke@1: this.kind = kind; duke@1: this.flags_field = flags; duke@1: this.type = type; duke@1: this.owner = owner; duke@1: this.completer = null; duke@1: this.erasure_field = null; duke@1: this.name = name; duke@1: } duke@1: duke@1: /** Clone this symbol with new owner. duke@1: * Legal only for fields and methods. duke@1: */ duke@1: public Symbol clone(Symbol newOwner) { duke@1: throw new AssertionError(); duke@1: } duke@1: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitSymbol(this, p); mcimadamore@121: } mcimadamore@121: duke@1: /** The Java source which this symbol represents. duke@1: * A description of this symbol; overrides Object. duke@1: */ duke@1: public String toString() { duke@1: return name.toString(); duke@1: } duke@1: duke@1: /** A Java source description of the location of this symbol; used for mcimadamore@80: * error reporting. mcimadamore@80: * mcimadamore@80: * @return null if the symbol is a package or a toplevel class defined in mcimadamore@80: * the default package; otherwise, the owner symbol is returned duke@1: */ mcimadamore@80: public Symbol location() { mcimadamore@1085: if (owner.name == null || (owner.name.isEmpty() && mcimadamore@1085: (owner.flags() & BLOCK) == 0 && owner.kind != PCK && owner.kind != TYP)) { mcimadamore@80: return null; duke@1: } mcimadamore@80: return owner; duke@1: } duke@1: mcimadamore@80: public Symbol location(Type site, Types types) { jjg@113: if (owner.name == null || owner.name.isEmpty()) { duke@1: return location(); duke@1: } jjg@1374: if (owner.type.hasTag(CLASS)) { duke@1: Type ownertype = types.asOuterSuper(site, owner); mcimadamore@80: if (ownertype != null) return ownertype.tsym; duke@1: } mcimadamore@80: return owner; duke@1: } duke@1: mcimadamore@1341: public Symbol baseSymbol() { mcimadamore@1341: return this; mcimadamore@1341: } mcimadamore@1341: duke@1: /** The symbol's erased type. duke@1: */ duke@1: public Type erasure(Types types) { duke@1: if (erasure_field == null) duke@1: erasure_field = types.erasure(type); duke@1: return erasure_field; duke@1: } duke@1: duke@1: /** The external type of a symbol. This is the symbol's erased type duke@1: * except for constructors of inner classes which get the enclosing duke@1: * instance class added as first argument. duke@1: */ duke@1: public Type externalType(Types types) { duke@1: Type t = erasure(types); jjg@113: if (name == name.table.names.init && owner.hasOuterInstance()) { duke@1: Type outerThisType = types.erasure(owner.type.getEnclosingType()); duke@1: return new MethodType(t.getParameterTypes().prepend(outerThisType), duke@1: t.getReturnType(), duke@1: t.getThrownTypes(), duke@1: t.tsym); duke@1: } else { duke@1: return t; duke@1: } duke@1: } duke@1: duke@1: public boolean isStatic() { duke@1: return duke@1: (flags() & STATIC) != 0 || duke@1: (owner.flags() & INTERFACE) != 0 && kind != MTH; duke@1: } duke@1: duke@1: public boolean isInterface() { duke@1: return (flags() & INTERFACE) != 0; duke@1: } duke@1: mcimadamore@1565: public boolean isPrivate() { mcimadamore@1565: return (flags_field & Flags.AccessFlags) == PRIVATE; mcimadamore@1565: } mcimadamore@1565: mcimadamore@1565: public boolean isEnum() { mcimadamore@1565: return (flags() & ENUM) != 0; mcimadamore@1565: } mcimadamore@1565: duke@1: /** Is this symbol declared (directly or indirectly) local duke@1: * to a method or variable initializer? duke@1: * Also includes fields of inner classes which are in duke@1: * turn local to a method or variable initializer. duke@1: */ duke@1: public boolean isLocal() { duke@1: return duke@1: (owner.kind & (VAR | MTH)) != 0 || duke@1: (owner.kind == TYP && owner.isLocal()); duke@1: } duke@1: mcimadamore@537: /** Has this symbol an empty name? This includes anonymous mcimadamore@537: * inner classses. mcimadamore@537: */ mcimadamore@537: public boolean isAnonymous() { mcimadamore@537: return name.isEmpty(); mcimadamore@537: } mcimadamore@537: duke@1: /** Is this symbol a constructor? duke@1: */ duke@1: public boolean isConstructor() { jjg@113: return name == name.table.names.init; duke@1: } duke@1: duke@1: /** The fully qualified name of this symbol. duke@1: * This is the same as the symbol's name except for class symbols, duke@1: * which are handled separately. duke@1: */ duke@1: public Name getQualifiedName() { duke@1: return name; duke@1: } duke@1: duke@1: /** The fully qualified name of this symbol after converting to flat duke@1: * representation. This is the same as the symbol's name except for duke@1: * class symbols, which are handled separately. duke@1: */ duke@1: public Name flatName() { duke@1: return getQualifiedName(); duke@1: } duke@1: duke@1: /** If this is a class or package, its members, otherwise null. duke@1: */ duke@1: public Scope members() { duke@1: return null; duke@1: } duke@1: duke@1: /** A class is an inner class if it it has an enclosing instance class. duke@1: */ duke@1: public boolean isInner() { jjg@1374: return type.getEnclosingType().hasTag(CLASS); duke@1: } duke@1: duke@1: /** An inner class has an outer instance if it is not an interface duke@1: * it has an enclosing instance class which might be referenced from the class. duke@1: * Nested classes can see instance members of their enclosing class. duke@1: * Their constructors carry an additional this$n parameter, inserted duke@1: * implicitly by the compiler. duke@1: * duke@1: * @see #isInner duke@1: */ duke@1: public boolean hasOuterInstance() { duke@1: return jjg@1374: type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0; duke@1: } duke@1: duke@1: /** The closest enclosing class of this symbol's declaration. duke@1: */ duke@1: public ClassSymbol enclClass() { duke@1: Symbol c = this; duke@1: while (c != null && jjg@1374: ((c.kind & TYP) == 0 || !c.type.hasTag(CLASS))) { duke@1: c = c.owner; duke@1: } duke@1: return (ClassSymbol)c; duke@1: } duke@1: duke@1: /** The outermost class which indirectly owns this symbol. duke@1: */ duke@1: public ClassSymbol outermostClass() { duke@1: Symbol sym = this; duke@1: Symbol prev = null; duke@1: while (sym.kind != PCK) { duke@1: prev = sym; duke@1: sym = sym.owner; duke@1: } duke@1: return (ClassSymbol) prev; duke@1: } duke@1: duke@1: /** The package which indirectly owns this symbol. duke@1: */ duke@1: public PackageSymbol packge() { duke@1: Symbol sym = this; duke@1: while (sym.kind != PCK) { duke@1: sym = sym.owner; duke@1: } duke@1: return (PackageSymbol) sym; duke@1: } duke@1: duke@1: /** Is this symbol a subclass of `base'? Only defined for ClassSymbols. duke@1: */ duke@1: public boolean isSubClass(Symbol base, Types types) { duke@1: throw new AssertionError("isSubClass " + this); duke@1: } duke@1: duke@1: /** Fully check membership: hierarchy, protection, and hiding. duke@1: * Does not exclude methods not inherited due to overriding. duke@1: */ duke@1: public boolean isMemberOf(TypeSymbol clazz, Types types) { duke@1: return duke@1: owner == clazz || duke@1: clazz.isSubClass(owner, types) && duke@1: isInheritedIn(clazz, types) && duke@1: !hiddenIn((ClassSymbol)clazz, types); duke@1: } duke@1: duke@1: /** Is this symbol the same as or enclosed by the given class? */ duke@1: public boolean isEnclosedBy(ClassSymbol clazz) { duke@1: for (Symbol sym = this; sym.kind != PCK; sym = sym.owner) duke@1: if (sym == clazz) return true; duke@1: return false; duke@1: } duke@1: duke@1: /** Check for hiding. Note that this doesn't handle multiple duke@1: * (interface) inheritance. */ duke@1: private boolean hiddenIn(ClassSymbol clazz, Types types) { duke@1: if (kind == MTH && (flags() & STATIC) == 0) return false; duke@1: while (true) { duke@1: if (owner == clazz) return false; duke@1: Scope.Entry e = clazz.members().lookup(name); duke@1: while (e.scope != null) { duke@1: if (e.sym == this) return false; duke@1: if (e.sym.kind == kind && duke@1: (kind != MTH || duke@1: (e.sym.flags() & STATIC) != 0 && duke@1: types.isSubSignature(e.sym.type, type))) duke@1: return true; duke@1: e = e.next(); duke@1: } duke@1: Type superType = types.supertype(clazz.type); jjg@1374: if (!superType.hasTag(CLASS)) return false; duke@1: clazz = (ClassSymbol)superType.tsym; duke@1: } duke@1: } duke@1: duke@1: /** Is this symbol inherited into a given class? duke@1: * PRE: If symbol's owner is a interface, duke@1: * it is already assumed that the interface is a superinterface duke@1: * of given class. duke@1: * @param clazz The class for which we want to establish membership. duke@1: * This must be a subclass of the member's owner. duke@1: */ duke@1: public boolean isInheritedIn(Symbol clazz, Types types) { duke@1: switch ((int)(flags_field & Flags.AccessFlags)) { duke@1: default: // error recovery duke@1: case PUBLIC: duke@1: return true; duke@1: case PRIVATE: duke@1: return this.owner == clazz; duke@1: case PROTECTED: duke@1: // we model interfaces as extending Object duke@1: return (clazz.flags() & INTERFACE) == 0; duke@1: case 0: duke@1: PackageSymbol thisPackage = this.packge(); duke@1: for (Symbol sup = clazz; duke@1: sup != null && sup != this.owner; duke@1: sup = types.supertype(sup.type).tsym) { jjg@1374: while (sup.type.hasTag(TYPEVAR)) mcimadamore@155: sup = sup.type.getUpperBound().tsym; duke@1: if (sup.type.isErroneous()) duke@1: return true; // error recovery duke@1: if ((sup.flags() & COMPOUND) != 0) duke@1: continue; duke@1: if (sup.packge() != thisPackage) duke@1: return false; duke@1: } duke@1: return (clazz.flags() & INTERFACE) == 0; duke@1: } duke@1: } duke@1: duke@1: /** The (variable or method) symbol seen as a member of given duke@1: * class type`site' (this might change the symbol's type). duke@1: * This is used exclusively for producing diagnostics. duke@1: */ duke@1: public Symbol asMemberOf(Type site, Types types) { duke@1: throw new AssertionError(); duke@1: } duke@1: duke@1: /** Does this method symbol override `other' symbol, when both are seen as duke@1: * members of class `origin'? It is assumed that _other is a member duke@1: * of origin. duke@1: * duke@1: * It is assumed that both symbols have the same name. The static duke@1: * modifier is ignored for this test. duke@1: * duke@1: * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4 duke@1: */ duke@1: public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) { duke@1: return false; duke@1: } duke@1: duke@1: /** Complete the elaboration of this symbol's definition. duke@1: */ duke@1: public void complete() throws CompletionFailure { duke@1: if (completer != null) { duke@1: Completer c = completer; duke@1: completer = null; duke@1: c.complete(this); duke@1: } duke@1: } duke@1: duke@1: /** True if the symbol represents an entity that exists. duke@1: */ duke@1: public boolean exists() { duke@1: return true; duke@1: } duke@1: duke@1: public Type asType() { duke@1: return type; duke@1: } duke@1: duke@1: public Symbol getEnclosingElement() { duke@1: return owner; duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: return ElementKind.OTHER; // most unkind duke@1: } duke@1: duke@1: public Set getModifiers() { mcimadamore@1415: long flags = flags(); mcimadamore@1415: return Flags.asModifierSet((flags & DEFAULT) != 0 ? flags & ~ABSTRACT : flags); duke@1: } duke@1: duke@1: public Name getSimpleName() { duke@1: return name; duke@1: } duke@1: duke@1: /** jfranck@1464: * This is the implementation for {@code jfranck@1464: * javax.lang.model.element.Element.getAnnotationMirrors()}. jfranck@1464: */ jfranck@1491: public final List getAnnotationMirrors() { jfranck@1464: return getRawAttributes(); jfranck@1464: } jfranck@1464: jfranck@1464: /** jjg@1521: * TODO: Should there be a {@code jjg@1521: * javax.lang.model.element.Element.getTypeAnnotationMirrors()}. jjg@1521: */ jjg@1521: public final List getTypeAnnotationMirrors() { jjg@1521: return getRawTypeAttributes(); jjg@1521: } jjg@1521: jjg@1521: /** duke@1: * @deprecated this method should never be used by javac internally. duke@1: */ duke@1: @Deprecated duke@1: public A getAnnotation(Class annoType) { duke@1: return JavacElements.getAnnotation(this, annoType); duke@1: } duke@1: jfranck@1491: // This method is part of the javax.lang.model API, do not use this in javac code. jfranck@1564: public A[] getAnnotationsByType(Class annoType) { jfranck@1491: return JavacElements.getAnnotations(this, annoType); jfranck@1491: } jfranck@1491: duke@1: // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList duke@1: public java.util.List getEnclosedElements() { duke@1: return List.nil(); duke@1: } duke@1: duke@1: public List getTypeParameters() { duke@1: ListBuffer l = ListBuffer.lb(); duke@1: for (Type t : type.getTypeArguments()) { duke@1: l.append(t.tsym); duke@1: } duke@1: return l.toList(); duke@1: } duke@1: vromero@1541: public static class DelegatedSymbol extends Symbol { vromero@1541: protected T other; vromero@1541: public DelegatedSymbol(T other) { duke@1: super(other.kind, other.flags_field, other.name, other.type, other.owner); duke@1: this.other = other; duke@1: } duke@1: public String toString() { return other.toString(); } mcimadamore@80: public Symbol location() { return other.location(); } mcimadamore@80: public Symbol location(Type site, Types types) { return other.location(site, types); } mcimadamore@1415: public Symbol baseSymbol() { return other; } duke@1: public Type erasure(Types types) { return other.erasure(types); } duke@1: public Type externalType(Types types) { return other.externalType(types); } duke@1: public boolean isLocal() { return other.isLocal(); } duke@1: public boolean isConstructor() { return other.isConstructor(); } duke@1: public Name getQualifiedName() { return other.getQualifiedName(); } duke@1: public Name flatName() { return other.flatName(); } duke@1: public Scope members() { return other.members(); } duke@1: public boolean isInner() { return other.isInner(); } duke@1: public boolean hasOuterInstance() { return other.hasOuterInstance(); } duke@1: public ClassSymbol enclClass() { return other.enclClass(); } duke@1: public ClassSymbol outermostClass() { return other.outermostClass(); } duke@1: public PackageSymbol packge() { return other.packge(); } duke@1: public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); } duke@1: public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); } duke@1: public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); } duke@1: public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); } duke@1: public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); } duke@1: public void complete() throws CompletionFailure { other.complete(); } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return other.accept(v, p); duke@1: } mcimadamore@121: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitSymbol(other, p); mcimadamore@121: } vromero@1541: vromero@1541: public T getUnderlyingSymbol() { vromero@1541: return other; vromero@1541: } duke@1: } duke@1: duke@1: /** A class for type symbols. Type variables are represented by instances duke@1: * of this class, classes and packages by instances of subclasses. duke@1: */ duke@1: public static class TypeSymbol duke@1: extends Symbol implements TypeParameterElement { duke@1: // Implements TypeParameterElement because type parameters don't duke@1: // have their own TypeSymbol subclass. duke@1: // TODO: type parameters should have their own TypeSymbol subclass duke@1: duke@1: public TypeSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(TYP, flags, name, type, owner); duke@1: } duke@1: duke@1: /** form a fully qualified name from a name and an owner duke@1: */ duke@1: static public Name formFullName(Name name, Symbol owner) { duke@1: if (owner == null) return name; duke@1: if (((owner.kind != ERR)) && duke@1: ((owner.kind & (VAR | MTH)) != 0 jjg@1374: || (owner.kind == TYP && owner.type.hasTag(TYPEVAR)) duke@1: )) return name; duke@1: Name prefix = owner.getQualifiedName(); jjg@113: if (prefix == null || prefix == prefix.table.names.empty) duke@1: return name; duke@1: else return prefix.append('.', name); duke@1: } duke@1: duke@1: /** form a fully qualified name from a name and an owner, after duke@1: * converting to flat representation duke@1: */ duke@1: static public Name formFlatName(Name name, Symbol owner) { duke@1: if (owner == null || duke@1: (owner.kind & (VAR | MTH)) != 0 jjg@1374: || (owner.kind == TYP && owner.type.hasTag(TYPEVAR)) duke@1: ) return name; duke@1: char sep = owner.kind == TYP ? '$' : '.'; duke@1: Name prefix = owner.flatName(); jjg@113: if (prefix == null || prefix == prefix.table.names.empty) duke@1: return name; duke@1: else return prefix.append(sep, name); duke@1: } duke@1: duke@1: /** duke@1: * A total ordering between type symbols that refines the duke@1: * class inheritance graph. duke@1: * duke@1: * Typevariables always precede other kinds of symbols. duke@1: */ duke@1: public final boolean precedes(TypeSymbol that, Types types) { duke@1: if (this == that) duke@1: return false; duke@1: if (this.type.tag == that.type.tag) { jjg@1374: if (this.type.hasTag(CLASS)) { duke@1: return duke@1: types.rank(that.type) < types.rank(this.type) || duke@1: types.rank(that.type) == types.rank(this.type) && duke@1: that.getQualifiedName().compareTo(this.getQualifiedName()) < 0; jjg@1374: } else if (this.type.hasTag(TYPEVAR)) { duke@1: return types.isSubtype(this.type, that.type); duke@1: } duke@1: } jjg@1374: return this.type.hasTag(TYPEVAR); duke@1: } duke@1: duke@1: // For type params; overridden in subclasses. duke@1: public ElementKind getKind() { duke@1: return ElementKind.TYPE_PARAMETER; duke@1: } duke@1: duke@1: public java.util.List getEnclosedElements() { duke@1: List list = List.nil(); jjg@1374: if (kind == TYP && type.hasTag(TYPEVAR)) { sundar@666: return list; sundar@666: } duke@1: for (Scope.Entry e = members().elems; e != null; e = e.sibling) { duke@1: if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this) duke@1: list = list.prepend(e.sym); duke@1: } duke@1: return list; duke@1: } duke@1: duke@1: // For type params. duke@1: // Perhaps not needed if getEnclosingElement can be spec'ed duke@1: // to do the same thing. duke@1: // TODO: getGenericElement() might not be needed duke@1: public Symbol getGenericElement() { duke@1: return owner; duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { jjg@1374: Assert.check(type.hasTag(TYPEVAR)); // else override will be invoked duke@1: return v.visitTypeParameter(this, p); duke@1: } duke@1: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitTypeSymbol(this, p); mcimadamore@121: } mcimadamore@121: duke@1: public List getBounds() { duke@1: TypeVar t = (TypeVar)type; duke@1: Type bound = t.getUpperBound(); duke@1: if (!bound.isCompound()) duke@1: return List.of(bound); duke@1: ClassType ct = (ClassType)bound; duke@1: if (!ct.tsym.erasure_field.isInterface()) { duke@1: return ct.interfaces_field.prepend(ct.supertype_field); duke@1: } else { duke@1: // No superclass was given in bounds. duke@1: // In this case, supertype is Object, erasure is first interface. duke@1: return ct.interfaces_field; duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** A class for package symbols duke@1: */ duke@1: public static class PackageSymbol extends TypeSymbol duke@1: implements PackageElement { duke@1: duke@1: public Scope members_field; duke@1: public Name fullname; duke@1: public ClassSymbol package_info; // see bug 6443073 duke@1: duke@1: public PackageSymbol(Name name, Type type, Symbol owner) { duke@1: super(0, name, type, owner); duke@1: this.kind = PCK; duke@1: this.members_field = null; duke@1: this.fullname = formFullName(name, owner); duke@1: } duke@1: duke@1: public PackageSymbol(Name name, Symbol owner) { duke@1: this(name, null, owner); duke@1: this.type = new PackageType(this); duke@1: } duke@1: duke@1: public String toString() { duke@1: return fullname.toString(); duke@1: } duke@1: duke@1: public Name getQualifiedName() { duke@1: return fullname; duke@1: } duke@1: duke@1: public boolean isUnnamed() { duke@1: return name.isEmpty() && owner != null; duke@1: } duke@1: duke@1: public Scope members() { duke@1: if (completer != null) complete(); duke@1: return members_field; duke@1: } duke@1: duke@1: public long flags() { duke@1: if (completer != null) complete(); duke@1: return flags_field; duke@1: } duke@1: jfranck@1464: @Override jfranck@1464: public List getRawAttributes() { duke@1: if (completer != null) complete(); jjg@483: if (package_info != null && package_info.completer != null) { jjg@483: package_info.complete(); jfranck@1464: mergeAttributes(); jjg@483: } jfranck@1464: return super.getRawAttributes(); jfranck@1464: } jfranck@1464: jfranck@1464: private void mergeAttributes() { jfranck@1464: if (annotations.isEmpty() && jfranck@1464: !package_info.annotations.isEmpty()) { jfranck@1464: annotations.setAttributes(package_info.annotations); jfranck@1313: } duke@1: } duke@1: duke@1: /** A package "exists" if a type or package that exists has duke@1: * been seen within it. duke@1: */ duke@1: public boolean exists() { duke@1: return (flags_field & EXISTS) != 0; duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: return ElementKind.PACKAGE; duke@1: } duke@1: duke@1: public Symbol getEnclosingElement() { duke@1: return null; duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitPackage(this, p); duke@1: } mcimadamore@121: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitPackageSymbol(this, p); mcimadamore@121: } duke@1: } duke@1: duke@1: /** A class for class symbols duke@1: */ duke@1: public static class ClassSymbol extends TypeSymbol implements TypeElement { duke@1: duke@1: /** a scope for all class members; variables, methods and inner classes duke@1: * type parameters are not part of this scope duke@1: */ duke@1: public Scope members_field; duke@1: duke@1: /** the fully qualified name of the class, i.e. pck.outer.inner. duke@1: * null for anonymous classes duke@1: */ duke@1: public Name fullname; duke@1: duke@1: /** the fully qualified name of the class after converting to flat duke@1: * representation, i.e. pck.outer$inner, duke@1: * set externally for local and anonymous classes duke@1: */ duke@1: public Name flatname; duke@1: duke@1: /** the sourcefile where the class came from duke@1: */ duke@1: public JavaFileObject sourcefile; duke@1: duke@1: /** the classfile from where to load this class duke@1: * this will have extension .class or .java duke@1: */ duke@1: public JavaFileObject classfile; duke@1: mcimadamore@1086: /** the list of translated local classes (used for generating mcimadamore@1086: * InnerClasses attribute) mcimadamore@1086: */ mcimadamore@1086: public List trans_local; mcimadamore@1086: duke@1: /** the constant pool of the class duke@1: */ duke@1: public Pool pool; duke@1: duke@1: public ClassSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(flags, name, type, owner); duke@1: this.members_field = null; duke@1: this.fullname = formFullName(name, owner); duke@1: this.flatname = formFlatName(name, owner); duke@1: this.sourcefile = null; duke@1: this.classfile = null; duke@1: this.pool = null; duke@1: } duke@1: duke@1: public ClassSymbol(long flags, Name name, Symbol owner) { duke@1: this( duke@1: flags, duke@1: name, duke@1: new ClassType(Type.noType, null, null), duke@1: owner); duke@1: this.type.tsym = this; duke@1: } duke@1: duke@1: /** The Java source which this symbol represents. duke@1: */ duke@1: public String toString() { duke@1: return className(); duke@1: } duke@1: duke@1: public long flags() { duke@1: if (completer != null) complete(); duke@1: return flags_field; duke@1: } duke@1: duke@1: public Scope members() { duke@1: if (completer != null) complete(); duke@1: return members_field; duke@1: } duke@1: jfranck@1464: @Override jfranck@1464: public List getRawAttributes() { duke@1: if (completer != null) complete(); jfranck@1464: return super.getRawAttributes(); duke@1: } duke@1: jjg@1521: @Override jjg@1521: public List getRawTypeAttributes() { jjg@1521: if (completer != null) complete(); jjg@1521: return super.getRawTypeAttributes(); jjg@1521: } jjg@1521: duke@1: public Type erasure(Types types) { duke@1: if (erasure_field == null) duke@1: erasure_field = new ClassType(types.erasure(type.getEnclosingType()), duke@1: List.nil(), this); duke@1: return erasure_field; duke@1: } duke@1: duke@1: public String className() { jjg@113: if (name.isEmpty()) duke@1: return duke@1: Log.getLocalizedString("anonymous.class", flatname); duke@1: else duke@1: return fullname.toString(); duke@1: } duke@1: duke@1: public Name getQualifiedName() { duke@1: return fullname; duke@1: } duke@1: duke@1: public Name flatName() { duke@1: return flatname; duke@1: } duke@1: duke@1: public boolean isSubClass(Symbol base, Types types) { duke@1: if (this == base) { duke@1: return true; duke@1: } else if ((base.flags() & INTERFACE) != 0) { jjg@1374: for (Type t = type; t.hasTag(CLASS); t = types.supertype(t)) duke@1: for (List is = types.interfaces(t); duke@1: is.nonEmpty(); duke@1: is = is.tail) duke@1: if (is.head.tsym.isSubClass(base, types)) return true; duke@1: } else { jjg@1374: for (Type t = type; t.hasTag(CLASS); t = types.supertype(t)) duke@1: if (t.tsym == base) return true; duke@1: } duke@1: return false; duke@1: } duke@1: duke@1: /** Complete the elaboration of this symbol's definition. duke@1: */ duke@1: public void complete() throws CompletionFailure { duke@1: try { duke@1: super.complete(); duke@1: } catch (CompletionFailure ex) { duke@1: // quiet error recovery duke@1: flags_field |= (PUBLIC|STATIC); jjg@110: this.type = new ErrorType(this, Type.noType); duke@1: throw ex; duke@1: } duke@1: } duke@1: duke@1: public List getInterfaces() { duke@1: complete(); duke@1: if (type instanceof ClassType) { duke@1: ClassType t = (ClassType)type; duke@1: if (t.interfaces_field == null) // FIXME: shouldn't be null duke@1: t.interfaces_field = List.nil(); jjg@904: if (t.all_interfaces_field != null) jjg@904: return Type.getModelTypes(t.all_interfaces_field); duke@1: return t.interfaces_field; duke@1: } else { duke@1: return List.nil(); duke@1: } duke@1: } duke@1: duke@1: public Type getSuperclass() { duke@1: complete(); duke@1: if (type instanceof ClassType) { duke@1: ClassType t = (ClassType)type; duke@1: if (t.supertype_field == null) // FIXME: shouldn't be null duke@1: t.supertype_field = Type.noType; duke@1: // An interface has no superclass; its supertype is Object. duke@1: return t.isInterface() duke@1: ? Type.noType jjg@904: : t.supertype_field.getModelType(); duke@1: } else { duke@1: return Type.noType; duke@1: } duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: long flags = flags(); duke@1: if ((flags & ANNOTATION) != 0) duke@1: return ElementKind.ANNOTATION_TYPE; duke@1: else if ((flags & INTERFACE) != 0) duke@1: return ElementKind.INTERFACE; duke@1: else if ((flags & ENUM) != 0) duke@1: return ElementKind.ENUM; duke@1: else duke@1: return ElementKind.CLASS; duke@1: } duke@1: duke@1: public NestingKind getNestingKind() { duke@1: complete(); duke@1: if (owner.kind == PCK) duke@1: return NestingKind.TOP_LEVEL; duke@1: else if (name.isEmpty()) duke@1: return NestingKind.ANONYMOUS; duke@1: else if (owner.kind == MTH) duke@1: return NestingKind.LOCAL; duke@1: else duke@1: return NestingKind.MEMBER; duke@1: } duke@1: duke@1: /** duke@1: * @deprecated this method should never be used by javac internally. duke@1: */ duke@1: @Override @Deprecated duke@1: public A getAnnotation(Class annoType) { duke@1: return JavacElements.getAnnotation(this, annoType); duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitType(this, p); duke@1: } mcimadamore@121: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitClassSymbol(this, p); mcimadamore@121: } duke@1: } duke@1: duke@1: duke@1: /** A class for variable symbols duke@1: */ duke@1: public static class VarSymbol extends Symbol implements VariableElement { duke@1: duke@1: /** The variable's declaration position. duke@1: */ duke@1: public int pos = Position.NOPOS; duke@1: duke@1: /** The variable's address. Used for different purposes during duke@1: * flow analysis, translation and code generation. duke@1: * Flow analysis: duke@1: * If this is a blank final or local variable, its sequence number. duke@1: * Translation: duke@1: * If this is a private field, its access number. duke@1: * Code generation: duke@1: * If this is a local variable, its logical slot number. duke@1: */ duke@1: public int adr = -1; duke@1: duke@1: /** Construct a variable symbol, given its flags, name, type and owner. duke@1: */ duke@1: public VarSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(VAR, flags, name, type, owner); duke@1: } duke@1: duke@1: /** Clone this symbol with new owner. duke@1: */ duke@1: public VarSymbol clone(Symbol newOwner) { mcimadamore@1352: VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) { mcimadamore@1352: @Override mcimadamore@1352: public Symbol baseSymbol() { mcimadamore@1352: return VarSymbol.this; mcimadamore@1352: } mcimadamore@1352: }; duke@1: v.pos = pos; duke@1: v.adr = adr; duke@1: v.data = data; duke@1: // System.out.println("clone " + v + " in " + newOwner);//DEBUG duke@1: return v; duke@1: } duke@1: duke@1: public String toString() { duke@1: return name.toString(); duke@1: } duke@1: duke@1: public Symbol asMemberOf(Type site, Types types) { duke@1: return new VarSymbol(flags_field, name, types.memberType(site, this), owner); duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: long flags = flags(); duke@1: if ((flags & PARAMETER) != 0) { duke@1: if (isExceptionParameter()) duke@1: return ElementKind.EXCEPTION_PARAMETER; duke@1: else duke@1: return ElementKind.PARAMETER; duke@1: } else if ((flags & ENUM) != 0) { duke@1: return ElementKind.ENUM_CONSTANT; duke@1: } else if (owner.kind == TYP || owner.kind == ERR) { duke@1: return ElementKind.FIELD; sundar@697: } else if (isResourceVariable()) { sundar@697: return ElementKind.RESOURCE_VARIABLE; duke@1: } else { duke@1: return ElementKind.LOCAL_VARIABLE; duke@1: } duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitVariable(this, p); duke@1: } duke@1: duke@1: public Object getConstantValue() { // Mirror API duke@1: return Constants.decode(getConstValue(), type); duke@1: } duke@1: duke@1: public void setLazyConstValue(final Env env, duke@1: final Attr attr, duke@1: final JCTree.JCExpression initializer) duke@1: { duke@1: setData(new Callable() { duke@1: public Object call() { jjg@841: return attr.attribLazyConstantValue(env, initializer, type); duke@1: } duke@1: }); duke@1: } duke@1: duke@1: /** duke@1: * The variable's constant value, if this is a constant. duke@1: * Before the constant value is evaluated, it points to an duke@1: * initalizer environment. If this is not a constant, it can duke@1: * be used for other stuff. duke@1: */ duke@1: private Object data; duke@1: duke@1: public boolean isExceptionParameter() { duke@1: return data == ElementKind.EXCEPTION_PARAMETER; duke@1: } duke@1: darcy@609: public boolean isResourceVariable() { darcy@609: return data == ElementKind.RESOURCE_VARIABLE; darcy@609: } darcy@609: duke@1: public Object getConstValue() { duke@1: // TODO: Consider if getConstValue and getConstantValue can be collapsed darcy@609: if (data == ElementKind.EXCEPTION_PARAMETER || darcy@609: data == ElementKind.RESOURCE_VARIABLE) { duke@1: return null; duke@1: } else if (data instanceof Callable) { darcy@609: // In this case, this is a final variable, with an as duke@1: // yet unevaluated initializer. duke@1: Callable eval = (Callable)data; duke@1: data = null; // to make sure we don't evaluate this twice. duke@1: try { duke@1: data = eval.call(); duke@1: } catch (Exception ex) { duke@1: throw new AssertionError(ex); duke@1: } duke@1: } duke@1: return data; duke@1: } duke@1: duke@1: public void setData(Object data) { jjg@816: Assert.check(!(data instanceof Env), this); duke@1: this.data = data; duke@1: } mcimadamore@121: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitVarSymbol(this, p); mcimadamore@121: } duke@1: } duke@1: duke@1: /** A class for method symbols. duke@1: */ duke@1: public static class MethodSymbol extends Symbol implements ExecutableElement { duke@1: duke@1: /** The code of the method. */ duke@1: public Code code = null; duke@1: mcimadamore@1565: /** The extra (synthetic/mandated) parameters of the method. */ mcimadamore@1565: public List extraParams = List.nil(); mcimadamore@1565: duke@1: /** The parameters of the method. */ duke@1: public List params = null; duke@1: duke@1: /** The names of the parameters */ duke@1: public List savedParameterNames; duke@1: duke@1: /** For an attribute field accessor, its default value if any. duke@1: * The value is null if none appeared in the method duke@1: * declaration. duke@1: */ duke@1: public Attribute defaultValue = null; duke@1: duke@1: /** Construct a method symbol, given its flags, name, type and owner. duke@1: */ duke@1: public MethodSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(MTH, flags, name, type, owner); jjg@1374: if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name); duke@1: } duke@1: duke@1: /** Clone this symbol with new owner. duke@1: */ duke@1: public MethodSymbol clone(Symbol newOwner) { mcimadamore@1352: MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner) { mcimadamore@1352: @Override mcimadamore@1352: public Symbol baseSymbol() { mcimadamore@1352: return MethodSymbol.this; mcimadamore@1352: } mcimadamore@1352: }; duke@1: m.code = code; duke@1: return m; duke@1: } duke@1: duke@1: /** The Java source which this symbol represents. duke@1: */ duke@1: public String toString() { duke@1: if ((flags() & BLOCK) != 0) { duke@1: return owner.name.toString(); duke@1: } else { jjg@113: String s = (name == name.table.names.init) duke@1: ? owner.name.toString() duke@1: : name.toString(); duke@1: if (type != null) { jjg@1374: if (type.hasTag(FORALL)) duke@1: s = "<" + ((ForAll)type).getTypeArguments() + ">" + s; duke@1: s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")"; duke@1: } duke@1: return s; duke@1: } duke@1: } duke@1: mcimadamore@1336: public boolean isDynamic() { mcimadamore@1336: return false; mcimadamore@1336: } mcimadamore@1336: duke@1: /** find a symbol that this (proxy method) symbol implements. duke@1: * @param c The class whose members are searched for duke@1: * implementations duke@1: */ duke@1: public Symbol implemented(TypeSymbol c, Types types) { duke@1: Symbol impl = null; duke@1: for (List is = types.interfaces(c.type); duke@1: impl == null && is.nonEmpty(); duke@1: is = is.tail) { duke@1: TypeSymbol i = is.head.tsym; mcimadamore@780: impl = implementedIn(i, types); mcimadamore@780: if (impl == null) mcimadamore@780: impl = implemented(i, types); mcimadamore@780: } mcimadamore@780: return impl; mcimadamore@780: } mcimadamore@780: mcimadamore@780: public Symbol implementedIn(TypeSymbol c, Types types) { mcimadamore@780: Symbol impl = null; mcimadamore@780: for (Scope.Entry e = c.members().lookup(name); mcimadamore@780: impl == null && e.scope != null; mcimadamore@780: e = e.next()) { mcimadamore@780: if (this.overrides(e.sym, (TypeSymbol)owner, types, true) && mcimadamore@780: // FIXME: I suspect the following requires a mcimadamore@780: // subst() for a parametric return type. mcimadamore@780: types.isSameType(type.getReturnType(), mcimadamore@780: types.memberType(owner.type, e.sym).getReturnType())) { mcimadamore@780: impl = e.sym; duke@1: } duke@1: } duke@1: return impl; duke@1: } duke@1: duke@1: /** Will the erasure of this method be considered by the VM to duke@1: * override the erasure of the other when seen from class `origin'? duke@1: */ duke@1: public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) { duke@1: if (isConstructor() || _other.kind != MTH) return false; duke@1: duke@1: if (this == _other) return true; duke@1: MethodSymbol other = (MethodSymbol)_other; duke@1: duke@1: // check for a direct implementation duke@1: if (other.isOverridableIn((TypeSymbol)owner) && duke@1: types.asSuper(owner.type, other.owner) != null && duke@1: types.isSameType(erasure(types), other.erasure(types))) duke@1: return true; duke@1: duke@1: // check for an inherited implementation duke@1: return duke@1: (flags() & ABSTRACT) == 0 && duke@1: other.isOverridableIn(origin) && duke@1: this.isMemberOf(origin, types) && duke@1: types.isSameType(erasure(types), other.erasure(types)); duke@1: } duke@1: duke@1: /** The implementation of this (abstract) symbol in class origin, duke@1: * from the VM's point of view, null if method does not have an duke@1: * implementation in class. duke@1: * @param origin The class of which the implementation is a member. duke@1: */ duke@1: public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) { duke@1: for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) { duke@1: for (Scope.Entry e = c.members().lookup(name); duke@1: e.scope != null; duke@1: e = e.next()) { duke@1: if (e.sym.kind == MTH && duke@1: ((MethodSymbol)e.sym).binaryOverrides(this, origin, types)) duke@1: return (MethodSymbol)e.sym; duke@1: } duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** Does this symbol override `other' symbol, when both are seen as duke@1: * members of class `origin'? It is assumed that _other is a member duke@1: * of origin. duke@1: * duke@1: * It is assumed that both symbols have the same name. The static duke@1: * modifier is ignored for this test. duke@1: * duke@1: * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4 duke@1: */ duke@1: public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) { duke@1: if (isConstructor() || _other.kind != MTH) return false; duke@1: duke@1: if (this == _other) return true; duke@1: MethodSymbol other = (MethodSymbol)_other; duke@1: duke@1: // check for a direct implementation duke@1: if (other.isOverridableIn((TypeSymbol)owner) && duke@1: types.asSuper(owner.type, other.owner) != null) { duke@1: Type mt = types.memberType(owner.type, this); duke@1: Type ot = types.memberType(owner.type, other); duke@1: if (types.isSubSignature(mt, ot)) { duke@1: if (!checkResult) duke@1: return true; duke@1: if (types.returnTypeSubstitutable(mt, ot)) duke@1: return true; duke@1: } duke@1: } duke@1: duke@1: // check for an inherited implementation duke@1: if ((flags() & ABSTRACT) != 0 || mcimadamore@1415: ((other.flags() & ABSTRACT) == 0 && (other.flags() & DEFAULT) == 0) || mcimadamore@1393: !other.isOverridableIn(origin) || mcimadamore@1393: !this.isMemberOf(origin, types)) duke@1: return false; duke@1: duke@1: // assert types.asSuper(origin.type, other.owner) != null; duke@1: Type mt = types.memberType(origin.type, this); duke@1: Type ot = types.memberType(origin.type, other); duke@1: return duke@1: types.isSubSignature(mt, ot) && mcimadamore@1415: (!checkResult || types.resultSubtype(mt, ot, types.noWarnings)); duke@1: } duke@1: duke@1: private boolean isOverridableIn(TypeSymbol origin) { jjh@972: // JLS 8.4.6.1 duke@1: switch ((int)(flags_field & Flags.AccessFlags)) { duke@1: case Flags.PRIVATE: duke@1: return false; duke@1: case Flags.PUBLIC: mcimadamore@1513: return !this.owner.isInterface() || mcimadamore@1513: (flags_field & STATIC) == 0; duke@1: case Flags.PROTECTED: duke@1: return (origin.flags() & INTERFACE) == 0; duke@1: case 0: duke@1: // for package private: can only override in the same duke@1: // package duke@1: return duke@1: this.packge() == origin.packge() && duke@1: (origin.flags() & INTERFACE) == 0; duke@1: default: duke@1: return false; duke@1: } duke@1: } duke@1: mcimadamore@1513: @Override mcimadamore@1513: public boolean isInheritedIn(Symbol clazz, Types types) { mcimadamore@1513: switch ((int)(flags_field & Flags.AccessFlags)) { mcimadamore@1513: case PUBLIC: mcimadamore@1513: return !this.owner.isInterface() || mcimadamore@1513: clazz == owner || mcimadamore@1513: (flags_field & STATIC) == 0; mcimadamore@1513: default: mcimadamore@1513: return super.isInheritedIn(clazz, types); mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: duke@1: /** The implementation of this (abstract) symbol in class origin; duke@1: * null if none exists. Synthetic methods are not considered duke@1: * as possible implementations. duke@1: */ duke@1: public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) { mcimadamore@673: return implementation(origin, types, checkResult, implementation_filter); mcimadamore@673: } mcimadamore@673: // where mcimadamore@673: private static final Filter implementation_filter = new Filter() { mcimadamore@673: public boolean accepts(Symbol s) { mcimadamore@673: return s.kind == Kinds.MTH && mcimadamore@673: (s.flags() & SYNTHETIC) == 0; mcimadamore@673: } mcimadamore@673: }; mcimadamore@673: mcimadamore@673: public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter implFilter) { mcimadamore@858: MethodSymbol res = types.implementation(this, origin, checkResult, implFilter); mcimadamore@341: if (res != null) mcimadamore@341: return res; duke@1: // if origin is derived from a raw type, we might have missed duke@1: // an implementation because we do not know enough about instantiations. duke@1: // in this case continue with the supertype as origin. mcimadamore@1222: if (types.isDerivedRaw(origin.type) && !origin.isInterface()) duke@1: return implementation(types.supertype(origin.type).tsym, types, checkResult); duke@1: else duke@1: return null; duke@1: } duke@1: duke@1: public List params() { duke@1: owner.complete(); duke@1: if (params == null) { jjg@428: // If ClassReader.saveParameterNames has been set true, then jjg@428: // savedParameterNames will be set to a list of names that jjg@428: // matches the types in type.getParameterTypes(). If any names jjg@428: // were not found in the class file, those names in the list will jjg@428: // be set to the empty name. jjg@428: // If ClassReader.saveParameterNames has been set false, then jjg@428: // savedParameterNames will be null. jjg@428: List paramNames = savedParameterNames; duke@1: savedParameterNames = null; jjg@428: // discard the provided names if the list of names is the wrong size. jjg@1473: if (paramNames == null || paramNames.size() != type.getParameterTypes().size()) { jjg@428: paramNames = List.nil(); jjg@1473: } duke@1: ListBuffer buf = new ListBuffer(); jjg@428: List remaining = paramNames; jjg@428: // assert: remaining and paramNames are both empty or both jjg@428: // have same cardinality as type.getParameterTypes() jjg@428: int i = 0; duke@1: for (Type t : type.getParameterTypes()) { jjg@428: Name paramName; jjg@428: if (remaining.isEmpty()) { jjg@428: // no names for any parameters available jjg@428: paramName = createArgName(i, paramNames); jjg@428: } else { jjg@428: paramName = remaining.head; jjg@428: remaining = remaining.tail; jjg@428: if (paramName.isEmpty()) { jjg@428: // no name for this specific parameter jjg@428: paramName = createArgName(i, paramNames); jjg@428: } jjg@428: } jjg@428: buf.append(new VarSymbol(PARAMETER, paramName, t, this)); jjg@428: i++; duke@1: } duke@1: params = buf.toList(); duke@1: } duke@1: return params; duke@1: } duke@1: jjg@428: // Create a name for the argument at position 'index' that is not in jjg@428: // the exclude list. In normal use, either no names will have been jjg@428: // provided, in which case the exclude list is empty, or all the names jjg@428: // will have been provided, in which case this method will not be called. jjg@428: private Name createArgName(int index, List exclude) { jjg@428: String prefix = "arg"; jjg@428: while (true) { jjg@428: Name argName = name.table.fromString(prefix + index); jjg@428: if (!exclude.contains(argName)) jjg@428: return argName; jjg@428: prefix += "$"; jjg@428: } jjg@428: } jjg@428: duke@1: public Symbol asMemberOf(Type site, Types types) { duke@1: return new MethodSymbol(flags_field, name, types.memberType(site, this), owner); duke@1: } duke@1: duke@1: public ElementKind getKind() { jjg@113: if (name == name.table.names.init) duke@1: return ElementKind.CONSTRUCTOR; jjg@113: else if (name == name.table.names.clinit) duke@1: return ElementKind.STATIC_INIT; mcimadamore@1085: else if ((flags() & BLOCK) != 0) mcimadamore@1085: return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT; duke@1: else duke@1: return ElementKind.METHOD; duke@1: } duke@1: mcimadamore@1085: public boolean isStaticOrInstanceInit() { mcimadamore@1085: return getKind() == ElementKind.STATIC_INIT || mcimadamore@1085: getKind() == ElementKind.INSTANCE_INIT; mcimadamore@1085: } mcimadamore@1085: mcimadamore@1239: /** mcimadamore@1239: * A polymorphic signature method (JLS SE 7, 8.4.1) is a method that mcimadamore@1239: * (i) is declared in the java.lang.invoke.MethodHandle class, (ii) takes mcimadamore@1239: * a single variable arity parameter (iii) whose declared type is Object[], mcimadamore@1239: * (iv) has a return type of Object and (v) is native. mcimadamore@1239: */ mcimadamore@1239: public boolean isSignaturePolymorphic(Types types) { mcimadamore@1239: List argtypes = type.getParameterTypes(); mcimadamore@1239: Type firstElemType = argtypes.nonEmpty() ? mcimadamore@1239: types.elemtype(argtypes.head) : mcimadamore@1239: null; mcimadamore@1239: return owner == types.syms.methodHandleType.tsym && mcimadamore@1239: argtypes.length() == 1 && mcimadamore@1239: firstElemType != null && mcimadamore@1239: types.isSameType(firstElemType, types.syms.objectType) && mcimadamore@1239: types.isSameType(type.getReturnType(), types.syms.objectType) && mcimadamore@1239: (flags() & NATIVE) != 0; mcimadamore@1239: } mcimadamore@1239: duke@1: public Attribute getDefaultValue() { duke@1: return defaultValue; duke@1: } duke@1: jjg@1521: public List getParameters() { duke@1: return params(); duke@1: } duke@1: duke@1: public boolean isVarArgs() { duke@1: return (flags() & VARARGS) != 0; duke@1: } duke@1: darcy@1459: public boolean isDefault() { darcy@1459: return (flags() & DEFAULT) != 0; darcy@1459: } darcy@1459: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitExecutable(this, p); duke@1: } duke@1: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitMethodSymbol(this, p); mcimadamore@121: } mcimadamore@121: duke@1: public Type getReturnType() { duke@1: return asType().getReturnType(); duke@1: } duke@1: duke@1: public List getThrownTypes() { duke@1: return asType().getThrownTypes(); duke@1: } duke@1: } duke@1: mcimadamore@1336: /** A class for invokedynamic method calls. mcimadamore@1336: */ mcimadamore@1336: public static class DynamicMethodSymbol extends MethodSymbol { mcimadamore@1336: mcimadamore@1336: public Object[] staticArgs; mcimadamore@1336: public Symbol bsm; mcimadamore@1336: public int bsmKind; mcimadamore@1336: mcimadamore@1336: public DynamicMethodSymbol(Name name, Symbol owner, int bsmKind, MethodSymbol bsm, Type type, Object[] staticArgs) { mcimadamore@1336: super(0, name, type, owner); mcimadamore@1336: this.bsm = bsm; mcimadamore@1336: this.bsmKind = bsmKind; mcimadamore@1336: this.staticArgs = staticArgs; mcimadamore@1336: } mcimadamore@1336: mcimadamore@1336: @Override mcimadamore@1336: public boolean isDynamic() { mcimadamore@1336: return true; mcimadamore@1336: } mcimadamore@1336: } mcimadamore@1336: duke@1: /** A class for predefined operators. duke@1: */ duke@1: public static class OperatorSymbol extends MethodSymbol { duke@1: duke@1: public int opcode; duke@1: duke@1: public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) { duke@1: super(PUBLIC | STATIC, name, type, owner); duke@1: this.opcode = opcode; duke@1: } mcimadamore@121: mcimadamore@121: public R accept(Symbol.Visitor v, P p) { mcimadamore@121: return v.visitOperatorSymbol(this, p); mcimadamore@121: } duke@1: } duke@1: duke@1: /** Symbol completer interface. duke@1: */ duke@1: public static interface Completer { duke@1: void complete(Symbol sym) throws CompletionFailure; duke@1: } duke@1: duke@1: public static class CompletionFailure extends RuntimeException { duke@1: private static final long serialVersionUID = 0; duke@1: public Symbol sym; duke@1: jjg@12: /** A diagnostic object describing the failure jjg@12: */ jjg@12: public JCDiagnostic diag; jjg@12: duke@1: /** A localized string describing the failure. jjg@12: * @deprecated Use {@code getDetail()} or {@code getMessage()} duke@1: */ jjg@12: @Deprecated duke@1: public String errmsg; duke@1: duke@1: public CompletionFailure(Symbol sym, String errmsg) { duke@1: this.sym = sym; duke@1: this.errmsg = errmsg; duke@1: // this.printStackTrace();//DEBUG duke@1: } duke@1: jjg@12: public CompletionFailure(Symbol sym, JCDiagnostic diag) { jjg@12: this.sym = sym; jjg@12: this.diag = diag; jjg@12: // this.printStackTrace();//DEBUG jjg@12: } jjg@12: jjg@12: public JCDiagnostic getDiagnostic() { jjg@12: return diag; jjg@12: } jjg@12: jjg@12: @Override duke@1: public String getMessage() { jjg@12: if (diag != null) jjg@12: return diag.getMessage(null); jjg@12: else jjg@12: return errmsg; jjg@12: } jjg@12: jjg@12: public Object getDetailValue() { jjg@12: return (diag != null ? diag : errmsg); duke@1: } duke@1: duke@1: @Override duke@1: public CompletionFailure initCause(Throwable cause) { duke@1: super.initCause(cause); duke@1: return this; duke@1: } duke@1: duke@1: } mcimadamore@121: mcimadamore@121: /** mcimadamore@121: * A visitor for symbols. A visitor is used to implement operations mcimadamore@121: * (or relations) on symbols. Most common operations on types are mcimadamore@121: * binary relations and this interface is designed for binary mcimadamore@121: * relations, that is, operations on the form mcimadamore@121: * Symbol × P → R. mcimadamore@121: * mcimadamore@121: * mcimadamore@121: * @param the return type of the operation implemented by this mcimadamore@121: * visitor; use Void if no return type is needed. mcimadamore@121: * @param

the type of the second argument (the first being the mcimadamore@121: * symbol itself) of the operation implemented by this visitor; use mcimadamore@121: * Void if a second argument is not needed. mcimadamore@121: */ mcimadamore@121: public interface Visitor { mcimadamore@121: R visitClassSymbol(ClassSymbol s, P arg); mcimadamore@121: R visitMethodSymbol(MethodSymbol s, P arg); mcimadamore@121: R visitPackageSymbol(PackageSymbol s, P arg); mcimadamore@121: R visitOperatorSymbol(OperatorSymbol s, P arg); mcimadamore@121: R visitVarSymbol(VarSymbol s, P arg); mcimadamore@121: R visitTypeSymbol(TypeSymbol s, P arg); mcimadamore@121: R visitSymbol(Symbol s, P arg); mcimadamore@121: } duke@1: }