author | darcy |
Mon, 25 Mar 2019 19:40:01 -0700 | |
changeset 54271 | 44edf64cb206 |
parent 53023 | 6879069d9d94 |
child 55306 | ea43db53de91 |
child 58678 | 9cf78a70fa4f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
54271
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package javax.lang.model; |
|
27 |
||
28 |
import java.util.Collections; |
|
29 |
import java.util.Set; |
|
30 |
import java.util.HashSet; |
|
31 |
||
32 |
/** |
|
33 |
* Source versions of the Java™ programming language. |
|
34 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
35 |
* See the appropriate edition of |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
36 |
* <cite>The Java™ Language Specification</cite> |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
37 |
* for information about a particular source version. |
10 | 38 |
* |
39 |
* <p>Note that additional source version constants will be added to |
|
40 |
* model future releases of the language. |
|
41 |
* |
|
42 |
* @author Joseph D. Darcy |
|
43 |
* @author Scott Seligman |
|
44 |
* @author Peter von der Ahé |
|
45 |
* @since 1.6 |
|
46 |
*/ |
|
47 |
public enum SourceVersion { |
|
48 |
/* |
|
15385 | 49 |
* Summary of language evolution |
10 | 50 |
* 1.1: nested classes |
51 |
* 1.2: strictfp |
|
52 |
* 1.3: no changes |
|
53 |
* 1.4: assert |
|
54 |
* 1.5: annotations, generics, autoboxing, var-args... |
|
55 |
* 1.6: no changes |
|
18660
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
56 |
* 1.7: diamond syntax, try-with-resources, etc. |
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
57 |
* 1.8: lambda expressions and default methods |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
58 |
* 9: modules, small cleanups to 1.7 and 1.8 changes |
48329
984c4b99afc9
8193489: Add information about local variable type inference to SourceVersion.RELEASE_10
darcy
parents:
47475
diff
changeset
|
59 |
* 10: local-variable type inference (var) |
51198
979e4708da65
8208201: Update SourceVersion.RELEASE_11 docs to mention var for lambda param
darcy
parents:
50892
diff
changeset
|
60 |
* 11: local-variable syntax for lambda parameters |
54271
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
61 |
* 12: no changes (switch expressions in preview) |
53023 | 62 |
* 13: TBD |
10 | 63 |
*/ |
64 |
||
65 |
/** |
|
66 |
* The original version. |
|
67 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
68 |
* The language described in |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
69 |
* <cite>The Java™ Language Specification, First Edition</cite>. |
10 | 70 |
*/ |
71 |
RELEASE_0, |
|
72 |
||
73 |
/** |
|
74 |
* The version recognized by the Java Platform 1.1. |
|
75 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
76 |
* The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
77 |
* <cite>The Java™ Language Specification, First Edition</cite>. |
10 | 78 |
*/ |
79 |
RELEASE_1, |
|
80 |
||
81 |
/** |
|
82 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
83 |
* v 1.2. |
|
84 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
85 |
* The language described in |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
86 |
* <cite>The Java™ Language Specification, |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
87 |
* Second Edition</cite>, which includes the {@code |
10 | 88 |
* strictfp} modifier. |
89 |
*/ |
|
90 |
RELEASE_2, |
|
91 |
||
92 |
/** |
|
93 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
94 |
* v 1.3. |
|
95 |
* |
|
96 |
* No major changes from {@code RELEASE_2}. |
|
97 |
*/ |
|
98 |
RELEASE_3, |
|
99 |
||
100 |
/** |
|
101 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
102 |
* v 1.4. |
|
103 |
* |
|
104 |
* Added a simple assertion facility. |
|
105 |
*/ |
|
106 |
RELEASE_4, |
|
107 |
||
108 |
/** |
|
109 |
* The version recognized by the Java 2 Platform, Standard |
|
110 |
* Edition 5.0. |
|
111 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
112 |
* The language described in |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
113 |
* <cite>The Java™ Language Specification, |
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
114 |
* Third Edition</cite>. First release to support |
10 | 115 |
* generics, annotations, autoboxing, var-args, enhanced {@code |
116 |
* for} loop, and hexadecimal floating-point literals. |
|
117 |
*/ |
|
118 |
RELEASE_5, |
|
119 |
||
120 |
/** |
|
121 |
* The version recognized by the Java Platform, Standard Edition |
|
122 |
* 6. |
|
123 |
* |
|
124 |
* No major changes from {@code RELEASE_5}. |
|
125 |
*/ |
|
126 |
RELEASE_6, |
|
127 |
||
128 |
/** |
|
129 |
* The version recognized by the Java Platform, Standard Edition |
|
130 |
* 7. |
|
131 |
* |
|
18660
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
132 |
* Additions in this release include, diamond syntax for |
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
133 |
* constructors, {@code try}-with-resources, strings in switch, |
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
134 |
* binary literals, and multi-catch. |
10 | 135 |
* @since 1.7 |
136 |
*/ |
|
10180 | 137 |
RELEASE_7, |
138 |
||
139 |
/** |
|
140 |
* The version recognized by the Java Platform, Standard Edition |
|
141 |
* 8. |
|
142 |
* |
|
18660
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
143 |
* Additions in this release include lambda expressions and default methods. |
10180 | 144 |
* @since 1.8 |
145 |
*/ |
|
22169 | 146 |
RELEASE_8, |
147 |
||
148 |
/** |
|
149 |
* The version recognized by the Java Platform, Standard Edition |
|
150 |
* 9. |
|
151 |
* |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
152 |
* Additions in this release include modules and removal of a |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
153 |
* single underscore from the set of legal identifier names. |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
154 |
* |
34912
954fa9df99c7
8146572: Update "@since 1.9" to "@since 9" to match java.version.specification [langtools]
jjg
parents:
34072
diff
changeset
|
155 |
* @since 9 |
22169 | 156 |
*/ |
43646 | 157 |
RELEASE_9, |
158 |
||
159 |
/** |
|
160 |
* The version recognized by the Java Platform, Standard Edition |
|
161 |
* 10. |
|
162 |
* |
|
48329
984c4b99afc9
8193489: Add information about local variable type inference to SourceVersion.RELEASE_10
darcy
parents:
47475
diff
changeset
|
163 |
* Additions in this release include local-variable type inference |
984c4b99afc9
8193489: Add information about local variable type inference to SourceVersion.RELEASE_10
darcy
parents:
47475
diff
changeset
|
164 |
* ({@code var}). |
984c4b99afc9
8193489: Add information about local variable type inference to SourceVersion.RELEASE_10
darcy
parents:
47475
diff
changeset
|
165 |
* |
43646 | 166 |
* @since 10 |
167 |
*/ |
|
48343 | 168 |
RELEASE_10, |
169 |
||
170 |
/** |
|
171 |
* The version recognized by the Java Platform, Standard Edition |
|
172 |
* 11. |
|
173 |
* |
|
51198
979e4708da65
8208201: Update SourceVersion.RELEASE_11 docs to mention var for lambda param
darcy
parents:
50892
diff
changeset
|
174 |
* Additions in this release include local-variable syntax for |
979e4708da65
8208201: Update SourceVersion.RELEASE_11 docs to mention var for lambda param
darcy
parents:
50892
diff
changeset
|
175 |
* lambda parameters. |
979e4708da65
8208201: Update SourceVersion.RELEASE_11 docs to mention var for lambda param
darcy
parents:
50892
diff
changeset
|
176 |
* |
48343 | 177 |
* @since 11 |
178 |
*/ |
|
50892 | 179 |
RELEASE_11, |
180 |
||
181 |
/** |
|
182 |
* The version recognized by the Java Platform, Standard Edition |
|
183 |
* 12. |
|
184 |
* |
|
185 |
* @since 12 |
|
186 |
*/ |
|
53023 | 187 |
RELEASE_12, |
188 |
||
189 |
/** |
|
190 |
* The version recognized by the Java Platform, Standard Edition |
|
191 |
* 13. |
|
192 |
* |
|
193 |
* @since 13 |
|
194 |
*/ |
|
195 |
RELEASE_13; |
|
10 | 196 |
|
197 |
// Note that when adding constants for newer releases, the |
|
198 |
// behavior of latest() and latestSupported() must be updated too. |
|
199 |
||
200 |
/** |
|
201 |
* Returns the latest source version that can be modeled. |
|
202 |
* |
|
203 |
* @return the latest source version that can be modeled |
|
204 |
*/ |
|
205 |
public static SourceVersion latest() { |
|
53023 | 206 |
return RELEASE_13; |
10 | 207 |
} |
208 |
||
209 |
private static final SourceVersion latestSupported = getLatestSupported(); |
|
210 |
||
54271
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
211 |
/* |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
212 |
* The integer version to enum constant mapping implemented by |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
213 |
* this method assumes the JEP 322: "Time-Based Release |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
214 |
* Versioning" scheme is in effect. This scheme began in JDK |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
215 |
* 10. If the JDK versioning scheme is revised, this method may |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
216 |
* need to be updated accordingly. |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
217 |
*/ |
10 | 218 |
private static SourceVersion getLatestSupported() { |
54271
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
219 |
int intVersion = Runtime.version().feature(); |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
220 |
return (intVersion >= 11) ? |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
221 |
valueOf("RELEASE_" + Math.min(13, intVersion)): |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
222 |
RELEASE_10; |
10 | 223 |
} |
224 |
||
225 |
/** |
|
226 |
* Returns the latest source version fully supported by the |
|
54271
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
227 |
* current execution environment. {@code RELEASE_9} or later must |
10 | 228 |
* be returned. |
229 |
* |
|
54271
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
230 |
* @apiNote This method is included alongside {@link latest} to |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
231 |
* allow identification of situations where the language model API |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
232 |
* is running on a platform version different than the latest |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
233 |
* version modeled by the API. One way that sort of situation can |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
234 |
* occur is if an IDE or similar tool is using the API to model |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
235 |
* source version <i>N</i> while running on platform version |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
236 |
* (<i>N</i> - 1). Running in this configuration is |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
237 |
* supported by the API. Running an API on platform versions |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
238 |
* earlier than (<i>N</i> - 1) or later than <i>N</i> |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
239 |
* may or may not work as an implementation detail. If an |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
240 |
* annotation processor was generating code to run under the |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
241 |
* current execution environment, the processor should only use |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
242 |
* platform features up to the {@code latestSupported} release, |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
243 |
* which may be earlier than the {@code latest} release. |
44edf64cb206
8221264: Refactor and update SourceVersion.latestSupported
darcy
parents:
53023
diff
changeset
|
244 |
* |
10 | 245 |
* @return the latest source version that is fully supported |
246 |
*/ |
|
247 |
public static SourceVersion latestSupported() { |
|
248 |
return latestSupported; |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Returns whether or not {@code name} is a syntactically valid |
|
253 |
* identifier (simple name) or keyword in the latest source |
|
254 |
* version. The method returns {@code true} if the name consists |
|
255 |
* of an initial character for which {@link |
|
256 |
* Character#isJavaIdentifierStart(int)} returns {@code true}, |
|
257 |
* followed only by characters for which {@link |
|
258 |
* Character#isJavaIdentifierPart(int)} returns {@code true}. |
|
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
259 |
* This pattern matches regular identifiers, keywords, restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
260 |
* keywords, and the literals {@code "true"}, {@code "false"}, |
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
261 |
* {@code "null"}, and {@code "var"}. |
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
262 |
* |
10 | 263 |
* The method returns {@code false} for all other strings. |
264 |
* |
|
265 |
* @param name the string to check |
|
266 |
* @return {@code true} if this string is a |
|
267 |
* syntactically valid identifier or keyword, {@code false} |
|
268 |
* otherwise. |
|
269 |
*/ |
|
270 |
public static boolean isIdentifier(CharSequence name) { |
|
271 |
String id = name.toString(); |
|
272 |
||
273 |
if (id.length() == 0) { |
|
274 |
return false; |
|
275 |
} |
|
276 |
int cp = id.codePointAt(0); |
|
277 |
if (!Character.isJavaIdentifierStart(cp)) { |
|
278 |
return false; |
|
279 |
} |
|
280 |
for (int i = Character.charCount(cp); |
|
281 |
i < id.length(); |
|
282 |
i += Character.charCount(cp)) { |
|
283 |
cp = id.codePointAt(i); |
|
284 |
if (!Character.isJavaIdentifierPart(cp)) { |
|
285 |
return false; |
|
286 |
} |
|
287 |
} |
|
288 |
return true; |
|
289 |
} |
|
290 |
||
291 |
/** |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
292 |
* Returns whether or not {@code name} is a syntactically valid |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
293 |
* qualified name in the latest source version. Unlike {@link |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
294 |
* #isIdentifier isIdentifier}, this method returns {@code false} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
295 |
* for keywords, boolean literals, and the null literal. |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
296 |
* |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
297 |
* This method returns {@code true} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
298 |
* keywords</i> and {@code "var"}. |
10 | 299 |
* |
300 |
* @param name the string to check |
|
301 |
* @return {@code true} if this string is a |
|
302 |
* syntactically valid name, {@code false} otherwise. |
|
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
303 |
* @jls 3.9 Keywords |
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
304 |
* @jls 6.2 Names and Identifiers |
10 | 305 |
*/ |
306 |
public static boolean isName(CharSequence name) { |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
307 |
return isName(name, latest()); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
308 |
} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
309 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
310 |
/** |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
311 |
* Returns whether or not {@code name} is a syntactically valid |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
312 |
* qualified name in the given source version. Unlike {@link |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
313 |
* #isIdentifier isIdentifier}, this method returns {@code false} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
314 |
* for keywords, boolean literals, and the null literal. |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
315 |
* |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
316 |
* This method returns {@code true} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
317 |
* keywords</i> and {@code "var"}. |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
318 |
* |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
319 |
* @param name the string to check |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
320 |
* @param version the version to use |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
321 |
* @return {@code true} if this string is a |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
322 |
* syntactically valid name, {@code false} otherwise. |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
323 |
* @jls 3.9 Keywords |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
324 |
* @jls 6.2 Names and Identifiers |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
325 |
* @since 9 |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
326 |
*/ |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
327 |
public static boolean isName(CharSequence name, SourceVersion version) { |
10 | 328 |
String id = name.toString(); |
329 |
||
330 |
for(String s : id.split("\\.", -1)) { |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
331 |
if (!isIdentifier(s) || isKeyword(s, version)) |
10 | 332 |
return false; |
333 |
} |
|
334 |
return true; |
|
335 |
} |
|
336 |
||
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
337 |
/** |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
338 |
* Returns whether or not {@code s} is a keyword, boolean literal, |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
339 |
* or null literal in the latest source version. |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
340 |
* This method returns {@code false} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
341 |
* keywords</i> and {@code "var"}. |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
342 |
* |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
343 |
* @param s the string to check |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
344 |
* @return {@code true} if {@code s} is a keyword, or boolean |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
345 |
* literal, or null literal, {@code false} otherwise. |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
346 |
* @jls 3.9 Keywords |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
347 |
* @jls 3.10.3 Boolean Literals |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
348 |
* @jls 3.10.7 The Null Literal |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
349 |
*/ |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
350 |
public static boolean isKeyword(CharSequence s) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
351 |
return isKeyword(s, latest()); |
10 | 352 |
} |
353 |
||
354 |
/** |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
355 |
* Returns whether or not {@code s} is a keyword, boolean literal, |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
356 |
* or null literal in the given source version. |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
357 |
* This method returns {@code false} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
358 |
* keywords</i> and {@code "var"}. |
10 | 359 |
* |
360 |
* @param s the string to check |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
361 |
* @param version the version to use |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
362 |
* @return {@code true} if {@code s} is a keyword, or boolean |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
363 |
* literal, or null literal, {@code false} otherwise. |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
364 |
* @jls 3.9 Keywords |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
365 |
* @jls 3.10.3 Boolean Literals |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
366 |
* @jls 3.10.7 The Null Literal |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
367 |
* @since 9 |
10 | 368 |
*/ |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
369 |
public static boolean isKeyword(CharSequence s, SourceVersion version) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
370 |
String id = s.toString(); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
371 |
switch(id) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
372 |
// A trip through history |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
373 |
case "strictfp": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
374 |
return version.compareTo(RELEASE_2) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
375 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
376 |
case "assert": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
377 |
return version.compareTo(RELEASE_4) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
378 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
379 |
case "enum": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
380 |
return version.compareTo(RELEASE_5) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
381 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
382 |
case "_": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
383 |
return version.compareTo(RELEASE_9) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
384 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
385 |
// Keywords common across versions |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
386 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
387 |
// Modifiers |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
388 |
case "public": case "protected": case "private": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
389 |
case "abstract": case "static": case "final": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
390 |
case "transient": case "volatile": case "synchronized": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
391 |
case "native": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
392 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
393 |
// Declarations |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
394 |
case "class": case "interface": case "extends": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
395 |
case "package": case "throws": case "implements": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
396 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
397 |
// Primitive types and void |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
398 |
case "boolean": case "byte": case "char": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
399 |
case "short": case "int": case "long": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
400 |
case "float": case "double": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
401 |
case "void": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
402 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
403 |
// Control flow |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
404 |
case "if": case "else": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
405 |
case "try": case "catch": case "finally": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
406 |
case "do": case "while": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
407 |
case "for": case "continue": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
408 |
case "switch": case "case": case "default": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
409 |
case "break": case "throw": case "return": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
410 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
411 |
// Other keywords |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
412 |
case "this": case "new": case "super": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
413 |
case "import": case "instanceof": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
414 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
415 |
// Forbidden! |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
416 |
case "goto": case "const": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
417 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
418 |
// literals |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
419 |
case "null": case "true": case "false": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
420 |
return true; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
421 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
422 |
default: |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
423 |
return false; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
424 |
} |
10 | 425 |
} |
426 |
} |