This example Java source code file (TypeHarness.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.comp.Check;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.file.JavacFileManager;
/**
* Test harness whose goal is to simplify the task of writing type-system
* regression test. It provides functionalities to build custom types as well
* as to access the underlying javac's symbol table in order to retrieve
* predefined types. Among the features supported by the harness are: type
* substitution, type containment, subtyping, cast-conversion, assigment
* conversion.
*
* This class is meant to be a common super class for all concrete type test
* classes. A subclass can access the type-factory and the test methods so as
* to write compact tests. An example is reported below:
*
* <pre>
* Type X = fac.TypeVariable();
* Type Y = fac.TypeVariable();
* Type A_X_Y = fac.Class(0, X, Y);
* Type A_Obj_Obj = fac.Class(0,
* predef.objectType,
* predef.objectType);
* checkSameType(A_Obj_Obj, subst(A_X_Y,
* Mapping(X, predef.objectType),
* Mapping(Y, predef.objectType)));
* </pre>
*
* The above code is used to create two class types, namely {@code A<X,Y>} and
* {@code A<Object,Object>} where both {@code X} and {@code Y} are type-variables.
* The code then verifies that {@code [X:=Object,Y:=Object]A<X,Y> == A
Other Java examples (source code examples)
Here is a short list of links related to this Java TypeHarness.java source code file: