type.java

来自「是一款用JAVA 编写的编译器 具有很强的编译功能」· Java 代码 · 共 1,249 行 · 第 1/3 页

JAVA
1,249
字号
            throw new AssertionError();    }    public static class WildcardType extends Type            implements javax.lang.model.type.WildcardType {        public Type type;        public BoundKind kind;        public TypeVar bound;        @Override        public <R,S> R accept(Type.Visitor<R,S> v, S s) {            return v.visitWildcardType(this, s);        }        public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {            super(WILDCARD, tsym);            assert(type != null);            this.kind = kind;            this.type = type;        }        public WildcardType(WildcardType t, TypeVar bound) {            this(t.type, t.kind, t.tsym, bound);        }        public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {            this(type, kind, tsym);            this.bound = bound;        }        public boolean isSuperBound() {            return kind == SUPER ||                kind == UNBOUND;        }        public boolean isExtendsBound() {            return kind == EXTENDS ||                kind == UNBOUND;        }        public boolean isUnbound() {            return kind == UNBOUND;        }        public Type withTypeVar(Type t) {            //-System.err.println(this+".withTypeVar("+t+");");//DEBUG            if (bound == t)                return this;            bound = (TypeVar)t;            return this;        }        boolean isPrintingBound = false;        public String toString() {            StringBuffer s = new StringBuffer();            s.append(kind.toString());            if (kind != UNBOUND)                s.append(type);            if (moreInfo && bound != null && !isPrintingBound)                try {                    isPrintingBound = true;                    s.append("{:").append(bound.bound).append(":}");                } finally {                    isPrintingBound = false;                }            return s.toString();        }        public Type map(Mapping f) {            //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG            Type t = type;            if (t != null)                t = f.apply(t);            if (t == type)                return this;            else                return new WildcardType(t, kind, tsym, bound);        }        public Type removeBounds() {            return isUnbound() ? this : type;        }        public Type getExtendsBound() {            if (kind == EXTENDS)                return type;            else                return null;        }        public Type getSuperBound() {            if (kind == SUPER)                return type;            else                return null;        }        public TypeKind getKind() {            return TypeKind.WILDCARD;        }        public <R, P> R accept(TypeVisitor<R, P> v, P p) {            return v.visitWildcard(this, p);        }    }    public static class ClassType extends Type implements DeclaredType {        /** The enclosing type of this type. If this is the type of an inner         *  class, outer_field refers to the type of its enclosing         *  instance class, in all other cases it referes to noType.         */        private Type outer_field;        /** The type parameters of this type (to be set once class is loaded).         */        public List<Type> typarams_field;        /** A cache variable for the type parameters of this type,         *  appended to all parameters of its enclosing class.         *  @see #allparams         */        public List<Type> allparams_field;        /** The supertype of this class (to be set once class is loaded).         */        public Type supertype_field;        /** The interfaces of this class (to be set once class is loaded).         */        public List<Type> interfaces_field;        public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {            super(CLASS, tsym);            this.outer_field = outer;            this.typarams_field = typarams;            this.allparams_field = null;            this.supertype_field = null;            this.interfaces_field = null;            /*            // this can happen during error recovery            assert                outer.isParameterized() ?                typarams.length() == tsym.type.typarams().length() :                outer.isRaw() ?                typarams.length() == 0 :                true;            */        }        @Override        public <R,S> R accept(Type.Visitor<R,S> v, S s) {            return v.visitClassType(this, s);        }        public Type constType(Object constValue) {            final Object value = constValue;            return new ClassType(getEnclosingType(), typarams_field, tsym) {                    @Override                    public Object constValue() {                        return value;                    }                    @Override                    public Type baseType() {                        return tsym.type;                    }                };        }        /** The Java source which this type represents.         */        public String toString() {            StringBuffer buf = new StringBuffer();            if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {                buf.append(getEnclosingType().toString());                buf.append(".");                buf.append(className(tsym, false));            } else {                buf.append(className(tsym, true));            }            if (getTypeArguments().nonEmpty()) {                buf.append('<');                buf.append(getTypeArguments().toString());                buf.append(">");            }            return buf.toString();        }//where            private String className(Symbol sym, boolean longform) {                if (sym.name.len == 0 && (sym.flags() & COMPOUND) != 0) {                    StringBuffer s = new StringBuffer(supertype_field.toString());                    for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {                        s.append("&");                        s.append(is.head.toString());                    }                    return s.toString();                } else if (sym.name.len == 0) {                    String s;                    ClassType norm = (ClassType) tsym.type;                    if (norm == null) {                        s = Log.getLocalizedString("anonymous.class", (Object)null);                    } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {                        s = Log.getLocalizedString("anonymous.class",                                                   norm.interfaces_field.head);                    } else {                        s = Log.getLocalizedString("anonymous.class",                                                   norm.supertype_field);                    }                    if (moreInfo)                        s += String.valueOf(sym.hashCode());                    return s;                } else if (longform) {                    return sym.getQualifiedName().toString();                } else {                    return sym.name.toString();                }            }        public List<Type> getTypeArguments() {            if (typarams_field == null) {                complete();                if (typarams_field == null)                    typarams_field = List.nil();            }            return typarams_field;        }        public Type getEnclosingType() {            return outer_field;        }        public void setEnclosingType(Type outer) {            outer_field = outer;        }        public List<Type> allparams() {            if (allparams_field == null) {                allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());            }            return allparams_field;        }        public boolean isErroneous() {            return                getEnclosingType().isErroneous() ||                isErroneous(getTypeArguments()) ||                this != tsym.type && tsym.type.isErroneous();        }        public boolean isParameterized() {            return allparams().tail != null;            // optimization, was: allparams().nonEmpty();        }        /** A cache for the rank. */        int rank_field = -1;        /** A class type is raw if it misses some         *  of its type parameter sections.         *  After validation, this is equivalent to:         *  allparams.isEmpty() && tsym.type.allparams.nonEmpty();         */        public boolean isRaw() {            return                this != tsym.type && // necessary, but not sufficient condition                tsym.type.allparams().nonEmpty() &&                allparams().isEmpty();        }        public Type map(Mapping f) {            Type outer = getEnclosingType();            Type outer1 = f.apply(outer);            List<Type> typarams = getTypeArguments();            List<Type> typarams1 = map(typarams, f);            if (outer1 == outer && typarams1 == typarams) return this;            else return new ClassType(outer1, typarams1, tsym);        }        public boolean contains(Type elem) {            return                elem == this                || (isParameterized()                    && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)));        }        public void complete() {            if (tsym.completer != null) tsym.complete();        }        public TypeKind getKind() {            return TypeKind.DECLARED;        }        public <R, P> R accept(TypeVisitor<R, P> v, P p) {            return v.visitDeclared(this, p);        }    }    public static class ArrayType extends Type            implements javax.lang.model.type.ArrayType {        public Type elemtype;        public ArrayType(Type elemtype, TypeSymbol arrayClass) {            super(ARRAY, arrayClass);            this.elemtype = elemtype;        }        @Override        public <R,S> R accept(Type.Visitor<R,S> v, S s) {            return v.visitArrayType(this, s);        }        public String toString() {            return elemtype + "[]";        }        public boolean equals(Object obj) {            return                this == obj ||                (obj instanceof ArrayType &&                 this.elemtype.equals(((ArrayType)obj).elemtype));        }        public int hashCode() {            return (ARRAY << 5) + elemtype.hashCode();        }        public List<Type> allparams() { return elemtype.allparams(); }        public boolean isErroneous() {            return elemtype.isErroneous();        }        public boolean isParameterized() {            return elemtype.isParameterized();        }        public boolean isRaw() {            return elemtype.isRaw();        }        public Type map(Mapping f) {            Type elemtype1 = f.apply(elemtype);            if (elemtype1 == elemtype) return this;            else return new ArrayType(elemtype1, tsym);        }        public boolean contains(Type elem) {            return elem == this || elemtype.contains(elem);        }        public void complete() {            elemtype.complete();        }        public Type getComponentType() {            return elemtype;        }        public TypeKind getKind() {            return TypeKind.ARRAY;        }        public <R, P> R accept(TypeVisitor<R, P> v, P p) {            return v.visitArray(this, p);        }    }    public static class MethodType extends Type                    implements Cloneable, ExecutableType {        public List<Type> argtypes;        public Type restype;        public List<Type> thrown;        public MethodType(List<Type> argtypes,                          Type restype,                          List<Type> thrown,                          TypeSymbol methodClass) {            super(METHOD, methodClass);            this.argtypes = argtypes;            this.restype = restype;            this.thrown = thrown;        }        @Override        public <R,S> R accept(Type.Visitor<R,S> v, S s) {            return v.visitMethodType(this, s);        }        /** The Java source which this type represents.         *         *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably         *  should be.         */        public String toString() {            return "(" + argtypes + ")" + restype;        }        public boolean equals(Object obj) {            if (this == obj)                return true;            if (!(obj instanceof MethodType))                return false;            MethodType m = (MethodType)obj;            List<Type> args1 = argtypes;            List<Type> args2 = m.argtypes;            while (!args1.isEmpty() && !args2.isEmpty()) {                if (!args1.head.equals(args2.head))                    return false;                args1 = args1.tail;                args2 = args2.tail;            }            if (!args1.isEmpty() || !args2.isEmpty())                return false;            return restype.equals(m.restype);        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?