test/tools/javac/types/TypeHarness.java

Fri, 12 Apr 2013 12:05:04 +0200

author
jfranck
date
Fri, 12 Apr 2013 12:05:04 +0200
changeset 1689
137994c189e5
parent 962
0ff2bbd38f10
child 2047
5f915a0c9615
permissions
-rw-r--r--

7015104: use new subtype of TypeSymbol for type parameters
Reviewed-by: jjg, mcimadamore

     1 /*
     2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 import com.sun.tools.javac.code.BoundKind;
    25 import com.sun.tools.javac.code.Flags;
    26 import com.sun.tools.javac.util.Context;
    27 import com.sun.tools.javac.code.Types;
    28 import com.sun.tools.javac.code.Symtab;
    29 import com.sun.tools.javac.code.Type;
    30 import com.sun.tools.javac.code.Type.*;
    31 import com.sun.tools.javac.code.Symbol.*;
    32 import com.sun.tools.javac.comp.Check;
    33 import com.sun.tools.javac.util.List;
    34 import com.sun.tools.javac.util.ListBuffer;
    35 import com.sun.tools.javac.util.Name;
    36 import com.sun.tools.javac.util.Names;
    37 import com.sun.tools.javac.file.JavacFileManager;
    39 /**
    40  * Test harness whose goal is to simplify the task of writing type-system
    41  * regression test. It provides functionalities to build custom types as well
    42  * as to access the underlying javac's symbol table in order to retrieve
    43  * predefined types. Among the features supported by the harness are: type
    44  * substitution, type containment, subtyping, cast-conversion, assigment
    45  * conversion.
    46  *
    47  * This class is meant to be a common super class for all concrete type test
    48  * classes. A subclass can access the type-factory and the test methods so as
    49  * to write compact tests. An example is reported below:
    50  *
    51  * <pre>
    52  * Type X = fac.TypeVariable();
    53  * Type Y = fac.TypeVariable();
    54  * Type A_X_Y = fac.Class(0, X, Y);
    55  * Type A_Obj_Obj = fac.Class(0,
    56  *           predef.objectType,
    57  *           predef.objectType);
    58  * checkSameType(A_Obj_Obj, subst(A_X_Y,
    59  *           Mapping(X, predef.objectType),
    60  *           Mapping(Y, predef.objectType)));
    61  * </pre>
    62  *
    63  * The above code is used to create two class types, namely {@code A<X,Y>} and
    64  * {@code A<Object,Object>} where both {@code X} and {@code Y} are type-variables.
    65  * The code then verifies that {@code [X:=Object,Y:=Object]A<X,Y> == A<Object,Object>}.
    66  *
    67  * @author mcimadamore
    68  */
    69 public class TypeHarness {
    71     protected Types types;
    72     protected Check chk;
    73     protected Symtab predef;
    74     protected Names names;
    75     protected Factory fac;
    77     protected TypeHarness() {
    78         Context ctx = new Context();
    79         JavacFileManager.preRegister(ctx);
    80         types = Types.instance(ctx);
    81         chk = Check.instance(ctx);
    82         predef = Symtab.instance(ctx);
    83         names = Names.instance(ctx);
    84         fac = new Factory();
    85     }
    87     // <editor-fold defaultstate="collapsed" desc="type assertions">
    89     /** assert that 's' is a subtype of 't' */
    90     public void assertSubtype(Type s, Type t) {
    91         assertSubtype(s, t, true);
    92     }
    94     /** assert that 's' is/is not a subtype of 't' */
    95     public void assertSubtype(Type s, Type t, boolean expected) {
    96         if (types.isSubtype(s, t) != expected) {
    97             String msg = expected ?
    98                 " is not a subtype of " :
    99                 " is a subtype of ";
   100             error(s + msg + t);
   101         }
   102     }
   104     /** assert that 's' is the same type as 't' */
   105     public void assertSameType(Type s, Type t) {
   106         assertSameType(s, t, true);
   107     }
   109     /** assert that 's' is/is not the same type as 't' */
   110     public void assertSameType(Type s, Type t, boolean expected) {
   111         if (types.isSameType(s, t) != expected) {
   112             String msg = expected ?
   113                 " is not the same type as " :
   114                 " is the same type as ";
   115             error(s + msg + t);
   116         }
   117     }
   119     /** assert that 's' is castable to 't' */
   120     public void assertCastable(Type s, Type t) {
   121         assertCastable(s, t, true);
   122     }
   124     /** assert that 's' is/is not castable to 't' */
   125     public void assertCastable(Type s, Type t, boolean expected) {
   126         if (types.isCastable(s, t) != expected) {
   127             String msg = expected ?
   128                 " is not castable to " :
   129                 " is castable to ";
   130             error(s + msg + t);
   131         }
   132     }
   134     /** assert that 's' is convertible (method invocation conversion) to 't' */
   135     public void assertConvertible(Type s, Type t) {
   136         assertCastable(s, t, true);
   137     }
   139     /** assert that 's' is/is not convertible (method invocation conversion) to 't' */
   140     public void assertConvertible(Type s, Type t, boolean expected) {
   141         if (types.isConvertible(s, t) != expected) {
   142             String msg = expected ?
   143                 " is not convertible to " :
   144                 " is convertible to ";
   145             error(s + msg + t);
   146         }
   147     }
   149     /** assert that 's' is assignable to 't' */
   150     public void assertAssignable(Type s, Type t) {
   151         assertCastable(s, t, true);
   152     }
   154     /** assert that 's' is/is not assignable to 't' */
   155     public void assertAssignable(Type s, Type t, boolean expected) {
   156         if (types.isAssignable(s, t) != expected) {
   157             String msg = expected ?
   158                 " is not assignable to " :
   159                 " is assignable to ";
   160             error(s + msg + t);
   161         }
   162     }
   164     /** assert that generic type 't' is well-formed */
   165     public void assertValidGenericType(Type t) {
   166         assertValidGenericType(t, true);
   167     }
   169     /** assert that 's' is/is not assignable to 't' */
   170     public void assertValidGenericType(Type t, boolean expected) {
   171         if (chk.checkValidGenericType(t) != expected) {
   172             String msg = expected ?
   173                 " is not a valid generic type" :
   174                 " is a valid generic type";
   175             error(t + msg + "   " + t.tsym.type);
   176         }
   177     }
   178     // </editor-fold>
   180     private void error(String msg) {
   181         throw new AssertionError("Unexpected result: " + msg);
   182     }
   184     // <editor-fold defaultstate="collapsed" desc="type functions">
   186     /** compute the erasure of a type 't' */
   187     public Type erasure(Type t) {
   188         return types.erasure(t);
   189     }
   191     /** compute the capture of a type 't' */
   192     public Type capture(Type t) {
   193         return types.capture(t);
   194     }
   196     /** compute the boxed type associated with 't' */
   197     public Type box(Type t) {
   198         if (!t.isPrimitive()) {
   199             throw new AssertionError("Cannot box non-primitive type: " + t);
   200         }
   201         return types.boxedClass(t).type;
   202     }
   204     /** compute the unboxed type associated with 't' */
   205     public Type unbox(Type t) {
   206         Type u = types.unboxedType(t);
   207         if (t == null) {
   208             throw new AssertionError("Cannot unbox reference type: " + t);
   209         } else {
   210             return u;
   211         }
   212     }
   214     /** compute a type substitution on 't' given a list of type mappings */
   215     public Type subst(Type t, Mapping... maps) {
   216         ListBuffer<Type> from = ListBuffer.lb();
   217         ListBuffer<Type> to = ListBuffer.lb();
   218         for (Mapping tm : maps) {
   219             from.append(tm.from);
   220             to.append(tm.to);
   221         }
   222         return types.subst(t, from.toList(), to.toList());
   223     }
   225     /** create a fresh type mapping from a type to another */
   226     public Mapping Mapping(Type from, Type to) {
   227         return new Mapping(from, to);
   228     }
   230     public static class Mapping {
   231         Type from;
   232         Type to;
   233         private Mapping(Type from, Type to) {
   234             this.from = from;
   235             this.to = to;
   236         }
   237     }
   238     // </editor-fold>
   240     // <editor-fold defaultstate="collapsed" desc="type factory">
   242     /**
   243      * This class is used to create Java types in a simple way. All main
   244      * kinds of type are supported: primitive, reference, non-denotable. The
   245      * factory also supports creation of constant types (used by the compiler
   246      * to represent the type of a literal).
   247      */
   248     public class Factory {
   250         private int synthNameCount = 0;
   252         private Name syntheticName() {
   253             return names.fromString("A$" + synthNameCount++);
   254         }
   256         public ClassType Class(long flags, Type... typeArgs) {
   257             ClassSymbol csym = new ClassSymbol(flags, syntheticName(), predef.noSymbol);
   258             csym.type = new ClassType(Type.noType, List.from(typeArgs), csym);
   259             ((ClassType)csym.type).supertype_field = predef.objectType;
   260             return (ClassType)csym.type;
   261         }
   263         public ClassType Class(Type... typeArgs) {
   264             return Class(0, typeArgs);
   265         }
   267         public ClassType Interface(Type... typeArgs) {
   268             return Class(Flags.INTERFACE, typeArgs);
   269         }
   271         public ClassType Interface(long flags, Type... typeArgs) {
   272             return Class(Flags.INTERFACE | flags, typeArgs);
   273         }
   275         public Type Constant(byte b) {
   276             return predef.byteType.constType(b);
   277         }
   279         public Type Constant(short s) {
   280             return predef.shortType.constType(s);
   281         }
   283         public Type Constant(int i) {
   284             return predef.intType.constType(i);
   285         }
   287         public Type Constant(long l) {
   288             return predef.longType.constType(l);
   289         }
   291         public Type Constant(float f) {
   292             return predef.floatType.constType(f);
   293         }
   295         public Type Constant(double d) {
   296             return predef.doubleType.constType(d);
   297         }
   299         public Type Constant(char c) {
   300             return predef.charType.constType(c + 0);
   301         }
   303         public ArrayType Array(Type elemType) {
   304             return new ArrayType(elemType, predef.arrayClass);
   305         }
   307         public TypeVar TypeVariable() {
   308             return TypeVariable(predef.objectType);
   309         }
   311         public TypeVar TypeVariable(Type bound) {
   312             TypeSymbol tvsym = new TypeVariableSymbol(0, syntheticName(), null, predef.noSymbol);
   313             tvsym.type = new TypeVar(tvsym, bound, null);
   314             return (TypeVar)tvsym.type;
   315         }
   317         public WildcardType Wildcard(BoundKind bk, Type bound) {
   318             return new WildcardType(bound, bk, predef.boundClass);
   319         }
   321         public CapturedType CapturedVariable(Type upper, Type lower) {
   322             return new CapturedType(syntheticName(), predef.noSymbol, upper, lower, null);
   323         }
   325         public ClassType Intersection(Type classBound, Type... intfBounds) {
   326             ClassType ct = Class(Flags.COMPOUND);
   327             ct.supertype_field = classBound;
   328             ct.interfaces_field = List.from(intfBounds);
   329             return ct;
   330         }
   331     }
   332     // </editor-fold>
   333 }

mercurial