test/tools/javac/cast/intersection/IntersectionTypeParserTest.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, 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 8002099
aoqi@0 27 * @summary Add support for intersection types in cast expression
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import com.sun.source.util.JavacTask;
aoqi@0 31 import java.net.URI;
aoqi@0 32 import java.util.Arrays;
aoqi@0 33 import javax.tools.Diagnostic;
aoqi@0 34 import javax.tools.JavaCompiler;
aoqi@0 35 import javax.tools.JavaFileObject;
aoqi@0 36 import javax.tools.SimpleJavaFileObject;
aoqi@0 37 import javax.tools.StandardJavaFileManager;
aoqi@0 38 import javax.tools.ToolProvider;
aoqi@0 39
aoqi@0 40 public class IntersectionTypeParserTest {
aoqi@0 41
aoqi@0 42 static int checkCount = 0;
aoqi@0 43
aoqi@0 44 enum TypeKind {
aoqi@0 45 SIMPLE("A"),
aoqi@0 46 GENERIC("A<X>"),
aoqi@0 47 WILDCARD("A<? super X, ? extends Y>");
aoqi@0 48
aoqi@0 49 String typeStr;
aoqi@0 50
aoqi@0 51 TypeKind(String typeStr) {
aoqi@0 52 this.typeStr = typeStr;
aoqi@0 53 }
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 enum ArrayKind {
aoqi@0 57 NONE(""),
aoqi@0 58 SINGLE("[]"),
aoqi@0 59 DOUBLE("[][]");
aoqi@0 60
aoqi@0 61 String arrStr;
aoqi@0 62
aoqi@0 63 ArrayKind(String arrStr) {
aoqi@0 64 this.arrStr = arrStr;
aoqi@0 65 }
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 static class Type {
aoqi@0 69 TypeKind tk;
aoqi@0 70 ArrayKind ak;
aoqi@0 71
aoqi@0 72 Type(TypeKind tk, ArrayKind ak) {
aoqi@0 73 this.tk = tk;
aoqi@0 74 this.ak = ak;
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 String asString() {
aoqi@0 78 return tk.typeStr + ak.arrStr;
aoqi@0 79 }
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 enum CastKind {
aoqi@0 83 ONE("(#T0)", 1),
aoqi@0 84 TWO("(#T0 & T1)", 2),
aoqi@0 85 THREE("(#T0 & #T1 & #T2)", 3);
aoqi@0 86
aoqi@0 87 String castTemplate;
aoqi@0 88 int nBounds;
aoqi@0 89
aoqi@0 90 CastKind(String castTemplate, int nBounds) {
aoqi@0 91 this.castTemplate = castTemplate;
aoqi@0 92 this.nBounds = nBounds;
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 String asString(Type... types) {
aoqi@0 96 String res = castTemplate;
aoqi@0 97 for (int i = 0; i < nBounds ; i++) {
aoqi@0 98 res = res.replaceAll(String.format("#T%d", i), types[i].asString());
aoqi@0 99 }
aoqi@0 100 return res;
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 public static void main(String... args) throws Exception {
aoqi@0 105 //create default shared JavaCompiler - reused across multiple compilations
aoqi@0 106 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
aoqi@0 107 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
aoqi@0 108
aoqi@0 109 for (CastKind ck : CastKind.values()) {
aoqi@0 110 for (TypeKind t1 : TypeKind.values()) {
aoqi@0 111 for (ArrayKind ak1 : ArrayKind.values()) {
aoqi@0 112 Type typ1 = new Type(t1, ak1);
aoqi@0 113 if (ck.nBounds == 1) {
aoqi@0 114 new IntersectionTypeParserTest(ck, typ1).run(comp, fm);
aoqi@0 115 continue;
aoqi@0 116 }
aoqi@0 117 for (TypeKind t2 : TypeKind.values()) {
aoqi@0 118 for (ArrayKind ak2 : ArrayKind.values()) {
aoqi@0 119 Type typ2 = new Type(t2, ak2);
aoqi@0 120 if (ck.nBounds == 2) {
aoqi@0 121 new IntersectionTypeParserTest(ck, typ1, typ2).run(comp, fm);
aoqi@0 122 continue;
aoqi@0 123 }
aoqi@0 124 for (TypeKind t3 : TypeKind.values()) {
aoqi@0 125 for (ArrayKind ak3 : ArrayKind.values()) {
aoqi@0 126 Type typ3 = new Type(t3, ak3);
aoqi@0 127 new IntersectionTypeParserTest(ck, typ1, typ2, typ3).run(comp, fm);
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134 }
aoqi@0 135 System.out.println("Total check executed: " + checkCount);
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 CastKind ck;
aoqi@0 139 Type[] types;
aoqi@0 140 JavaSource source;
aoqi@0 141 DiagnosticChecker diagChecker;
aoqi@0 142
aoqi@0 143 IntersectionTypeParserTest(CastKind ck, Type... types) {
aoqi@0 144 this.ck = ck;
aoqi@0 145 this.types = types;
aoqi@0 146 this.source = new JavaSource();
aoqi@0 147 this.diagChecker = new DiagnosticChecker();
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 class JavaSource extends SimpleJavaFileObject {
aoqi@0 151
aoqi@0 152 String bodyTemplate = "class Test {\n" +
aoqi@0 153 " void test() {\n" +
aoqi@0 154 " Object o = #Cnull;\n" +
aoqi@0 155 " } }";
aoqi@0 156
aoqi@0 157 String source = "";
aoqi@0 158
aoqi@0 159 public JavaSource() {
aoqi@0 160 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 161 source += bodyTemplate.replaceAll("#C", ck.asString(types));
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 @Override
aoqi@0 165 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 166 return source;
aoqi@0 167 }
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
aoqi@0 171 checkCount++;
aoqi@0 172 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
aoqi@0 173 null, null, Arrays.asList(source));
aoqi@0 174 ct.parse();
aoqi@0 175 if (diagChecker.errorFound) {
aoqi@0 176 throw new Error("Unexpected parser error for source:\n" +
aoqi@0 177 source.getCharContent(true));
aoqi@0 178 }
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 182
aoqi@0 183 boolean errorFound;
aoqi@0 184
aoqi@0 185 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 186 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
aoqi@0 187 errorFound = true;
aoqi@0 188 }
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191 }

mercurial