symbol.java
来自「是一款用JAVA 编写的编译器 具有很强的编译功能」· Java 代码 · 共 1,297 行 · 第 1/3 页
JAVA
1,297 行
public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) { return JavacElements.getAnnotation(this, annoType); } // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList public java.util.List<Symbol> getEnclosedElements() { return List.nil(); } public List<TypeSymbol> getTypeParameters() { ListBuffer<TypeSymbol> l = ListBuffer.lb(); for (Type t : type.getTypeArguments()) { l.append(t.tsym); } return l.toList(); } public static class DelegatedSymbol extends Symbol { protected Symbol other; public DelegatedSymbol(Symbol other) { super(other.kind, other.flags_field, other.name, other.type, other.owner); this.other = other; } public String toString() { return other.toString(); } public String location() { return other.location(); } public String location(Type site, Types types) { return other.location(site, types); } public Type erasure(Types types) { return other.erasure(types); } public Type externalType(Types types) { return other.externalType(types); } public boolean isLocal() { return other.isLocal(); } public boolean isConstructor() { return other.isConstructor(); } public Name getQualifiedName() { return other.getQualifiedName(); } public Name flatName() { return other.flatName(); } public Scope members() { return other.members(); } public boolean isInner() { return other.isInner(); } public boolean hasOuterInstance() { return other.hasOuterInstance(); } public ClassSymbol enclClass() { return other.enclClass(); } public ClassSymbol outermostClass() { return other.outermostClass(); } public PackageSymbol packge() { return other.packge(); } public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); } public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); } public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); } public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); } public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); } public void complete() throws CompletionFailure { other.complete(); } public <R, P> R accept(ElementVisitor<R, P> v, P p) { return other.accept(v, p); } } /** A class for type symbols. Type variables are represented by instances * of this class, classes and packages by instances of subclasses. */ public static class TypeSymbol extends Symbol implements TypeParameterElement { // Implements TypeParameterElement because type parameters don't // have their own TypeSymbol subclass. // TODO: type parameters should have their own TypeSymbol subclass public TypeSymbol(long flags, Name name, Type type, Symbol owner) { super(TYP, flags, name, type, owner); } /** form a fully qualified name from a name and an owner */ static public Name formFullName(Name name, Symbol owner) { if (owner == null) return name; if (((owner.kind != ERR)) && ((owner.kind & (VAR | MTH)) != 0 || (owner.kind == TYP && owner.type.tag == TYPEVAR) )) return name; Name prefix = owner.getQualifiedName(); if (prefix == null || prefix == prefix.table.empty) return name; else return prefix.append('.', name); } /** form a fully qualified name from a name and an owner, after * converting to flat representation */ static public Name formFlatName(Name name, Symbol owner) { if (owner == null || (owner.kind & (VAR | MTH)) != 0 || (owner.kind == TYP && owner.type.tag == TYPEVAR) ) return name; char sep = owner.kind == TYP ? '$' : '.'; Name prefix = owner.flatName(); if (prefix == null || prefix == prefix.table.empty) return name; else return prefix.append(sep, name); } /** * A total ordering between type symbols that refines the * class inheritance graph. * * Typevariables always precede other kinds of symbols. */ public final boolean precedes(TypeSymbol that, Types types) { if (this == that) return false; if (this.type.tag == that.type.tag) { if (this.type.tag == CLASS) { return types.rank(that.type) < types.rank(this.type) || types.rank(that.type) == types.rank(this.type) && that.getQualifiedName().compareTo(this.getQualifiedName()) < 0; } else if (this.type.tag == TYPEVAR) { return types.isSubtype(this.type, that.type); } } return this.type.tag == TYPEVAR; } // For type params; overridden in subclasses. public ElementKind getKind() { return ElementKind.TYPE_PARAMETER; } public java.util.List<Symbol> getEnclosedElements() { List<Symbol> list = List.nil(); for (Scope.Entry e = members().elems; e != null; e = e.sibling) { if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this) list = list.prepend(e.sym); } return list; } // For type params. // Perhaps not needed if getEnclosingElement can be spec'ed // to do the same thing. // TODO: getGenericElement() might not be needed public Symbol getGenericElement() { return owner; } public <R, P> R accept(ElementVisitor<R, P> v, P p) { assert type.tag == TYPEVAR; // else override will be invoked return v.visitTypeParameter(this, p); } public List<Type> getBounds() { TypeVar t = (TypeVar)type; Type bound = t.getUpperBound(); if (!bound.isCompound()) return List.of(bound); ClassType ct = (ClassType)bound; if (!ct.tsym.erasure_field.isInterface()) { return ct.interfaces_field.prepend(ct.supertype_field); } else { // No superclass was given in bounds. // In this case, supertype is Object, erasure is first interface. return ct.interfaces_field; } } } /** A class for package symbols */ public static class PackageSymbol extends TypeSymbol implements PackageElement { public Scope members_field; public Name fullname; public ClassSymbol package_info; // see bug 6443073 public PackageSymbol(Name name, Type type, Symbol owner) { super(0, name, type, owner); this.kind = PCK; this.members_field = null; this.fullname = formFullName(name, owner); } public PackageSymbol(Name name, Symbol owner) { this(name, null, owner); this.type = new PackageType(this); } public String toString() { return fullname.toString(); } public Name getQualifiedName() { return fullname; } public boolean isUnnamed() { return name.isEmpty() && owner != null; } public Scope members() { if (completer != null) complete(); return members_field; } public long flags() { if (completer != null) complete(); return flags_field; } public List<Attribute.Compound> getAnnotationMirrors() { if (completer != null) complete(); assert attributes_field != null; return attributes_field; } /** A package "exists" if a type or package that exists has * been seen within it. */ public boolean exists() { return (flags_field & EXISTS) != 0; } public ElementKind getKind() { return ElementKind.PACKAGE; } public Symbol getEnclosingElement() { return null; } public <R, P> R accept(ElementVisitor<R, P> v, P p) { return v.visitPackage(this, p); } } /** A class for class symbols */ public static class ClassSymbol extends TypeSymbol implements TypeElement { /** a scope for all class members; variables, methods and inner classes * type parameters are not part of this scope */ public Scope members_field; /** the fully qualified name of the class, i.e. pck.outer.inner. * null for anonymous classes */ public Name fullname; /** the fully qualified name of the class after converting to flat * representation, i.e. pck.outer$inner, * set externally for local and anonymous classes */ public Name flatname; /** the sourcefile where the class came from */ public JavaFileObject sourcefile; /** the classfile from where to load this class * this will have extension .class or .java */ public JavaFileObject classfile; /** the constant pool of the class */ public Pool pool; public ClassSymbol(long flags, Name name, Type type, Symbol owner) { super(flags, name, type, owner); this.members_field = null; this.fullname = formFullName(name, owner); this.flatname = formFlatName(name, owner); this.sourcefile = null; this.classfile = null; this.pool = null; } public ClassSymbol(long flags, Name name, Symbol owner) { this( flags, name, new ClassType(Type.noType, null, null), owner); this.type.tsym = this; } /** The Java source which this symbol represents. */ public String toString() { return className(); } public long flags() { if (completer != null) complete(); return flags_field; } public Scope members() { if (completer != null) complete(); return members_field; } public List<Attribute.Compound> getAnnotationMirrors() { if (completer != null) complete(); assert attributes_field != null; return attributes_field; } public Type erasure(Types types) { if (erasure_field == null) erasure_field = new ClassType(types.erasure(type.getEnclosingType()), List.<Type>nil(), this); return erasure_field; } public String className() { if (name.len == 0) return Log.getLocalizedString("anonymous.class", flatname); else return fullname.toString(); } public Name getQualifiedName() { return fullname; } public Name flatName() { return flatname; } public boolean isSubClass(Symbol base, Types types) { if (this == base) { return true; } else if ((base.flags() & INTERFACE) != 0) { for (Type t = type; t.tag == CLASS; t = types.supertype(t)) for (List<Type> is = types.interfaces(t); is.nonEmpty(); is = is.tail) if (is.head.tsym.isSubClass(base, types)) return true; } else { for (Type t = type; t.tag == CLASS; t = types.supertype(t)) if (t.tsym == base) return true; } return false; } /** Complete the elaboration of this symbol's definition. */ public void complete() throws CompletionFailure { try { super.complete(); } catch (CompletionFailure ex) { // quiet error recovery flags_field |= (PUBLIC|STATIC); this.type = new ErrorType(this); throw ex; } } public List<Type> getInterfaces() { complete(); if (type instanceof ClassType) { ClassType t = (ClassType)type; if (t.interfaces_field == null) // FIXME: shouldn't be null t.interfaces_field = List.nil(); return t.interfaces_field; } else { return List.nil(); } } public Type getSuperclass() { complete(); if (type instanceof ClassType) { ClassType t = (ClassType)type; if (t.supertype_field == null) // FIXME: shouldn't be null t.supertype_field = Type.noType; // An interface has no superclass; its supertype is Object. return t.isInterface() ? Type.noType : t.supertype_field; } else { return Type.noType; } } public ElementKind getKind() { long flags = flags(); if ((flags & ANNOTATION) != 0) return ElementKind.ANNOTATION_TYPE; else if ((flags & INTERFACE) != 0) return ElementKind.INTERFACE; else if ((flags & ENUM) != 0) return ElementKind.ENUM; else return ElementKind.CLASS; } public NestingKind getNestingKind() { complete(); if (owner.kind == PCK) return NestingKind.TOP_LEVEL; else if (name.isEmpty()) return NestingKind.ANONYMOUS; else if (owner.kind == MTH) return NestingKind.LOCAL; else return NestingKind.MEMBER; } /** * @deprecated this method should never be used by javac internally. */ @Override @Deprecated public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) { return JavacElements.getAnnotation(this, annoType); } public <R, P> R accept(ElementVisitor<R, P> v, P p) { return v.visitType(this, p); } } /** A class for variable symbols */ public static class VarSymbol extends Symbol implements VariableElement { /** The variable's declaration position. */ public int pos = Position.NOPOS; /** The variable's address. Used for different purposes during * flow analysis, translation and code generation. * Flow analysis: * If this is a blank final or local variable, its sequence number. * Translation: * If this is a private field, its access number. * Code generation: * If this is a local variable, its logical slot number.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?