mcimadamore@792: /* ohair@962: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. mcimadamore@792: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@792: * mcimadamore@792: * This code is free software; you can redistribute it and/or modify it mcimadamore@792: * under the terms of the GNU General Public License version 2 only, as mcimadamore@792: * published by the Free Software Foundation. mcimadamore@792: * mcimadamore@792: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@792: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@792: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@792: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@792: * accompanied this code). mcimadamore@792: * mcimadamore@792: * You should have received a copy of the GNU General Public License version mcimadamore@792: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@792: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@792: * mcimadamore@792: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@792: * or visit www.oracle.com if you need additional information or have any mcimadamore@792: * questions. mcimadamore@792: */ mcimadamore@792: mcimadamore@792: import com.sun.tools.javac.code.BoundKind; mcimadamore@792: import com.sun.tools.javac.code.Flags; mcimadamore@792: import com.sun.tools.javac.util.Context; mcimadamore@792: import com.sun.tools.javac.code.Types; mcimadamore@792: import com.sun.tools.javac.code.Symtab; mcimadamore@792: import com.sun.tools.javac.code.Type; mcimadamore@792: import com.sun.tools.javac.code.Type.*; mcimadamore@792: import com.sun.tools.javac.code.Symbol.*; mcimadamore@821: import com.sun.tools.javac.comp.Check; mcimadamore@792: import com.sun.tools.javac.util.List; mcimadamore@792: import com.sun.tools.javac.util.ListBuffer; mcimadamore@792: import com.sun.tools.javac.util.Name; mcimadamore@792: import com.sun.tools.javac.util.Names; mcimadamore@792: import com.sun.tools.javac.file.JavacFileManager; mcimadamore@792: mcimadamore@792: /** mcimadamore@792: * Test harness whose goal is to simplify the task of writing type-system mcimadamore@792: * regression test. It provides functionalities to build custom types as well mcimadamore@792: * as to access the underlying javac's symbol table in order to retrieve mcimadamore@792: * predefined types. Among the features supported by the harness are: type mcimadamore@792: * substitution, type containment, subtyping, cast-conversion, assigment mcimadamore@792: * conversion. mcimadamore@792: * mcimadamore@792: * This class is meant to be a common super class for all concrete type test mcimadamore@792: * classes. A subclass can access the type-factory and the test methods so as mcimadamore@792: * to write compact tests. An example is reported below: mcimadamore@792: * mcimadamore@792: *
mcimadamore@792:  * Type X = fac.TypeVariable();
mcimadamore@792:  * Type Y = fac.TypeVariable();
mcimadamore@792:  * Type A_X_Y = fac.Class(0, X, Y);
mcimadamore@792:  * Type A_Obj_Obj = fac.Class(0,
mcimadamore@792:  *           predef.objectType,
mcimadamore@792:  *           predef.objectType);
mcimadamore@792:  * checkSameType(A_Obj_Obj, subst(A_X_Y,
mcimadamore@792:  *           Mapping(X, predef.objectType),
mcimadamore@792:  *           Mapping(Y, predef.objectType)));
mcimadamore@792:  * 
mcimadamore@792: * mcimadamore@792: * The above code is used to create two class types, namely {@code A} and mcimadamore@792: * {@code A} where both {@code X} and {@code Y} are type-variables. mcimadamore@792: * The code then verifies that {@code [X:=Object,Y:=Object]A == A}. mcimadamore@792: * mcimadamore@792: * @author mcimadamore mcimadamore@792: */ mcimadamore@792: public class TypeHarness { mcimadamore@792: mcimadamore@792: protected Types types; mcimadamore@821: protected Check chk; mcimadamore@792: protected Symtab predef; mcimadamore@792: protected Names names; mcimadamore@792: protected Factory fac; mcimadamore@792: mcimadamore@792: protected TypeHarness() { mcimadamore@792: Context ctx = new Context(); mcimadamore@792: JavacFileManager.preRegister(ctx); mcimadamore@792: types = Types.instance(ctx); mcimadamore@821: chk = Check.instance(ctx); mcimadamore@792: predef = Symtab.instance(ctx); mcimadamore@792: names = Names.instance(ctx); mcimadamore@792: fac = new Factory(); mcimadamore@792: } mcimadamore@792: mcimadamore@792: // mcimadamore@792: mcimadamore@792: /** assert that 's' is a subtype of 't' */ mcimadamore@792: public void assertSubtype(Type s, Type t) { mcimadamore@792: assertSubtype(s, t, true); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is/is not a subtype of 't' */ mcimadamore@792: public void assertSubtype(Type s, Type t, boolean expected) { mcimadamore@792: if (types.isSubtype(s, t) != expected) { mcimadamore@792: String msg = expected ? mcimadamore@792: " is not a subtype of " : mcimadamore@792: " is a subtype of "; mcimadamore@792: error(s + msg + t); mcimadamore@792: } mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is the same type as 't' */ mcimadamore@792: public void assertSameType(Type s, Type t) { mcimadamore@792: assertSameType(s, t, true); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is/is not the same type as 't' */ mcimadamore@792: public void assertSameType(Type s, Type t, boolean expected) { mcimadamore@792: if (types.isSameType(s, t) != expected) { mcimadamore@792: String msg = expected ? mcimadamore@792: " is not the same type as " : mcimadamore@792: " is the same type as "; mcimadamore@792: error(s + msg + t); mcimadamore@792: } mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is castable to 't' */ mcimadamore@792: public void assertCastable(Type s, Type t) { mcimadamore@792: assertCastable(s, t, true); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is/is not castable to 't' */ mcimadamore@792: public void assertCastable(Type s, Type t, boolean expected) { mcimadamore@792: if (types.isCastable(s, t) != expected) { mcimadamore@792: String msg = expected ? mcimadamore@792: " is not castable to " : mcimadamore@792: " is castable to "; mcimadamore@792: error(s + msg + t); mcimadamore@792: } mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is convertible (method invocation conversion) to 't' */ mcimadamore@792: public void assertConvertible(Type s, Type t) { mcimadamore@792: assertCastable(s, t, true); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is/is not convertible (method invocation conversion) to 't' */ mcimadamore@792: public void assertConvertible(Type s, Type t, boolean expected) { mcimadamore@792: if (types.isConvertible(s, t) != expected) { mcimadamore@792: String msg = expected ? mcimadamore@792: " is not convertible to " : mcimadamore@792: " is convertible to "; mcimadamore@792: error(s + msg + t); mcimadamore@792: } mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is assignable to 't' */ mcimadamore@792: public void assertAssignable(Type s, Type t) { mcimadamore@792: assertCastable(s, t, true); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** assert that 's' is/is not assignable to 't' */ mcimadamore@792: public void assertAssignable(Type s, Type t, boolean expected) { mcimadamore@792: if (types.isAssignable(s, t) != expected) { mcimadamore@792: String msg = expected ? mcimadamore@792: " is not assignable to " : mcimadamore@792: " is assignable to "; mcimadamore@792: error(s + msg + t); mcimadamore@792: } mcimadamore@792: } mcimadamore@821: mcimadamore@821: /** assert that generic type 't' is well-formed */ mcimadamore@821: public void assertValidGenericType(Type t) { mcimadamore@821: assertValidGenericType(t, true); mcimadamore@821: } mcimadamore@821: mcimadamore@821: /** assert that 's' is/is not assignable to 't' */ mcimadamore@821: public void assertValidGenericType(Type t, boolean expected) { mcimadamore@821: if (chk.checkValidGenericType(t) != expected) { mcimadamore@821: String msg = expected ? mcimadamore@821: " is not a valid generic type" : mcimadamore@821: " is a valid generic type"; mcimadamore@821: error(t + msg + " " + t.tsym.type); mcimadamore@821: } mcimadamore@821: } mcimadamore@792: // mcimadamore@792: mcimadamore@792: private void error(String msg) { mcimadamore@792: throw new AssertionError("Unexpected result: " + msg); mcimadamore@792: } mcimadamore@792: mcimadamore@792: // mcimadamore@792: mcimadamore@792: /** compute the erasure of a type 't' */ mcimadamore@792: public Type erasure(Type t) { mcimadamore@792: return types.erasure(t); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** compute the capture of a type 't' */ mcimadamore@792: public Type capture(Type t) { mcimadamore@792: return types.capture(t); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** compute the boxed type associated with 't' */ mcimadamore@792: public Type box(Type t) { mcimadamore@792: if (!t.isPrimitive()) { mcimadamore@792: throw new AssertionError("Cannot box non-primitive type: " + t); mcimadamore@792: } mcimadamore@792: return types.boxedClass(t).type; mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** compute the unboxed type associated with 't' */ mcimadamore@792: public Type unbox(Type t) { mcimadamore@792: Type u = types.unboxedType(t); mcimadamore@792: if (t == null) { mcimadamore@792: throw new AssertionError("Cannot unbox reference type: " + t); mcimadamore@792: } else { mcimadamore@792: return u; mcimadamore@792: } mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** compute a type substitution on 't' given a list of type mappings */ mcimadamore@792: public Type subst(Type t, Mapping... maps) { mcimadamore@792: ListBuffer from = ListBuffer.lb(); mcimadamore@792: ListBuffer to = ListBuffer.lb(); mcimadamore@792: for (Mapping tm : maps) { mcimadamore@792: from.append(tm.from); mcimadamore@792: to.append(tm.to); mcimadamore@792: } mcimadamore@792: return types.subst(t, from.toList(), to.toList()); mcimadamore@792: } mcimadamore@792: mcimadamore@792: /** create a fresh type mapping from a type to another */ mcimadamore@792: public Mapping Mapping(Type from, Type to) { mcimadamore@792: return new Mapping(from, to); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public static class Mapping { mcimadamore@792: Type from; mcimadamore@792: Type to; mcimadamore@792: private Mapping(Type from, Type to) { mcimadamore@792: this.from = from; mcimadamore@792: this.to = to; mcimadamore@792: } mcimadamore@792: } mcimadamore@792: // mcimadamore@792: mcimadamore@792: // mcimadamore@792: mcimadamore@792: /** mcimadamore@792: * This class is used to create Java types in a simple way. All main mcimadamore@792: * kinds of type are supported: primitive, reference, non-denotable. The mcimadamore@792: * factory also supports creation of constant types (used by the compiler mcimadamore@792: * to represent the type of a literal). mcimadamore@792: */ mcimadamore@792: public class Factory { mcimadamore@792: mcimadamore@792: private int synthNameCount = 0; mcimadamore@792: mcimadamore@792: private Name syntheticName() { mcimadamore@792: return names.fromString("A$" + synthNameCount++); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public ClassType Class(long flags, Type... typeArgs) { mcimadamore@792: ClassSymbol csym = new ClassSymbol(flags, syntheticName(), predef.noSymbol); mcimadamore@792: csym.type = new ClassType(Type.noType, List.from(typeArgs), csym); mcimadamore@792: ((ClassType)csym.type).supertype_field = predef.objectType; mcimadamore@792: return (ClassType)csym.type; mcimadamore@792: } mcimadamore@792: mcimadamore@792: public ClassType Class(Type... typeArgs) { mcimadamore@792: return Class(0, typeArgs); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public ClassType Interface(Type... typeArgs) { mcimadamore@792: return Class(Flags.INTERFACE, typeArgs); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public ClassType Interface(long flags, Type... typeArgs) { mcimadamore@792: return Class(Flags.INTERFACE | flags, typeArgs); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public Type Constant(byte b) { mcimadamore@792: return predef.byteType.constType(b); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public Type Constant(short s) { mcimadamore@792: return predef.shortType.constType(s); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public Type Constant(int i) { mcimadamore@792: return predef.intType.constType(i); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public Type Constant(long l) { mcimadamore@792: return predef.longType.constType(l); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public Type Constant(float f) { mcimadamore@792: return predef.floatType.constType(f); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public Type Constant(double d) { mcimadamore@792: return predef.doubleType.constType(d); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public Type Constant(char c) { mcimadamore@792: return predef.charType.constType(c + 0); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public ArrayType Array(Type elemType) { mcimadamore@792: return new ArrayType(elemType, predef.arrayClass); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public TypeVar TypeVariable() { mcimadamore@792: return TypeVariable(predef.objectType); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public TypeVar TypeVariable(Type bound) { mcimadamore@792: TypeSymbol tvsym = new TypeSymbol(0, syntheticName(), null, predef.noSymbol); mcimadamore@792: tvsym.type = new TypeVar(tvsym, bound, null); mcimadamore@792: return (TypeVar)tvsym.type; mcimadamore@792: } mcimadamore@792: mcimadamore@792: public WildcardType Wildcard(BoundKind bk, Type bound) { mcimadamore@792: return new WildcardType(bound, bk, predef.boundClass); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public CapturedType CapturedVariable(Type upper, Type lower) { mcimadamore@792: return new CapturedType(syntheticName(), predef.noSymbol, upper, lower, null); mcimadamore@792: } mcimadamore@792: mcimadamore@792: public ClassType Intersection(Type classBound, Type... intfBounds) { mcimadamore@792: ClassType ct = Class(Flags.COMPOUND); mcimadamore@792: ct.supertype_field = classBound; mcimadamore@792: ct.interfaces_field = List.from(intfBounds); mcimadamore@792: return ct; mcimadamore@792: } mcimadamore@792: } mcimadamore@792: // mcimadamore@792: }