1 /*
2 * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
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
26 * @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096 8275308
27 * @summary Test latest, latestSupported, underscore as keyword, etc.
28 * @modules java.compiler
29 * jdk.compiler
30 */
31
32 import java.util.*;
33 import java.util.function.Predicate;
34 import javax.lang.model.SourceVersion;
35 import static javax.lang.model.SourceVersion.*;
36
37 /**
38 * Verify behavior of latest[Supported] and other methods.
39 */
40 public class TestSourceVersion {
41 public static void main(String... args) {
42 testLatestSupported();
43 testVersionVaryingKeywords();
44 testRestrictedKeywords();
45 testVar();
46 testYield();
47 testValueOfRV();
48 testRuntimeVersion();
49 }
50
51 private static void testLatestSupported() {
52 SourceVersion[] values = SourceVersion.values();
53 SourceVersion last = values[values.length - 1];
54 SourceVersion latest = SourceVersion.latest();
55 SourceVersion latestSupported = SourceVersion.latestSupported();
56
57 if (latest == last &&
58 latestSupported == SourceVersion.valueOf("RELEASE_" +
59 Runtime.version().feature()) &&
60 (latest == latestSupported ||
61 (latest.ordinal() - latestSupported.ordinal() == 1)) )
62 return;
63 else {
64 throw new RuntimeException("Unexpected release value(s) found:\n" +
65 "latest:\t" + latest + "\n" +
66 "latestSupported:\t" + latestSupported);
67 }
68 }
69
70 private static void testVersionVaryingKeywords() {
71 Map<String, SourceVersion> keyWordStart =
72 Map.of("strictfp", RELEASE_2,
73 "assert", RELEASE_4,
74 "enum", RELEASE_5,
75 "_", RELEASE_9);
76
77 for (Map.Entry<String, SourceVersion> entry : keyWordStart.entrySet()) {
78 String key = entry.getKey();
79 SourceVersion value = entry.getValue();
80
81 check(true, key, (String s) -> isKeyword(s), "keyword", latest());
82 check(false, key, (String s) -> isName(s), "name", latest());
83
84 for(SourceVersion version : SourceVersion.values()) {
85 boolean isKeyword = version.compareTo(value) >= 0;
86
87 check(isKeyword, key, (String s) -> isKeyword(s, version), "keyword", version);
88 check(!isKeyword, key, (String s) -> isName(s, version), "name", version);
89 }
90 }
91 }
92
93 private static void testRestrictedKeywords() {
94 // Restricted keywords are not full keywords
95
96 /*
97 * JLS 3.9
98 * " A further ten character sequences are restricted
99 * keywords: open, module, requires, transitive, exports,
100 * opens, to, uses, provides, and with"
101 */
102 Set<String> restrictedKeywords =
103 Set.of("open", "module", "requires", "transitive", "exports",
104 "opens", "to", "uses", "provides", "with",
105 // Assume "record" and "sealed" will be restricted keywords.
106 "record", "sealed");
107
108 for (String key : restrictedKeywords) {
109 for (SourceVersion version : SourceVersion.values()) {
110 check(false, key, (String s) -> isKeyword(s, version), "keyword", version);
111 check(true, key, (String s) -> isName(s, version), "name", version);
112 }
113 }
114 }
115
116 private static void testVar() {
117 for (SourceVersion version : SourceVersion.values()) {
118 Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version);
119 Predicate<String> isNameVersion = (String s) -> isName(s, version);
120
121 for (String name : List.of("var", "foo.var", "var.foo")) {
122 check(false, name, isKeywordVersion, "keyword", version);
123 check(true, name, isNameVersion, "name", version);
124 }
125 }
126 }
127
128 private static void testYield() {
129 for (SourceVersion version : SourceVersion.values()) {
130 Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version);
131 Predicate<String> isNameVersion = (String s) -> isName(s, version);
132
133 for (String name : List.of("yield", "foo.yield", "yield.foo")) {
134 check(false, name, isKeywordVersion, "keyword", version);
135 check(true, name, isNameVersion, "name", version);
136 }
137 }
138 }
139
140 private static void check(boolean expected,
141 String input,
142 Predicate<String> predicate,
143 String message,
144 SourceVersion version) {
145 boolean result = predicate.test(input);
146 if (result != expected) {
147 throw new RuntimeException("Unexpected " + message + "-ness of " + input +
148 " on " + version);
149 }
150 }
151
152 /**
153 * Test that SourceVersion.valueOf() maps a Runtime.Version to a
154 * SourceVersion properly. The SourceVersion result is only a
155 * function of the feature() component of a Runtime.Version.
156 */
157 private static void testValueOfRV() {
158 for (SourceVersion sv : SourceVersion.values()) {
159 if (sv == RELEASE_0) {
160 continue;
161 } else {
162 // Plain mapping; e.g. "17" -> RELEASE_17
163 String featureBase = Integer.toString(sv.ordinal());
164 checkValueOfResult(sv, featureBase);
165
166 // More populated runtime version, N.N
167 checkValueOfResult(sv, featureBase + "." + featureBase);
168 }
169 }
170
171 // Out of range test
172 try {
173 int latestFeature = SourceVersion.latest().runtimeVersion().feature();
174 SourceVersion.valueOf(Runtime.Version.parse(Integer.toString(latestFeature +1)));
175 throw new RuntimeException("Should not reach");
176 } catch (IllegalArgumentException iae) {
177 ; // Expected
178 }
179 }
180
181 private static void checkValueOfResult(SourceVersion expected, String versionString) {
182 Runtime.Version rv = Runtime.Version.parse(versionString);
183 SourceVersion result = SourceVersion.valueOf(rv);
184 if (result != expected) {
185 throw new RuntimeException("Unexpected result " + result +
186 " of mapping Runtime.Version " + versionString +
187 " intead of " + expected);
188 }
189 }
190
191 private static void testRuntimeVersion() {
192 for (SourceVersion sv : SourceVersion.values()) {
193 Runtime.Version result = sv.runtimeVersion();
194 if (sv.compareTo(RELEASE_6) < 0) {
195 if (result != null) {
196 throw new RuntimeException("Unexpected result non-null " + result +
197 " as runtime version of " + sv);
198 }
199 } else {
200 Runtime.Version expected = Runtime.Version.parse(Integer.toString(sv.ordinal()));
201 if (!result.equals(expected)) {
202 throw new RuntimeException("Unexpected result " + result +
203 " as runtime version of " + sv);
204 }
205 }
206 }
207 }
208 }