author | jlahoda |
Tue, 12 Nov 2019 06:32:13 +0000 | |
changeset 59021 | cfc7bb9a5a92 |
parent 55306 | ea43db53de91 |
permissions | -rw-r--r-- |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
1 |
/* |
53878
4584d0331318
8219254: Update explicit uses of latest source/target in langtools tests to a property
darcy
parents:
53023
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
4 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
8 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
13 |
* accompanied this code). |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
14 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
18 |
* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
21 |
* questions. |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
22 |
*/ |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
23 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
24 |
/* |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
25 |
* @test |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
26 |
* @bug 8206986 |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
27 |
* @summary Ensure than parser can parse incomplete sources |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
28 |
* @modules jdk.compiler |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
29 |
*/ |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
30 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
31 |
import java.io.StringWriter; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
32 |
import java.net.URI; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
33 |
import java.util.Arrays; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
34 |
import java.util.List; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
35 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
36 |
import javax.tools.*; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
37 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
38 |
import com.sun.source.util.JavacTask; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
39 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
40 |
public class ParseIncomplete { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
41 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
42 |
private static final String CODE = |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
43 |
"public class C {" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
44 |
" void t1(Integer i) {" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
45 |
" switch (i) {" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
46 |
" case null: i++; break;" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
47 |
" case 0, 1: i++; break;" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
48 |
" default: i++; break;" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
49 |
" }" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
50 |
" }" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
51 |
" int t2(Integer i) {" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
52 |
" return switch (i) {" + |
55306 | 53 |
" case null: yield 0;" + |
54 |
" case 0, 1: yield 1;" + |
|
55 |
" default: yield 2;" + |
|
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
56 |
" }" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
57 |
" }" + |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
58 |
"}"; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
59 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
60 |
public static void main(String[] args) throws Exception { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
61 |
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
62 |
assert tool != null; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
63 |
DiagnosticListener<JavaFileObject> noErrors = d -> {}; |
53878
4584d0331318
8219254: Update explicit uses of latest source/target in langtools tests to a property
darcy
parents:
53023
diff
changeset
|
64 |
String sourceVersion = Integer.toString(Runtime.version().feature()); |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
65 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
66 |
for (int i = 0; i < CODE.length(); i++) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
67 |
String code = CODE.substring(0, i + 1); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
68 |
StringWriter out = new StringWriter(); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
69 |
try { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
70 |
JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors, |
59021 | 71 |
List.of("-XDdev"), null, |
51563
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
72 |
Arrays.asList(new MyFileObject(code))); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
73 |
ct.parse().iterator().next(); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
74 |
} catch (Throwable t) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
75 |
System.err.println("Unexpected exception for code: " + code); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
76 |
System.err.println("output: " + out); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
77 |
throw t; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
78 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
79 |
if (!out.toString().isEmpty()) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
80 |
System.err.println("Unexpected compiler for code: " + code); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
81 |
System.err.println(out); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
82 |
throw new AssertionError(); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
83 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
84 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
85 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
86 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
87 |
static class MyFileObject extends SimpleJavaFileObject { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
88 |
private String text; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
89 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
90 |
public MyFileObject(String text) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
91 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
92 |
this.text = text; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
93 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
94 |
|
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
95 |
@Override |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
96 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
97 |
return text; |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
98 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
99 |
} |
de411d537aae
8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff
changeset
|
100 |
} |