Mon, 02 Sep 2013 22:38:36 +0100
8016177: structural most specific and stuckness
Reviewed-by: jjg, vromero
Contributed-by: maurizio.cimadamore@oracle.com
mcimadamore@1436 | 1 | /* |
mcimadamore@1436 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1436 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1436 | 4 | * |
mcimadamore@1436 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1436 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1436 | 7 | * published by the Free Software Foundation. |
mcimadamore@1436 | 8 | * |
mcimadamore@1436 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1436 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1436 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1436 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1436 | 13 | * accompanied this code). |
mcimadamore@1436 | 14 | * |
mcimadamore@1436 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1436 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1436 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1436 | 18 | * |
mcimadamore@1436 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1436 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1436 | 21 | * questions. |
mcimadamore@1436 | 22 | */ |
mcimadamore@1436 | 23 | |
mcimadamore@1436 | 24 | /* |
mcimadamore@1436 | 25 | * @test |
mcimadamore@1436 | 26 | * @bug 8002099 |
mcimadamore@1436 | 27 | * @summary Add support for intersection types in cast expression |
mcimadamore@1436 | 28 | */ |
mcimadamore@1436 | 29 | |
mcimadamore@1436 | 30 | import com.sun.source.util.JavacTask; |
mcimadamore@1436 | 31 | import java.net.URI; |
mcimadamore@1436 | 32 | import java.util.Arrays; |
mcimadamore@1436 | 33 | import javax.tools.Diagnostic; |
mcimadamore@1436 | 34 | import javax.tools.JavaCompiler; |
mcimadamore@1436 | 35 | import javax.tools.JavaFileObject; |
mcimadamore@1436 | 36 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@1436 | 37 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1436 | 38 | import javax.tools.ToolProvider; |
mcimadamore@1436 | 39 | |
mcimadamore@1436 | 40 | public class IntersectionTypeParserTest { |
mcimadamore@1436 | 41 | |
mcimadamore@1436 | 42 | static int checkCount = 0; |
mcimadamore@1436 | 43 | |
mcimadamore@1436 | 44 | enum TypeKind { |
mcimadamore@1436 | 45 | SIMPLE("A"), |
mcimadamore@1436 | 46 | GENERIC("A<X>"), |
mcimadamore@1436 | 47 | WILDCARD("A<? super X, ? extends Y>"); |
mcimadamore@1436 | 48 | |
mcimadamore@1436 | 49 | String typeStr; |
mcimadamore@1436 | 50 | |
mcimadamore@1436 | 51 | TypeKind(String typeStr) { |
mcimadamore@1436 | 52 | this.typeStr = typeStr; |
mcimadamore@1436 | 53 | } |
mcimadamore@1436 | 54 | } |
mcimadamore@1436 | 55 | |
mcimadamore@1436 | 56 | enum ArrayKind { |
mcimadamore@1436 | 57 | NONE(""), |
mcimadamore@1436 | 58 | SINGLE("[]"), |
mcimadamore@1436 | 59 | DOUBLE("[][]"); |
mcimadamore@1436 | 60 | |
mcimadamore@1436 | 61 | String arrStr; |
mcimadamore@1436 | 62 | |
mcimadamore@1436 | 63 | ArrayKind(String arrStr) { |
mcimadamore@1436 | 64 | this.arrStr = arrStr; |
mcimadamore@1436 | 65 | } |
mcimadamore@1436 | 66 | } |
mcimadamore@1436 | 67 | |
mcimadamore@1436 | 68 | static class Type { |
mcimadamore@1436 | 69 | TypeKind tk; |
mcimadamore@1436 | 70 | ArrayKind ak; |
mcimadamore@1436 | 71 | |
mcimadamore@1436 | 72 | Type(TypeKind tk, ArrayKind ak) { |
mcimadamore@1436 | 73 | this.tk = tk; |
mcimadamore@1436 | 74 | this.ak = ak; |
mcimadamore@1436 | 75 | } |
mcimadamore@1436 | 76 | |
mcimadamore@1436 | 77 | String asString() { |
mcimadamore@1436 | 78 | return tk.typeStr + ak.arrStr; |
mcimadamore@1436 | 79 | } |
mcimadamore@1436 | 80 | } |
mcimadamore@1436 | 81 | |
mcimadamore@1436 | 82 | enum CastKind { |
mcimadamore@1436 | 83 | ONE("(#T0)", 1), |
mcimadamore@1436 | 84 | TWO("(#T0 & T1)", 2), |
mcimadamore@1436 | 85 | THREE("(#T0 & #T1 & #T2)", 3); |
mcimadamore@1436 | 86 | |
mcimadamore@1436 | 87 | String castTemplate; |
mcimadamore@1436 | 88 | int nBounds; |
mcimadamore@1436 | 89 | |
mcimadamore@1436 | 90 | CastKind(String castTemplate, int nBounds) { |
mcimadamore@1436 | 91 | this.castTemplate = castTemplate; |
mcimadamore@1436 | 92 | this.nBounds = nBounds; |
mcimadamore@1436 | 93 | } |
mcimadamore@1436 | 94 | |
mcimadamore@1436 | 95 | String asString(Type... types) { |
mcimadamore@1436 | 96 | String res = castTemplate; |
mcimadamore@1436 | 97 | for (int i = 0; i < nBounds ; i++) { |
mcimadamore@1436 | 98 | res = res.replaceAll(String.format("#T%d", i), types[i].asString()); |
mcimadamore@1436 | 99 | } |
mcimadamore@1436 | 100 | return res; |
mcimadamore@1436 | 101 | } |
mcimadamore@1436 | 102 | } |
mcimadamore@1436 | 103 | |
mcimadamore@1436 | 104 | public static void main(String... args) throws Exception { |
mcimadamore@1436 | 105 | //create default shared JavaCompiler - reused across multiple compilations |
mcimadamore@1436 | 106 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1436 | 107 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1436 | 108 | |
mcimadamore@1436 | 109 | for (CastKind ck : CastKind.values()) { |
mcimadamore@1436 | 110 | for (TypeKind t1 : TypeKind.values()) { |
mcimadamore@1436 | 111 | for (ArrayKind ak1 : ArrayKind.values()) { |
mcimadamore@1436 | 112 | Type typ1 = new Type(t1, ak1); |
mcimadamore@1436 | 113 | if (ck.nBounds == 1) { |
mcimadamore@1436 | 114 | new IntersectionTypeParserTest(ck, typ1).run(comp, fm); |
mcimadamore@1436 | 115 | continue; |
mcimadamore@1436 | 116 | } |
mcimadamore@1436 | 117 | for (TypeKind t2 : TypeKind.values()) { |
mcimadamore@1436 | 118 | for (ArrayKind ak2 : ArrayKind.values()) { |
mcimadamore@1436 | 119 | Type typ2 = new Type(t2, ak2); |
mcimadamore@1436 | 120 | if (ck.nBounds == 2) { |
mcimadamore@1436 | 121 | new IntersectionTypeParserTest(ck, typ1, typ2).run(comp, fm); |
mcimadamore@1436 | 122 | continue; |
mcimadamore@1436 | 123 | } |
mcimadamore@1436 | 124 | for (TypeKind t3 : TypeKind.values()) { |
mcimadamore@1436 | 125 | for (ArrayKind ak3 : ArrayKind.values()) { |
mcimadamore@1436 | 126 | Type typ3 = new Type(t3, ak3); |
mcimadamore@1436 | 127 | new IntersectionTypeParserTest(ck, typ1, typ2, typ3).run(comp, fm); |
mcimadamore@1436 | 128 | } |
mcimadamore@1436 | 129 | } |
mcimadamore@1436 | 130 | } |
mcimadamore@1436 | 131 | } |
mcimadamore@1436 | 132 | } |
mcimadamore@1436 | 133 | } |
mcimadamore@1436 | 134 | } |
mcimadamore@1436 | 135 | System.out.println("Total check executed: " + checkCount); |
mcimadamore@1436 | 136 | } |
mcimadamore@1436 | 137 | |
mcimadamore@1436 | 138 | CastKind ck; |
mcimadamore@1436 | 139 | Type[] types; |
mcimadamore@1436 | 140 | JavaSource source; |
mcimadamore@1436 | 141 | DiagnosticChecker diagChecker; |
mcimadamore@1436 | 142 | |
mcimadamore@1436 | 143 | IntersectionTypeParserTest(CastKind ck, Type... types) { |
mcimadamore@1436 | 144 | this.ck = ck; |
mcimadamore@1436 | 145 | this.types = types; |
mcimadamore@1436 | 146 | this.source = new JavaSource(); |
mcimadamore@1436 | 147 | this.diagChecker = new DiagnosticChecker(); |
mcimadamore@1436 | 148 | } |
mcimadamore@1436 | 149 | |
mcimadamore@1436 | 150 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1436 | 151 | |
mcimadamore@1436 | 152 | String bodyTemplate = "class Test {\n" + |
mcimadamore@1436 | 153 | " void test() {\n" + |
mcimadamore@1436 | 154 | " Object o = #Cnull;\n" + |
mcimadamore@1436 | 155 | " } }"; |
mcimadamore@1436 | 156 | |
mcimadamore@1436 | 157 | String source = ""; |
mcimadamore@1436 | 158 | |
mcimadamore@1436 | 159 | public JavaSource() { |
mcimadamore@1436 | 160 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1436 | 161 | source += bodyTemplate.replaceAll("#C", ck.asString(types)); |
mcimadamore@1436 | 162 | } |
mcimadamore@1436 | 163 | |
mcimadamore@1436 | 164 | @Override |
mcimadamore@1436 | 165 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1436 | 166 | return source; |
mcimadamore@1436 | 167 | } |
mcimadamore@1436 | 168 | } |
mcimadamore@1436 | 169 | |
mcimadamore@1436 | 170 | void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { |
mcimadamore@1436 | 171 | checkCount++; |
mcimadamore@1436 | 172 | JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, |
mcimadamore@1511 | 173 | null, null, Arrays.asList(source)); |
mcimadamore@1436 | 174 | ct.parse(); |
mcimadamore@1436 | 175 | if (diagChecker.errorFound) { |
mcimadamore@1436 | 176 | throw new Error("Unexpected parser error for source:\n" + |
mcimadamore@1436 | 177 | source.getCharContent(true)); |
mcimadamore@1436 | 178 | } |
mcimadamore@1436 | 179 | } |
mcimadamore@1436 | 180 | |
mcimadamore@1436 | 181 | static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1436 | 182 | |
mcimadamore@1436 | 183 | boolean errorFound; |
mcimadamore@1436 | 184 | |
mcimadamore@1436 | 185 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1436 | 186 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
mcimadamore@1436 | 187 | errorFound = true; |
mcimadamore@1436 | 188 | } |
mcimadamore@1436 | 189 | } |
mcimadamore@1436 | 190 | } |
mcimadamore@1436 | 191 | } |