author | darcy |
Wed, 13 Nov 2019 15:16:45 -0800 | |
changeset 59068 | dc45ed0ab083 |
parent 48694 | b0a54e2ba484 |
permissions | -rw-r--r-- |
10181 | 1 |
/* |
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
10181 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
26 |
* @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096 |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
27 |
* @summary Test latest, latestSupported, underscore as keyword, etc. |
10181 | 28 |
* @author Joseph D. Darcy |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
22169
diff
changeset
|
29 |
* @modules java.compiler |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
22169
diff
changeset
|
30 |
* jdk.compiler |
10181 | 31 |
*/ |
32 |
||
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
33 |
import java.util.*; |
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
34 |
import java.util.function.Predicate; |
10181 | 35 |
import javax.lang.model.SourceVersion; |
36 |
import static javax.lang.model.SourceVersion.*; |
|
37 |
||
38 |
/** |
|
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
39 |
* Verify behavior of latest[Supported] and other methods. |
10181 | 40 |
*/ |
41 |
public class TestSourceVersion { |
|
42 |
public static void main(String... args) { |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
43 |
testLatestSupported(); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
44 |
testVersionVaryingKeywords(); |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
45 |
testRestrictedKeywords(); |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
47283
diff
changeset
|
46 |
testVar(); |
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
47 |
testYield(); |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
48 |
} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
49 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
50 |
private static void testLatestSupported() { |
48694
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
51 |
SourceVersion[] values = SourceVersion.values(); |
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
52 |
SourceVersion last = values[values.length - 1]; |
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
53 |
SourceVersion latest = SourceVersion.latest(); |
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
54 |
SourceVersion latestSupported = SourceVersion.latestSupported(); |
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
55 |
|
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
56 |
if (latest == last && |
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
57 |
latestSupported == SourceVersion.valueOf("RELEASE_" + |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
58 |
Runtime.version().feature()) && |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
59 |
(latest == latestSupported || |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
60 |
(latest.ordinal() - latestSupported.ordinal() == 1)) ) |
48694
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
61 |
return; |
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
62 |
else { |
10181 | 63 |
throw new RuntimeException("Unexpected release value(s) found:\n" + |
48694
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
64 |
"latest:\t" + latest + "\n" + |
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
65 |
"latestSupported:\t" + latestSupported); |
b0a54e2ba484
8196551: Update TestSourceVersion.java to be robust across version changes
darcy
parents:
48538
diff
changeset
|
66 |
} |
10181 | 67 |
} |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
68 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
69 |
private static void testVersionVaryingKeywords() { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
70 |
Map<String, SourceVersion> keyWordStart = |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
71 |
Map.of("strictfp", RELEASE_2, |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
72 |
"assert", RELEASE_4, |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
73 |
"enum", RELEASE_5, |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
74 |
"_", RELEASE_9); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
75 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
76 |
for (Map.Entry<String, SourceVersion> entry : keyWordStart.entrySet()) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
77 |
String key = entry.getKey(); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
78 |
SourceVersion value = entry.getValue(); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
79 |
|
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
80 |
check(true, key, (String s) -> isKeyword(s), "keyword", latest()); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
81 |
check(false, key, (String s) -> isName(s), "name", latest()); |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
82 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
83 |
for(SourceVersion version : SourceVersion.values()) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
84 |
boolean isKeyword = version.compareTo(value) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
85 |
|
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
86 |
check(isKeyword, key, (String s) -> isKeyword(s, version), "keyword", version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
87 |
check(!isKeyword, key, (String s) -> isName(s, version), "name", version); |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
88 |
} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
89 |
} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
90 |
} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
91 |
|
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
92 |
private static void testRestrictedKeywords() { |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
93 |
// Restricted keywords are not full keywords |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
94 |
|
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
95 |
/* |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
96 |
* JLS 3.9 |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
97 |
* " A further ten character sequences are restricted |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
98 |
* keywords: open, module, requires, transitive, exports, |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
99 |
* opens, to, uses, provides, and with" |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
100 |
*/ |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
101 |
Set<String> restrictedKeywords = |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
102 |
Set.of("open", "module", "requires", "transitive", "exports", |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
103 |
"opens", "to", "uses", "provides", "with"); |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
104 |
|
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
105 |
for (String key : restrictedKeywords) { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
106 |
for (SourceVersion version : SourceVersion.values()) { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
107 |
check(false, key, (String s) -> isKeyword(s, version), "keyword", version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
108 |
check(true, key, (String s) -> isName(s, version), "name", version); |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
109 |
} |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
110 |
} |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
111 |
} |
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
112 |
|
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
47283
diff
changeset
|
113 |
private static void testVar() { |
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
114 |
for (SourceVersion version : SourceVersion.values()) { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
115 |
Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
116 |
Predicate<String> isNameVersion = (String s) -> isName(s, version); |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
47283
diff
changeset
|
117 |
|
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
118 |
for (String name : List.of("var", "foo.var", "var.foo")) { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
119 |
check(false, name, isKeywordVersion, "keyword", version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
120 |
check(true, name, isNameVersion, "name", version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
121 |
} |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
47283
diff
changeset
|
122 |
} |
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
47283
diff
changeset
|
123 |
} |
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
47283
diff
changeset
|
124 |
|
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
125 |
private static void testYield() { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
126 |
for (SourceVersion version : SourceVersion.values()) { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
127 |
Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
128 |
Predicate<String> isNameVersion = (String s) -> isName(s, version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
129 |
|
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
130 |
for (String name : List.of("yield", "foo.yield", "yield.foo")) { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
131 |
check(false, name, isKeywordVersion, "keyword", version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
132 |
check(true, name, isNameVersion, "name", version); |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
133 |
} |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
134 |
} |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
135 |
} |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
136 |
|
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
137 |
private static void check(boolean expected, |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
138 |
String input, |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
139 |
Predicate<String> predicate, |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
140 |
String message, |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
141 |
SourceVersion version) { |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
142 |
boolean result = predicate.test(input); |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
143 |
if (result != expected) { |
59068
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
144 |
throw new RuntimeException("Unexpected " + message + "-ness of " + input + |
dc45ed0ab083
8233096: Update javax.lang.model for switch expressions
darcy
parents:
48694
diff
changeset
|
145 |
" on " + version); |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
146 |
} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
30730
diff
changeset
|
147 |
} |
10181 | 148 |
} |