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