test/tools/javac/types/TypeHarness.java

changeset 792
2199365892b1
child 821
c8d312dd17bc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/types/TypeHarness.java	Mon Dec 13 14:56:50 2010 +0000
     1.3 @@ -0,0 +1,315 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +import com.sun.tools.javac.code.BoundKind;
    1.28 +import com.sun.tools.javac.code.Flags;
    1.29 +import com.sun.tools.javac.util.Context;
    1.30 +import com.sun.tools.javac.code.Types;
    1.31 +import com.sun.tools.javac.code.Symtab;
    1.32 +import com.sun.tools.javac.code.Type;
    1.33 +import com.sun.tools.javac.code.Type.*;
    1.34 +import com.sun.tools.javac.code.Symbol.*;
    1.35 +import com.sun.tools.javac.util.List;
    1.36 +import com.sun.tools.javac.util.ListBuffer;
    1.37 +import com.sun.tools.javac.util.Name;
    1.38 +import com.sun.tools.javac.util.Names;
    1.39 +import com.sun.tools.javac.file.JavacFileManager;
    1.40 +
    1.41 +/**
    1.42 + * Test harness whose goal is to simplify the task of writing type-system
    1.43 + * regression test. It provides functionalities to build custom types as well
    1.44 + * as to access the underlying javac's symbol table in order to retrieve
    1.45 + * predefined types. Among the features supported by the harness are: type
    1.46 + * substitution, type containment, subtyping, cast-conversion, assigment
    1.47 + * conversion.
    1.48 + *
    1.49 + * This class is meant to be a common super class for all concrete type test
    1.50 + * classes. A subclass can access the type-factory and the test methods so as
    1.51 + * to write compact tests. An example is reported below:
    1.52 + *
    1.53 + * <pre>
    1.54 + * Type X = fac.TypeVariable();
    1.55 + * Type Y = fac.TypeVariable();
    1.56 + * Type A_X_Y = fac.Class(0, X, Y);
    1.57 + * Type A_Obj_Obj = fac.Class(0,
    1.58 + *           predef.objectType,
    1.59 + *           predef.objectType);
    1.60 + * checkSameType(A_Obj_Obj, subst(A_X_Y,
    1.61 + *           Mapping(X, predef.objectType),
    1.62 + *           Mapping(Y, predef.objectType)));
    1.63 + * </pre>
    1.64 + *
    1.65 + * The above code is used to create two class types, namely {@code A<X,Y>} and
    1.66 + * {@code A<Object,Object>} where both {@code X} and {@code Y} are type-variables.
    1.67 + * The code then verifies that {@code [X:=Object,Y:=Object]A<X,Y> == A<Object,Object>}.
    1.68 + *
    1.69 + * @author mcimadamore
    1.70 + */
    1.71 +public class TypeHarness {
    1.72 +
    1.73 +    protected Types types;
    1.74 +    protected Symtab predef;
    1.75 +    protected Names names;
    1.76 +    protected Factory fac;
    1.77 +
    1.78 +    protected TypeHarness() {
    1.79 +        Context ctx = new Context();
    1.80 +        JavacFileManager.preRegister(ctx);
    1.81 +        types = Types.instance(ctx);
    1.82 +        predef = Symtab.instance(ctx);
    1.83 +        names = Names.instance(ctx);
    1.84 +        fac = new Factory();
    1.85 +    }
    1.86 +
    1.87 +    // <editor-fold defaultstate="collapsed" desc="type assertions">
    1.88 +
    1.89 +    /** assert that 's' is a subtype of 't' */
    1.90 +    public void assertSubtype(Type s, Type t) {
    1.91 +        assertSubtype(s, t, true);
    1.92 +    }
    1.93 +
    1.94 +    /** assert that 's' is/is not a subtype of 't' */
    1.95 +    public void assertSubtype(Type s, Type t, boolean expected) {
    1.96 +        if (types.isSubtype(s, t) != expected) {
    1.97 +            String msg = expected ?
    1.98 +                " is not a subtype of " :
    1.99 +                " is a subtype of ";
   1.100 +            error(s + msg + t);
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    /** assert that 's' is the same type as 't' */
   1.105 +    public void assertSameType(Type s, Type t) {
   1.106 +        assertSameType(s, t, true);
   1.107 +    }
   1.108 +
   1.109 +    /** assert that 's' is/is not the same type as 't' */
   1.110 +    public void assertSameType(Type s, Type t, boolean expected) {
   1.111 +        if (types.isSameType(s, t) != expected) {
   1.112 +            String msg = expected ?
   1.113 +                " is not the same type as " :
   1.114 +                " is the same type as ";
   1.115 +            error(s + msg + t);
   1.116 +        }
   1.117 +    }
   1.118 +
   1.119 +    /** assert that 's' is castable to 't' */
   1.120 +    public void assertCastable(Type s, Type t) {
   1.121 +        assertCastable(s, t, true);
   1.122 +    }
   1.123 +
   1.124 +    /** assert that 's' is/is not castable to 't' */
   1.125 +    public void assertCastable(Type s, Type t, boolean expected) {
   1.126 +        if (types.isCastable(s, t) != expected) {
   1.127 +            String msg = expected ?
   1.128 +                " is not castable to " :
   1.129 +                " is castable to ";
   1.130 +            error(s + msg + t);
   1.131 +        }
   1.132 +    }
   1.133 +
   1.134 +    /** assert that 's' is convertible (method invocation conversion) to 't' */
   1.135 +    public void assertConvertible(Type s, Type t) {
   1.136 +        assertCastable(s, t, true);
   1.137 +    }
   1.138 +
   1.139 +    /** assert that 's' is/is not convertible (method invocation conversion) to 't' */
   1.140 +    public void assertConvertible(Type s, Type t, boolean expected) {
   1.141 +        if (types.isConvertible(s, t) != expected) {
   1.142 +            String msg = expected ?
   1.143 +                " is not convertible to " :
   1.144 +                " is convertible to ";
   1.145 +            error(s + msg + t);
   1.146 +        }
   1.147 +    }
   1.148 +
   1.149 +    /** assert that 's' is assignable to 't' */
   1.150 +    public void assertAssignable(Type s, Type t) {
   1.151 +        assertCastable(s, t, true);
   1.152 +    }
   1.153 +
   1.154 +    /** assert that 's' is/is not assignable to 't' */
   1.155 +    public void assertAssignable(Type s, Type t, boolean expected) {
   1.156 +        if (types.isAssignable(s, t) != expected) {
   1.157 +            String msg = expected ?
   1.158 +                " is not assignable to " :
   1.159 +                " is assignable to ";
   1.160 +            error(s + msg + t);
   1.161 +        }
   1.162 +    }
   1.163 +    // </editor-fold>
   1.164 +
   1.165 +    private void error(String msg) {
   1.166 +        throw new AssertionError("Unexpected result: " + msg);
   1.167 +    }
   1.168 +
   1.169 +    // <editor-fold defaultstate="collapsed" desc="type functions">
   1.170 +
   1.171 +    /** compute the erasure of a type 't' */
   1.172 +    public Type erasure(Type t) {
   1.173 +        return types.erasure(t);
   1.174 +    }
   1.175 +
   1.176 +    /** compute the capture of a type 't' */
   1.177 +    public Type capture(Type t) {
   1.178 +        return types.capture(t);
   1.179 +    }
   1.180 +
   1.181 +    /** compute the boxed type associated with 't' */
   1.182 +    public Type box(Type t) {
   1.183 +        if (!t.isPrimitive()) {
   1.184 +            throw new AssertionError("Cannot box non-primitive type: " + t);
   1.185 +        }
   1.186 +        return types.boxedClass(t).type;
   1.187 +    }
   1.188 +
   1.189 +    /** compute the unboxed type associated with 't' */
   1.190 +    public Type unbox(Type t) {
   1.191 +        Type u = types.unboxedType(t);
   1.192 +        if (t == null) {
   1.193 +            throw new AssertionError("Cannot unbox reference type: " + t);
   1.194 +        } else {
   1.195 +            return u;
   1.196 +        }
   1.197 +    }
   1.198 +
   1.199 +    /** compute a type substitution on 't' given a list of type mappings */
   1.200 +    public Type subst(Type t, Mapping... maps) {
   1.201 +        ListBuffer<Type> from = ListBuffer.lb();
   1.202 +        ListBuffer<Type> to = ListBuffer.lb();
   1.203 +        for (Mapping tm : maps) {
   1.204 +            from.append(tm.from);
   1.205 +            to.append(tm.to);
   1.206 +        }
   1.207 +        return types.subst(t, from.toList(), to.toList());
   1.208 +    }
   1.209 +
   1.210 +    /** create a fresh type mapping from a type to another */
   1.211 +    public Mapping Mapping(Type from, Type to) {
   1.212 +        return new Mapping(from, to);
   1.213 +    }
   1.214 +
   1.215 +    public static class Mapping {
   1.216 +        Type from;
   1.217 +        Type to;
   1.218 +        private Mapping(Type from, Type to) {
   1.219 +            this.from = from;
   1.220 +            this.to = to;
   1.221 +        }
   1.222 +    }
   1.223 +    // </editor-fold>
   1.224 +
   1.225 +    // <editor-fold defaultstate="collapsed" desc="type factory">
   1.226 +
   1.227 +    /**
   1.228 +     * This class is used to create Java types in a simple way. All main
   1.229 +     * kinds of type are supported: primitive, reference, non-denotable. The
   1.230 +     * factory also supports creation of constant types (used by the compiler
   1.231 +     * to represent the type of a literal).
   1.232 +     */
   1.233 +    public class Factory {
   1.234 +
   1.235 +        private int synthNameCount = 0;
   1.236 +
   1.237 +        private Name syntheticName() {
   1.238 +            return names.fromString("A$" + synthNameCount++);
   1.239 +        }
   1.240 +
   1.241 +        public ClassType Class(long flags, Type... typeArgs) {
   1.242 +            ClassSymbol csym = new ClassSymbol(flags, syntheticName(), predef.noSymbol);
   1.243 +            csym.type = new ClassType(Type.noType, List.from(typeArgs), csym);
   1.244 +            ((ClassType)csym.type).supertype_field = predef.objectType;
   1.245 +            return (ClassType)csym.type;
   1.246 +        }
   1.247 +
   1.248 +        public ClassType Class(Type... typeArgs) {
   1.249 +            return Class(0, typeArgs);
   1.250 +        }
   1.251 +
   1.252 +        public ClassType Interface(Type... typeArgs) {
   1.253 +            return Class(Flags.INTERFACE, typeArgs);
   1.254 +        }
   1.255 +
   1.256 +        public ClassType Interface(long flags, Type... typeArgs) {
   1.257 +            return Class(Flags.INTERFACE | flags, typeArgs);
   1.258 +        }
   1.259 +
   1.260 +        public Type Constant(byte b) {
   1.261 +            return predef.byteType.constType(b);
   1.262 +        }
   1.263 +
   1.264 +        public Type Constant(short s) {
   1.265 +            return predef.shortType.constType(s);
   1.266 +        }
   1.267 +
   1.268 +        public Type Constant(int i) {
   1.269 +            return predef.intType.constType(i);
   1.270 +        }
   1.271 +
   1.272 +        public Type Constant(long l) {
   1.273 +            return predef.longType.constType(l);
   1.274 +        }
   1.275 +
   1.276 +        public Type Constant(float f) {
   1.277 +            return predef.floatType.constType(f);
   1.278 +        }
   1.279 +
   1.280 +        public Type Constant(double d) {
   1.281 +            return predef.doubleType.constType(d);
   1.282 +        }
   1.283 +
   1.284 +        public Type Constant(char c) {
   1.285 +            return predef.charType.constType(c + 0);
   1.286 +        }
   1.287 +
   1.288 +        public ArrayType Array(Type elemType) {
   1.289 +            return new ArrayType(elemType, predef.arrayClass);
   1.290 +        }
   1.291 +
   1.292 +        public TypeVar TypeVariable() {
   1.293 +            return TypeVariable(predef.objectType);
   1.294 +        }
   1.295 +
   1.296 +        public TypeVar TypeVariable(Type bound) {
   1.297 +            TypeSymbol tvsym = new TypeSymbol(0, syntheticName(), null, predef.noSymbol);
   1.298 +            tvsym.type = new TypeVar(tvsym, bound, null);
   1.299 +            return (TypeVar)tvsym.type;
   1.300 +        }
   1.301 +
   1.302 +        public WildcardType Wildcard(BoundKind bk, Type bound) {
   1.303 +            return new WildcardType(bound, bk, predef.boundClass);
   1.304 +        }
   1.305 +
   1.306 +        public CapturedType CapturedVariable(Type upper, Type lower) {
   1.307 +            return new CapturedType(syntheticName(), predef.noSymbol, upper, lower, null);
   1.308 +        }
   1.309 +
   1.310 +        public ClassType Intersection(Type classBound, Type... intfBounds) {
   1.311 +            ClassType ct = Class(Flags.COMPOUND);
   1.312 +            ct.supertype_field = classBound;
   1.313 +            ct.interfaces_field = List.from(intfBounds);
   1.314 +            return ct;
   1.315 +        }
   1.316 +    }
   1.317 +    // </editor-fold>
   1.318 +}

mercurial