0N/A/*
815N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javac.code;
0N/A
340N/Aimport java.lang.ref.SoftReference;
0N/Aimport java.util.*;
0N/A
0N/Aimport com.sun.tools.javac.util.*;
0N/Aimport com.sun.tools.javac.util.List;
0N/A
0N/Aimport com.sun.tools.javac.jvm.ClassReader;
656N/Aimport com.sun.tools.javac.code.Attribute.RetentionPolicy;
794N/Aimport com.sun.tools.javac.code.Lint.LintCategory;
0N/Aimport com.sun.tools.javac.comp.Check;
0N/A
857N/Aimport static com.sun.tools.javac.code.Scope.*;
0N/Aimport static com.sun.tools.javac.code.Type.*;
0N/Aimport static com.sun.tools.javac.code.TypeTags.*;
0N/Aimport static com.sun.tools.javac.code.Symbol.*;
0N/Aimport static com.sun.tools.javac.code.Flags.*;
0N/Aimport static com.sun.tools.javac.code.BoundKind.*;
0N/Aimport static com.sun.tools.javac.util.ListBuffer.lb;
0N/A
0N/A/**
0N/A * Utility class containing various operations on types.
0N/A *
0N/A * <p>Unless other names are more illustrative, the following naming
0N/A * conventions should be observed in this file:
0N/A *
0N/A * <dl>
0N/A * <dt>t</dt>
0N/A * <dd>If the first argument to an operation is a type, it should be named t.</dd>
0N/A * <dt>s</dt>
0N/A * <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd>
0N/A * <dt>ts</dt>
0N/A * <dd>If an operations takes a list of types, the first should be named ts.</dd>
0N/A * <dt>ss</dt>
0N/A * <dd>A second list of types should be named ss.</dd>
0N/A * </dl>
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
0N/A * If you write code that depends on this, you do so at your own risk.
0N/A * This code and its internal interfaces are subject to change or
0N/A * deletion without notice.</b>
0N/A */
0N/Apublic class Types {
0N/A protected static final Context.Key<Types> typesKey =
0N/A new Context.Key<Types>();
0N/A
0N/A final Symtab syms;
135N/A final JavacMessages messages;
112N/A final Names names;
0N/A final boolean allowBoxing;
983N/A final boolean allowCovariantReturns;
983N/A final boolean allowObjectToPrimitiveCast;
0N/A final ClassReader reader;
0N/A final Check chk;
0N/A List<Warner> warnStack = List.nil();
0N/A final Name capturedName;
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Instantiating">
0N/A public static Types instance(Context context) {
0N/A Types instance = context.get(typesKey);
0N/A if (instance == null)
0N/A instance = new Types(context);
0N/A return instance;
0N/A }
0N/A
0N/A protected Types(Context context) {
0N/A context.put(typesKey, this);
0N/A syms = Symtab.instance(context);
112N/A names = Names.instance(context);
983N/A Source source = Source.instance(context);
983N/A allowBoxing = source.allowBoxing();
983N/A allowCovariantReturns = source.allowCovariantReturns();
983N/A allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast();
0N/A reader = ClassReader.instance(context);
0N/A chk = Check.instance(context);
0N/A capturedName = names.fromString("<captured wildcard>");
135N/A messages = JavacMessages.instance(context);
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="upperBound">
0N/A /**
0N/A * The "rvalue conversion".<br>
0N/A * The upper bound of most types is the type
0N/A * itself. Wildcards, on the other hand have upper
0N/A * and lower bounds.
0N/A * @param t a type
0N/A * @return the upper bound of the given type
0N/A */
0N/A public Type upperBound(Type t) {
0N/A return upperBound.visit(t);
0N/A }
0N/A // where
0N/A private final MapVisitor<Void> upperBound = new MapVisitor<Void>() {
0N/A
0N/A @Override
0N/A public Type visitWildcardType(WildcardType t, Void ignored) {
0N/A if (t.isSuperBound())
0N/A return t.bound == null ? syms.objectType : t.bound.bound;
0N/A else
0N/A return visit(t.type);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitCapturedType(CapturedType t, Void ignored) {
0N/A return visit(t.bound);
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="lowerBound">
0N/A /**
0N/A * The "lvalue conversion".<br>
0N/A * The lower bound of most types is the type
0N/A * itself. Wildcards, on the other hand have upper
0N/A * and lower bounds.
0N/A * @param t a type
0N/A * @return the lower bound of the given type
0N/A */
0N/A public Type lowerBound(Type t) {
0N/A return lowerBound.visit(t);
0N/A }
0N/A // where
0N/A private final MapVisitor<Void> lowerBound = new MapVisitor<Void>() {
0N/A
0N/A @Override
0N/A public Type visitWildcardType(WildcardType t, Void ignored) {
0N/A return t.isExtendsBound() ? syms.botType : visit(t.type);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitCapturedType(CapturedType t, Void ignored) {
0N/A return visit(t.getLowerBound());
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isUnbounded">
0N/A /**
0N/A * Checks that all the arguments to a class are unbounded
0N/A * wildcards or something else that doesn't make any restrictions
0N/A * on the arguments. If a class isUnbounded, a raw super- or
0N/A * subclass can be cast to it without a warning.
0N/A * @param t a type
0N/A * @return true iff the given type is unbounded or raw
0N/A */
0N/A public boolean isUnbounded(Type t) {
0N/A return isUnbounded.visit(t);
0N/A }
0N/A // where
0N/A private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() {
0N/A
0N/A public Boolean visitType(Type t, Void ignored) {
0N/A return true;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitClassType(ClassType t, Void ignored) {
0N/A List<Type> parms = t.tsym.type.allparams();
0N/A List<Type> args = t.allparams();
0N/A while (parms.nonEmpty()) {
0N/A WildcardType unb = new WildcardType(syms.objectType,
0N/A BoundKind.UNBOUND,
0N/A syms.boundClass,
0N/A (TypeVar)parms.head);
0N/A if (!containsType(args.head, unb))
0N/A return false;
0N/A parms = parms.tail;
0N/A args = args.tail;
0N/A }
0N/A return true;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="asSub">
0N/A /**
0N/A * Return the least specific subtype of t that starts with symbol
0N/A * sym. If none exists, return null. The least specific subtype
0N/A * is determined as follows:
0N/A *
0N/A * <p>If there is exactly one parameterized instance of sym that is a
0N/A * subtype of t, that parameterized instance is returned.<br>
0N/A * Otherwise, if the plain type or raw type `sym' is a subtype of
0N/A * type t, the type `sym' itself is returned. Otherwise, null is
0N/A * returned.
0N/A */
0N/A public Type asSub(Type t, Symbol sym) {
0N/A return asSub.visit(t, sym);
0N/A }
0N/A // where
0N/A private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() {
0N/A
0N/A public Type visitType(Type t, Symbol sym) {
0N/A return null;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitClassType(ClassType t, Symbol sym) {
0N/A if (t.tsym == sym)
0N/A return t;
0N/A Type base = asSuper(sym.type, t.tsym);
0N/A if (base == null)
0N/A return null;
0N/A ListBuffer<Type> from = new ListBuffer<Type>();
0N/A ListBuffer<Type> to = new ListBuffer<Type>();
0N/A try {
0N/A adapt(base, t, from, to);
0N/A } catch (AdaptFailure ex) {
0N/A return null;
0N/A }
0N/A Type res = subst(sym.type, from.toList(), to.toList());
0N/A if (!isSubtype(res, t))
0N/A return null;
0N/A ListBuffer<Type> openVars = new ListBuffer<Type>();
0N/A for (List<Type> l = sym.type.allparams();
0N/A l.nonEmpty(); l = l.tail)
0N/A if (res.contains(l.head) && !t.contains(l.head))
0N/A openVars.append(l.head);
0N/A if (openVars.nonEmpty()) {
0N/A if (t.isRaw()) {
0N/A // The subtype of a raw type is raw
0N/A res = erasure(res);
0N/A } else {
0N/A // Unbound type arguments default to ?
0N/A List<Type> opens = openVars.toList();
0N/A ListBuffer<Type> qs = new ListBuffer<Type>();
0N/A for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) {
0N/A qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, syms.boundClass, (TypeVar) iter.head));
0N/A }
0N/A res = subst(res, opens, qs.toList());
0N/A }
0N/A }
0N/A return res;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitErrorType(ErrorType t, Symbol sym) {
0N/A return t;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isConvertible">
0N/A /**
1070N/A * Is t a subtype of or convertible via boxing/unboxing
1070N/A * conversion to s?
0N/A */
0N/A public boolean isConvertible(Type t, Type s, Warner warn) {
1070N/A if (t.tag == ERROR)
1070N/A return true;
0N/A boolean tPrimitive = t.isPrimitive();
0N/A boolean sPrimitive = s.isPrimitive();
794N/A if (tPrimitive == sPrimitive) {
0N/A return isSubtypeUnchecked(t, s, warn);
794N/A }
0N/A if (!allowBoxing) return false;
0N/A return tPrimitive
0N/A ? isSubtype(boxedClass(t).type, s)
0N/A : isSubtype(unboxedType(t), s);
0N/A }
0N/A
0N/A /**
0N/A * Is t a subtype of or convertiable via boxing/unboxing
0N/A * convertions to s?
0N/A */
0N/A public boolean isConvertible(Type t, Type s) {
0N/A return isConvertible(t, s, Warner.noWarnings);
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isSubtype">
0N/A /**
0N/A * Is t an unchecked subtype of s?
0N/A */
0N/A public boolean isSubtypeUnchecked(Type t, Type s) {
0N/A return isSubtypeUnchecked(t, s, Warner.noWarnings);
0N/A }
0N/A /**
0N/A * Is t an unchecked subtype of s?
0N/A */
0N/A public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
1107N/A boolean result = isSubtypeUncheckedInternal(t, s, warn);
1107N/A if (result) {
1107N/A checkUnsafeVarargsConversion(t, s, warn);
40N/A }
1107N/A return result;
1107N/A }
1107N/A //where
1107N/A private boolean isSubtypeUncheckedInternal(Type t, Type s, Warner warn) {
1107N/A if (t.tag == ARRAY && s.tag == ARRAY) {
1107N/A if (((ArrayType)t).elemtype.tag <= lastBaseTag) {
1107N/A return isSameType(elemtype(t), elemtype(s));
1107N/A } else {
1107N/A return isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
1107N/A }
1107N/A } else if (isSubtype(t, s)) {
0N/A return true;
0N/A }
1107N/A else if (t.tag == TYPEVAR) {
1107N/A return isSubtypeUnchecked(t.getUpperBound(), s, warn);
1107N/A }
1107N/A else if (s.tag == UNDETVAR) {
1107N/A UndetVar uv = (UndetVar)s;
1107N/A if (uv.inst != null)
1107N/A return isSubtypeUnchecked(t, uv.inst, warn);
1107N/A }
1107N/A else if (!s.isRaw()) {
1107N/A Type t2 = asSuper(t, s.tsym);
1107N/A if (t2 != null && t2.isRaw()) {
1107N/A if (isReifiable(s))
1107N/A warn.silentWarn(LintCategory.UNCHECKED);
1107N/A else
1107N/A warn.warn(LintCategory.UNCHECKED);
1107N/A return true;
1107N/A }
1107N/A }
1107N/A return false;
0N/A }
1107N/A
1107N/A private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {
1107N/A if (t.tag != ARRAY || isReifiable(t)) return;
1107N/A ArrayType from = (ArrayType)t;
1107N/A boolean shouldWarn = false;
1107N/A switch (s.tag) {
1107N/A case ARRAY:
1107N/A ArrayType to = (ArrayType)s;
1107N/A shouldWarn = from.isVarargs() &&
1107N/A !to.isVarargs() &&
1107N/A !isReifiable(from);
1107N/A break;
1107N/A case CLASS:
1107N/A shouldWarn = from.isVarargs();
1107N/A break;
1107N/A }
1107N/A if (shouldWarn) {
1107N/A warn.warn(LintCategory.VARARGS);
1107N/A }
1107N/A }
0N/A
0N/A /**
0N/A * Is t a subtype of s?<br>
0N/A * (not defined for Method and ForAll types)
0N/A */
0N/A final public boolean isSubtype(Type t, Type s) {
0N/A return isSubtype(t, s, true);
0N/A }
0N/A final public boolean isSubtypeNoCapture(Type t, Type s) {
0N/A return isSubtype(t, s, false);
0N/A }
0N/A public boolean isSubtype(Type t, Type s, boolean capture) {
0N/A if (t == s)
0N/A return true;
0N/A
0N/A if (s.tag >= firstPartialTag)
0N/A return isSuperType(s, t);
0N/A
298N/A if (s.isCompound()) {
298N/A for (Type s2 : interfaces(s).prepend(supertype(s))) {
298N/A if (!isSubtype(t, s2, capture))
298N/A return false;
298N/A }
298N/A return true;
298N/A }
298N/A
0N/A Type lower = lowerBound(s);
0N/A if (s != lower)
0N/A return isSubtype(capture ? capture(t) : t, lower, false);
0N/A
0N/A return isSubtype.visit(capture ? capture(t) : t, s);
0N/A }
0N/A // where
0N/A private TypeRelation isSubtype = new TypeRelation()
0N/A {
0N/A public Boolean visitType(Type t, Type s) {
0N/A switch (t.tag) {
0N/A case BYTE: case CHAR:
0N/A return (t.tag == s.tag ||
0N/A t.tag + 2 <= s.tag && s.tag <= DOUBLE);
0N/A case SHORT: case INT: case LONG: case FLOAT: case DOUBLE:
0N/A return t.tag <= s.tag && s.tag <= DOUBLE;
0N/A case BOOLEAN: case VOID:
0N/A return t.tag == s.tag;
0N/A case TYPEVAR:
0N/A return isSubtypeNoCapture(t.getUpperBound(), s);
0N/A case BOT:
0N/A return
0N/A s.tag == BOT || s.tag == CLASS ||
0N/A s.tag == ARRAY || s.tag == TYPEVAR;
990N/A case WILDCARD: //we shouldn't be here - avoids crash (see 7034495)
0N/A case NONE:
0N/A return false;
0N/A default:
0N/A throw new AssertionError("isSubtype " + t.tag);
0N/A }
0N/A }
0N/A
0N/A private Set<TypePair> cache = new HashSet<TypePair>();
0N/A
0N/A private boolean containsTypeRecursive(Type t, Type s) {
0N/A TypePair pair = new TypePair(t, s);
0N/A if (cache.add(pair)) {
0N/A try {
0N/A return containsType(t.getTypeArguments(),
0N/A s.getTypeArguments());
0N/A } finally {
0N/A cache.remove(pair);
0N/A }
0N/A } else {
0N/A return containsType(t.getTypeArguments(),
0N/A rewriteSupers(s).getTypeArguments());
0N/A }
0N/A }
0N/A
0N/A private Type rewriteSupers(Type t) {
0N/A if (!t.isParameterized())
0N/A return t;
0N/A ListBuffer<Type> from = lb();
0N/A ListBuffer<Type> to = lb();
0N/A adaptSelf(t, from, to);
0N/A if (from.isEmpty())
0N/A return t;
0N/A ListBuffer<Type> rewrite = lb();
0N/A boolean changed = false;
0N/A for (Type orig : to.toList()) {
0N/A Type s = rewriteSupers(orig);
0N/A if (s.isSuperBound() && !s.isExtendsBound()) {
0N/A s = new WildcardType(syms.objectType,
0N/A BoundKind.UNBOUND,
0N/A syms.boundClass);
0N/A changed = true;
0N/A } else if (s != orig) {
0N/A s = new WildcardType(upperBound(s),
0N/A BoundKind.EXTENDS,
0N/A syms.boundClass);
0N/A changed = true;
0N/A }
0N/A rewrite.append(s);
0N/A }
0N/A if (changed)
0N/A return subst(t.tsym.type, from.toList(), rewrite.toList());
0N/A else
0N/A return t;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitClassType(ClassType t, Type s) {
0N/A Type sup = asSuper(t, s.tsym);
0N/A return sup != null
0N/A && sup.tsym == s.tsym
0N/A // You're not allowed to write
0N/A // Vector<Object> vec = new Vector<String>();
0N/A // But with wildcards you can write
0N/A // Vector<? extends Object> vec = new Vector<String>();
0N/A // which means that subtype checking must be done
0N/A // here instead of same-type checking (via containsType).
0N/A && (!s.isParameterized() || containsTypeRecursive(s, sup))
0N/A && isSubtypeNoCapture(sup.getEnclosingType(),
0N/A s.getEnclosingType());
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitArrayType(ArrayType t, Type s) {
0N/A if (s.tag == ARRAY) {
0N/A if (t.elemtype.tag <= lastBaseTag)
0N/A return isSameType(t.elemtype, elemtype(s));
0N/A else
0N/A return isSubtypeNoCapture(t.elemtype, elemtype(s));
0N/A }
0N/A
0N/A if (s.tag == CLASS) {
0N/A Name sname = s.tsym.getQualifiedName();
0N/A return sname == names.java_lang_Object
0N/A || sname == names.java_lang_Cloneable
0N/A || sname == names.java_io_Serializable;
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitUndetVar(UndetVar t, Type s) {
0N/A //todo: test against origin needed? or replace with substitution?
0N/A if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN)
0N/A return true;
0N/A
0N/A if (t.inst != null)
0N/A return isSubtypeNoCapture(t.inst, s); // TODO: ", warn"?
0N/A
0N/A t.hibounds = t.hibounds.prepend(s);
0N/A return true;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitErrorType(ErrorType t, Type s) {
0N/A return true;
0N/A }
0N/A };
0N/A
0N/A /**
0N/A * Is t a subtype of every type in given list `ts'?<br>
0N/A * (not defined for Method and ForAll types)<br>
0N/A * Allows unchecked conversions.
0N/A */
0N/A public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) {
0N/A for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
0N/A if (!isSubtypeUnchecked(t, l.head, warn))
0N/A return false;
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Are corresponding elements of ts subtypes of ss? If lists are
0N/A * of different length, return false.
0N/A */
0N/A public boolean isSubtypes(List<Type> ts, List<Type> ss) {
0N/A while (ts.tail != null && ss.tail != null
0N/A /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
0N/A isSubtype(ts.head, ss.head)) {
0N/A ts = ts.tail;
0N/A ss = ss.tail;
0N/A }
0N/A return ts.tail == null && ss.tail == null;
0N/A /*inlined: ts.isEmpty() && ss.isEmpty();*/
0N/A }
0N/A
0N/A /**
0N/A * Are corresponding elements of ts subtypes of ss, allowing
0N/A * unchecked conversions? If lists are of different length,
0N/A * return false.
0N/A **/
0N/A public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) {
0N/A while (ts.tail != null && ss.tail != null
0N/A /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
0N/A isSubtypeUnchecked(ts.head, ss.head, warn)) {
0N/A ts = ts.tail;
0N/A ss = ss.tail;
0N/A }
0N/A return ts.tail == null && ss.tail == null;
0N/A /*inlined: ts.isEmpty() && ss.isEmpty();*/
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isSuperType">
0N/A /**
0N/A * Is t a supertype of s?
0N/A */
0N/A public boolean isSuperType(Type t, Type s) {
0N/A switch (t.tag) {
0N/A case ERROR:
0N/A return true;
0N/A case UNDETVAR: {
0N/A UndetVar undet = (UndetVar)t;
0N/A if (t == s ||
0N/A undet.qtype == s ||
0N/A s.tag == ERROR ||
0N/A s.tag == BOT) return true;
0N/A if (undet.inst != null)
0N/A return isSubtype(s, undet.inst);
0N/A undet.lobounds = undet.lobounds.prepend(s);
0N/A return true;
0N/A }
0N/A default:
0N/A return isSubtype(s, t);
0N/A }
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isSameType">
0N/A /**
0N/A * Are corresponding elements of the lists the same type? If
0N/A * lists are of different length, return false.
0N/A */
0N/A public boolean isSameTypes(List<Type> ts, List<Type> ss) {
0N/A while (ts.tail != null && ss.tail != null
0N/A /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
0N/A isSameType(ts.head, ss.head)) {
0N/A ts = ts.tail;
0N/A ss = ss.tail;
0N/A }
0N/A return ts.tail == null && ss.tail == null;
0N/A /*inlined: ts.isEmpty() && ss.isEmpty();*/
0N/A }
0N/A
0N/A /**
0N/A * Is t the same type as s?
0N/A */
0N/A public boolean isSameType(Type t, Type s) {
0N/A return isSameType.visit(t, s);
0N/A }
0N/A // where
0N/A private TypeRelation isSameType = new TypeRelation() {
0N/A
0N/A public Boolean visitType(Type t, Type s) {
0N/A if (t == s)
0N/A return true;
0N/A
0N/A if (s.tag >= firstPartialTag)
0N/A return visit(s, t);
0N/A
0N/A switch (t.tag) {
0N/A case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
0N/A case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
0N/A return t.tag == s.tag;
560N/A case TYPEVAR: {
560N/A if (s.tag == TYPEVAR) {
560N/A //type-substitution does not preserve type-var types
560N/A //check that type var symbols and bounds are indeed the same
560N/A return t.tsym == s.tsym &&
560N/A visit(t.getUpperBound(), s.getUpperBound());
560N/A }
560N/A else {
560N/A //special case for s == ? super X, where upper(s) = u
560N/A //check that u == t, where u has been set by Type.withTypeVar
560N/A return s.isSuperBound() &&
560N/A !s.isExtendsBound() &&
560N/A visit(t, upperBound(s));
560N/A }
560N/A }
0N/A default:
0N/A throw new AssertionError("isSameType " + t.tag);
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitWildcardType(WildcardType t, Type s) {
0N/A if (s.tag >= firstPartialTag)
0N/A return visit(s, t);
0N/A else
0N/A return false;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitClassType(ClassType t, Type s) {
0N/A if (t == s)
0N/A return true;
0N/A
0N/A if (s.tag >= firstPartialTag)
0N/A return visit(s, t);
0N/A
0N/A if (s.isSuperBound() && !s.isExtendsBound())
0N/A return visit(t, upperBound(s)) && visit(t, lowerBound(s));
0N/A
0N/A if (t.isCompound() && s.isCompound()) {
0N/A if (!visit(supertype(t), supertype(s)))
0N/A return false;
0N/A
0N/A HashSet<SingletonType> set = new HashSet<SingletonType>();
0N/A for (Type x : interfaces(t))
0N/A set.add(new SingletonType(x));
0N/A for (Type x : interfaces(s)) {
0N/A if (!set.remove(new SingletonType(x)))
0N/A return false;
0N/A }
788N/A return (set.isEmpty());
0N/A }
0N/A return t.tsym == s.tsym
0N/A && visit(t.getEnclosingType(), s.getEnclosingType())
0N/A && containsTypeEquivalent(t.getTypeArguments(), s.getTypeArguments());
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitArrayType(ArrayType t, Type s) {
0N/A if (t == s)
0N/A return true;
0N/A
0N/A if (s.tag >= firstPartialTag)
0N/A return visit(s, t);
0N/A
0N/A return s.tag == ARRAY
0N/A && containsTypeEquivalent(t.elemtype, elemtype(s));
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitMethodType(MethodType t, Type s) {
0N/A // isSameType for methods does not take thrown
0N/A // exceptions into account!
0N/A return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType());
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitPackageType(PackageType t, Type s) {
0N/A return t == s;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitForAll(ForAll t, Type s) {
0N/A if (s.tag != FORALL)
0N/A return false;
0N/A
0N/A ForAll forAll = (ForAll)s;
0N/A return hasSameBounds(t, forAll)
0N/A && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitUndetVar(UndetVar t, Type s) {
0N/A if (s.tag == WILDCARD)
0N/A // FIXME, this might be leftovers from before capture conversion
0N/A return false;
0N/A
0N/A if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN)
0N/A return true;
0N/A
0N/A if (t.inst != null)
0N/A return visit(t.inst, s);
0N/A
0N/A t.inst = fromUnknownFun.apply(s);
0N/A for (List<Type> l = t.lobounds; l.nonEmpty(); l = l.tail) {
0N/A if (!isSubtype(l.head, t.inst))
0N/A return false;
0N/A }
0N/A for (List<Type> l = t.hibounds; l.nonEmpty(); l = l.tail) {
0N/A if (!isSubtype(t.inst, l.head))
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitErrorType(ErrorType t, Type s) {
0N/A return true;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="fromUnknownFun">
0N/A /**
0N/A * A mapping that turns all unknown types in this type to fresh
0N/A * unknown variables.
0N/A */
0N/A public Mapping fromUnknownFun = new Mapping("fromUnknownFun") {
0N/A public Type apply(Type t) {
0N/A if (t.tag == UNKNOWN) return new UndetVar(t);
0N/A else return t.map(this);
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Contains Type">
0N/A public boolean containedBy(Type t, Type s) {
0N/A switch (t.tag) {
0N/A case UNDETVAR:
0N/A if (s.tag == WILDCARD) {
0N/A UndetVar undetvar = (UndetVar)t;
209N/A WildcardType wt = (WildcardType)s;
209N/A switch(wt.kind) {
209N/A case UNBOUND: //similar to ? extends Object
209N/A case EXTENDS: {
209N/A Type bound = upperBound(s);
209N/A // We should check the new upper bound against any of the
209N/A // undetvar's lower bounds.
209N/A for (Type t2 : undetvar.lobounds) {
209N/A if (!isSubtype(t2, bound))
209N/A return false;
209N/A }
209N/A undetvar.hibounds = undetvar.hibounds.prepend(bound);
209N/A break;
209N/A }
209N/A case SUPER: {
209N/A Type bound = lowerBound(s);
209N/A // We should check the new lower bound against any of the
209N/A // undetvar's lower bounds.
209N/A for (Type t2 : undetvar.hibounds) {
209N/A if (!isSubtype(bound, t2))
209N/A return false;
209N/A }
209N/A undetvar.lobounds = undetvar.lobounds.prepend(bound);
209N/A break;
209N/A }
161N/A }
0N/A return true;
0N/A } else {
0N/A return isSameType(t, s);
0N/A }
0N/A case ERROR:
0N/A return true;
0N/A default:
0N/A return containsType(s, t);
0N/A }
0N/A }
0N/A
0N/A boolean containsType(List<Type> ts, List<Type> ss) {
0N/A while (ts.nonEmpty() && ss.nonEmpty()
0N/A && containsType(ts.head, ss.head)) {
0N/A ts = ts.tail;
0N/A ss = ss.tail;
0N/A }
0N/A return ts.isEmpty() && ss.isEmpty();
0N/A }
0N/A
0N/A /**
0N/A * Check if t contains s.
0N/A *
0N/A * <p>T contains S if:
0N/A *
0N/A * <p>{@code L(T) <: L(S) && U(S) <: U(T)}
0N/A *
0N/A * <p>This relation is only used by ClassType.isSubtype(), that
0N/A * is,
0N/A *
0N/A * <p>{@code C<S> <: C<T> if T contains S.}
0N/A *
0N/A * <p>Because of F-bounds, this relation can lead to infinite
0N/A * recursion. Thus we must somehow break that recursion. Notice
0N/A * that containsType() is only called from ClassType.isSubtype().
0N/A * Since the arguments have already been checked against their
0N/A * bounds, we know:
0N/A *
0N/A * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)}
0N/A *
0N/A * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)}
0N/A *
0N/A * @param t a type
0N/A * @param s a type
0N/A */
0N/A public boolean containsType(Type t, Type s) {
0N/A return containsType.visit(t, s);
0N/A }
0N/A // where
0N/A private TypeRelation containsType = new TypeRelation() {
0N/A
0N/A private Type U(Type t) {
0N/A while (t.tag == WILDCARD) {
0N/A WildcardType w = (WildcardType)t;
0N/A if (w.isSuperBound())
0N/A return w.bound == null ? syms.objectType : w.bound.bound;
0N/A else
0N/A t = w.type;
0N/A }
0N/A return t;
0N/A }
0N/A
0N/A private Type L(Type t) {
0N/A while (t.tag == WILDCARD) {
0N/A WildcardType w = (WildcardType)t;
0N/A if (w.isExtendsBound())
0N/A return syms.botType;
0N/A else
0N/A t = w.type;
0N/A }
0N/A return t;
0N/A }
0N/A
0N/A public Boolean visitType(Type t, Type s) {
0N/A if (s.tag >= firstPartialTag)
0N/A return containedBy(s, t);
0N/A else
0N/A return isSameType(t, s);
0N/A }
0N/A
788N/A// void debugContainsType(WildcardType t, Type s) {
788N/A// System.err.println();
788N/A// System.err.format(" does %s contain %s?%n", t, s);
788N/A// System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
788N/A// upperBound(s), s, t, U(t),
788N/A// t.isSuperBound()
788N/A// || isSubtypeNoCapture(upperBound(s), U(t)));
788N/A// System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
788N/A// L(t), t, s, lowerBound(s),
788N/A// t.isExtendsBound()
788N/A// || isSubtypeNoCapture(L(t), lowerBound(s)));
788N/A// System.err.println();
788N/A// }
0N/A
0N/A @Override
0N/A public Boolean visitWildcardType(WildcardType t, Type s) {
0N/A if (s.tag >= firstPartialTag)
0N/A return containedBy(s, t);
0N/A else {
788N/A// debugContainsType(t, s);
0N/A return isSameWildcard(t, s)
0N/A || isCaptureOf(s, t)
0N/A || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), lowerBound(s))) &&
0N/A (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t))));
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitUndetVar(UndetVar t, Type s) {
0N/A if (s.tag != WILDCARD)
0N/A return isSameType(t, s);
0N/A else
0N/A return false;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitErrorType(ErrorType t, Type s) {
0N/A return true;
0N/A }
0N/A };
0N/A
0N/A public boolean isCaptureOf(Type s, WildcardType t) {
78N/A if (s.tag != TYPEVAR || !((TypeVar)s).isCaptured())
0N/A return false;
0N/A return isSameWildcard(t, ((CapturedType)s).wildcard);
0N/A }
0N/A
0N/A public boolean isSameWildcard(WildcardType t, Type s) {
0N/A if (s.tag != WILDCARD)
0N/A return false;
0N/A WildcardType w = (WildcardType)s;
0N/A return w.kind == t.kind && w.type == t.type;
0N/A }
0N/A
0N/A public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) {
0N/A while (ts.nonEmpty() && ss.nonEmpty()
0N/A && containsTypeEquivalent(ts.head, ss.head)) {
0N/A ts = ts.tail;
0N/A ss = ss.tail;
0N/A }
0N/A return ts.isEmpty() && ss.isEmpty();
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isCastable">
0N/A public boolean isCastable(Type t, Type s) {
0N/A return isCastable(t, s, Warner.noWarnings);
0N/A }
0N/A
0N/A /**
0N/A * Is t is castable to s?<br>
0N/A * s is assumed to be an erased type.<br>
0N/A * (not defined for Method and ForAll types).
0N/A */
0N/A public boolean isCastable(Type t, Type s, Warner warn) {
0N/A if (t == s)
0N/A return true;
0N/A
0N/A if (t.isPrimitive() != s.isPrimitive())
983N/A return allowBoxing && (
983N/A isConvertible(t, s, warn)
1006N/A || (allowObjectToPrimitiveCast &&
1006N/A s.isPrimitive() &&
1006N/A isSubtype(boxedClass(s).type, t)));
0N/A if (warn != warnStack.head) {
0N/A try {
0N/A warnStack = warnStack.prepend(warn);
794N/A checkUnsafeVarargsConversion(t, s, warn);
184N/A return isCastable.visit(t,s);
0N/A } finally {
0N/A warnStack = warnStack.tail;
0N/A }
0N/A } else {
184N/A return isCastable.visit(t,s);
0N/A }
0N/A }
0N/A // where
0N/A private TypeRelation isCastable = new TypeRelation() {
0N/A
0N/A public Boolean visitType(Type t, Type s) {
0N/A if (s.tag == ERROR)
0N/A return true;
0N/A
0N/A switch (t.tag) {
0N/A case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
0N/A case DOUBLE:
0N/A return s.tag <= DOUBLE;
0N/A case BOOLEAN:
0N/A return s.tag == BOOLEAN;
0N/A case VOID:
0N/A return false;
0N/A case BOT:
0N/A return isSubtype(t, s);
0N/A default:
0N/A throw new AssertionError();
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitWildcardType(WildcardType t, Type s) {
0N/A return isCastable(upperBound(t), s, warnStack.head);
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitClassType(ClassType t, Type s) {
0N/A if (s.tag == ERROR || s.tag == BOT)
0N/A return true;
0N/A
0N/A if (s.tag == TYPEVAR) {
639N/A if (isCastable(t, s.getUpperBound(), Warner.noWarnings)) {
794N/A warnStack.head.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A if (t.isCompound()) {
210N/A Warner oldWarner = warnStack.head;
210N/A warnStack.head = Warner.noWarnings;
0N/A if (!visit(supertype(t), s))
0N/A return false;
0N/A for (Type intf : interfaces(t)) {
0N/A if (!visit(intf, s))
0N/A return false;
0N/A }
794N/A if (warnStack.head.hasLint(LintCategory.UNCHECKED))
794N/A oldWarner.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A }
0N/A
0N/A if (s.isCompound()) {
0N/A // call recursively to reuse the above code
0N/A return visitClassType((ClassType)s, t);
0N/A }
0N/A
0N/A if (s.tag == CLASS || s.tag == ARRAY) {
0N/A boolean upcast;
0N/A if ((upcast = isSubtype(erasure(t), erasure(s)))
0N/A || isSubtype(erasure(s), erasure(t))) {
0N/A if (!upcast && s.tag == ARRAY) {
0N/A if (!isReifiable(s))
794N/A warnStack.head.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A } else if (s.isRaw()) {
0N/A return true;
0N/A } else if (t.isRaw()) {
0N/A if (!isUnbounded(s))
794N/A warnStack.head.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A }
0N/A // Assume |a| <: |b|
0N/A final Type a = upcast ? t : s;
0N/A final Type b = upcast ? s : t;
0N/A final boolean HIGH = true;
0N/A final boolean LOW = false;
0N/A final boolean DONT_REWRITE_TYPEVARS = false;
0N/A Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS);
0N/A Type aLow = rewriteQuantifiers(a, LOW, DONT_REWRITE_TYPEVARS);
0N/A Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS);
0N/A Type bLow = rewriteQuantifiers(b, LOW, DONT_REWRITE_TYPEVARS);
0N/A Type lowSub = asSub(bLow, aLow.tsym);
0N/A Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
0N/A if (highSub == null) {
0N/A final boolean REWRITE_TYPEVARS = true;
0N/A aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS);
0N/A aLow = rewriteQuantifiers(a, LOW, REWRITE_TYPEVARS);
0N/A bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS);
0N/A bLow = rewriteQuantifiers(b, LOW, REWRITE_TYPEVARS);
0N/A lowSub = asSub(bLow, aLow.tsym);
0N/A highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
0N/A }
0N/A if (highSub != null) {
815N/A if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) {
815N/A Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym);
815N/A }
184N/A if (!disjointTypes(aHigh.allparams(), highSub.allparams())
184N/A && !disjointTypes(aHigh.allparams(), lowSub.allparams())
184N/A && !disjointTypes(aLow.allparams(), highSub.allparams())
184N/A && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
778N/A if (upcast ? giveWarning(a, b) :
234N/A giveWarning(b, a))
794N/A warnStack.head.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A }
0N/A }
0N/A if (isReifiable(s))
0N/A return isSubtypeUnchecked(a, b);
0N/A else
0N/A return isSubtypeUnchecked(a, b, warnStack.head);
0N/A }
0N/A
0N/A // Sidecast
0N/A if (s.tag == CLASS) {
0N/A if ((s.tsym.flags() & INTERFACE) != 0) {
0N/A return ((t.tsym.flags() & FINAL) == 0)
0N/A ? sideCast(t, s, warnStack.head)
0N/A : sideCastFinal(t, s, warnStack.head);
0N/A } else if ((t.tsym.flags() & INTERFACE) != 0) {
0N/A return ((s.tsym.flags() & FINAL) == 0)
0N/A ? sideCast(t, s, warnStack.head)
0N/A : sideCastFinal(t, s, warnStack.head);
0N/A } else {
0N/A // unrelated class types
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitArrayType(ArrayType t, Type s) {
0N/A switch (s.tag) {
0N/A case ERROR:
0N/A case BOT:
0N/A return true;
0N/A case TYPEVAR:
0N/A if (isCastable(s, t, Warner.noWarnings)) {
794N/A warnStack.head.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A case CLASS:
0N/A return isSubtype(t, s);
0N/A case ARRAY:
785N/A if (elemtype(t).tag <= lastBaseTag ||
785N/A elemtype(s).tag <= lastBaseTag) {
0N/A return elemtype(t).tag == elemtype(s).tag;
0N/A } else {
0N/A return visit(elemtype(t), elemtype(s));
0N/A }
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitTypeVar(TypeVar t, Type s) {
0N/A switch (s.tag) {
0N/A case ERROR:
0N/A case BOT:
0N/A return true;
0N/A case TYPEVAR:
0N/A if (isSubtype(t, s)) {
0N/A return true;
0N/A } else if (isCastable(t.bound, s, Warner.noWarnings)) {
794N/A warnStack.head.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A default:
0N/A return isCastable(t.bound, s, warnStack.head);
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitErrorType(ErrorType t, Type s) {
0N/A return true;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="disjointTypes">
0N/A public boolean disjointTypes(List<Type> ts, List<Type> ss) {
0N/A while (ts.tail != null && ss.tail != null) {
0N/A if (disjointType(ts.head, ss.head)) return true;
0N/A ts = ts.tail;
0N/A ss = ss.tail;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Two types or wildcards are considered disjoint if it can be
0N/A * proven that no type can be contained in both. It is
0N/A * conservative in that it is allowed to say that two types are
0N/A * not disjoint, even though they actually are.
0N/A *
0N/A * The type C<X> is castable to C<Y> exactly if X and Y are not
0N/A * disjoint.
0N/A */
0N/A public boolean disjointType(Type t, Type s) {
0N/A return disjointType.visit(t, s);
0N/A }
0N/A // where
0N/A private TypeRelation disjointType = new TypeRelation() {
0N/A
0N/A private Set<TypePair> cache = new HashSet<TypePair>();
0N/A
0N/A public Boolean visitType(Type t, Type s) {
0N/A if (s.tag == WILDCARD)
0N/A return visit(s, t);
0N/A else
0N/A return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t);
0N/A }
0N/A
0N/A private boolean isCastableRecursive(Type t, Type s) {
0N/A TypePair pair = new TypePair(t, s);
0N/A if (cache.add(pair)) {
0N/A try {
0N/A return Types.this.isCastable(t, s);
0N/A } finally {
0N/A cache.remove(pair);
0N/A }
0N/A } else {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A private boolean notSoftSubtypeRecursive(Type t, Type s) {
0N/A TypePair pair = new TypePair(t, s);
0N/A if (cache.add(pair)) {
0N/A try {
0N/A return Types.this.notSoftSubtype(t, s);
0N/A } finally {
0N/A cache.remove(pair);
0N/A }
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitWildcardType(WildcardType t, Type s) {
0N/A if (t.isUnbound())
0N/A return false;
0N/A
0N/A if (s.tag != WILDCARD) {
0N/A if (t.isExtendsBound())
0N/A return notSoftSubtypeRecursive(s, t.type);
0N/A else // isSuperBound()
0N/A return notSoftSubtypeRecursive(t.type, s);
0N/A }
0N/A
0N/A if (s.isUnbound())
0N/A return false;
0N/A
0N/A if (t.isExtendsBound()) {
0N/A if (s.isExtendsBound())
0N/A return !isCastableRecursive(t.type, upperBound(s));
0N/A else if (s.isSuperBound())
0N/A return notSoftSubtypeRecursive(lowerBound(s), t.type);
0N/A } else if (t.isSuperBound()) {
0N/A if (s.isExtendsBound())
0N/A return notSoftSubtypeRecursive(t.type, upperBound(s));
0N/A }
0N/A return false;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="lowerBoundArgtypes">
0N/A /**
0N/A * Returns the lower bounds of the formals of a method.
0N/A */
0N/A public List<Type> lowerBoundArgtypes(Type t) {
0N/A return map(t.getParameterTypes(), lowerBoundMapping);
0N/A }
0N/A private final Mapping lowerBoundMapping = new Mapping("lowerBound") {
0N/A public Type apply(Type t) {
0N/A return lowerBound(t);
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="notSoftSubtype">
0N/A /**
0N/A * This relation answers the question: is impossible that
0N/A * something of type `t' can be a subtype of `s'? This is
0N/A * different from the question "is `t' not a subtype of `s'?"
0N/A * when type variables are involved: Integer is not a subtype of T
0N/A * where <T extends Number> but it is not true that Integer cannot
0N/A * possibly be a subtype of T.
0N/A */
0N/A public boolean notSoftSubtype(Type t, Type s) {
0N/A if (t == s) return false;
0N/A if (t.tag == TYPEVAR) {
0N/A TypeVar tv = (TypeVar) t;
0N/A return !isCastable(tv.bound,
639N/A relaxBound(s),
0N/A Warner.noWarnings);
0N/A }
0N/A if (s.tag != WILDCARD)
0N/A s = upperBound(s);
639N/A
639N/A return !isSubtype(t, relaxBound(s));
639N/A }
639N/A
639N/A private Type relaxBound(Type t) {
639N/A if (t.tag == TYPEVAR) {
639N/A while (t.tag == TYPEVAR)
639N/A t = t.getUpperBound();
639N/A t = rewriteQuantifiers(t, true, true);
639N/A }
639N/A return t;
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isReifiable">
0N/A public boolean isReifiable(Type t) {
0N/A return isReifiable.visit(t);
0N/A }
0N/A // where
0N/A private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() {
0N/A
0N/A public Boolean visitType(Type t, Void ignored) {
0N/A return true;
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitClassType(ClassType t, Void ignored) {
355N/A if (t.isCompound())
355N/A return false;
355N/A else {
355N/A if (!t.isParameterized())
355N/A return true;
355N/A
355N/A for (Type param : t.allparams()) {
355N/A if (!param.isUnbound())
355N/A return false;
355N/A }
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitArrayType(ArrayType t, Void ignored) {
0N/A return visit(t.elemtype);
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitTypeVar(TypeVar t, Void ignored) {
0N/A return false;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Array Utils">
0N/A public boolean isArray(Type t) {
0N/A while (t.tag == WILDCARD)
0N/A t = upperBound(t);
0N/A return t.tag == ARRAY;
0N/A }
0N/A
0N/A /**
0N/A * The element type of an array.
0N/A */
0N/A public Type elemtype(Type t) {
0N/A switch (t.tag) {
0N/A case WILDCARD:
0N/A return elemtype(upperBound(t));
0N/A case ARRAY:
0N/A return ((ArrayType)t).elemtype;
0N/A case FORALL:
0N/A return elemtype(((ForAll)t).qtype);
0N/A case ERROR:
0N/A return t;
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A
786N/A public Type elemtypeOrType(Type t) {
786N/A Type elemtype = elemtype(t);
786N/A return elemtype != null ?
786N/A elemtype :
786N/A t;
786N/A }
786N/A
0N/A /**
0N/A * Mapping to take element type of an arraytype
0N/A */
0N/A private Mapping elemTypeFun = new Mapping ("elemTypeFun") {
0N/A public Type apply(Type t) { return elemtype(t); }
0N/A };
0N/A
0N/A /**
0N/A * The number of dimensions of an array type.
0N/A */
0N/A public int dimensions(Type t) {
0N/A int result = 0;
0N/A while (t.tag == ARRAY) {
0N/A result++;
0N/A t = elemtype(t);
0N/A }
0N/A return result;
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="asSuper">
0N/A /**
0N/A * Return the (most specific) base type of t that starts with the
0N/A * given symbol. If none exists, return null.
0N/A *
0N/A * @param t a type
0N/A * @param sym a symbol
0N/A */
0N/A public Type asSuper(Type t, Symbol sym) {
0N/A return asSuper.visit(t, sym);
0N/A }
0N/A // where
0N/A private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
0N/A
0N/A public Type visitType(Type t, Symbol sym) {
0N/A return null;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitClassType(ClassType t, Symbol sym) {
0N/A if (t.tsym == sym)
0N/A return t;
0N/A
0N/A Type st = supertype(t);
18N/A if (st.tag == CLASS || st.tag == TYPEVAR || st.tag == ERROR) {
0N/A Type x = asSuper(st, sym);
0N/A if (x != null)
0N/A return x;
0N/A }
0N/A if ((sym.flags() & INTERFACE) != 0) {
0N/A for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
0N/A Type x = asSuper(l.head, sym);
0N/A if (x != null)
0N/A return x;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitArrayType(ArrayType t, Symbol sym) {
0N/A return isSubtype(t, sym.type) ? sym.type : null;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitTypeVar(TypeVar t, Symbol sym) {
18N/A if (t.tsym == sym)
18N/A return t;
18N/A else
18N/A return asSuper(t.bound, sym);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitErrorType(ErrorType t, Symbol sym) {
0N/A return t;
0N/A }
0N/A };
0N/A
0N/A /**
0N/A * Return the base type of t or any of its outer types that starts
0N/A * with the given symbol. If none exists, return null.
0N/A *
0N/A * @param t a type
0N/A * @param sym a symbol
0N/A */
0N/A public Type asOuterSuper(Type t, Symbol sym) {
0N/A switch (t.tag) {
0N/A case CLASS:
0N/A do {
0N/A Type s = asSuper(t, sym);
0N/A if (s != null) return s;
0N/A t = t.getEnclosingType();
0N/A } while (t.tag == CLASS);
0N/A return null;
0N/A case ARRAY:
0N/A return isSubtype(t, sym.type) ? sym.type : null;
0N/A case TYPEVAR:
0N/A return asSuper(t, sym);
0N/A case ERROR:
0N/A return t;
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the base type of t or any of its enclosing types that
0N/A * starts with the given symbol. If none exists, return null.
0N/A *
0N/A * @param t a type
0N/A * @param sym a symbol
0N/A */
0N/A public Type asEnclosingSuper(Type t, Symbol sym) {
0N/A switch (t.tag) {
0N/A case CLASS:
0N/A do {
0N/A Type s = asSuper(t, sym);
0N/A if (s != null) return s;
0N/A Type outer = t.getEnclosingType();
0N/A t = (outer.tag == CLASS) ? outer :
0N/A (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type :
0N/A Type.noType;
0N/A } while (t.tag == CLASS);
0N/A return null;
0N/A case ARRAY:
0N/A return isSubtype(t, sym.type) ? sym.type : null;
0N/A case TYPEVAR:
0N/A return asSuper(t, sym);
0N/A case ERROR:
0N/A return t;
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="memberType">
0N/A /**
0N/A * The type of given symbol, seen as a member of t.
0N/A *
0N/A * @param t a type
0N/A * @param sym a symbol
0N/A */
0N/A public Type memberType(Type t, Symbol sym) {
0N/A return (sym.flags() & STATIC) != 0
0N/A ? sym.type
0N/A : memberType.visit(t, sym);
340N/A }
0N/A // where
0N/A private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() {
0N/A
0N/A public Type visitType(Type t, Symbol sym) {
0N/A return sym.type;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitWildcardType(WildcardType t, Symbol sym) {
0N/A return memberType(upperBound(t), sym);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitClassType(ClassType t, Symbol sym) {
0N/A Symbol owner = sym.owner;
0N/A long flags = sym.flags();
0N/A if (((flags & STATIC) == 0) && owner.type.isParameterized()) {
0N/A Type base = asOuterSuper(t, owner);
133N/A //if t is an intersection type T = CT & I1 & I2 ... & In
133N/A //its supertypes CT, I1, ... In might contain wildcards
133N/A //so we need to go through capture conversion
133N/A base = t.isCompound() ? capture(base) : base;
0N/A if (base != null) {
0N/A List<Type> ownerParams = owner.type.allparams();
0N/A List<Type> baseParams = base.allparams();
0N/A if (ownerParams.nonEmpty()) {
0N/A if (baseParams.isEmpty()) {
0N/A // then base is a raw type
0N/A return erasure(sym.type);
0N/A } else {
0N/A return subst(sym.type, ownerParams, baseParams);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return sym.type;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitTypeVar(TypeVar t, Symbol sym) {
0N/A return memberType(t.bound, sym);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitErrorType(ErrorType t, Symbol sym) {
0N/A return t;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isAssignable">
0N/A public boolean isAssignable(Type t, Type s) {
0N/A return isAssignable(t, s, Warner.noWarnings);
0N/A }
0N/A
0N/A /**
0N/A * Is t assignable to s?<br>
0N/A * Equivalent to subtype except for constant values and raw
0N/A * types.<br>
0N/A * (not defined for Method and ForAll types)
0N/A */
0N/A public boolean isAssignable(Type t, Type s, Warner warn) {
0N/A if (t.tag == ERROR)
0N/A return true;
0N/A if (t.tag <= INT && t.constValue() != null) {
0N/A int value = ((Number)t.constValue()).intValue();
0N/A switch (s.tag) {
0N/A case BYTE:
0N/A if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE)
0N/A return true;
0N/A break;
0N/A case CHAR:
0N/A if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE)
0N/A return true;
0N/A break;
0N/A case SHORT:
0N/A if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE)
0N/A return true;
0N/A break;
0N/A case INT:
0N/A return true;
0N/A case CLASS:
0N/A switch (unboxedType(s).tag) {
0N/A case BYTE:
0N/A case CHAR:
0N/A case SHORT:
0N/A return isAssignable(t, unboxedType(s), warn);
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A return isConvertible(t, s, warn);
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="erasure">
0N/A /**
0N/A * The erasure of t {@code |t|} -- the type that results when all
0N/A * type parameters in t are deleted.
0N/A */
0N/A public Type erasure(Type t) {
1260N/A return eraseNotNeeded(t)? t : erasure(t, false);
29N/A }
29N/A //where
1260N/A private boolean eraseNotNeeded(Type t) {
1260N/A return (t.tag <= lastBaseTag) || (syms.stringType.tsym == t.tsym);
1260N/A }
1260N/A
1260N/A //where
29N/A private Type erasure(Type t, boolean recurse) {
0N/A if (t.tag <= lastBaseTag)
0N/A return t; /* fast special case */
0N/A else
29N/A return erasure.visit(t, recurse);
340N/A }
0N/A // where
29N/A private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() {
29N/A public Type visitType(Type t, Boolean recurse) {
0N/A if (t.tag <= lastBaseTag)
0N/A return t; /*fast special case*/
0N/A else
29N/A return t.map(recurse ? erasureRecFun : erasureFun);
0N/A }
0N/A
0N/A @Override
29N/A public Type visitWildcardType(WildcardType t, Boolean recurse) {
29N/A return erasure(upperBound(t), recurse);
0N/A }
0N/A
0N/A @Override
29N/A public Type visitClassType(ClassType t, Boolean recurse) {
29N/A Type erased = t.tsym.erasure(Types.this);
29N/A if (recurse) {
29N/A erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym);
29N/A }
29N/A return erased;
0N/A }
0N/A
0N/A @Override
29N/A public Type visitTypeVar(TypeVar t, Boolean recurse) {
29N/A return erasure(t.bound, recurse);
29N/A }
29N/A
29N/A @Override
29N/A public Type visitErrorType(ErrorType t, Boolean recurse) {
0N/A return t;
0N/A }
0N/A };
29N/A
0N/A private Mapping erasureFun = new Mapping ("erasure") {
0N/A public Type apply(Type t) { return erasure(t); }
0N/A };
0N/A
29N/A private Mapping erasureRecFun = new Mapping ("erasureRecursive") {
29N/A public Type apply(Type t) { return erasureRecursive(t); }
29N/A };
29N/A
0N/A public List<Type> erasure(List<Type> ts) {
0N/A return Type.map(ts, erasureFun);
0N/A }
29N/A
29N/A public Type erasureRecursive(Type t) {
29N/A return erasure(t, true);
29N/A }
29N/A
29N/A public List<Type> erasureRecursive(List<Type> ts) {
29N/A return Type.map(ts, erasureRecFun);
29N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="makeCompoundType">
0N/A /**
0N/A * Make a compound type from non-empty list of types
0N/A *
0N/A * @param bounds the types from which the compound type is formed
0N/A * @param supertype is objectType if all bounds are interfaces,
0N/A * null otherwise.
0N/A */
0N/A public Type makeCompoundType(List<Type> bounds,
0N/A Type supertype) {
0N/A ClassSymbol bc =
0N/A new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC,
0N/A Type.moreInfo
0N/A ? names.fromString(bounds.toString())
0N/A : names.empty,
0N/A syms.noSymbol);
0N/A if (bounds.head.tag == TYPEVAR)
0N/A // error condition, recover
120N/A bc.erasure_field = syms.objectType;
120N/A else
120N/A bc.erasure_field = erasure(bounds.head);
120N/A bc.members_field = new Scope(bc);
0N/A ClassType bt = (ClassType)bc.type;
0N/A bt.allparams_field = List.nil();
0N/A if (supertype != null) {
0N/A bt.supertype_field = supertype;
0N/A bt.interfaces_field = bounds;
0N/A } else {
0N/A bt.supertype_field = bounds.head;
0N/A bt.interfaces_field = bounds.tail;
0N/A }
815N/A Assert.check(bt.supertype_field.tsym.completer != null
815N/A || !bt.supertype_field.isInterface(),
815N/A bt.supertype_field);
0N/A return bt;
0N/A }
0N/A
0N/A /**
0N/A * Same as {@link #makeCompoundType(List,Type)}, except that the
0N/A * second parameter is computed directly. Note that this might
0N/A * cause a symbol completion. Hence, this version of
0N/A * makeCompoundType may not be called during a classfile read.
0N/A */
0N/A public Type makeCompoundType(List<Type> bounds) {
0N/A Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
0N/A supertype(bounds.head) : null;
0N/A return makeCompoundType(bounds, supertype);
0N/A }
0N/A
0N/A /**
0N/A * A convenience wrapper for {@link #makeCompoundType(List)}; the
0N/A * arguments are converted to a list and passed to the other
0N/A * method. Note that this might cause a symbol completion.
0N/A * Hence, this version of makeCompoundType may not be called
0N/A * during a classfile read.
0N/A */
0N/A public Type makeCompoundType(Type bound1, Type bound2) {
0N/A return makeCompoundType(List.of(bound1, bound2));
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="supertype">
0N/A public Type supertype(Type t) {
0N/A return supertype.visit(t);
0N/A }
0N/A // where
0N/A private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() {
0N/A
0N/A public Type visitType(Type t, Void ignored) {
0N/A // A note on wildcards: there is no good way to
0N/A // determine a supertype for a super bounded wildcard.
0N/A return null;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitClassType(ClassType t, Void ignored) {
0N/A if (t.supertype_field == null) {
0N/A Type supertype = ((ClassSymbol)t.tsym).getSuperclass();
0N/A // An interface has no superclass; its supertype is Object.
0N/A if (t.isInterface())
0N/A supertype = ((ClassType)t.tsym.type).supertype_field;
0N/A if (t.supertype_field == null) {
0N/A List<Type> actuals = classBound(t).allparams();
0N/A List<Type> formals = t.tsym.type.allparams();
29N/A if (t.hasErasedSupertypes()) {
29N/A t.supertype_field = erasureRecursive(supertype);
29N/A } else if (formals.nonEmpty()) {
0N/A t.supertype_field = subst(supertype, formals, actuals);
0N/A }
29N/A else {
29N/A t.supertype_field = supertype;
29N/A }
0N/A }
0N/A }
0N/A return t.supertype_field;
0N/A }
0N/A
0N/A /**
0N/A * The supertype is always a class type. If the type
0N/A * variable's bounds start with a class type, this is also
0N/A * the supertype. Otherwise, the supertype is
0N/A * java.lang.Object.
0N/A */
0N/A @Override
0N/A public Type visitTypeVar(TypeVar t, Void ignored) {
0N/A if (t.bound.tag == TYPEVAR ||
0N/A (!t.bound.isCompound() && !t.bound.isInterface())) {
0N/A return t.bound;
0N/A } else {
0N/A return supertype(t.bound);
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Type visitArrayType(ArrayType t, Void ignored) {
0N/A if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType))
0N/A return arraySuperType();
0N/A else
0N/A return new ArrayType(supertype(t.elemtype), t.tsym);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitErrorType(ErrorType t, Void ignored) {
0N/A return t;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="interfaces">
0N/A /**
0N/A * Return the interfaces implemented by this class.
0N/A */
0N/A public List<Type> interfaces(Type t) {
0N/A return interfaces.visit(t);
0N/A }
0N/A // where
0N/A private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() {
0N/A
0N/A public List<Type> visitType(Type t, Void ignored) {
0N/A return List.nil();
0N/A }
0N/A
0N/A @Override
0N/A public List<Type> visitClassType(ClassType t, Void ignored) {
0N/A if (t.interfaces_field == null) {
0N/A List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces();
0N/A if (t.interfaces_field == null) {
0N/A // If t.interfaces_field is null, then t must
0N/A // be a parameterized type (not to be confused
0N/A // with a generic type declaration).
0N/A // Terminology:
0N/A // Parameterized type: List<String>
0N/A // Generic type declaration: class List<E> { ... }
0N/A // So t corresponds to List<String> and
0N/A // t.tsym.type corresponds to List<E>.
0N/A // The reason t must be parameterized type is
0N/A // that completion will happen as a side
0N/A // effect of calling
0N/A // ClassSymbol.getInterfaces. Since
0N/A // t.interfaces_field is null after
0N/A // completion, we can assume that t is not the
0N/A // type of a class/interface declaration.
815N/A Assert.check(t != t.tsym.type, t);
0N/A List<Type> actuals = t.allparams();
0N/A List<Type> formals = t.tsym.type.allparams();
29N/A if (t.hasErasedSupertypes()) {
29N/A t.interfaces_field = erasureRecursive(interfaces);
29N/A } else if (formals.nonEmpty()) {
0N/A t.interfaces_field =
0N/A upperBounds(subst(interfaces, formals, actuals));
0N/A }
29N/A else {
29N/A t.interfaces_field = interfaces;
29N/A }
0N/A }
0N/A }
0N/A return t.interfaces_field;
0N/A }
0N/A
0N/A @Override
0N/A public List<Type> visitTypeVar(TypeVar t, Void ignored) {
0N/A if (t.bound.isCompound())
0N/A return interfaces(t.bound);
0N/A
0N/A if (t.bound.isInterface())
0N/A return List.of(t.bound);
0N/A
0N/A return List.nil();
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="isDerivedRaw">
0N/A Map<Type,Boolean> isDerivedRawCache = new HashMap<Type,Boolean>();
0N/A
0N/A public boolean isDerivedRaw(Type t) {
0N/A Boolean result = isDerivedRawCache.get(t);
0N/A if (result == null) {
0N/A result = isDerivedRawInternal(t);
0N/A isDerivedRawCache.put(t, result);
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A public boolean isDerivedRawInternal(Type t) {
0N/A if (t.isErroneous())
0N/A return false;
0N/A return
0N/A t.isRaw() ||
0N/A supertype(t) != null && isDerivedRaw(supertype(t)) ||
0N/A isDerivedRaw(interfaces(t));
0N/A }
0N/A
0N/A public boolean isDerivedRaw(List<Type> ts) {
0N/A List<Type> l = ts;
0N/A while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail;
0N/A return l.nonEmpty();
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="setBounds">
0N/A /**
0N/A * Set the bounds field of the given type variable to reflect a
0N/A * (possibly multiple) list of bounds.
0N/A * @param t a type variable
0N/A * @param bounds the bounds, must be nonempty
0N/A * @param supertype is objectType if all bounds are interfaces,
0N/A * null otherwise.
0N/A */
0N/A public void setBounds(TypeVar t, List<Type> bounds, Type supertype) {
0N/A if (bounds.tail.isEmpty())
0N/A t.bound = bounds.head;
0N/A else
0N/A t.bound = makeCompoundType(bounds, supertype);
0N/A t.rank_field = -1;
0N/A }
0N/A
0N/A /**
0N/A * Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that
562N/A * third parameter is computed directly, as follows: if all
562N/A * all bounds are interface types, the computed supertype is Object,
562N/A * otherwise the supertype is simply left null (in this case, the supertype
562N/A * is assumed to be the head of the bound list passed as second argument).
562N/A * Note that this check might cause a symbol completion. Hence, this version of
0N/A * setBounds may not be called during a classfile read.
0N/A */
0N/A public void setBounds(TypeVar t, List<Type> bounds) {
0N/A Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
562N/A syms.objectType : null;
0N/A setBounds(t, bounds, supertype);
0N/A t.rank_field = -1;
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="getBounds">
0N/A /**
0N/A * Return list of bounds of the given type variable.
0N/A */
0N/A public List<Type> getBounds(TypeVar t) {
0N/A if (t.bound.isErroneous() || !t.bound.isCompound())
0N/A return List.of(t.bound);
0N/A else if ((erasure(t).tsym.flags() & INTERFACE) == 0)
0N/A return interfaces(t).prepend(supertype(t));
0N/A else
0N/A // No superclass was given in bounds.
0N/A // In this case, supertype is Object, erasure is first interface.
0N/A return interfaces(t);
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="classBound">
0N/A /**
0N/A * If the given type is a (possibly selected) type variable,
0N/A * return the bounding class of this type, otherwise return the
0N/A * type itself.
0N/A */
0N/A public Type classBound(Type t) {
0N/A return classBound.visit(t);
0N/A }
0N/A // where
0N/A private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() {
0N/A
0N/A public Type visitType(Type t, Void ignored) {
0N/A return t;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitClassType(ClassType t, Void ignored) {
0N/A Type outer1 = classBound(t.getEnclosingType());
0N/A if (outer1 != t.getEnclosingType())
0N/A return new ClassType(outer1, t.getTypeArguments(), t.tsym);
0N/A else
0N/A return t;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitTypeVar(TypeVar t, Void ignored) {
0N/A return classBound(supertype(t));
0N/A }
0N/A
0N/A @Override
0N/A public Type visitErrorType(ErrorType t, Void ignored) {
0N/A return t;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence">
0N/A /**
0N/A * Returns true iff the first signature is a <em>sub
0N/A * signature</em> of the other. This is <b>not</b> an equivalence
0N/A * relation.
0N/A *
971N/A * @jls section 8.4.2.
0N/A * @see #overrideEquivalent(Type t, Type s)
0N/A * @param t first signature (possibly raw).
0N/A * @param s second signature (could be subjected to erasure).
0N/A * @return true if t is a sub signature of s.
0N/A */
0N/A public boolean isSubSignature(Type t, Type s) {
906N/A return isSubSignature(t, s, true);
906N/A }
906N/A
906N/A public boolean isSubSignature(Type t, Type s, boolean strict) {
906N/A return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict);
0N/A }
0N/A
0N/A /**
0N/A * Returns true iff these signatures are related by <em>override
0N/A * equivalence</em>. This is the natural extension of
0N/A * isSubSignature to an equivalence relation.
0N/A *
971N/A * @jls section 8.4.2.
0N/A * @see #isSubSignature(Type t, Type s)
0N/A * @param t a signature (possible raw, could be subjected to
0N/A * erasure).
0N/A * @param s a signature (possible raw, could be subjected to
0N/A * erasure).
0N/A * @return true if either argument is a sub signature of the other.
0N/A */
0N/A public boolean overrideEquivalent(Type t, Type s) {
0N/A return hasSameArgs(t, s) ||
0N/A hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s);
0N/A }
0N/A
672N/A // <editor-fold defaultstate="collapsed" desc="Determining method implementation in given site">
672N/A class ImplementationCache {
672N/A
672N/A private WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>> _map =
672N/A new WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>>();
672N/A
672N/A class Entry {
672N/A final MethodSymbol cachedImpl;
672N/A final Filter<Symbol> implFilter;
672N/A final boolean checkResult;
876N/A final int prevMark;
672N/A
672N/A public Entry(MethodSymbol cachedImpl,
672N/A Filter<Symbol> scopeFilter,
876N/A boolean checkResult,
876N/A int prevMark) {
672N/A this.cachedImpl = cachedImpl;
672N/A this.implFilter = scopeFilter;
672N/A this.checkResult = checkResult;
876N/A this.prevMark = prevMark;
672N/A }
672N/A
876N/A boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) {
672N/A return this.implFilter == scopeFilter &&
876N/A this.checkResult == checkResult &&
876N/A this.prevMark == mark;
672N/A }
340N/A }
672N/A
857N/A MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
672N/A SoftReference<Map<TypeSymbol, Entry>> ref_cache = _map.get(ms);
672N/A Map<TypeSymbol, Entry> cache = ref_cache != null ? ref_cache.get() : null;
672N/A if (cache == null) {
672N/A cache = new HashMap<TypeSymbol, Entry>();
672N/A _map.put(ms, new SoftReference<Map<TypeSymbol, Entry>>(cache));
672N/A }
672N/A Entry e = cache.get(origin);
1013N/A CompoundScope members = membersClosure(origin.type, true);
672N/A if (e == null ||
876N/A !e.matches(implFilter, checkResult, members.getMark())) {
876N/A MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter);
876N/A cache.put(origin, new Entry(impl, implFilter, checkResult, members.getMark()));
672N/A return impl;
672N/A }
672N/A else {
672N/A return e.cachedImpl;
672N/A }
672N/A }
672N/A
876N/A private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
876N/A for (Type t = origin.type; t.tag == CLASS || t.tag == TYPEVAR; t = supertype(t)) {
340N/A while (t.tag == TYPEVAR)
340N/A t = t.getUpperBound();
340N/A TypeSymbol c = t.tsym;
672N/A for (Scope.Entry e = c.members().lookup(ms.name, implFilter);
340N/A e.scope != null;
779N/A e = e.next(implFilter)) {
672N/A if (e.sym != null &&
876N/A e.sym.overrides(ms, origin, Types.this, checkResult))
672N/A return (MethodSymbol)e.sym;
340N/A }
340N/A }
672N/A return null;
340N/A }
340N/A }
340N/A
672N/A private ImplementationCache implCache = new ImplementationCache();
672N/A
857N/A public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
857N/A return implCache.get(ms, origin, checkResult, implFilter);
857N/A }
857N/A // </editor-fold>
857N/A
857N/A // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site">
1013N/A class MembersClosureCache extends SimpleVisitor<CompoundScope, Boolean> {
1013N/A
1013N/A private WeakHashMap<TypeSymbol, Entry> _map =
1013N/A new WeakHashMap<TypeSymbol, Entry>();
1013N/A
1013N/A class Entry {
1013N/A final boolean skipInterfaces;
1013N/A final CompoundScope compoundScope;
1013N/A
1013N/A public Entry(boolean skipInterfaces, CompoundScope compoundScope) {
1013N/A this.skipInterfaces = skipInterfaces;
1013N/A this.compoundScope = compoundScope;
1013N/A }
1013N/A
1013N/A boolean matches(boolean skipInterfaces) {
1013N/A return this.skipInterfaces == skipInterfaces;
1013N/A }
1013N/A }
1013N/A
1013N/A /** members closure visitor methods **/
1013N/A
1013N/A public CompoundScope visitType(Type t, Boolean skipInterface) {
857N/A return null;
857N/A }
857N/A
857N/A @Override
1013N/A public CompoundScope visitClassType(ClassType t, Boolean skipInterface) {
857N/A ClassSymbol csym = (ClassSymbol)t.tsym;
1013N/A Entry e = _map.get(csym);
1013N/A if (e == null || !e.matches(skipInterface)) {
876N/A CompoundScope membersClosure = new CompoundScope(csym);
1013N/A if (!skipInterface) {
1013N/A for (Type i : interfaces(t)) {
1013N/A membersClosure.addSubScope(visit(i, skipInterface));
1013N/A }
857N/A }
1013N/A membersClosure.addSubScope(visit(supertype(t), skipInterface));
876N/A membersClosure.addSubScope(csym.members());
1013N/A e = new Entry(skipInterface, membersClosure);
1013N/A _map.put(csym, e);
857N/A }
1013N/A return e.compoundScope;
857N/A }
857N/A
857N/A @Override
1013N/A public CompoundScope visitTypeVar(TypeVar t, Boolean skipInterface) {
1013N/A return visit(t.getUpperBound(), skipInterface);
857N/A }
1013N/A }
1013N/A
1013N/A private MembersClosureCache membersCache = new MembersClosureCache();
1013N/A
1013N/A public CompoundScope membersClosure(Type site, boolean skipInterface) {
1013N/A return membersCache.visit(site, skipInterface);
1013N/A }
672N/A // </editor-fold>
672N/A
0N/A /**
0N/A * Does t have the same arguments as s? It is assumed that both
0N/A * types are (possibly polymorphic) method types. Monomorphic
0N/A * method types "have the same arguments", if their argument lists
0N/A * are equal. Polymorphic method types "have the same arguments",
0N/A * if they have the same arguments after renaming all type
0N/A * variables of one to corresponding type variables in the other,
0N/A * where correspondence is by position in the type parameter list.
0N/A */
0N/A public boolean hasSameArgs(Type t, Type s) {
906N/A return hasSameArgs(t, s, true);
906N/A }
906N/A
906N/A public boolean hasSameArgs(Type t, Type s, boolean strict) {
906N/A return hasSameArgs(t, s, strict ? hasSameArgs_strict : hasSameArgs_nonstrict);
906N/A }
906N/A
906N/A private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) {
0N/A return hasSameArgs.visit(t, s);
0N/A }
0N/A // where
906N/A private class HasSameArgs extends TypeRelation {
906N/A
906N/A boolean strict;
906N/A
906N/A public HasSameArgs(boolean strict) {
906N/A this.strict = strict;
906N/A }
0N/A
0N/A public Boolean visitType(Type t, Type s) {
0N/A throw new AssertionError();
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitMethodType(MethodType t, Type s) {
0N/A return s.tag == METHOD
0N/A && containsTypeEquivalent(t.argtypes, s.getParameterTypes());
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitForAll(ForAll t, Type s) {
0N/A if (s.tag != FORALL)
906N/A return strict ? false : visitMethodType(t.asMethodType(), s);
0N/A
0N/A ForAll forAll = (ForAll)s;
0N/A return hasSameBounds(t, forAll)
0N/A && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
0N/A }
0N/A
0N/A @Override
0N/A public Boolean visitErrorType(ErrorType t, Type s) {
0N/A return false;
0N/A }
0N/A };
906N/A
906N/A TypeRelation hasSameArgs_strict = new HasSameArgs(true);
906N/A TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false);
906N/A
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="subst">
0N/A public List<Type> subst(List<Type> ts,
0N/A List<Type> from,
0N/A List<Type> to) {
0N/A return new Subst(from, to).subst(ts);
0N/A }
0N/A
0N/A /**
0N/A * Substitute all occurrences of a type in `from' with the
0N/A * corresponding type in `to' in 't'. Match lists `from' and `to'
0N/A * from the right: If lists have different length, discard leading
0N/A * elements of the longer list.
0N/A */
0N/A public Type subst(Type t, List<Type> from, List<Type> to) {
0N/A return new Subst(from, to).subst(t);
0N/A }
0N/A
0N/A private class Subst extends UnaryVisitor<Type> {
0N/A List<Type> from;
0N/A List<Type> to;
0N/A
0N/A public Subst(List<Type> from, List<Type> to) {
0N/A int fromLength = from.length();
0N/A int toLength = to.length();
0N/A while (fromLength > toLength) {
0N/A fromLength--;
0N/A from = from.tail;
0N/A }
0N/A while (fromLength < toLength) {
0N/A toLength--;
0N/A to = to.tail;
0N/A }
0N/A this.from = from;
0N/A this.to = to;
0N/A }
0N/A
0N/A Type subst(Type t) {
0N/A if (from.tail == null)
0N/A return t;
0N/A else
0N/A return visit(t);
237N/A }
0N/A
0N/A List<Type> subst(List<Type> ts) {
0N/A if (from.tail == null)
0N/A return ts;
0N/A boolean wild = false;
0N/A if (ts.nonEmpty() && from.nonEmpty()) {
0N/A Type head1 = subst(ts.head);
0N/A List<Type> tail1 = subst(ts.tail);
0N/A if (head1 != ts.head || tail1 != ts.tail)
0N/A return tail1.prepend(head1);
0N/A }
0N/A return ts;
0N/A }
0N/A
0N/A public Type visitType(Type t, Void ignored) {
0N/A return t;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitMethodType(MethodType t, Void ignored) {
0N/A List<Type> argtypes = subst(t.argtypes);
0N/A Type restype = subst(t.restype);
0N/A List<Type> thrown = subst(t.thrown);
0N/A if (argtypes == t.argtypes &&
0N/A restype == t.restype &&
0N/A thrown == t.thrown)
0N/A return t;
0N/A else
0N/A return new MethodType(argtypes, restype, thrown, t.tsym);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitTypeVar(TypeVar t, Void ignored) {
0N/A for (List<Type> from = this.from, to = this.to;
0N/A from.nonEmpty();
0N/A from = from.tail, to = to.tail) {
0N/A if (t == from.head) {
0N/A return to.head.withTypeVar(t);
0N/A }
0N/A }
0N/A return t;
0N/A }
0N/A
0N/A @Override
0N/A public Type visitClassType(ClassType t, Void ignored) {
0N/A if (!t.isCompound()) {
0N/A List<Type> typarams = t.getTypeArguments();
0N/A List<Type> typarams1 = subst(typarams);
0N/A Type outer = t.getEnclosingType();
0N/A Type outer1 = subst(outer);
0N/A if (typarams1 == typarams && outer1 == outer)
0N/A return t;
0N/A else
0N/A return new ClassType(outer1, typarams1, t.tsym);
0N/A } else {
0N/A Type st = subst(supertype(t));
0N/A List<Type> is = upperBounds(subst(interfaces(t)));
0N/A if (st == supertype(t) && is == interfaces(t))
0N/A return t;
0N/A else
0N/A return makeCompoundType(is.prepend(st));
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Type visitWildcardType(WildcardType t, Void ignored) {
0N/A Type bound = t.type;
0N/A if (t.kind != BoundKind.UNBOUND)
0N/A bound = subst(bound);
0N/A if (bound == t.type) {
0N/A return t;
0N/A } else {
0N/A if (t.isExtendsBound() && bound.isExtendsBound())
0N/A bound = upperBound(bound);
0N/A return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Type visitArrayType(ArrayType t, Void ignored) {
0N/A Type elemtype = subst(t.elemtype);
0N/A if (elemtype == t.elemtype)
0N/A return t;
0N/A else
995N/A return new ArrayType(upperBound(elemtype), t.tsym);
0N/A }
0N/A
0N/A @Override
0N/A public Type visitForAll(ForAll t, Void ignored) {
845N/A if (Type.containsAny(to, t.tvars)) {
845N/A //perform alpha-renaming of free-variables in 't'
845N/A //if 'to' types contain variables that are free in 't'
845N/A List<Type> freevars = newInstances(t.tvars);
845N/A t = new ForAll(freevars,
845N/A Types.this.subst(t.qtype, t.tvars, freevars));
845N/A }
0N/A List<Type> tvars1 = substBounds(t.tvars, from, to);
0N/A Type qtype1 = subst(t.qtype);
0N/A if (tvars1 == t.tvars && qtype1 == t.qtype) {
0N/A return t;
0N/A } else if (tvars1 == t.tvars) {
0N/A return new ForAll(tvars1, qtype1);
0N/A } else {
0N/A return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1));
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public Type visitErrorType(ErrorType t, Void ignored) {
0N/A return t;
0N/A }
0N/A }
0N/A
0N/A public List<Type> substBounds(List<Type> tvars,
0N/A List<Type> from,
0N/A List<Type> to) {
0N/A if (tvars.isEmpty())
0N/A return tvars;
0N/A ListBuffer<Type> newBoundsBuf = lb();
0N/A boolean changed = false;
0N/A // calculate new bounds
0N/A for (Type t : tvars) {
0N/A TypeVar tv = (TypeVar) t;
0N/A Type bound = subst(tv.bound, from, to);
0N/A if (bound != tv.bound)
0N/A changed = true;
0N/A newBoundsBuf.append(bound);
0N/A }
0N/A if (!changed)
0N/A return tvars;
0N/A ListBuffer<Type> newTvars = lb();
0N/A // create new type variables without bounds
0N/A for (Type t : tvars) {
0N/A newTvars.append(new TypeVar(t.tsym, null, syms.botType));
0N/A }
0N/A // the new bounds should use the new type variables in place
0N/A // of the old
0N/A List<Type> newBounds = newBoundsBuf.toList();
0N/A from = tvars;
0N/A to = newTvars.toList();
0N/A for (; !newBounds.isEmpty(); newBounds = newBounds.tail) {
0N/A newBounds.head = subst(newBounds.head, from, to);
0N/A }
0N/A newBounds = newBoundsBuf.toList();
0N/A // set the bounds of new type variables to the new bounds
0N/A for (Type t : newTvars.toList()) {
0N/A TypeVar tv = (TypeVar) t;
0N/A tv.bound = newBounds.head;
0N/A newBounds = newBounds.tail;
0N/A }
0N/A return newTvars.toList();
0N/A }
0N/A
0N/A public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) {
0N/A Type bound1 = subst(t.bound, from, to);
0N/A if (bound1 == t.bound)
0N/A return t;
211N/A else {
211N/A // create new type variable without bounds
211N/A TypeVar tv = new TypeVar(t.tsym, null, syms.botType);
211N/A // the new bound should use the new type variable in place
211N/A // of the old
211N/A tv.bound = subst(bound1, List.<Type>of(t), List.<Type>of(tv));
211N/A return tv;
211N/A }
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="hasSameBounds">
0N/A /**
0N/A * Does t have the same bounds for quantified variables as s?
0N/A */
0N/A boolean hasSameBounds(ForAll t, ForAll s) {
0N/A List<Type> l1 = t.tvars;
0N/A List<Type> l2 = s.tvars;
0N/A while (l1.nonEmpty() && l2.nonEmpty() &&
0N/A isSameType(l1.head.getUpperBound(),
0N/A subst(l2.head.getUpperBound(),
0N/A s.tvars,
0N/A t.tvars))) {
0N/A l1 = l1.tail;
0N/A l2 = l2.tail;
0N/A }
0N/A return l1.isEmpty() && l2.isEmpty();
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="newInstances">
0N/A /** Create new vector of type variables from list of variables
0N/A * changing all recursive bounds from old to new list.
0N/A */
0N/A public List<Type> newInstances(List<Type> tvars) {
0N/A List<Type> tvars1 = Type.map(tvars, newInstanceFun);
0N/A for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) {
0N/A TypeVar tv = (TypeVar) l.head;
0N/A tv.bound = subst(tv.bound, tvars, tvars1);
0N/A }
0N/A return tvars1;
0N/A }
0N/A static private Mapping newInstanceFun = new Mapping("newInstanceFun") {
0N/A public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound()); }
0N/A };
0N/A // </editor-fold>
0N/A
879N/A public Type createMethodTypeWithParameters(Type original, List<Type> newParams) {
879N/A return original.accept(methodWithParameters, newParams);
879N/A }
879N/A // where
879N/A private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() {
879N/A public Type visitType(Type t, List<Type> newParams) {
879N/A throw new IllegalArgumentException("Not a method type: " + t);
879N/A }
879N/A public Type visitMethodType(MethodType t, List<Type> newParams) {
879N/A return new MethodType(newParams, t.restype, t.thrown, t.tsym);
879N/A }
879N/A public Type visitForAll(ForAll t, List<Type> newParams) {
879N/A return new ForAll(t.tvars, t.qtype.accept(this, newParams));
879N/A }
879N/A };
879N/A
879N/A public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) {
879N/A return original.accept(methodWithThrown, newThrown);
879N/A }
879N/A // where
879N/A private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() {
879N/A public Type visitType(Type t, List<Type> newThrown) {
879N/A throw new IllegalArgumentException("Not a method type: " + t);
879N/A }
879N/A public Type visitMethodType(MethodType t, List<Type> newThrown) {
879N/A return new MethodType(t.argtypes, t.restype, newThrown, t.tsym);
879N/A }
879N/A public Type visitForAll(ForAll t, List<Type> newThrown) {
879N/A return new ForAll(t.tvars, t.qtype.accept(this, newThrown));
879N/A }
879N/A };
879N/A
949N/A public Type createMethodTypeWithReturn(Type original, Type newReturn) {
949N/A return original.accept(methodWithReturn, newReturn);
949N/A }
949N/A // where
949N/A private final MapVisitor<Type> methodWithReturn = new MapVisitor<Type>() {
949N/A public Type visitType(Type t, Type newReturn) {
949N/A throw new IllegalArgumentException("Not a method type: " + t);
949N/A }
949N/A public Type visitMethodType(MethodType t, Type newReturn) {
949N/A return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym);
949N/A }
949N/A public Type visitForAll(ForAll t, Type newReturn) {
949N/A return new ForAll(t.tvars, t.qtype.accept(this, newReturn));
949N/A }
949N/A };
949N/A
109N/A // <editor-fold defaultstate="collapsed" desc="createErrorType">
109N/A public Type createErrorType(Type originalType) {
109N/A return new ErrorType(originalType, syms.errSymbol);
109N/A }
109N/A
109N/A public Type createErrorType(ClassSymbol c, Type originalType) {
109N/A return new ErrorType(c, originalType);
109N/A }
109N/A
109N/A public Type createErrorType(Name name, TypeSymbol container, Type originalType) {
109N/A return new ErrorType(name, container, originalType);
109N/A }
109N/A // </editor-fold>
109N/A
0N/A // <editor-fold defaultstate="collapsed" desc="rank">
0N/A /**
0N/A * The rank of a class is the length of the longest path between
0N/A * the class and java.lang.Object in the class inheritance
0N/A * graph. Undefined for all but reference types.
0N/A */
0N/A public int rank(Type t) {
0N/A switch(t.tag) {
0N/A case CLASS: {
0N/A ClassType cls = (ClassType)t;
0N/A if (cls.rank_field < 0) {
0N/A Name fullname = cls.tsym.getQualifiedName();
112N/A if (fullname == names.java_lang_Object)
0N/A cls.rank_field = 0;
0N/A else {
0N/A int r = rank(supertype(cls));
0N/A for (List<Type> l = interfaces(cls);
0N/A l.nonEmpty();
0N/A l = l.tail) {
0N/A if (rank(l.head) > r)
0N/A r = rank(l.head);
0N/A }
0N/A cls.rank_field = r + 1;
0N/A }
0N/A }
0N/A return cls.rank_field;
0N/A }
0N/A case TYPEVAR: {
0N/A TypeVar tvar = (TypeVar)t;
0N/A if (tvar.rank_field < 0) {
0N/A int r = rank(supertype(tvar));
0N/A for (List<Type> l = interfaces(tvar);
0N/A l.nonEmpty();
0N/A l = l.tail) {
0N/A if (rank(l.head) > r) r = rank(l.head);
0N/A }
0N/A tvar.rank_field = r + 1;
0N/A }
0N/A return tvar.rank_field;
0N/A }
0N/A case ERROR:
0N/A return 0;
0N/A default:
0N/A throw new AssertionError();
0N/A }
0N/A }
0N/A // </editor-fold>
0N/A
120N/A /**
237N/A * Helper method for generating a string representation of a given type
120N/A * accordingly to a given locale
120N/A */
120N/A public String toString(Type t, Locale locale) {
237N/A return Printer.createStandardPrinter(messages).visit(t, locale);
120N/A }
120N/A
120N/A /**
237N/A * Helper method for generating a string representation of a given type
120N/A * accordingly to a given locale
120N/A */
120N/A public String toString(Symbol t, Locale locale) {
237N/A return Printer.createStandardPrinter(messages).visit(t, locale);
120N/A }
120N/A
0N/A // <editor-fold defaultstate="collapsed" desc="toString">
0N/A /**
0N/A * This toString is slightly more descriptive than the one on Type.
120N/A *
120N/A * @deprecated Types.toString(Type t, Locale l) provides better support
120N/A * for localization
0N/A */
120N/A @Deprecated
0N/A public String toString(Type t) {
0N/A if (t.tag == FORALL) {
0N/A ForAll forAll = (ForAll)t;
0N/A return typaramsString(forAll.tvars) + forAll.qtype;
0N/A }
0N/A return "" + t;
0N/A }
0N/A // where
0N/A private String typaramsString(List<Type> tvars) {
903N/A StringBuilder s = new StringBuilder();
0N/A s.append('<');
0N/A boolean first = true;
0N/A for (Type t : tvars) {
0N/A if (!first) s.append(", ");
0N/A first = false;
0N/A appendTyparamString(((TypeVar)t), s);
0N/A }
0N/A s.append('>');
0N/A return s.toString();
0N/A }
903N/A private void appendTyparamString(TypeVar t, StringBuilder buf) {
0N/A buf.append(t);
0N/A if (t.bound == null ||
0N/A t.bound.tsym.getQualifiedName() == names.java_lang_Object)
0N/A return;
0N/A buf.append(" extends "); // Java syntax; no need for i18n
0N/A Type bound = t.bound;
0N/A if (!bound.isCompound()) {
0N/A buf.append(bound);
0N/A } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
0N/A buf.append(supertype(t));
0N/A for (Type intf : interfaces(t)) {
0N/A buf.append('&');
0N/A buf.append(intf);
0N/A }
0N/A } else {
0N/A // No superclass was given in bounds.
0N/A // In this case, supertype is Object, erasure is first interface.
0N/A boolean first = true;
0N/A for (Type intf : interfaces(t)) {
0N/A if (!first) buf.append('&');
0N/A first = false;
0N/A buf.append(intf);
0N/A }
0N/A }
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types">
0N/A /**
0N/A * A cache for closures.
0N/A *
0N/A * <p>A closure is a list of all the supertypes and interfaces of
0N/A * a class or interface type, ordered by ClassSymbol.precedes
0N/A * (that is, subclasses come first, arbitrary but fixed
0N/A * otherwise).
0N/A */
0N/A private Map<Type,List<Type>> closureCache = new HashMap<Type,List<Type>>();
0N/A
0N/A /**
0N/A * Returns the closure of a class or interface type.
0N/A */
0N/A public List<Type> closure(Type t) {
0N/A List<Type> cl = closureCache.get(t);
0N/A if (cl == null) {
0N/A Type st = supertype(t);
0N/A if (!t.isCompound()) {
0N/A if (st.tag == CLASS) {
0N/A cl = insert(closure(st), t);
0N/A } else if (st.tag == TYPEVAR) {
0N/A cl = closure(st).prepend(t);
0N/A } else {
0N/A cl = List.of(t);
0N/A }
0N/A } else {
0N/A cl = closure(supertype(t));
0N/A }
0N/A for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail)
0N/A cl = union(cl, closure(l.head));
0N/A closureCache.put(t, cl);
0N/A }
0N/A return cl;
0N/A }
0N/A
0N/A /**
0N/A * Insert a type in a closure
0N/A */
0N/A public List<Type> insert(List<Type> cl, Type t) {
0N/A if (cl.isEmpty() || t.tsym.precedes(cl.head.tsym, this)) {
0N/A return cl.prepend(t);
0N/A } else if (cl.head.tsym.precedes(t.tsym, this)) {
0N/A return insert(cl.tail, t).prepend(cl.head);
0N/A } else {
0N/A return cl;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Form the union of two closures
0N/A */
0N/A public List<Type> union(List<Type> cl1, List<Type> cl2) {
0N/A if (cl1.isEmpty()) {
0N/A return cl2;
0N/A } else if (cl2.isEmpty()) {
0N/A return cl1;
0N/A } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) {
0N/A return union(cl1.tail, cl2).prepend(cl1.head);
0N/A } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) {
0N/A return union(cl1, cl2.tail).prepend(cl2.head);
0N/A } else {
0N/A return union(cl1.tail, cl2.tail).prepend(cl1.head);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Intersect two closures
0N/A */
0N/A public List<Type> intersect(List<Type> cl1, List<Type> cl2) {
0N/A if (cl1 == cl2)
0N/A return cl1;
0N/A if (cl1.isEmpty() || cl2.isEmpty())
0N/A return List.nil();
0N/A if (cl1.head.tsym.precedes(cl2.head.tsym, this))
0N/A return intersect(cl1.tail, cl2);
0N/A if (cl2.head.tsym.precedes(cl1.head.tsym, this))
0N/A return intersect(cl1, cl2.tail);
0N/A if (isSameType(cl1.head, cl2.head))
0N/A return intersect(cl1.tail, cl2.tail).prepend(cl1.head);
0N/A if (cl1.head.tsym == cl2.head.tsym &&
0N/A cl1.head.tag == CLASS && cl2.head.tag == CLASS) {
0N/A if (cl1.head.isParameterized() && cl2.head.isParameterized()) {
0N/A Type merge = merge(cl1.head,cl2.head);
0N/A return intersect(cl1.tail, cl2.tail).prepend(merge);
0N/A }
0N/A if (cl1.head.isRaw() || cl2.head.isRaw())
0N/A return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head));
0N/A }
0N/A return intersect(cl1.tail, cl2.tail);
0N/A }
0N/A // where
0N/A class TypePair {
0N/A final Type t1;
0N/A final Type t2;
0N/A TypePair(Type t1, Type t2) {
0N/A this.t1 = t1;
0N/A this.t2 = t2;
0N/A }
0N/A @Override
0N/A public int hashCode() {
506N/A return 127 * Types.hashCode(t1) + Types.hashCode(t2);
0N/A }
0N/A @Override
0N/A public boolean equals(Object obj) {
0N/A if (!(obj instanceof TypePair))
0N/A return false;
0N/A TypePair typePair = (TypePair)obj;
0N/A return isSameType(t1, typePair.t1)
0N/A && isSameType(t2, typePair.t2);
0N/A }
0N/A }
0N/A Set<TypePair> mergeCache = new HashSet<TypePair>();
0N/A private Type merge(Type c1, Type c2) {
0N/A ClassType class1 = (ClassType) c1;
0N/A List<Type> act1 = class1.getTypeArguments();
0N/A ClassType class2 = (ClassType) c2;
0N/A List<Type> act2 = class2.getTypeArguments();
0N/A ListBuffer<Type> merged = new ListBuffer<Type>();
0N/A List<Type> typarams = class1.tsym.type.getTypeArguments();
0N/A
0N/A while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) {
0N/A if (containsType(act1.head, act2.head)) {
0N/A merged.append(act1.head);
0N/A } else if (containsType(act2.head, act1.head)) {
0N/A merged.append(act2.head);
0N/A } else {
0N/A TypePair pair = new TypePair(c1, c2);
0N/A Type m;
0N/A if (mergeCache.add(pair)) {
0N/A m = new WildcardType(lub(upperBound(act1.head),
0N/A upperBound(act2.head)),
0N/A BoundKind.EXTENDS,
0N/A syms.boundClass);
0N/A mergeCache.remove(pair);
0N/A } else {
0N/A m = new WildcardType(syms.objectType,
0N/A BoundKind.UNBOUND,
0N/A syms.boundClass);
0N/A }
0N/A merged.append(m.withTypeVar(typarams.head));
0N/A }
0N/A act1 = act1.tail;
0N/A act2 = act2.tail;
0N/A typarams = typarams.tail;
0N/A }
815N/A Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
0N/A return new ClassType(class1.getEnclosingType(), merged.toList(), class1.tsym);
0N/A }
0N/A
0N/A /**
0N/A * Return the minimum type of a closure, a compound type if no
0N/A * unique minimum exists.
0N/A */
0N/A private Type compoundMin(List<Type> cl) {
0N/A if (cl.isEmpty()) return syms.objectType;
0N/A List<Type> compound = closureMin(cl);
0N/A if (compound.isEmpty())
0N/A return null;
0N/A else if (compound.tail.isEmpty())
0N/A return compound.head;
0N/A else
0N/A return makeCompoundType(compound);
0N/A }
0N/A
0N/A /**
0N/A * Return the minimum types of a closure, suitable for computing
0N/A * compoundMin or glb.
0N/A */
0N/A private List<Type> closureMin(List<Type> cl) {
0N/A ListBuffer<Type> classes = lb();
0N/A ListBuffer<Type> interfaces = lb();
0N/A while (!cl.isEmpty()) {
0N/A Type current = cl.head;
0N/A if (current.isInterface())
0N/A interfaces.append(current);
0N/A else
0N/A classes.append(current);
0N/A ListBuffer<Type> candidates = lb();
0N/A for (Type t : cl.tail) {
0N/A if (!isSubtypeNoCapture(current, t))
0N/A candidates.append(t);
0N/A }
0N/A cl = candidates.toList();
0N/A }
0N/A return classes.appendList(interfaces).toList();
0N/A }
0N/A
0N/A /**
0N/A * Return the least upper bound of pair of types. if the lub does
0N/A * not exist return null.
0N/A */
0N/A public Type lub(Type t1, Type t2) {
0N/A return lub(List.of(t1, t2));
0N/A }
0N/A
0N/A /**
0N/A * Return the least upper bound (lub) of set of types. If the lub
0N/A * does not exist return the type of null (bottom).
0N/A */
0N/A public Type lub(List<Type> ts) {
0N/A final int ARRAY_BOUND = 1;
0N/A final int CLASS_BOUND = 2;
0N/A int boundkind = 0;
0N/A for (Type t : ts) {
0N/A switch (t.tag) {
0N/A case CLASS:
0N/A boundkind |= CLASS_BOUND;
0N/A break;
0N/A case ARRAY:
0N/A boundkind |= ARRAY_BOUND;
0N/A break;
0N/A case TYPEVAR:
0N/A do {
0N/A t = t.getUpperBound();
0N/A } while (t.tag == TYPEVAR);
0N/A if (t.tag == ARRAY) {
0N/A boundkind |= ARRAY_BOUND;
0N/A } else {
0N/A boundkind |= CLASS_BOUND;
0N/A }
0N/A break;
0N/A default:
0N/A if (t.isPrimitive())
4N/A return syms.errType;
0N/A }
0N/A }
0N/A switch (boundkind) {
0N/A case 0:
0N/A return syms.botType;
0N/A
0N/A case ARRAY_BOUND:
0N/A // calculate lub(A[], B[])
0N/A List<Type> elements = Type.map(ts, elemTypeFun);
0N/A for (Type t : elements) {
0N/A if (t.isPrimitive()) {
0N/A // if a primitive type is found, then return
0N/A // arraySuperType unless all the types are the
0N/A // same
0N/A Type first = ts.head;
0N/A for (Type s : ts.tail) {
0N/A if (!isSameType(first, s)) {
0N/A // lub(int[], B[]) is Cloneable & Serializable
0N/A return arraySuperType();
0N/A }
0N/A }
0N/A // all the array types are the same, return one
0N/A // lub(int[], int[]) is int[]
0N/A return first;
0N/A }
0N/A }
0N/A // lub(A[], B[]) is lub(A, B)[]
0N/A return new ArrayType(lub(elements), syms.arrayClass);
0N/A
0N/A case CLASS_BOUND:
0N/A // calculate lub(A, B)
0N/A while (ts.head.tag != CLASS && ts.head.tag != TYPEVAR)
0N/A ts = ts.tail;
815N/A Assert.check(!ts.isEmpty());
895N/A //step 1 - compute erased candidate set (EC)
895N/A List<Type> cl = erasedSupertypes(ts.head);
0N/A for (Type t : ts.tail) {
0N/A if (t.tag == CLASS || t.tag == TYPEVAR)
895N/A cl = intersect(cl, erasedSupertypes(t));
0N/A }
895N/A //step 2 - compute minimal erased candidate set (MEC)
895N/A List<Type> mec = closureMin(cl);
895N/A //step 3 - for each element G in MEC, compute lci(Inv(G))
895N/A List<Type> candidates = List.nil();
895N/A for (Type erasedSupertype : mec) {
895N/A List<Type> lci = List.of(asSuper(ts.head, erasedSupertype.tsym));
895N/A for (Type t : ts) {
895N/A lci = intersect(lci, List.of(asSuper(t, erasedSupertype.tsym)));
895N/A }
895N/A candidates = candidates.appendList(lci);
895N/A }
895N/A //step 4 - let MEC be { G1, G2 ... Gn }, then we have that
895N/A //lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn))
895N/A return compoundMin(candidates);
0N/A
0N/A default:
0N/A // calculate lub(A, B[])
0N/A List<Type> classes = List.of(arraySuperType());
0N/A for (Type t : ts) {
0N/A if (t.tag != ARRAY) // Filter out any arrays
0N/A classes = classes.prepend(t);
0N/A }
0N/A // lub(A, B[]) is lub(A, arraySuperType)
0N/A return lub(classes);
0N/A }
0N/A }
0N/A // where
895N/A List<Type> erasedSupertypes(Type t) {
895N/A ListBuffer<Type> buf = lb();
895N/A for (Type sup : closure(t)) {
895N/A if (sup.tag == TYPEVAR) {
895N/A buf.append(sup);
895N/A } else {
895N/A buf.append(erasure(sup));
895N/A }
895N/A }
895N/A return buf.toList();
895N/A }
895N/A
0N/A private Type arraySuperType = null;
0N/A private Type arraySuperType() {
0N/A // initialized lazily to avoid problems during compiler startup
0N/A if (arraySuperType == null) {
0N/A synchronized (this) {
0N/A if (arraySuperType == null) {
0N/A // JLS 10.8: all arrays implement Cloneable and Serializable.
0N/A arraySuperType = makeCompoundType(List.of(syms.serializableType,
0N/A syms.cloneableType),
0N/A syms.objectType);
0N/A }
0N/A }
0N/A }
0N/A return arraySuperType;
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Greatest lower bound">
209N/A public Type glb(List<Type> ts) {
209N/A Type t1 = ts.head;
209N/A for (Type t2 : ts.tail) {
209N/A if (t1.isErroneous())
209N/A return t1;
209N/A t1 = glb(t1, t2);
209N/A }
209N/A return t1;
209N/A }
209N/A //where
0N/A public Type glb(Type t, Type s) {
0N/A if (s == null)
0N/A return t;
752N/A else if (t.isPrimitive() || s.isPrimitive())
752N/A return syms.errType;
0N/A else if (isSubtypeNoCapture(t, s))
0N/A return t;
0N/A else if (isSubtypeNoCapture(s, t))
0N/A return s;
0N/A
0N/A List<Type> closure = union(closure(t), closure(s));
0N/A List<Type> bounds = closureMin(closure);
0N/A
0N/A if (bounds.isEmpty()) { // length == 0
0N/A return syms.objectType;
0N/A } else if (bounds.tail.isEmpty()) { // length == 1
0N/A return bounds.head;
0N/A } else { // length > 1
0N/A int classCount = 0;
0N/A for (Type bound : bounds)
0N/A if (!bound.isInterface())
0N/A classCount++;
0N/A if (classCount > 1)
109N/A return createErrorType(t);
0N/A }
0N/A return makeCompoundType(bounds);
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="hashCode">
0N/A /**
0N/A * Compute a hash code on a type.
0N/A */
0N/A public static int hashCode(Type t) {
0N/A return hashCode.visit(t);
0N/A }
0N/A // where
0N/A private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() {
0N/A
0N/A public Integer visitType(Type t, Void ignored) {
0N/A return t.tag;
0N/A }
0N/A
0N/A @Override
0N/A public Integer visitClassType(ClassType t, Void ignored) {
0N/A int result = visit(t.getEnclosingType());
0N/A result *= 127;
0N/A result += t.tsym.flatName().hashCode();
0N/A for (Type s : t.getTypeArguments()) {
0N/A result *= 127;
0N/A result += visit(s);
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A @Override
0N/A public Integer visitWildcardType(WildcardType t, Void ignored) {
0N/A int result = t.kind.hashCode();
0N/A if (t.type != null) {
0N/A result *= 127;
0N/A result += visit(t.type);
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A @Override
0N/A public Integer visitArrayType(ArrayType t, Void ignored) {
0N/A return visit(t.elemtype) + 12;
0N/A }
0N/A
0N/A @Override
0N/A public Integer visitTypeVar(TypeVar t, Void ignored) {
0N/A return System.identityHashCode(t.tsym);
0N/A }
0N/A
0N/A @Override
0N/A public Integer visitUndetVar(UndetVar t, Void ignored) {
0N/A return System.identityHashCode(t);
0N/A }
0N/A
0N/A @Override
0N/A public Integer visitErrorType(ErrorType t, Void ignored) {
0N/A return 0;
0N/A }
0N/A };
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable">
0N/A /**
0N/A * Does t have a result that is a subtype of the result type of s,
0N/A * suitable for covariant returns? It is assumed that both types
0N/A * are (possibly polymorphic) method types. Monomorphic method
0N/A * types are handled in the obvious way. Polymorphic method types
0N/A * require renaming all type variables of one to corresponding
0N/A * type variables in the other, where correspondence is by
0N/A * position in the type parameter list. */
0N/A public boolean resultSubtype(Type t, Type s, Warner warner) {
0N/A List<Type> tvars = t.getTypeArguments();
0N/A List<Type> svars = s.getTypeArguments();
0N/A Type tres = t.getReturnType();
0N/A Type sres = subst(s.getReturnType(), svars, tvars);
0N/A return covariantReturnType(tres, sres, warner);
0N/A }
0N/A
0N/A /**
0N/A * Return-Type-Substitutable.
971N/A * @jls section 8.4.5
0N/A */
0N/A public boolean returnTypeSubstitutable(Type r1, Type r2) {
0N/A if (hasSameArgs(r1, r2))
201N/A return resultSubtype(r1, r2, Warner.noWarnings);
0N/A else
0N/A return covariantReturnType(r1.getReturnType(),
201N/A erasure(r2.getReturnType()),
201N/A Warner.noWarnings);
201N/A }
201N/A
201N/A public boolean returnTypeSubstitutable(Type r1,
201N/A Type r2, Type r2res,
201N/A Warner warner) {
201N/A if (isSameType(r1.getReturnType(), r2res))
201N/A return true;
201N/A if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
201N/A return false;
201N/A
201N/A if (hasSameArgs(r1, r2))
201N/A return covariantReturnType(r1.getReturnType(), r2res, warner);
983N/A if (!allowCovariantReturns)
201N/A return false;
201N/A if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
201N/A return true;
201N/A if (!isSubtype(r1.getReturnType(), erasure(r2res)))
201N/A return false;
794N/A warner.warn(LintCategory.UNCHECKED);
201N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Is t an appropriate return type in an overrider for a
0N/A * method that returns s?
0N/A */
0N/A public boolean covariantReturnType(Type t, Type s, Warner warner) {
201N/A return
201N/A isSameType(t, s) ||
983N/A allowCovariantReturns &&
0N/A !t.isPrimitive() &&
201N/A !s.isPrimitive() &&
201N/A isAssignable(t, s, warner);
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Box/unbox support">
0N/A /**
0N/A * Return the class that boxes the given primitive.
0N/A */
0N/A public ClassSymbol boxedClass(Type t) {
0N/A return reader.enterClass(syms.boxedName[t.tag]);
0N/A }
0N/A
0N/A /**
752N/A * Return the boxed type if 't' is primitive, otherwise return 't' itself.
752N/A */
752N/A public Type boxedTypeOrType(Type t) {
752N/A return t.isPrimitive() ?
752N/A boxedClass(t).type :
752N/A t;
752N/A }
752N/A
752N/A /**
0N/A * Return the primitive type corresponding to a boxed type.
0N/A */
0N/A public Type unboxedType(Type t) {
0N/A if (allowBoxing) {
0N/A for (int i=0; i<syms.boxedName.length; i++) {
0N/A Name box = syms.boxedName[i];
0N/A if (box != null &&
0N/A asSuper(t, reader.enterClass(box)) != null)
0N/A return syms.typeOfTag[i];
0N/A }
0N/A }
0N/A return Type.noType;
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Capture conversion">
0N/A /*
971N/A * JLS 5.1.10 Capture Conversion:
0N/A *
0N/A * Let G name a generic type declaration with n formal type
0N/A * parameters A1 ... An with corresponding bounds U1 ... Un. There
0N/A * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>,
0N/A * where, for 1 <= i <= n:
0N/A *
0N/A * + If Ti is a wildcard type argument (4.5.1) of the form ? then
0N/A * Si is a fresh type variable whose upper bound is
0N/A * Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null
0N/A * type.
0N/A *
0N/A * + If Ti is a wildcard type argument of the form ? extends Bi,
0N/A * then Si is a fresh type variable whose upper bound is
0N/A * glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is
0N/A * the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is
0N/A * a compile-time error if for any two classes (not interfaces)
0N/A * Vi and Vj,Vi is not a subclass of Vj or vice versa.
0N/A *
0N/A * + If Ti is a wildcard type argument of the form ? super Bi,
0N/A * then Si is a fresh type variable whose upper bound is
0N/A * Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi.
0N/A *
0N/A * + Otherwise, Si = Ti.
0N/A *
0N/A * Capture conversion on any type other than a parameterized type
0N/A * (4.5) acts as an identity conversion (5.1.1). Capture
0N/A * conversions never require a special action at run time and
0N/A * therefore never throw an exception at run time.
0N/A *
0N/A * Capture conversion is not applied recursively.
0N/A */
0N/A /**
971N/A * Capture conversion as specified by the JLS.
0N/A */
298N/A
298N/A public List<Type> capture(List<Type> ts) {
298N/A List<Type> buf = List.nil();
298N/A for (Type t : ts) {
298N/A buf = buf.prepend(capture(t));
298N/A }
298N/A return buf.reverse();
298N/A }
0N/A public Type capture(Type t) {
0N/A if (t.tag != CLASS)
0N/A return t;
636N/A if (t.getEnclosingType() != Type.noType) {
636N/A Type capturedEncl = capture(t.getEnclosingType());
636N/A if (capturedEncl != t.getEnclosingType()) {
636N/A Type type1 = memberType(capturedEncl, t.tsym);
636N/A t = subst(type1, t.tsym.type.getTypeArguments(), t.getTypeArguments());
636N/A }
636N/A }
0N/A ClassType cls = (ClassType)t;
0N/A if (cls.isRaw() || !cls.isParameterized())
0N/A return cls;
0N/A
0N/A ClassType G = (ClassType)cls.asElement().asType();
0N/A List<Type> A = G.getTypeArguments();
0N/A List<Type> T = cls.getTypeArguments();
0N/A List<Type> S = freshTypeVariables(T);
0N/A
0N/A List<Type> currentA = A;
0N/A List<Type> currentT = T;
0N/A List<Type> currentS = S;
0N/A boolean captured = false;
0N/A while (!currentA.isEmpty() &&
0N/A !currentT.isEmpty() &&
0N/A !currentS.isEmpty()) {
0N/A if (currentS.head != currentT.head) {
0N/A captured = true;
0N/A WildcardType Ti = (WildcardType)currentT.head;
0N/A Type Ui = currentA.head.getUpperBound();
0N/A CapturedType Si = (CapturedType)currentS.head;
0N/A if (Ui == null)
0N/A Ui = syms.objectType;
0N/A switch (Ti.kind) {
0N/A case UNBOUND:
0N/A Si.bound = subst(Ui, A, S);
0N/A Si.lower = syms.botType;
0N/A break;
0N/A case EXTENDS:
0N/A Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S));
0N/A Si.lower = syms.botType;
0N/A break;
0N/A case SUPER:
0N/A Si.bound = subst(Ui, A, S);
0N/A Si.lower = Ti.getSuperBound();
0N/A break;
0N/A }
0N/A if (Si.bound == Si.lower)
0N/A currentS.head = Si.bound;
0N/A }
0N/A currentA = currentA.tail;
0N/A currentT = currentT.tail;
0N/A currentS = currentS.tail;
0N/A }
0N/A if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty())
0N/A return erasure(t); // some "rare" type involved
0N/A
0N/A if (captured)
0N/A return new ClassType(cls.getEnclosingType(), S, cls.tsym);
0N/A else
0N/A return t;
0N/A }
0N/A // where
237N/A public List<Type> freshTypeVariables(List<Type> types) {
0N/A ListBuffer<Type> result = lb();
0N/A for (Type t : types) {
0N/A if (t.tag == WILDCARD) {
0N/A Type bound = ((WildcardType)t).getExtendsBound();
0N/A if (bound == null)
0N/A bound = syms.objectType;
0N/A result.append(new CapturedType(capturedName,
0N/A syms.noSymbol,
0N/A bound,
0N/A syms.botType,
0N/A (WildcardType)t));
0N/A } else {
0N/A result.append(t);
0N/A }
0N/A }
0N/A return result.toList();
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
0N/A private List<Type> upperBounds(List<Type> ss) {
0N/A if (ss.isEmpty()) return ss;
0N/A Type head = upperBound(ss.head);
0N/A List<Type> tail = upperBounds(ss.tail);
0N/A if (head != ss.head || tail != ss.tail)
0N/A return tail.prepend(head);
0N/A else
0N/A return ss;
0N/A }
0N/A
0N/A private boolean sideCast(Type from, Type to, Warner warn) {
0N/A // We are casting from type $from$ to type $to$, which are
0N/A // non-final unrelated types. This method
0N/A // tries to reject a cast by transferring type parameters
0N/A // from $to$ to $from$ by common superinterfaces.
0N/A boolean reverse = false;
0N/A Type target = to;
0N/A if ((to.tsym.flags() & INTERFACE) == 0) {
815N/A Assert.check((from.tsym.flags() & INTERFACE) != 0);
0N/A reverse = true;
0N/A to = from;
0N/A from = target;
0N/A }
0N/A List<Type> commonSupers = superClosure(to, erasure(from));
0N/A boolean giveWarning = commonSupers.isEmpty();
0N/A // The arguments to the supers could be unified here to
0N/A // get a more accurate analysis
0N/A while (commonSupers.nonEmpty()) {
0N/A Type t1 = asSuper(from, commonSupers.head.tsym);
0N/A Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
0N/A if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
0N/A return false;
0N/A giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
0N/A commonSupers = commonSupers.tail;
0N/A }
186N/A if (giveWarning && !isReifiable(reverse ? from : to))
794N/A warn.warn(LintCategory.UNCHECKED);
983N/A if (!allowCovariantReturns)
0N/A // reject if there is a common method signature with
0N/A // incompatible return types.
0N/A chk.checkCompatibleAbstracts(warn.pos(), from, to);
0N/A return true;
0N/A }
0N/A
0N/A private boolean sideCastFinal(Type from, Type to, Warner warn) {
0N/A // We are casting from type $from$ to type $to$, which are
0N/A // unrelated types one of which is final and the other of
0N/A // which is an interface. This method
0N/A // tries to reject a cast by transferring type parameters
0N/A // from the final class to the interface.
0N/A boolean reverse = false;
0N/A Type target = to;
0N/A if ((to.tsym.flags() & INTERFACE) == 0) {
815N/A Assert.check((from.tsym.flags() & INTERFACE) != 0);
0N/A reverse = true;
0N/A to = from;
0N/A from = target;
0N/A }
815N/A Assert.check((from.tsym.flags() & FINAL) != 0);
0N/A Type t1 = asSuper(from, to.tsym);
0N/A if (t1 == null) return false;
0N/A Type t2 = to;
0N/A if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
0N/A return false;
983N/A if (!allowCovariantReturns)
0N/A // reject if there is a common method signature with
0N/A // incompatible return types.
0N/A chk.checkCompatibleAbstracts(warn.pos(), from, to);
0N/A if (!isReifiable(target) &&
0N/A (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
794N/A warn.warn(LintCategory.UNCHECKED);
0N/A return true;
0N/A }
0N/A
0N/A private boolean giveWarning(Type from, Type to) {
234N/A Type subFrom = asSub(from, to.tsym);
234N/A return to.isParameterized() &&
234N/A (!(isUnbounded(to) ||
234N/A isSubtype(from, to) ||
735N/A ((subFrom != null) && containsType(to.allparams(), subFrom.allparams()))));
0N/A }
0N/A
0N/A private List<Type> superClosure(Type t, Type s) {
0N/A List<Type> cl = List.nil();
0N/A for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
0N/A if (isSubtype(s, erasure(l.head))) {
0N/A cl = insert(cl, l.head);
0N/A } else {
0N/A cl = union(cl, superClosure(l.head, s));
0N/A }
0N/A }
0N/A return cl;
0N/A }
0N/A
0N/A private boolean containsTypeEquivalent(Type t, Type s) {
0N/A return
0N/A isSameType(t, s) || // shortcut
0N/A containsType(t, s) && containsType(s, t);
0N/A }
0N/A
137N/A // <editor-fold defaultstate="collapsed" desc="adapt">
0N/A /**
0N/A * Adapt a type by computing a substitution which maps a source
0N/A * type to a target type.
0N/A *
0N/A * @param source the source type
0N/A * @param target the target type
0N/A * @param from the type variables of the computed substitution
0N/A * @param to the types of the computed substitution.
0N/A */
0N/A public void adapt(Type source,
0N/A Type target,
0N/A ListBuffer<Type> from,
0N/A ListBuffer<Type> to) throws AdaptFailure {
137N/A new Adapter(from, to).adapt(source, target);
0N/A }
137N/A
137N/A class Adapter extends SimpleVisitor<Void, Type> {
137N/A
137N/A ListBuffer<Type> from;
137N/A ListBuffer<Type> to;
137N/A Map<Symbol,Type> mapping;
137N/A
137N/A Adapter(ListBuffer<Type> from, ListBuffer<Type> to) {
137N/A this.from = from;
137N/A this.to = to;
137N/A mapping = new HashMap<Symbol,Type>();
137N/A }
137N/A
137N/A public void adapt(Type source, Type target) throws AdaptFailure {
137N/A visit(source, target);
137N/A List<Type> fromList = from.toList();
137N/A List<Type> toList = to.toList();
137N/A while (!fromList.isEmpty()) {
137N/A Type val = mapping.get(fromList.head.tsym);
137N/A if (toList.head != val)
137N/A toList.head = val;
137N/A fromList = fromList.tail;
137N/A toList = toList.tail;
137N/A }
137N/A }
137N/A
137N/A @Override
137N/A public Void visitClassType(ClassType source, Type target) throws AdaptFailure {
137N/A if (target.tag == CLASS)
137N/A adaptRecursive(source.allparams(), target.allparams());
137N/A return null;
137N/A }
137N/A
137N/A @Override
137N/A public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure {
137N/A if (target.tag == ARRAY)
137N/A adaptRecursive(elemtype(source), elemtype(target));
137N/A return null;
137N/A }
137N/A
137N/A @Override
137N/A public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
137N/A if (source.isExtendsBound())
137N/A adaptRecursive(upperBound(source), upperBound(target));
137N/A else if (source.isSuperBound())
137N/A adaptRecursive(lowerBound(source), lowerBound(target));
137N/A return null;
137N/A }
137N/A
137N/A @Override
137N/A public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure {
137N/A // Check to see if there is
137N/A // already a mapping for $source$, in which case
137N/A // the old mapping will be merged with the new
137N/A Type val = mapping.get(source.tsym);
137N/A if (val != null) {
137N/A if (val.isSuperBound() && target.isSuperBound()) {
137N/A val = isSubtype(lowerBound(val), lowerBound(target))
137N/A ? target : val;
137N/A } else if (val.isExtendsBound() && target.isExtendsBound()) {
137N/A val = isSubtype(upperBound(val), upperBound(target))
137N/A ? val : target;
137N/A } else if (!isSameType(val, target)) {
137N/A throw new AdaptFailure();
0N/A }
137N/A } else {
137N/A val = target;
137N/A from.append(source);
137N/A to.append(target);
137N/A }
137N/A mapping.put(source.tsym, val);
137N/A return null;
137N/A }
137N/A
137N/A @Override
137N/A public Void visitType(Type source, Type target) {
137N/A return null;
137N/A }
137N/A
137N/A private Set<TypePair> cache = new HashSet<TypePair>();
137N/A
137N/A private void adaptRecursive(Type source, Type target) {
137N/A TypePair pair = new TypePair(source, target);
137N/A if (cache.add(pair)) {
137N/A try {
137N/A visit(source, target);
137N/A } finally {
137N/A cache.remove(pair);
0N/A }
0N/A }
0N/A }
137N/A
137N/A private void adaptRecursive(List<Type> source, List<Type> target) {
137N/A if (source.length() == target.length()) {
137N/A while (source.nonEmpty()) {
137N/A adaptRecursive(source.head, target.head);
137N/A source = source.tail;
137N/A target = target.tail;
137N/A }
0N/A }
0N/A }
0N/A }
0N/A
137N/A public static class AdaptFailure extends RuntimeException {
137N/A static final long serialVersionUID = -7490231548272701566L;
137N/A }
137N/A
0N/A private void adaptSelf(Type t,
0N/A ListBuffer<Type> from,
0N/A ListBuffer<Type> to) {
0N/A try {
0N/A //if (t.tsym.type != t)
0N/A adapt(t.tsym.type, t, from, to);
0N/A } catch (AdaptFailure ex) {
0N/A // Adapt should never fail calculating a mapping from
0N/A // t.tsym.type to t as there can be no merge problem.
0N/A throw new AssertionError(ex);
0N/A }
0N/A }
137N/A // </editor-fold>
0N/A
0N/A /**
0N/A * Rewrite all type variables (universal quantifiers) in the given
0N/A * type to wildcards (existential quantifiers). This is used to
0N/A * determine if a cast is allowed. For example, if high is true
0N/A * and {@code T <: Number}, then {@code List<T>} is rewritten to
0N/A * {@code List<? extends Number>}. Since {@code List<Integer> <:
0N/A * List<? extends Number>} a {@code List<T>} can be cast to {@code
0N/A * List<Integer>} with a warning.
0N/A * @param t a type
0N/A * @param high if true return an upper bound; otherwise a lower
0N/A * bound
0N/A * @param rewriteTypeVars only rewrite captured wildcards if false;
0N/A * otherwise rewrite all type variables
0N/A * @return the type rewritten with wildcards (existential
0N/A * quantifiers) only
0N/A */
0N/A private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
639N/A return new Rewriter(high, rewriteTypeVars).visit(t);
156N/A }
156N/A
156N/A class Rewriter extends UnaryVisitor<Type> {
156N/A
156N/A boolean high;
156N/A boolean rewriteTypeVars;
156N/A
156N/A Rewriter(boolean high, boolean rewriteTypeVars) {
156N/A this.high = high;
156N/A this.rewriteTypeVars = rewriteTypeVars;
156N/A }
156N/A
639N/A @Override
639N/A public Type visitClassType(ClassType t, Void s) {
156N/A ListBuffer<Type> rewritten = new ListBuffer<Type>();
156N/A boolean changed = false;
639N/A for (Type arg : t.allparams()) {
156N/A Type bound = visit(arg);
156N/A if (arg != bound) {
156N/A changed = true;
156N/A }
156N/A rewritten.append(bound);
0N/A }
156N/A if (changed)
639N/A return subst(t.tsym.type,
639N/A t.tsym.type.allparams(),
639N/A rewritten.toList());
156N/A else
156N/A return t;
156N/A }
156N/A
156N/A public Type visitType(Type t, Void s) {
156N/A return high ? upperBound(t) : lowerBound(t);
156N/A }
156N/A
156N/A @Override
156N/A public Type visitCapturedType(CapturedType t, Void s) {
639N/A Type bound = visitWildcardType(t.wildcard, null);
639N/A return (bound.contains(t)) ?
778N/A erasure(bound) :
778N/A bound;
0N/A }
156N/A
156N/A @Override
156N/A public Type visitTypeVar(TypeVar t, Void s) {
639N/A if (rewriteTypeVars) {
639N/A Type bound = high ?
639N/A (t.bound.contains(t) ?
778N/A erasure(t.bound) :
639N/A visit(t.bound)) :
639N/A syms.botType;
639N/A return rewriteAsWildcardType(bound, t);
639N/A }
156N/A else
156N/A return t;
156N/A }
156N/A
156N/A @Override
156N/A public Type visitWildcardType(WildcardType t, Void s) {
156N/A Type bound = high ? t.getExtendsBound() :
156N/A t.getSuperBound();
156N/A if (bound == null)
639N/A bound = high ? syms.objectType : syms.botType;
639N/A return rewriteAsWildcardType(visit(bound), t.bound);
639N/A }
639N/A
639N/A private Type rewriteAsWildcardType(Type bound, TypeVar formal) {
639N/A return high ?
639N/A makeExtendsWildcard(B(bound), formal) :
639N/A makeSuperWildcard(B(bound), formal);
639N/A }
639N/A
639N/A Type B(Type t) {
639N/A while (t.tag == WILDCARD) {
639N/A WildcardType w = (WildcardType)t;
639N/A t = high ?
639N/A w.getExtendsBound() :
639N/A w.getSuperBound();
639N/A if (t == null) {
639N/A t = high ? syms.objectType : syms.botType;
639N/A }
639N/A }
639N/A return t;
156N/A }
0N/A }
0N/A
639N/A
0N/A /**
0N/A * Create a wildcard with the given upper (extends) bound; create
0N/A * an unbounded wildcard if bound is Object.
0N/A *
0N/A * @param bound the upper bound
0N/A * @param formal the formal type parameter that will be
0N/A * substituted by the wildcard
0N/A */
0N/A private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) {
0N/A if (bound == syms.objectType) {
0N/A return new WildcardType(syms.objectType,
0N/A BoundKind.UNBOUND,
0N/A syms.boundClass,
0N/A formal);
0N/A } else {
0N/A return new WildcardType(bound,
0N/A BoundKind.EXTENDS,
0N/A syms.boundClass,
0N/A formal);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create a wildcard with the given lower (super) bound; create an
0N/A * unbounded wildcard if bound is bottom (type of {@code null}).
0N/A *
0N/A * @param bound the lower bound
0N/A * @param formal the formal type parameter that will be
0N/A * substituted by the wildcard
0N/A */
0N/A private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
0N/A if (bound.tag == BOT) {
0N/A return new WildcardType(syms.objectType,
0N/A BoundKind.UNBOUND,
0N/A syms.boundClass,
0N/A formal);
0N/A } else {
0N/A return new WildcardType(bound,
0N/A BoundKind.SUPER,
0N/A syms.boundClass,
0N/A formal);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * A wrapper for a type that allows use in sets.
0N/A */
0N/A class SingletonType {
0N/A final Type t;
0N/A SingletonType(Type t) {
0N/A this.t = t;
0N/A }
0N/A public int hashCode() {
506N/A return Types.hashCode(t);
0N/A }
0N/A public boolean equals(Object obj) {
0N/A return (obj instanceof SingletonType) &&
0N/A isSameType(t, ((SingletonType)obj).t);
0N/A }
0N/A public String toString() {
0N/A return t.toString();
0N/A }
0N/A }
0N/A // </editor-fold>
0N/A
0N/A // <editor-fold defaultstate="collapsed" desc="Visitors">
0N/A /**
0N/A * A default visitor for types. All visitor methods except
0N/A * visitType are implemented by delegating to visitType. Concrete
0N/A * subclasses must provide an implementation of visitType and can
0N/A * override other methods as needed.
0N/A *
0N/A * @param <R> the return type of the operation implemented by this
0N/A * visitor; use Void if no return type is needed.
0N/A * @param <S> the type of the second argument (the first being the
0N/A * type itself) of the operation implemented by this visitor; use
0N/A * Void if a second argument is not needed.
0N/A */
0N/A public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> {
0N/A final public R visit(Type t, S s) { return t.accept(this, s); }
0N/A public R visitClassType(ClassType t, S s) { return visitType(t, s); }
0N/A public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }
0N/A public R visitArrayType(ArrayType t, S s) { return visitType(t, s); }
0N/A public R visitMethodType(MethodType t, S s) { return visitType(t, s); }
0N/A public R visitPackageType(PackageType t, S s) { return visitType(t, s); }
0N/A public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); }
0N/A public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }
0N/A public R visitForAll(ForAll t, S s) { return visitType(t, s); }
0N/A public R visitUndetVar(UndetVar t, S s) { return visitType(t, s); }
0N/A public R visitErrorType(ErrorType t, S s) { return visitType(t, s); }
0N/A }
0N/A
0N/A /**
120N/A * A default visitor for symbols. All visitor methods except
120N/A * visitSymbol are implemented by delegating to visitSymbol. Concrete
120N/A * subclasses must provide an implementation of visitSymbol and can
120N/A * override other methods as needed.
120N/A *
120N/A * @param <R> the return type of the operation implemented by this
120N/A * visitor; use Void if no return type is needed.
120N/A * @param <S> the type of the second argument (the first being the
120N/A * symbol itself) of the operation implemented by this visitor; use
120N/A * Void if a second argument is not needed.
120N/A */
120N/A public static abstract class DefaultSymbolVisitor<R,S> implements Symbol.Visitor<R,S> {
120N/A final public R visit(Symbol s, S arg) { return s.accept(this, arg); }
120N/A public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); }
120N/A public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); }
120N/A public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); }
120N/A public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); }
120N/A public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); }
120N/A public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); }
120N/A }
120N/A
120N/A /**
0N/A * A <em>simple</em> visitor for types. This visitor is simple as
0N/A * captured wildcards, for-all types (generic methods), and
0N/A * undetermined type variables (part of inference) are hidden.
0N/A * Captured wildcards are hidden by treating them as type
0N/A * variables and the rest are hidden by visiting their qtypes.
0N/A *
0N/A * @param <R> the return type of the operation implemented by this
0N/A * visitor; use Void if no return type is needed.
0N/A * @param <S> the type of the second argument (the first being the
0N/A * type itself) of the operation implemented by this visitor; use
0N/A * Void if a second argument is not needed.
0N/A */
0N/A public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> {
0N/A @Override
0N/A public R visitCapturedType(CapturedType t, S s) {
0N/A return visitTypeVar(t, s);
0N/A }
0N/A @Override
0N/A public R visitForAll(ForAll t, S s) {
0N/A return visit(t.qtype, s);
0N/A }
0N/A @Override
0N/A public R visitUndetVar(UndetVar t, S s) {
0N/A return visit(t.qtype, s);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * A plain relation on types. That is a 2-ary function on the
0N/A * form Type&nbsp;&times;&nbsp;Type&nbsp;&rarr;&nbsp;Boolean.
0N/A * <!-- In plain text: Type x Type -> Boolean -->
0N/A */
0N/A public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {}
0N/A
0N/A /**
0N/A * A convenience visitor for implementing operations that only
0N/A * require one argument (the type itself), that is, unary
0N/A * operations.
0N/A *
0N/A * @param <R> the return type of the operation implemented by this
0N/A * visitor; use Void if no return type is needed.
0N/A */
0N/A public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> {
0N/A final public R visit(Type t) { return t.accept(this, null); }
0N/A }
0N/A
0N/A /**
0N/A * A visitor for implementing a mapping from types to types. The
0N/A * default behavior of this class is to implement the identity
0N/A * mapping (mapping a type to itself). This can be overridden in
0N/A * subclasses.
0N/A *
0N/A * @param <S> the type of the second argument (the first being the
0N/A * type itself) of this mapping; use Void if a second argument is
0N/A * not needed.
0N/A */
0N/A public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> {
0N/A final public Type visit(Type t) { return t.accept(this, null); }
0N/A public Type visitType(Type t, S s) { return t; }
0N/A }
0N/A // </editor-fold>
656N/A
656N/A
656N/A // <editor-fold defaultstate="collapsed" desc="Annotation support">
656N/A
656N/A public RetentionPolicy getRetention(Attribute.Compound a) {
656N/A RetentionPolicy vis = RetentionPolicy.CLASS; // the default
656N/A Attribute.Compound c = a.type.tsym.attribute(syms.retentionType.tsym);
656N/A if (c != null) {
656N/A Attribute value = c.member(names.value);
656N/A if (value != null && value instanceof Attribute.Enum) {
656N/A Name levelName = ((Attribute.Enum)value).value.name;
656N/A if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE;
656N/A else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS;
656N/A else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME;
656N/A else ;// /* fail soft */ throw new AssertionError(levelName);
656N/A }
656N/A }
656N/A return vis;
656N/A }
656N/A // </editor-fold>
0N/A}