test/tools/javac/types/GenericTypeWellFormednessTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 7007432 7006109
aoqi@0 27 * @summary Test generic types well-formedness
aoqi@0 28 * @author mcimadamore
aoqi@0 29 * @library .
aoqi@0 30 * @run main GenericTypeWellFormednessTest
aoqi@0 31 */
aoqi@0 32
aoqi@0 33 import com.sun.tools.javac.code.BoundKind;
aoqi@0 34 import com.sun.tools.javac.code.Type;
aoqi@0 35 import com.sun.tools.javac.code.Type.*;
aoqi@0 36 import com.sun.tools.javac.code.Symbol;
aoqi@0 37 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 38 import java.lang.reflect.Array;
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Check parameterized type well-formedness. This test executes a number of checks
aoqi@0 42 * in order to establish as to whether an instantiation of a generic type conforms
aoqi@0 43 * to the generic class' declared bounds.
aoqi@0 44 */
aoqi@0 45 public class GenericTypeWellFormednessTest extends TypeHarness {
aoqi@0 46
aoqi@0 47 static int executedCount = 0;
aoqi@0 48 static int ignoredCount = 0;
aoqi@0 49
aoqi@0 50 InstantiableType[] rows;
aoqi@0 51 Type[] columns;
aoqi@0 52
aoqi@0 53 static class InstantiableType {
aoqi@0 54 protected Type type;
aoqi@0 55
aoqi@0 56 public InstantiableType(Type type) {
aoqi@0 57 this.type = type;
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 Type inst(Type clazz) {
aoqi@0 61 return type;
aoqi@0 62 }
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 enum Result {
aoqi@0 66 /* generic type is well-formed w.r.t. declared bounds */
aoqi@0 67 OK(true),
aoqi@0 68 /* generic type is not well-formed w.r.t. declared bounds */
aoqi@0 69 FAIL(false),
aoqi@0 70 /* generic type is not well-formed w.r.t. declared bounds according to the JLS 3rd,
aoqi@0 71 * but javac allows it (spec for generic type well-formedness is overly restrictive)
aoqi@0 72 * See regression test test/tools/generics/wildcards/T5097548.java
aoqi@0 73 */
aoqi@0 74 IGNORE(false);
aoqi@0 75
aoqi@0 76 boolean value;
aoqi@0 77
aoqi@0 78 Result(boolean value) {
aoqi@0 79 this.value = value;
aoqi@0 80 }
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 static final Result T = Result.OK;
aoqi@0 84 static final Result F = Result.FAIL;
aoqi@0 85 static final Result I = Result.IGNORE;
aoqi@0 86
aoqi@0 87 /*is a type in 'rows' a valid instantiation for the generic class in 'col' ? */
aoqi@0 88 Result[][] isValidInstantiation = {
aoqi@0 89 //Foo<X>, Foo<X ext Object>, Foo<X ext Number>, Foo<X ext Foo<X>>, Foo<X ext Foo<+X>>, Foo<X ext Foo<-X>>, Foo<X ext Foo<?>>
aoqi@0 90 /*Foo<Object>*/ { T , T , F , F , F , F , F },
aoqi@0 91 /*Foo<Number>*/ { T , T , T , F , F , F , F },
aoqi@0 92 /*Foo<Integer>*/ { T , T , T , F , F , F , F },
aoqi@0 93 /*Foo<Double>*/ { T , T , T , F , F , F , F },
aoqi@0 94 /*Foo<String>*/ { T , T , F , F , F , F , F },
aoqi@0 95 /*Foo<X1>*/ { T , T , F , F , F , F , F },
aoqi@0 96 /*Foo<X2>*/ { T , T , T , F , F , F , F },
aoqi@0 97 /*Foo<X3>*/ { T , T , T , F , F , F , F },
aoqi@0 98 /*Foo<X4>*/ { T , T , T , F , F , F , F },
aoqi@0 99 /*Foo<X5>*/ { T , T , F , F , F , F , F },
aoqi@0 100 /*Foo<X6>*/ { T , T , F , T , T , T , T },
aoqi@0 101 /*Foo<+Object>*/ { T , T , I , I , I , I , I },
aoqi@0 102 /*Foo<+Number>*/ { T , T , T , F , F , F , F },
aoqi@0 103 /*Foo<+Integer>*/{ T , T , T , F , F , F , F },
aoqi@0 104 /*Foo<+Double>*/ { T , T , T , F , F , F , F },
aoqi@0 105 /*Foo<+String>*/ { T , T , F , F , F , F , F },
aoqi@0 106 /*Foo<+X1>*/ { T , T , F , F , F , F , F },
aoqi@0 107 /*Foo<+X2>*/ { T , T , T , F , F , F , F },
aoqi@0 108 /*Foo<+X3>*/ { T , T , T , F , F , F , F },
aoqi@0 109 /*Foo<+X4>*/ { T , T , T , F , F , F , F },
aoqi@0 110 /*Foo<+X5>*/ { T , T , F , F , F , F , F },
aoqi@0 111 /*Foo<+X6>*/ { T , T , F , T , T , I , T },
aoqi@0 112 /*Foo<-Object>*/ { T , T , F , F , F , F , F },
aoqi@0 113 /*Foo<-Number>*/ { T , T , T , F , F , F , F },
aoqi@0 114 /*Foo<-Integer>*/{ T , T , T , F , F , F , F },
aoqi@0 115 /*Foo<-Double>*/ { T , T , T , F , F , F , F },
aoqi@0 116 /*Foo<-String>*/ { T , T , F , F , F , F , F },
aoqi@0 117 /*Foo<-X1>*/ { T , T , I , I , I , I , I },
aoqi@0 118 /*Foo<-X2>*/ { T , T , I , F , F , F , F },
aoqi@0 119 /*Foo<-X3>*/ { T , T , I , F , F , F , F },
aoqi@0 120 /*Foo<-X4>*/ { T , T , I , F , F , F , F },
aoqi@0 121 /*Foo<-X5>*/ { T , T , I , F , F , F , F },
aoqi@0 122 /*Foo<-X6>*/ { T , T , F , T , I , I , T },
aoqi@0 123 /*Foo<?>*/ { T , T , T , T , T , T , T }};
aoqi@0 124
aoqi@0 125 GenericTypeWellFormednessTest() {
aoqi@0 126 InstantiableType[] basicTypes = {
aoqi@0 127 new InstantiableType(predef.objectType),
aoqi@0 128 new InstantiableType(NumberType()),
aoqi@0 129 new InstantiableType(box(predef.intType)),
aoqi@0 130 new InstantiableType(box(predef.doubleType)),
aoqi@0 131 new InstantiableType(predef.stringType) };
aoqi@0 132
aoqi@0 133 InstantiableType[] typeVars = new InstantiableType[basicTypes.length + 1];
aoqi@0 134 for (int i = 0 ; i < basicTypes.length ; i++) {
aoqi@0 135 typeVars[i] = new InstantiableType(fac.TypeVariable(basicTypes[i].type));
aoqi@0 136 }
aoqi@0 137 typeVars[typeVars.length - 1] = new InstantiableType(null) {
aoqi@0 138 Type inst(Type clazz) {
aoqi@0 139 TypeVar tvar = fac.TypeVariable();
aoqi@0 140 tvar.bound = subst(clazz, Mapping(clazz.getTypeArguments().head, tvar));
aoqi@0 141 return tvar;
aoqi@0 142 }
aoqi@0 143 };
aoqi@0 144
aoqi@0 145 InstantiableType[] typeArgs = join(InstantiableType.class, basicTypes, typeVars);
aoqi@0 146
aoqi@0 147 InstantiableType[] invariantTypes = new InstantiableType[typeArgs.length];
aoqi@0 148 for (int i = 0 ; i < typeArgs.length ; i++) {
aoqi@0 149 final InstantiableType type1 = typeArgs[i];
aoqi@0 150 invariantTypes[i] = new InstantiableType(typeArgs[i].type) {
aoqi@0 151 Type inst(Type clazz) {
aoqi@0 152 return subst(clazz, Mapping(clazz.getTypeArguments().head, type1.inst(clazz)));
aoqi@0 153 }
aoqi@0 154 };
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 InstantiableType[] covariantTypes = new InstantiableType[typeArgs.length];
aoqi@0 158 for (int i = 0 ; i < typeArgs.length ; i++) {
aoqi@0 159 final InstantiableType type1 = typeArgs[i];
aoqi@0 160 covariantTypes[i] = new InstantiableType(null) {
aoqi@0 161 Type inst(Type clazz) {
aoqi@0 162 Type t = fac.Wildcard(BoundKind.EXTENDS, type1.inst(clazz));
aoqi@0 163 return subst(clazz, Mapping(clazz.getTypeArguments().head, t));
aoqi@0 164 }
aoqi@0 165 };
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 InstantiableType[] contravariantTypes = new InstantiableType[typeArgs.length];
aoqi@0 169 for (int i = 0 ; i < typeArgs.length ; i++) {
aoqi@0 170 final InstantiableType type1 = typeArgs[i];
aoqi@0 171 contravariantTypes[i] = new InstantiableType(null) {
aoqi@0 172 Type inst(Type clazz) {
aoqi@0 173 Type t = fac.Wildcard(BoundKind.SUPER, type1.inst(clazz));
aoqi@0 174 return subst(clazz, Mapping(clazz.getTypeArguments().head, t));
aoqi@0 175 }
aoqi@0 176 };
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 InstantiableType[] bivariantTypes = {
aoqi@0 180 new InstantiableType(fac.Wildcard(BoundKind.UNBOUND, predef.objectType)) {
aoqi@0 181 Type inst(Type clazz) {
aoqi@0 182 return subst(clazz, Mapping(clazz.getTypeArguments().head, type));
aoqi@0 183 }
aoqi@0 184 }
aoqi@0 185 };
aoqi@0 186
aoqi@0 187 rows = join(InstantiableType.class, invariantTypes, covariantTypes, contravariantTypes, bivariantTypes);
aoqi@0 188
aoqi@0 189 Type tv1 = fac.TypeVariable();
aoqi@0 190 Type decl1 = fac.Class(tv1);
aoqi@0 191
aoqi@0 192 Type tv2 = fac.TypeVariable(predef.objectType);
aoqi@0 193 Type decl2 = fac.Class(tv2);
aoqi@0 194
aoqi@0 195 Type tv3 = fac.TypeVariable(NumberType());
aoqi@0 196 Type decl3 = fac.Class(tv3);
aoqi@0 197
aoqi@0 198 TypeVar tv4 = fac.TypeVariable();
aoqi@0 199 Type decl4 = fac.Class(tv4);
aoqi@0 200 tv4.bound = decl4;
aoqi@0 201 tv4.tsym.name = predef.exceptionType.tsym.name;
aoqi@0 202
aoqi@0 203 TypeVar tv5 = fac.TypeVariable();
aoqi@0 204 Type decl5 = fac.Class(tv5);
aoqi@0 205 tv5.bound = subst(decl5, Mapping(tv5, fac.Wildcard(BoundKind.EXTENDS, tv5)));
aoqi@0 206
aoqi@0 207 TypeVar tv6 = fac.TypeVariable();
aoqi@0 208 Type decl6 = fac.Class(tv6);
aoqi@0 209 tv6.bound = subst(decl6, Mapping(tv6, fac.Wildcard(BoundKind.SUPER, tv6)));
aoqi@0 210
aoqi@0 211 TypeVar tv7 = fac.TypeVariable();
aoqi@0 212 Type decl7 = fac.Class(tv7);
aoqi@0 213 tv7.bound = subst(decl7, Mapping(tv7, fac.Wildcard(BoundKind.UNBOUND, predef.objectType)));
aoqi@0 214
aoqi@0 215 columns = new Type[] {
aoqi@0 216 decl1, decl2, decl3, decl4, decl5, decl6, decl7
aoqi@0 217 };
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 void test() {
aoqi@0 221 for (int i = 0; i < rows.length ; i++) {
aoqi@0 222 for (int j = 0; j < columns.length ; j++) {
aoqi@0 223 Type decl = columns[j];
aoqi@0 224 Type inst = rows[i].inst(decl);
aoqi@0 225 if (isValidInstantiation[i][j] != Result.IGNORE) {
aoqi@0 226 executedCount++;
aoqi@0 227 assertValidGenericType(inst, isValidInstantiation[i][j].value);
aoqi@0 228 } else {
aoqi@0 229 ignoredCount++;
aoqi@0 230 }
aoqi@0 231 }
aoqi@0 232 }
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 Type NumberType() {
aoqi@0 236 Symbol s = box(predef.intType).tsym;
aoqi@0 237 s.complete();
aoqi@0 238 return ((ClassType)s.type).supertype_field;
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 @SuppressWarnings("unchecked")
aoqi@0 242 <T> T[] join(Class<T> type, T[]... args) {
aoqi@0 243 int totalLength = 0;
aoqi@0 244 for (T[] arr : args) {
aoqi@0 245 totalLength += arr.length;
aoqi@0 246 }
aoqi@0 247 T[] new_arr = (T[])Array.newInstance(type, totalLength);
aoqi@0 248 int idx = 0;
aoqi@0 249 for (T[] arr : args) {
aoqi@0 250 System.arraycopy(arr, 0, new_arr, idx, arr.length);
aoqi@0 251 idx += arr.length;
aoqi@0 252 }
aoqi@0 253 return new_arr;
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 public static void main(String[] args) {
aoqi@0 257 new GenericTypeWellFormednessTest().test();
aoqi@0 258 System.out.println("Executed checks : " + executedCount);
aoqi@0 259 System.out.println("Ignored checks : " + ignoredCount);
aoqi@0 260 }
aoqi@0 261 }

mercurial