aoqi@0: /* aoqi@0: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8002099 aoqi@0: * @summary Add support for intersection types in cast expression aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: public class IntersectionTypeParserTest { aoqi@0: aoqi@0: static int checkCount = 0; aoqi@0: aoqi@0: enum TypeKind { aoqi@0: SIMPLE("A"), aoqi@0: GENERIC("A"), aoqi@0: WILDCARD("A"); aoqi@0: aoqi@0: String typeStr; aoqi@0: aoqi@0: TypeKind(String typeStr) { aoqi@0: this.typeStr = typeStr; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum ArrayKind { aoqi@0: NONE(""), aoqi@0: SINGLE("[]"), aoqi@0: DOUBLE("[][]"); aoqi@0: aoqi@0: String arrStr; aoqi@0: aoqi@0: ArrayKind(String arrStr) { aoqi@0: this.arrStr = arrStr; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class Type { aoqi@0: TypeKind tk; aoqi@0: ArrayKind ak; aoqi@0: aoqi@0: Type(TypeKind tk, ArrayKind ak) { aoqi@0: this.tk = tk; aoqi@0: this.ak = ak; aoqi@0: } aoqi@0: aoqi@0: String asString() { aoqi@0: return tk.typeStr + ak.arrStr; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum CastKind { aoqi@0: ONE("(#T0)", 1), aoqi@0: TWO("(#T0 & T1)", 2), aoqi@0: THREE("(#T0 & #T1 & #T2)", 3); aoqi@0: aoqi@0: String castTemplate; aoqi@0: int nBounds; aoqi@0: aoqi@0: CastKind(String castTemplate, int nBounds) { aoqi@0: this.castTemplate = castTemplate; aoqi@0: this.nBounds = nBounds; aoqi@0: } aoqi@0: aoqi@0: String asString(Type... types) { aoqi@0: String res = castTemplate; aoqi@0: for (int i = 0; i < nBounds ; i++) { aoqi@0: res = res.replaceAll(String.format("#T%d", i), types[i].asString()); aoqi@0: } aoqi@0: return res; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: //create default shared JavaCompiler - reused across multiple compilations aoqi@0: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); aoqi@0: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: for (CastKind ck : CastKind.values()) { aoqi@0: for (TypeKind t1 : TypeKind.values()) { aoqi@0: for (ArrayKind ak1 : ArrayKind.values()) { aoqi@0: Type typ1 = new Type(t1, ak1); aoqi@0: if (ck.nBounds == 1) { aoqi@0: new IntersectionTypeParserTest(ck, typ1).run(comp, fm); aoqi@0: continue; aoqi@0: } aoqi@0: for (TypeKind t2 : TypeKind.values()) { aoqi@0: for (ArrayKind ak2 : ArrayKind.values()) { aoqi@0: Type typ2 = new Type(t2, ak2); aoqi@0: if (ck.nBounds == 2) { aoqi@0: new IntersectionTypeParserTest(ck, typ1, typ2).run(comp, fm); aoqi@0: continue; aoqi@0: } aoqi@0: for (TypeKind t3 : TypeKind.values()) { aoqi@0: for (ArrayKind ak3 : ArrayKind.values()) { aoqi@0: Type typ3 = new Type(t3, ak3); aoqi@0: new IntersectionTypeParserTest(ck, typ1, typ2, typ3).run(comp, fm); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: System.out.println("Total check executed: " + checkCount); aoqi@0: } aoqi@0: aoqi@0: CastKind ck; aoqi@0: Type[] types; aoqi@0: JavaSource source; aoqi@0: DiagnosticChecker diagChecker; aoqi@0: aoqi@0: IntersectionTypeParserTest(CastKind ck, Type... types) { aoqi@0: this.ck = ck; aoqi@0: this.types = types; aoqi@0: this.source = new JavaSource(); aoqi@0: this.diagChecker = new DiagnosticChecker(); aoqi@0: } aoqi@0: aoqi@0: class JavaSource extends SimpleJavaFileObject { aoqi@0: aoqi@0: String bodyTemplate = "class Test {\n" + aoqi@0: " void test() {\n" + aoqi@0: " Object o = #Cnull;\n" + aoqi@0: " } }"; aoqi@0: aoqi@0: String source = ""; aoqi@0: aoqi@0: public JavaSource() { aoqi@0: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); aoqi@0: source += bodyTemplate.replaceAll("#C", ck.asString(types)); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return source; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { aoqi@0: checkCount++; aoqi@0: JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, aoqi@0: null, null, Arrays.asList(source)); aoqi@0: ct.parse(); aoqi@0: if (diagChecker.errorFound) { aoqi@0: throw new Error("Unexpected parser error for source:\n" + aoqi@0: source.getCharContent(true)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class DiagnosticChecker implements javax.tools.DiagnosticListener { aoqi@0: aoqi@0: boolean errorFound; aoqi@0: aoqi@0: public void report(Diagnostic diagnostic) { aoqi@0: if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { aoqi@0: errorFound = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }