package com.sun.tools.javac.code;
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ElementVisitor;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.Kinds.Kind;
import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.comp.Attr;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.jvm.*;
import com.sun.tools.javac.jvm.PoolConstant;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.JCTree.Tag;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Kinds.Kind.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import com.sun.tools.javac.code.Scope.WriteableScope;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.code.TypeTag.FORALL;
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
import static com.sun.tools.javac.jvm.ByteCodes.iadd;
import static com.sun.tools.javac.jvm.ByteCodes.ishll;
import static com.sun.tools.javac.jvm.ByteCodes.lushrl;
import static com.sun.tools.javac.jvm.ByteCodes.lxor;
import static com.sun.tools.javac.jvm.ByteCodes.string_add;
public abstract class Symbol extends AnnoConstruct implements PoolConstant, Element {
public Kind kind;
public long flags_field;
public long flags() { return flags_field; }
public Name name;
public Type type;
public Symbol owner;
public Completer completer;
public Type erasure_field;
protected SymbolMetadata metadata;
public List<Attribute.Compound> getRawAttributes() {
return (metadata == null)
? List.nil()
: metadata.getDeclarationAttributes();
}
public List<Attribute.TypeCompound> getRawTypeAttributes() {
return (metadata == null)
? List.nil()
: metadata.getTypeAttributes();
}
public Attribute.Compound attribute(Symbol anno) {
for (Attribute.Compound a : getRawAttributes()) {
if (a.type.tsym == anno) return a;
}
return null;
}
public boolean annotationsPendingCompletion() {
return metadata == null ? false : metadata.pendingCompletion();
}
public void appendAttributes(List<Attribute.Compound> l) {
if (l.nonEmpty()) {
initedMetadata().append(l);
}
}
public void appendClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
if (l.nonEmpty()) {
initedMetadata().appendClassInitTypeAttributes(l);
}
}
public void appendInitTypeAttributes(List<Attribute.TypeCompound> l) {
if (l.nonEmpty()) {
initedMetadata().appendInitTypeAttributes(l);
}
}
public void appendUniqueTypeAttributes(List<Attribute.TypeCompound> l) {
if (l.nonEmpty()) {
initedMetadata().appendUniqueTypes(l);
}
}
public List<Attribute.TypeCompound> getClassInitTypeAttributes() {
return (metadata == null)
? List.nil()
: metadata.getClassInitTypeAttributes();
}
public List<Attribute.TypeCompound> getInitTypeAttributes() {
return (metadata == null)
? List.nil()
: metadata.getInitTypeAttributes();
}
public void setInitTypeAttributes(List<Attribute.TypeCompound> l) {
initedMetadata().setInitTypeAttributes(l);
}
public void setClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
initedMetadata().setClassInitTypeAttributes(l);
}
public List<Attribute.Compound> getDeclarationAttributes() {
return (metadata == null)
? List.nil()
: metadata.getDeclarationAttributes();
}
public boolean hasAnnotations() {
return (metadata != null && !metadata.isEmpty());
}
public boolean hasTypeAnnotations() {
return (metadata != null && !metadata.isTypesEmpty());
}
public boolean isCompleted() {
return completer.isTerminal();
}
public void prependAttributes(List<Attribute.Compound> l) {
if (l.nonEmpty()) {
initedMetadata().prepend(l);
}
}
public void resetAnnotations() {
initedMetadata().reset();
}
public void setAttributes(Symbol other) {
if (metadata != null || other.metadata != null) {
initedMetadata().setAttributes(other.metadata);
}
}
public void setDeclarationAttributes(List<Attribute.Compound> a) {
if (metadata != null || a.nonEmpty()) {
initedMetadata().setDeclarationAttributes(a);
}
}
public void setTypeAttributes(List<Attribute.TypeCompound> a) {
if (metadata != null || a.nonEmpty()) {
if (metadata == null)
metadata = new SymbolMetadata(this);
metadata.setTypeAttributes(a);
}
}
private SymbolMetadata initedMetadata() {
if (metadata == null)
metadata = new SymbolMetadata(this);
return metadata;
}
public SymbolMetadata getMetadata() {
return metadata;
}
public Symbol(Kind kind, long flags, Name name, Type type, Symbol owner) {
this.kind = kind;
this.flags_field = flags;
this.type = type;
this.owner = owner;
this.completer = Completer.NULL_COMPLETER;
this.erasure_field = null;
this.name = name;
}
@Override
public int poolTag() {
throw new AssertionError("Invalid pool entry");
}
public Symbol clone(Symbol newOwner) {
throw new AssertionError();
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitSymbol(this, p);
}
public String toString() {
return name.toString();
}
public Symbol location() {
if (owner.name == null || (owner.name.isEmpty() &&
(owner.flags() & BLOCK) == 0 &&
owner.kind != PCK &&
owner.kind != TYP)) {
return null;
}
return owner;
}
public Symbol location(Type site, Types types) {
if (owner.name == null || owner.name.isEmpty()) {
return location();
}
if (owner.type.hasTag(CLASS)) {
Type ownertype = types.asOuterSuper(site, owner);
if (ownertype != null) return ownertype.tsym;
}
return owner;
}
public Symbol baseSymbol() {
return this;
}
public Type erasure(Types types) {
if (erasure_field == null)
erasure_field = types.erasure(type);
return erasure_field;
}
public Type externalType(Types types) {
Type t = erasure(types);
if (name == name.table.names.init && owner.hasOuterInstance()) {
Type outerThisType = types.erasure(owner.type.getEnclosingType());
return new MethodType(t.getParameterTypes().prepend(outerThisType),
t.getReturnType(),
t.getThrownTypes(),
t.tsym);
} else {
return t;
}
}
public boolean isDeprecated() {
return (flags_field & DEPRECATED) != 0;
}
public boolean hasDeprecatedAnnotation() {
return (flags_field & DEPRECATED_ANNOTATION) != 0;
}
public boolean isDeprecatedForRemoval() {
return (flags_field & DEPRECATED_REMOVAL) != 0;
}
public boolean isPreviewApi() {
return (flags_field & PREVIEW_API) != 0;
}
public boolean isDeprecatableViaAnnotation() {
switch (getKind()) {
case LOCAL_VARIABLE:
case PACKAGE:
case PARAMETER:
case RESOURCE_VARIABLE:
case EXCEPTION_PARAMETER:
return false;
default:
return true;
}
}
public boolean isStatic() {
return
(flags() & STATIC) != 0 ||
(owner.flags() & INTERFACE) != 0 && kind != MTH &&
name != name.table.names._this;
}
public boolean isInterface() {
return (flags() & INTERFACE) != 0;
}
public boolean isAbstract() {
return (flags_field & ABSTRACT) != 0;
}
public boolean isPrivate() {
return (flags_field & Flags.AccessFlags) == PRIVATE;
}
public boolean isPublic() {
return (flags_field & Flags.AccessFlags) == PUBLIC;
}
public boolean isEnum() {
return (flags() & ENUM) != 0;
}
public boolean isSealed() {
return (flags_field & SEALED) != 0;
}
public boolean isNonSealed() {
return (flags_field & NON_SEALED) != 0;
}
public boolean isFinal() {
return (flags_field & FINAL) != 0;
}
public boolean isDirectlyOrIndirectlyLocal() {
return
(owner.kind.matches(KindSelector.VAL_MTH) ||
(owner.kind == TYP && owner.isDirectlyOrIndirectlyLocal()));
}
public boolean isAnonymous() {
return name.isEmpty();
}
public boolean isConstructor() {
return name == name.table.names.init;
}
public boolean isDynamic() {
return false;
}
public Name getQualifiedName() {
return name;
}
public Name flatName() {
return getQualifiedName();
}
public WriteableScope members() {
return null;
}
public boolean isInner() {
return kind == TYP && type.getEnclosingType().hasTag(CLASS);
}
public boolean hasOuterInstance() {
return
type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
}
public ClassSymbol enclClass() {
Symbol c = this;
while (c != null &&
(!c.kind.matches(KindSelector.TYP) || !c.type.hasTag(CLASS))) {
c = c.owner;
}
return (ClassSymbol)c;
}
public ClassSymbol outermostClass() {
Symbol sym = this;
Symbol prev = null;
while (sym.kind != PCK) {
prev = sym;
sym = sym.owner;
}
return (ClassSymbol) prev;
}
public PackageSymbol packge() {
Symbol sym = this;
while (sym.kind != PCK) {
sym = sym.owner;
}
return (PackageSymbol) sym;
}
public boolean isSubClass(Symbol base, Types types) {
throw new AssertionError("isSubClass " + this);
}
public boolean isMemberOf(TypeSymbol clazz, Types types) {
return
owner == clazz ||
clazz.isSubClass(owner, types) &&
isInheritedIn(clazz, types) &&
!hiddenIn((ClassSymbol)clazz, types);
}
public boolean isEnclosedBy(ClassSymbol clazz) {
for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
if (sym == clazz) return true;
return false;
}
private boolean hiddenIn(ClassSymbol clazz, Types types) {
Symbol sym = hiddenInInternal(clazz, types);
Assert.check(sym != null, "the result of hiddenInInternal() can't be null");
return sym != this;
}
private Symbol hiddenInInternal(ClassSymbol currentClass, Types types) {
if (currentClass == owner) {
return this;
}
for (Symbol sym : currentClass.members().getSymbolsByName(name)) {
if (sym.kind == kind &&
(kind != MTH ||
(sym.flags() & STATIC) != 0 &&
types.isSubSignature(sym.type, type))) {
return sym;
}
}
Symbol hiddenSym = null;
for (Type st : types.interfaces(currentClass.type)
.prepend(types.supertype(currentClass.type))) {
if (st != null && (st.hasTag(CLASS))) {
Symbol sym = hiddenInInternal((ClassSymbol)st.tsym, types);
if (sym == this) {
return this;
} else if (sym != null) {
hiddenSym = sym;
}
}
}
return hiddenSym;
}
public final boolean isAccessibleIn(Symbol clazz, Types types) {
switch ((int)(flags_field & Flags.AccessFlags)) {
default:
case PUBLIC:
return true;
case PRIVATE:
return this.owner == clazz;
case PROTECTED:
return (clazz.flags() & INTERFACE) == 0;
case 0:
PackageSymbol thisPackage = this.packge();
for (Symbol sup = clazz;
sup != null && sup != this.owner;
sup = types.supertype(sup.type).tsym) {
while (sup.type.hasTag(TYPEVAR))
sup = sup.type.getUpperBound().tsym;
if (sup.type.isErroneous())
return true;
if ((sup.flags() & COMPOUND) != 0)
continue;
if (sup.packge() != thisPackage)
return false;
}
return (clazz.flags() & INTERFACE) == 0;
}
}
public boolean isInheritedIn(Symbol clazz, Types types) {
return isAccessibleIn(clazz, types);
}
public Symbol asMemberOf(Type site, Types types) {
throw new AssertionError();
}
public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
return false;
}
public void complete() throws CompletionFailure {
if (completer != Completer.NULL_COMPLETER) {
Completer c = completer;
completer = Completer.NULL_COMPLETER;
c.complete(this);
}
}
public void apiComplete() throws CompletionFailure {
try {
complete();
} catch (CompletionFailure cf) {
cf.dcfh.handleAPICompletionFailure(cf);
}
}
public boolean exists() {
return true;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Type asType() {
return type;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Symbol getEnclosingElement() {
return owner;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
return ElementKind.OTHER;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Set<Modifier> getModifiers() {
apiComplete();
return Flags.asModifierSet(flags());
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Name getSimpleName() {
return name;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Attribute.Compound> getAnnotationMirrors() {
apiComplete();
return getRawAttributes();
}
@DefinedBy(Api.LANGUAGE_MODEL)
public java.util.List<Symbol> getEnclosedElements() {
return List.nil();
}
public List<TypeVariableSymbol> getTypeParameters() {
ListBuffer<TypeVariableSymbol> l = new ListBuffer<>();
for (Type t : type.getTypeArguments()) {
Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER);
l.append((TypeVariableSymbol)t.tsym);
}
return l.toList();
}
public static class DelegatedSymbol<T extends Symbol> extends Symbol {
protected T other;
public DelegatedSymbol(T other) {
super(other.kind, other.flags_field, other.name, other.type, other.owner);
this.other = other;
}
public String toString() { return other.toString(); }
public Symbol location() { return other.location(); }
public Symbol location(Type site, Types types) { return other.location(site, types); }
public Symbol baseSymbol() { return other; }
public Type erasure(Types types) { return other.erasure(types); }
public Type externalType(Types types) { return other.externalType(types); }
public boolean isDirectlyOrIndirectlyLocal() { return other.isDirectlyOrIndirectlyLocal(); }
public boolean isConstructor() { return other.isConstructor(); }
public Name getQualifiedName() { return other.getQualifiedName(); }
public Name flatName() { return other.flatName(); }
public WriteableScope 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(); }
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return other.accept(v, p);
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitSymbol(other, p);
}
public T getUnderlyingSymbol() {
return other;
}
}
public static abstract class TypeSymbol extends Symbol {
public TypeSymbol(Kind kind, long flags, Name name, Type type, Symbol owner) {
super(kind, flags, name, type, owner);
}
static public Name formFullName(Name name, Symbol owner) {
if (owner == null) return name;
if ((owner.kind != ERR) &&
(owner.kind.matches(KindSelector.VAL_MTH) ||
(owner.kind == TYP && owner.type.hasTag(TYPEVAR))
)) return name;
Name prefix = owner.getQualifiedName();
if (prefix == null || prefix == prefix.table.names.empty)
return name;
else return prefix.append('.', name);
}
static public Name formFlatName(Name name, Symbol owner) {
if (owner == null || owner.kind.matches(KindSelector.VAL_MTH) ||
(owner.kind == TYP && owner.type.hasTag(TYPEVAR))
) return name;
char sep = owner.kind == TYP ? '$' : '.';
Name prefix = owner.flatName();
if (prefix == null || prefix == prefix.table.names.empty)
return name;
else return prefix.append(sep, name);
}
public final boolean precedes(TypeSymbol that, Types types) {
if (this == that)
return false;
if (type.hasTag(that.type.getTag())) {
if (type.hasTag(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 (type.hasTag(TYPEVAR)) {
return types.isSubtype(this.type, that.type);
}
}
return type.hasTag(TYPEVAR);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Symbol> getEnclosedElements() {
List<Symbol> list = List.nil();
if (kind == TYP && type.hasTag(TYPEVAR)) {
return list;
}
apiComplete();
for (Symbol sym : members().getSymbols(NON_RECURSIVE)) {
sym.apiComplete();
if ((sym.flags() & SYNTHETIC) == 0 && sym.owner == this && sym.kind != ERR) {
list = list.prepend(sym);
}
}
return list;
}
public AnnotationTypeMetadata getAnnotationTypeMetadata() {
Assert.error("Only on ClassSymbol");
return null;
}
public boolean isAnnotationType() { return false; }
@Override
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitTypeSymbol(this, p);
}
}
public static class TypeVariableSymbol
extends TypeSymbol implements TypeParameterElement {
public TypeVariableSymbol(long flags, Name name, Type type, Symbol owner) {
super(TYP, flags, name, type, owner);
}
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
return ElementKind.TYPE_PARAMETER;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Symbol getGenericElement() {
return owner;
}
@DefinedBy(Api.LANGUAGE_MODEL)
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 {
return ct.interfaces_field;
}
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Attribute.Compound> getAnnotationMirrors() {
List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
int index = owner.getTypeParameters().indexOf(this);
List<Attribute.Compound> res = List.nil();
for (Attribute.TypeCompound a : candidates) {
if (isCurrentSymbolsAnnotation(a, index))
res = res.prepend(a);
}
return res.reverse();
}
@Override
public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
String name = annoType.getName();
List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
int index = owner.getTypeParameters().indexOf(this);
for (Attribute.TypeCompound anno : candidates)
if (isCurrentSymbolsAnnotation(anno, index) &&
name.contentEquals(anno.type.tsym.flatName()))
return anno;
return null;
}
boolean isCurrentSymbolsAnnotation(Attribute.TypeCompound anno, int index) {
return (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
anno.position.type == TargetType.METHOD_TYPE_PARAMETER) &&
anno.position.parameter_index == index;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitTypeParameter(this, p);
}
}
public static class ModuleSymbol extends TypeSymbol
implements ModuleElement {
public Name version;
public JavaFileManager.Location sourceLocation;
public JavaFileManager.Location classLocation;
public JavaFileManager.Location patchLocation;
public JavaFileManager.Location patchOutputLocation;
public List<com.sun.tools.javac.code.Directive> directives;
public List<com.sun.tools.javac.code.Directive.RequiresDirective> requires;
public List<com.sun.tools.javac.code.Directive.ExportsDirective> exports;
public List<com.sun.tools.javac.code.Directive.OpensDirective> opens;
public List<com.sun.tools.javac.code.Directive.ProvidesDirective> provides;
public List<com.sun.tools.javac.code.Directive.UsesDirective> uses;
public ClassSymbol module_info;
public PackageSymbol unnamedPackage;
public Map<Name, PackageSymbol> visiblePackages;
public Set<ModuleSymbol> readModules;
public List<Symbol> enclosedPackages = List.nil();
public Completer usesProvidesCompleter = Completer.NULL_COMPLETER;
public final Set<ModuleFlags> flags = EnumSet.noneOf(ModuleFlags.class);
public final Set<ModuleResolutionFlags> resolutionFlags = EnumSet.noneOf(ModuleResolutionFlags.class);
public static ModuleSymbol create(Name name, Name module_info) {
ModuleSymbol msym = new ModuleSymbol(name, null);
ClassSymbol info = new ClassSymbol(Flags.MODULE, module_info, msym);
info.fullname = formFullName(module_info, msym);
info.flatname = info.fullname;
info.members_field = WriteableScope.create(info);
msym.module_info = info;
return msym;
}
public ModuleSymbol(Name name, Symbol owner) {
super(MDL, 0, name, null, owner);
Assert.checkNonNull(name);
this.type = new ModuleType(this);
}
@Override
public int poolTag() {
return ClassFile.CONSTANT_Module;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Name getSimpleName() {
return Convert.shortName(name);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public boolean isOpen() {
return flags.contains(ModuleFlags.OPEN);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public boolean isUnnamed() {
return name.isEmpty() && owner == null;
}
@Override
public boolean isDeprecated() {
return hasDeprecatedAnnotation();
}
public boolean isNoModule() {
return false;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
return ElementKind.MODULE;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public java.util.List<Directive> getDirectives() {
apiComplete();
completeUsesProvides();
return Collections.unmodifiableList(directives);
}
public void completeUsesProvides() {
if (usesProvidesCompleter != Completer.NULL_COMPLETER) {
Completer c = usesProvidesCompleter;
usesProvidesCompleter = Completer.NULL_COMPLETER;
c.complete(this);
}
}
@Override
public ClassSymbol outermostClass() {
return null;
}
@Override
public String toString() {
String n = (name == null) ? "<unknown>"
: (name.isEmpty()) ? "<unnamed>"
: String.valueOf(name);
return n;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitModule(this, p);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Symbol> getEnclosedElements() {
List<Symbol> list = List.nil();
for (Symbol sym : enclosedPackages) {
if (sym.members().anyMatch(m -> m.kind == TYP))
list = list.prepend(sym);
}
return list;
}
public void reset() {
this.directives = null;
this.requires = null;
this.exports = null;
this.provides = null;
this.uses = null;
this.visiblePackages = null;
}
}
public enum ModuleFlags {
OPEN(0x0020),
SYNTHETIC(0x1000),
MANDATED(0x8000);
public static int value(Set<ModuleFlags> s) {
int v = 0;
for (ModuleFlags f: s)
v |= f.value;
return v;
}
private ModuleFlags(int value) {
this.value = value;
}
public final int value;
}
public enum ModuleResolutionFlags {
DO_NOT_RESOLVE_BY_DEFAULT(0x0001),
WARN_DEPRECATED(0x0002),
WARN_DEPRECATED_REMOVAL(0x0004),
WARN_INCUBATING(0x0008);
public static int value(Set<ModuleResolutionFlags> s) {
int v = 0;
for (ModuleResolutionFlags f: s)
v |= f.value;
return v;
}
private ModuleResolutionFlags(int value) {
this.value = value;
}
public final int value;
}
public static class PackageSymbol extends TypeSymbol
implements PackageElement {
public WriteableScope members_field;
public Name fullname;
public ClassSymbol package_info;
public ModuleSymbol modle;
public JavaFileObject sourcefile;
public PackageSymbol(Name name, Type type, Symbol owner) {
super(PCK, 0, name, type, owner);
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();
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Name getQualifiedName() {
return fullname;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public boolean isUnnamed() {
return name.isEmpty() && owner != null;
}
public WriteableScope members() {
complete();
return members_field;
}
@Override
public int poolTag() {
return ClassFile.CONSTANT_Package;
}
public long flags() {
complete();
return flags_field;
}
@Override
public List<Attribute.Compound> getRawAttributes() {
complete();
if (package_info != null) {
package_info.complete();
mergeAttributes();
}
return super.getRawAttributes();
}
private void mergeAttributes() {
if (metadata == null &&
package_info.metadata != null) {
metadata = new SymbolMetadata(this);
metadata.setAttributes(package_info.metadata);
}
}
public boolean exists() {
return (flags_field & EXISTS) != 0;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
return ElementKind.PACKAGE;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Symbol getEnclosingElement() {
return modle != null && !modle.isNoModule() ? modle : null;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitPackage(this, p);
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitPackageSymbol(this, p);
}
public void reset() {
metadata = null;
}
}
public static class RootPackageSymbol extends PackageSymbol {
public final MissingInfoHandler missingInfoHandler;
public final boolean allowPrivateInvokeVirtual;
public RootPackageSymbol(Name name, Symbol owner,
MissingInfoHandler missingInfoHandler,
boolean allowPrivateInvokeVirtual) {
super(name, owner);
this.missingInfoHandler = missingInfoHandler;
this.allowPrivateInvokeVirtual = allowPrivateInvokeVirtual;
}
}
public static class ClassSymbol extends TypeSymbol implements TypeElement {
public WriteableScope members_field;
public Name fullname;
public Name flatname;
public JavaFileObject sourcefile;
public JavaFileObject classfile;
public List<ClassSymbol> trans_local;
private AnnotationTypeMetadata annotationTypeMetadata;
private List<RecordComponent> recordComponents = List.nil();
public List<Symbol> permitted;
public boolean isPermittedExplicit = false;
public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
super(TYP, 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.annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
this.permitted = List.nil();
}
public ClassSymbol(long flags, Name name, Symbol owner) {
this(
flags,
name,
new ClassType(Type.noType, null, null),
owner);
this.type.tsym = this;
}
public String toString() {
return className();
}
public long flags() {
complete();
return flags_field;
}
public WriteableScope members() {
complete();
return members_field;
}
@Override
public List<Attribute.Compound> getRawAttributes() {
complete();
return super.getRawAttributes();
}
@Override
public List<Attribute.TypeCompound> getRawTypeAttributes() {
complete();
return super.getRawTypeAttributes();
}
public Type erasure(Types types) {
if (erasure_field == null)
erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
List.nil(), this,
type.getMetadata());
return erasure_field;
}
public String className() {
if (name.isEmpty())
return
Log.getLocalizedString("anonymous.class", flatname);
else
return fullname.toString();
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Name getQualifiedName() {
return fullname;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Symbol> getEnclosedElements() {
List<Symbol> result = super.getEnclosedElements();
if (!recordComponents.isEmpty()) {
List<RecordComponent> reversed = recordComponents.reverse();
for (RecordComponent rc : reversed) {
result = result.prepend(rc);
}
}
return result;
}
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.hasTag(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.hasTag(CLASS); t = types.supertype(t))
if (t.tsym == base) return true;
}
return false;
}
public void complete() throws CompletionFailure {
Completer origCompleter = completer;
try {
super.complete();
} catch (CompletionFailure ex) {
ex.dcfh.classSymbolCompleteFailed(this, origCompleter);
flags_field |= (PUBLIC|STATIC);
this.type = new ErrorType(this, Type.noType);
throw ex;
}
}
@DefinedBy(Api.LANGUAGE_MODEL)
public List<Type> getInterfaces() {
apiComplete();
if (type instanceof ClassType) {
ClassType t = (ClassType)type;
if (t.interfaces_field == null)
t.interfaces_field = List.nil();
if (t.all_interfaces_field != null)
return Type.getModelTypes(t.all_interfaces_field);
return t.interfaces_field;
} else {
return List.nil();
}
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Type getSuperclass() {
apiComplete();
if (type instanceof ClassType) {
ClassType t = (ClassType)type;
if (t.supertype_field == null)
t.supertype_field = Type.noType;
return t.isInterface()
? Type.noType
: t.supertype_field.getModelType();
} else {
return Type.noType;
}
}
private ClassSymbol getSuperClassToSearchForAnnotations() {
Type sup = getSuperclass();
if (!sup.hasTag(CLASS) || sup.isErroneous())
return null;
return (ClassSymbol) sup.tsym;
}
@Override
protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
ClassSymbol sup = getSuperClassToSearchForAnnotations();
return sup == null ? super.getInheritedAnnotations(annoType)
: sup.getAnnotationsByType(annoType);
}
@DefinedBy(Api.LANGUAGE_MODEL)
@SuppressWarnings("preview")
public ElementKind getKind() {
apiComplete();
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 if ((flags & RECORD) != 0)
return ElementKind.RECORD;
else
return ElementKind.CLASS;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Set<Modifier> getModifiers() {
apiComplete();
long flags = flags();
return Flags.asModifierSet(flags & ~DEFAULT);
}
public RecordComponent getRecordComponent(VarSymbol field) {
for (RecordComponent rc : recordComponents) {
if (rc.name == field.name) {
return rc;
}
}
return null;
}
public RecordComponent getRecordComponent(JCVariableDecl var, boolean addIfMissing, List<JCAnnotation> annotations) {
for (RecordComponent rc : recordComponents) {
if (rc.name == var.name && var.pos == rc.pos) {
return rc;
}
}
RecordComponent rc = null;
if (addIfMissing) {
recordComponents = recordComponents.append(rc = new RecordComponent(var.sym, annotations));
}
return rc;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
@SuppressWarnings("preview")
public List<? extends RecordComponent> getRecordComponents() {
return recordComponents;
}
public void setRecordComponents(List<RecordComponent> recordComponents) {
this.recordComponents = recordComponents;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public NestingKind getNestingKind() {
apiComplete();
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;
}
@Override
protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {
Attribute.Compound attrib = super.getAttribute(annoType);
boolean inherited = annoType.isAnnotationPresent(Inherited.class);
if (attrib != null || !inherited)
return attrib;
ClassSymbol superType = getSuperClassToSearchForAnnotations();
return superType == null ? null
: superType.getAttribute(annoType);
}
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitType(this, p);
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitClassSymbol(this, p);
}
public void markAbstractIfNeeded(Types types) {
if (types.enter.getEnv(this) != null &&
(flags() & ENUM) != 0 && types.supertype(type).tsym == types.syms.enumSym &&
(flags() & (FINAL | ABSTRACT)) == 0) {
if (types.firstUnimplementedAbstract(this) != null)
flags_field |= ABSTRACT;
}
}
public void reset() {
kind = TYP;
erasure_field = null;
members_field = null;
flags_field = 0;
if (type instanceof ClassType) {
ClassType t = (ClassType)type;
t.setEnclosingType(Type.noType);
t.rank_field = -1;
t.typarams_field = null;
t.allparams_field = null;
t.supertype_field = null;
t.interfaces_field = null;
t.all_interfaces_field = null;
}
clearAnnotationMetadata();
}
public void clearAnnotationMetadata() {
metadata = null;
annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
}
@Override
public AnnotationTypeMetadata getAnnotationTypeMetadata() {
return annotationTypeMetadata;
}
@Override
public boolean isAnnotationType() {
return (flags_field & Flags.ANNOTATION) != 0;
}
public void setAnnotationTypeMetadata(AnnotationTypeMetadata a) {
Assert.checkNonNull(a);
Assert.check(!annotationTypeMetadata.isMetadataForAnnotationType());
this.annotationTypeMetadata = a;
}
public boolean isRecord() {
return (flags_field & RECORD) != 0;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public List<Type> getPermittedSubclasses() {
return permitted.map(s -> s.type);
}
}
public static class VarSymbol extends Symbol implements VariableElement {
public int pos = Position.NOPOS;
public int adr = -1;
public VarSymbol(long flags, Name name, Type type, Symbol owner) {
super(VAR, flags, name, type, owner);
}
@Override
public int poolTag() {
return ClassFile.CONSTANT_Fieldref;
}
public MethodHandleSymbol asMethodHandle(boolean getter) {
return new MethodHandleSymbol(this, getter);
}
public VarSymbol clone(Symbol newOwner) {
VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) {
@Override
public Symbol baseSymbol() {
return VarSymbol.this;
}
@Override
public Object poolKey(Types types) {
return new Pair<>(newOwner, baseSymbol());
}
};
v.pos = pos;
v.adr = adr;
v.data = data;
return v;
}
public String toString() {
return name.toString();
}
public Symbol asMemberOf(Type site, Types types) {
return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
}
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
long flags = flags();
if ((flags & PARAMETER) != 0) {
if (isExceptionParameter())
return ElementKind.EXCEPTION_PARAMETER;
else
return ElementKind.PARAMETER;
} else if ((flags & ENUM) != 0) {
return ElementKind.ENUM_CONSTANT;
} else if (owner.kind == TYP || owner.kind == ERR) {
return ElementKind.FIELD;
} else if (isResourceVariable()) {
return ElementKind.RESOURCE_VARIABLE;
} else if ((flags & MATCH_BINDING) != 0) {
@SuppressWarnings("preview")
ElementKind kind = ElementKind.BINDING_VARIABLE;
return kind;
} else {
return ElementKind.LOCAL_VARIABLE;
}
}
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitVariable(this, p);
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Object getConstantValue() {
return Constants.decode(getConstValue(), type);
}
public void setLazyConstValue(final Env<AttrContext> env,
final Attr attr,
final JCVariableDecl variable)
{
setData((Callable<Object>)() -> attr.attribLazyConstantValue(env, variable, type));
}
private Object data;
public boolean isExceptionParameter() {
return data == ElementKind.EXCEPTION_PARAMETER;
}
public boolean isResourceVariable() {
return data == ElementKind.RESOURCE_VARIABLE;
}
public Object getConstValue() {
if (data == ElementKind.EXCEPTION_PARAMETER ||
data == ElementKind.RESOURCE_VARIABLE) {
return null;
} else if (data instanceof Callable<?>) {
Callable<?> eval = (Callable<?>)data;
data = null;
try {
data = eval.call();
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
return data;
}
public void setData(Object data) {
Assert.check(!(data instanceof Env<?>), this);
this.data = data;
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitVarSymbol(this, p);
}
}
@SuppressWarnings("preview")
public static class RecordComponent extends VarSymbol implements RecordComponentElement {
public MethodSymbol accessor;
public JCTree.JCMethodDecl accessorMeth;
private final List<JCAnnotation> originalAnnos;
private final int pos;
private final boolean isVarargs;
public RecordComponent(Name name, Type type, Symbol owner) {
super(PUBLIC, name, type, owner);
pos = -1;
originalAnnos = List.nil();
isVarargs = false;
}
public RecordComponent(VarSymbol field, List<JCAnnotation> annotations) {
super(PUBLIC, field.name, field.type, field.owner);
this.originalAnnos = annotations;
this.pos = field.pos;
this.isVarargs = type.hasTag(TypeTag.ARRAY) && ((ArrayType)type).isVarargs();
}
public List<JCAnnotation> getOriginalAnnos() { return originalAnnos; }
public boolean isVarargs() {
return isVarargs;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
@SuppressWarnings("preview")
public ElementKind getKind() {
return ElementKind.RECORD_COMPONENT;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public ExecutableElement getAccessor() {
return accessor;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
@SuppressWarnings("preview")
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitRecordComponent(this, p);
}
}
public static class ParamSymbol extends VarSymbol {
public ParamSymbol(long flags, Name name, Type type, Symbol owner) {
super(flags, name, type, owner);
}
@Override
public Name getSimpleName() {
if ((flags_field & NAME_FILLED) == 0) {
flags_field |= NAME_FILLED;
Symbol rootPack = this;
while (rootPack != null && !(rootPack instanceof RootPackageSymbol)) {
rootPack = rootPack.owner;
}
if (rootPack != null) {
Name inferredName =
((RootPackageSymbol) rootPack).missingInfoHandler.getParameterName(this);
if (inferredName != null) {
this.name = inferredName;
}
}
}
return super.getSimpleName();
}
}
public static class BindingSymbol extends VarSymbol {
public BindingSymbol(long flags, Name name, Type type, Symbol owner) {
super(flags | Flags.HASINIT | Flags.MATCH_BINDING, name, type, owner);
}
public boolean isAliasFor(BindingSymbol b) {
return aliases().containsAll(b.aliases());
}
List<BindingSymbol> aliases() {
return List.of(this);
}
public void preserveBinding() {
flags_field |= Flags.MATCH_BINDING_TO_OUTER;
}
public boolean isPreserved() {
return (flags_field & Flags.MATCH_BINDING_TO_OUTER) != 0;
}
}
public static class MethodSymbol extends Symbol implements ExecutableElement {
public Code code = null;
public List<VarSymbol> = List.nil();
public List<VarSymbol> capturedLocals = List.nil();
public List<VarSymbol> params = null;
public Attribute defaultValue = null;
public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
super(MTH, flags, name, type, owner);
if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name);
}
public MethodSymbol clone(Symbol newOwner) {
MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner) {
@Override
public Symbol baseSymbol() {
return MethodSymbol.this;
}
@Override
public Object poolKey(Types types) {
return new Pair<>(newOwner, baseSymbol());
}
};
m.code = code;
return m;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Set<Modifier> getModifiers() {
long flags = flags();
return Flags.asModifierSet((flags & DEFAULT) != 0 ? flags & ~ABSTRACT : flags);
}
public String toString() {
if ((flags() & BLOCK) != 0) {
return owner.name.toString();
} else {
String s = (name == name.table.names.init)
? owner.name.toString()
: name.toString();
if (type != null) {
if (type.hasTag(FORALL))
s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
}
return s;
}
}
@Override
public int poolTag() {
return owner.isInterface() ?
ClassFile.CONSTANT_InterfaceMethodref : ClassFile.CONSTANT_Methodref;
}
public boolean isHandle() {
return false;
}
public MethodHandleSymbol asHandle() {
return new MethodHandleSymbol(this);
}
public Symbol implemented(TypeSymbol c, Types types) {
Symbol impl = null;
for (List<Type> is = types.interfaces(c.type);
impl == null && is.nonEmpty();
is = is.tail) {
TypeSymbol i = is.head.tsym;
impl = implementedIn(i, types);
if (impl == null)
impl = implemented(i, types);
}
return impl;
}
public Symbol implementedIn(TypeSymbol c, Types types) {
Symbol impl = null;
for (Symbol sym : c.members().getSymbolsByName(name)) {
if (this.overrides(sym, (TypeSymbol)owner, types, true) &&
types.isSameType(type.getReturnType(),
types.memberType(owner.type, sym).getReturnType())) {
impl = sym;
}
}
return impl;
}
public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
if (isConstructor() || _other.kind != MTH) return false;
if (this == _other) return true;
MethodSymbol other = (MethodSymbol)_other;
if (other.isOverridableIn((TypeSymbol)owner) &&
types.asSuper(owner.type, other.owner) != null &&
types.isSameType(erasure(types), other.erasure(types)))
return true;
return
(flags() & ABSTRACT) == 0 &&
other.isOverridableIn(origin) &&
this.isMemberOf(origin, types) &&
types.isSameType(erasure(types), other.erasure(types));
}
public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
for (Symbol sym : c.members().getSymbolsByName(name)) {
if (sym.kind == MTH &&
((MethodSymbol)sym).binaryOverrides(this, origin, types))
return (MethodSymbol)sym;
}
}
return null;
}
public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
return overrides(_other, origin, types, checkResult, true);
}
public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult,
boolean requireConcreteIfInherited) {
if (isConstructor() || _other.kind != MTH) return false;
if (this == _other) return true;
MethodSymbol other = (MethodSymbol)_other;
if (other.isOverridableIn((TypeSymbol)owner) &&
types.asSuper(owner.type, other.owner) != null) {
Type mt = types.memberType(owner.type, this);
Type ot = types.memberType(owner.type, other);
if (types.isSubSignature(mt, ot)) {
if (!checkResult)
return true;
if (types.returnTypeSubstitutable(mt, ot))
return true;
}
}
if (((flags() & ABSTRACT) != 0 && requireConcreteIfInherited) ||
((other.flags() & ABSTRACT) == 0 && (other.flags() & DEFAULT) == 0) ||
!other.isOverridableIn(origin) ||
!this.isMemberOf(origin, types))
return false;
Type mt = types.memberType(origin.type, this);
Type ot = types.memberType(origin.type, other);
return
types.isSubSignature(mt, ot) &&
(!checkResult || types.resultSubtype(mt, ot, types.noWarnings));
}
private boolean isOverridableIn(TypeSymbol origin) {
switch ((int)(flags_field & Flags.AccessFlags)) {
case Flags.PRIVATE:
return false;
case Flags.PUBLIC:
return !this.owner.isInterface() ||
(flags_field & STATIC) == 0;
case Flags.PROTECTED:
return (origin.flags() & INTERFACE) == 0;
case 0:
return
this.packge() == origin.packge() &&
(origin.flags() & INTERFACE) == 0;
default:
return false;
}
}
@Override
public boolean isInheritedIn(Symbol clazz, Types types) {
switch ((int)(flags_field & Flags.AccessFlags)) {
case PUBLIC:
return !this.owner.isInterface() ||
clazz == owner ||
(flags_field & STATIC) == 0;
default:
return super.isInheritedIn(clazz, types);
}
}
public boolean isLambdaMethod() {
return (flags() & LAMBDA_METHOD) == LAMBDA_METHOD;
}
public MethodSymbol originalEnclosingMethod() {
return this;
}
public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
return implementation(origin, types, checkResult, implementation_filter);
}
public static final Filter<Symbol> implementation_filter = s ->
s.kind == MTH && (s.flags() & SYNTHETIC) == 0;
public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
if (res != null)
return res;
if (types.isDerivedRaw(origin.type) && !origin.isInterface())
return implementation(types.supertype(origin.type).tsym, types, checkResult);
else
return null;
}
public List<VarSymbol> params() {
owner.complete();
if (params == null) {
ListBuffer<VarSymbol> newParams = new ListBuffer<>();
int i = 0;
for (Type t : type.getParameterTypes()) {
Name paramName = name.table.fromString("arg" + i);
VarSymbol param = new VarSymbol(PARAMETER, paramName, t, this);
newParams.append(param);
i++;
}
params = newParams.toList();
}
Assert.checkNonNull(params);
return params;
}
public Symbol asMemberOf(Type site, Types types) {
return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
}
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
if (name == name.table.names.init)
return ElementKind.CONSTRUCTOR;
else if (name == name.table.names.clinit)
return ElementKind.STATIC_INIT;
else if ((flags() & BLOCK) != 0)
return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
else
return ElementKind.METHOD;
}
public boolean isStaticOrInstanceInit() {
return getKind() == ElementKind.STATIC_INIT ||
getKind() == ElementKind.INSTANCE_INIT;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Attribute getDefaultValue() {
return defaultValue;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public List<VarSymbol> getParameters() {
return params();
}
@DefinedBy(Api.LANGUAGE_MODEL)
public boolean isVarArgs() {
return (flags() & VARARGS) != 0;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public boolean isDefault() {
return (flags() & DEFAULT) != 0;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitExecutable(this, p);
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitMethodSymbol(this, p);
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Type getReceiverType() {
Type result = asType().getReceiverType();
return (result == null) ? Type.noType : result;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Type getReturnType() {
return asType().getReturnType();
}
@DefinedBy(Api.LANGUAGE_MODEL)
public List<Type> getThrownTypes() {
return asType().getThrownTypes();
}
}
public static class DynamicMethodSymbol extends MethodSymbol implements Dynamic {
public LoadableConstant[] staticArgs;
public MethodHandleSymbol bsm;
public DynamicMethodSymbol(Name name, Symbol owner, MethodHandleSymbol bsm, Type type, LoadableConstant[] staticArgs) {
super(0, name, type, owner);
this.bsm = bsm;
this.staticArgs = staticArgs;
}
@Override
public boolean isDynamic() {
return true;
}
@Override
public LoadableConstant[] staticArgs() {
return staticArgs;
}
@Override
public MethodHandleSymbol bootstrapMethod() {
return bsm;
}
@Override
public int poolTag() {
return ClassFile.CONSTANT_InvokeDynamic;
}
@Override
public Type dynamicType() {
return type;
}
}
public static class DynamicVarSymbol extends VarSymbol implements Dynamic, LoadableConstant {
public LoadableConstant[] staticArgs;
public MethodHandleSymbol bsm;
public DynamicVarSymbol(Name name, Symbol owner, MethodHandleSymbol bsm, Type type, LoadableConstant[] staticArgs) {
super(0, name, type, owner);
this.bsm = bsm;
this.staticArgs = staticArgs;
}
@Override
public boolean isDynamic() {
return true;
}
@Override
public PoolConstant dynamicType() {
return type;
}
@Override
public LoadableConstant[] staticArgs() {
return staticArgs;
}
@Override
public LoadableConstant bootstrapMethod() {
return bsm;
}
@Override
public int poolTag() {
return ClassFile.CONSTANT_Dynamic;
}
}
public static class MethodHandleSymbol extends MethodSymbol implements LoadableConstant {
private Symbol refSym;
private boolean getter;
public MethodHandleSymbol(Symbol msym) {
this(msym, false);
}
public MethodHandleSymbol(Symbol msym, boolean getter) {
super(msym.flags_field, msym.name, msym.type, msym.owner);
this.refSym = msym;
this.getter = getter;
}
public int referenceKind() {
if (refSym.kind == VAR) {
return getter ?
refSym.isStatic() ? ClassFile.REF_getStatic : ClassFile.REF_getField :
refSym.isStatic() ? ClassFile.REF_putStatic : ClassFile.REF_putField;
} else {
if (refSym.isConstructor()) {
return ClassFile.REF_newInvokeSpecial;
} else {
if (refSym.isStatic()) {
return ClassFile.REF_invokeStatic;
} else if ((refSym.flags() & PRIVATE) != 0 && !allowPrivateInvokeVirtual()) {
return ClassFile.REF_invokeSpecial;
} else if (refSym.enclClass().isInterface()) {
return ClassFile.REF_invokeInterface;
} else {
return ClassFile.REF_invokeVirtual;
}
}
}
}
private boolean allowPrivateInvokeVirtual() {
Symbol rootPack = this;
while (rootPack != null && !(rootPack instanceof RootPackageSymbol)) {
rootPack = rootPack.owner;
}
return rootPack != null && ((RootPackageSymbol) rootPack).allowPrivateInvokeVirtual;
}
@Override
public int poolTag() {
return ClassFile.CONSTANT_MethodHandle;
}
@Override
public Object poolKey(Types types) {
return new Pair<>(baseSymbol(), referenceKind());
}
@Override
public MethodHandleSymbol asHandle() {
return this;
}
@Override
public Symbol baseSymbol() {
return refSym;
}
@Override
public boolean isHandle() {
return true;
}
}
public static class OperatorSymbol extends MethodSymbol {
public int opcode;
private int accessCode = Integer.MIN_VALUE;
public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
super(PUBLIC | STATIC, name, type, owner);
this.opcode = opcode;
}
@Override
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitOperatorSymbol(this, p);
}
public int getAccessCode(Tag tag) {
if (accessCode != Integer.MIN_VALUE && !tag.isIncOrDecUnaryOp()) {
return accessCode;
}
accessCode = AccessCode.from(tag, opcode);
return accessCode;
}
public enum AccessCode {
UNKNOWN(-1, Tag.NO_TAG),
DEREF(0, Tag.NO_TAG),
ASSIGN(2, Tag.ASSIGN),
PREINC(4, Tag.PREINC),
PREDEC(6, Tag.PREDEC),
POSTINC(8, Tag.POSTINC),
POSTDEC(10, Tag.POSTDEC),
FIRSTASGOP(12, Tag.NO_TAG);
public final int code;
public final Tag tag;
public static final int numberOfAccessCodes = (lushrl - ishll + lxor + 2 - iadd) * 2 + FIRSTASGOP.code + 2;
AccessCode(int code, Tag tag) {
this.code = code;
this.tag = tag;
}
static public AccessCode getFromCode(int code) {
for (AccessCode aCodes : AccessCode.values()) {
if (aCodes.code == code) {
return aCodes;
}
}
return UNKNOWN;
}
static int from(Tag tag, int opcode) {
switch (tag) {
case PREINC:
return AccessCode.PREINC.code;
case PREDEC:
return AccessCode.PREDEC.code;
case POSTINC:
return AccessCode.POSTINC.code;
case POSTDEC:
return AccessCode.POSTDEC.code;
}
if (iadd <= opcode && opcode <= lxor) {
return (opcode - iadd) * 2 + FIRSTASGOP.code;
} else if (opcode == string_add) {
return (lxor + 1 - iadd) * 2 + FIRSTASGOP.code;
} else if (ishll <= opcode && opcode <= lushrl) {
return (opcode - ishll + lxor + 2 - iadd) * 2 + FIRSTASGOP.code;
}
return -1;
}
}
}
public static interface Completer {
public final static Completer NULL_COMPLETER = new Completer() {
public void complete(Symbol sym) { }
public boolean isTerminal() { return true; }
};
void complete(Symbol sym) throws CompletionFailure;
default boolean isTerminal() {
return false;
}
}
public static class CompletionFailure extends RuntimeException {
private static final long serialVersionUID = 0;
public final transient DeferredCompletionFailureHandler dcfh;
public transient Symbol sym;
private transient JCDiagnostic diag;
private transient Supplier<JCDiagnostic> diagSupplier;
public CompletionFailure(Symbol sym, Supplier<JCDiagnostic> diagSupplier, DeferredCompletionFailureHandler dcfh) {
this.dcfh = dcfh;
this.sym = sym;
this.diagSupplier = diagSupplier;
}
public JCDiagnostic getDiagnostic() {
if (diag == null && diagSupplier != null) {
diag = diagSupplier.get();
}
return diag;
}
@Override
public String getMessage() {
return getDiagnostic().getMessage(null);
}
public JCDiagnostic getDetailValue() {
return getDiagnostic();
}
@Override
public CompletionFailure initCause(Throwable cause) {
super.initCause(cause);
return this;
}
public void resetDiagnostic(Supplier<JCDiagnostic> diagSupplier) {
this.diagSupplier = diagSupplier;
this.diag = null;
}
}
public interface Visitor<R,P> {
R visitClassSymbol(ClassSymbol s, P arg);
R visitMethodSymbol(MethodSymbol s, P arg);
R visitPackageSymbol(PackageSymbol s, P arg);
R visitOperatorSymbol(OperatorSymbol s, P arg);
R visitVarSymbol(VarSymbol s, P arg);
R visitTypeSymbol(TypeSymbol s, P arg);
R visitSymbol(Symbol s, P arg);
}
}