author | darcy |
Mon, 08 Jan 2018 17:32:54 -0800 | |
changeset 48499 | 239c7d9bb192 |
parent 48329 | 984c4b99afc9 |
child 48538 | 82c3d4173a53 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, 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) |
10 | 60 |
*/ |
61 |
||
62 |
/** |
|
63 |
* The original version. |
|
64 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
65 |
* 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
|
66 |
* <cite>The Java™ Language Specification, First Edition</cite>. |
10 | 67 |
*/ |
68 |
RELEASE_0, |
|
69 |
||
70 |
/** |
|
71 |
* The version recognized by the Java Platform 1.1. |
|
72 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
73 |
* 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
|
74 |
* <cite>The Java™ Language Specification, First Edition</cite>. |
10 | 75 |
*/ |
76 |
RELEASE_1, |
|
77 |
||
78 |
/** |
|
79 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
80 |
* v 1.2. |
|
81 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
82 |
* 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
|
83 |
* <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
|
84 |
* Second Edition</cite>, which includes the {@code |
10 | 85 |
* strictfp} modifier. |
86 |
*/ |
|
87 |
RELEASE_2, |
|
88 |
||
89 |
/** |
|
90 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
91 |
* v 1.3. |
|
92 |
* |
|
93 |
* No major changes from {@code RELEASE_2}. |
|
94 |
*/ |
|
95 |
RELEASE_3, |
|
96 |
||
97 |
/** |
|
98 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
99 |
* v 1.4. |
|
100 |
* |
|
101 |
* Added a simple assertion facility. |
|
102 |
*/ |
|
103 |
RELEASE_4, |
|
104 |
||
105 |
/** |
|
106 |
* The version recognized by the Java 2 Platform, Standard |
|
107 |
* Edition 5.0. |
|
108 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
109 |
* 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
|
110 |
* <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
|
111 |
* Third Edition</cite>. First release to support |
10 | 112 |
* generics, annotations, autoboxing, var-args, enhanced {@code |
113 |
* for} loop, and hexadecimal floating-point literals. |
|
114 |
*/ |
|
115 |
RELEASE_5, |
|
116 |
||
117 |
/** |
|
118 |
* The version recognized by the Java Platform, Standard Edition |
|
119 |
* 6. |
|
120 |
* |
|
121 |
* No major changes from {@code RELEASE_5}. |
|
122 |
*/ |
|
123 |
RELEASE_6, |
|
124 |
||
125 |
/** |
|
126 |
* The version recognized by the Java Platform, Standard Edition |
|
127 |
* 7. |
|
128 |
* |
|
18660
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
129 |
* 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
|
130 |
* 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
|
131 |
* binary literals, and multi-catch. |
10 | 132 |
* @since 1.7 |
133 |
*/ |
|
10180 | 134 |
RELEASE_7, |
135 |
||
136 |
/** |
|
137 |
* The version recognized by the Java Platform, Standard Edition |
|
138 |
* 8. |
|
139 |
* |
|
18660
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
140 |
* Additions in this release include lambda expressions and default methods. |
10180 | 141 |
* @since 1.8 |
142 |
*/ |
|
22169 | 143 |
RELEASE_8, |
144 |
||
145 |
/** |
|
146 |
* The version recognized by the Java Platform, Standard Edition |
|
147 |
* 9. |
|
148 |
* |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
149 |
* Additions in this release include modules and removal of a |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
150 |
* single underscore from the set of legal identifier names. |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
151 |
* |
34912
954fa9df99c7
8146572: Update "@since 1.9" to "@since 9" to match java.version.specification [langtools]
jjg
parents:
34072
diff
changeset
|
152 |
* @since 9 |
22169 | 153 |
*/ |
43646 | 154 |
RELEASE_9, |
155 |
||
156 |
/** |
|
157 |
* The version recognized by the Java Platform, Standard Edition |
|
158 |
* 10. |
|
159 |
* |
|
48329
984c4b99afc9
8193489: Add information about local variable type inference to SourceVersion.RELEASE_10
darcy
parents:
47475
diff
changeset
|
160 |
* 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
|
161 |
* ({@code var}). |
984c4b99afc9
8193489: Add information about local variable type inference to SourceVersion.RELEASE_10
darcy
parents:
47475
diff
changeset
|
162 |
* |
43646 | 163 |
* @since 10 |
164 |
*/ |
|
165 |
RELEASE_10; |
|
10 | 166 |
|
167 |
// Note that when adding constants for newer releases, the |
|
168 |
// behavior of latest() and latestSupported() must be updated too. |
|
169 |
||
170 |
/** |
|
171 |
* Returns the latest source version that can be modeled. |
|
172 |
* |
|
173 |
* @return the latest source version that can be modeled |
|
174 |
*/ |
|
175 |
public static SourceVersion latest() { |
|
43646 | 176 |
return RELEASE_10; |
10 | 177 |
} |
178 |
||
179 |
private static final SourceVersion latestSupported = getLatestSupported(); |
|
180 |
||
181 |
private static SourceVersion getLatestSupported() { |
|
182 |
try { |
|
183 |
String specVersion = System.getProperty("java.specification.version"); |
|
10180 | 184 |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
185 |
switch (specVersion) { |
43646 | 186 |
case "10": |
187 |
return RELEASE_10; |
|
34070
792098bae87e
8085822: JEP 223: New Version-String Scheme (initial integration)
ihse
parents:
25874
diff
changeset
|
188 |
case "9": |
22169 | 189 |
return RELEASE_9; |
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
190 |
case "1.8": |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
191 |
return RELEASE_8; |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
192 |
case "1.7": |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
193 |
return RELEASE_7; |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
194 |
case "1.6": |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
195 |
return RELEASE_6; |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
18660
diff
changeset
|
196 |
} |
10 | 197 |
} catch (SecurityException se) {} |
198 |
||
199 |
return RELEASE_5; |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Returns the latest source version fully supported by the |
|
204 |
* current execution environment. {@code RELEASE_5} or later must |
|
205 |
* be returned. |
|
206 |
* |
|
207 |
* @return the latest source version that is fully supported |
|
208 |
*/ |
|
209 |
public static SourceVersion latestSupported() { |
|
210 |
return latestSupported; |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Returns whether or not {@code name} is a syntactically valid |
|
215 |
* identifier (simple name) or keyword in the latest source |
|
216 |
* version. The method returns {@code true} if the name consists |
|
217 |
* of an initial character for which {@link |
|
218 |
* Character#isJavaIdentifierStart(int)} returns {@code true}, |
|
219 |
* followed only by characters for which {@link |
|
220 |
* Character#isJavaIdentifierPart(int)} returns {@code true}. |
|
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
221 |
* This pattern matches regular identifiers, keywords, restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
222 |
* keywords, and the literals {@code "true"}, {@code "false"}, |
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
223 |
* {@code "null"}, and {@code "var"}. |
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
224 |
* |
10 | 225 |
* The method returns {@code false} for all other strings. |
226 |
* |
|
227 |
* @param name the string to check |
|
228 |
* @return {@code true} if this string is a |
|
229 |
* syntactically valid identifier or keyword, {@code false} |
|
230 |
* otherwise. |
|
231 |
*/ |
|
232 |
public static boolean isIdentifier(CharSequence name) { |
|
233 |
String id = name.toString(); |
|
234 |
||
235 |
if (id.length() == 0) { |
|
236 |
return false; |
|
237 |
} |
|
238 |
int cp = id.codePointAt(0); |
|
239 |
if (!Character.isJavaIdentifierStart(cp)) { |
|
240 |
return false; |
|
241 |
} |
|
242 |
for (int i = Character.charCount(cp); |
|
243 |
i < id.length(); |
|
244 |
i += Character.charCount(cp)) { |
|
245 |
cp = id.codePointAt(i); |
|
246 |
if (!Character.isJavaIdentifierPart(cp)) { |
|
247 |
return false; |
|
248 |
} |
|
249 |
} |
|
250 |
return true; |
|
251 |
} |
|
252 |
||
253 |
/** |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
254 |
* Returns whether or not {@code name} is a syntactically valid |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
255 |
* qualified name in the latest source version. Unlike {@link |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
256 |
* #isIdentifier isIdentifier}, this method returns {@code false} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
257 |
* for keywords, boolean literals, and the null literal. |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
258 |
* |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
259 |
* This method returns {@code true} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
260 |
* keywords</i> and {@code "var"}. |
10 | 261 |
* |
262 |
* @param name the string to check |
|
263 |
* @return {@code true} if this string is a |
|
264 |
* syntactically valid name, {@code false} otherwise. |
|
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
265 |
* @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
|
266 |
* @jls 6.2 Names and Identifiers |
10 | 267 |
*/ |
268 |
public static boolean isName(CharSequence name) { |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
269 |
return isName(name, latest()); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
270 |
} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
271 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
272 |
/** |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
273 |
* Returns whether or not {@code name} is a syntactically valid |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
274 |
* qualified name in the given source version. Unlike {@link |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
275 |
* #isIdentifier isIdentifier}, this method returns {@code false} |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
276 |
* for keywords, boolean literals, and the null literal. |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
277 |
* |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
278 |
* This method returns {@code true} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
279 |
* keywords</i> and {@code "var"}. |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
280 |
* |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
281 |
* @param name the string to check |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
282 |
* @param version the version to use |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
283 |
* @return {@code true} if this string is a |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
284 |
* syntactically valid name, {@code false} otherwise. |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
285 |
* @jls 3.9 Keywords |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
286 |
* @jls 6.2 Names and Identifiers |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
287 |
* @since 9 |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
288 |
*/ |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
289 |
public static boolean isName(CharSequence name, SourceVersion version) { |
10 | 290 |
String id = name.toString(); |
291 |
||
292 |
for(String s : id.split("\\.", -1)) { |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
293 |
if (!isIdentifier(s) || isKeyword(s, version)) |
10 | 294 |
return false; |
295 |
} |
|
296 |
return true; |
|
297 |
} |
|
298 |
||
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
299 |
/** |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
300 |
* 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
|
301 |
* or null literal in the latest source version. |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
302 |
* This method returns {@code false} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
303 |
* keywords</i> and {@code "var"}. |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
304 |
* |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
305 |
* @param s the string to check |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
306 |
* @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
|
307 |
* literal, or null literal, {@code false} otherwise. |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
308 |
* @jls 3.9 Keywords |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
309 |
* @jls 3.10.3 Boolean Literals |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
310 |
* @jls 3.10.7 The Null Literal |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
311 |
*/ |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
312 |
public static boolean isKeyword(CharSequence s) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
313 |
return isKeyword(s, latest()); |
10 | 314 |
} |
315 |
||
316 |
/** |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
317 |
* 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
|
318 |
* or null literal in the given source version. |
47283
ce5fd3ba3fea
8187982: Update SourceVersion to mention restricted keywords
darcy
parents:
47216
diff
changeset
|
319 |
* This method returns {@code false} for <i>restricted |
48499
239c7d9bb192
8187951: Update javax.lang.model.SourceVersion for "var" name
darcy
parents:
48329
diff
changeset
|
320 |
* keywords</i> and {@code "var"}. |
10 | 321 |
* |
322 |
* @param s the string to check |
|
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
323 |
* @param version the version to use |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
324 |
* @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
|
325 |
* literal, or null literal, {@code false} otherwise. |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
326 |
* @jls 3.9 Keywords |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
327 |
* @jls 3.10.3 Boolean Literals |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
328 |
* @jls 3.10.7 The Null Literal |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
329 |
* @since 9 |
10 | 330 |
*/ |
38534
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
331 |
public static boolean isKeyword(CharSequence s, SourceVersion version) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
332 |
String id = s.toString(); |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
333 |
switch(id) { |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
334 |
// A trip through history |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
335 |
case "strictfp": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
336 |
return version.compareTo(RELEASE_2) >= 0; |
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 |
case "assert": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
339 |
return version.compareTo(RELEASE_4) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
340 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
341 |
case "enum": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
342 |
return version.compareTo(RELEASE_5) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
343 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
344 |
case "_": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
345 |
return version.compareTo(RELEASE_9) >= 0; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
346 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
347 |
// Keywords common across versions |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
348 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
349 |
// Modifiers |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
350 |
case "public": case "protected": case "private": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
351 |
case "abstract": case "static": case "final": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
352 |
case "transient": case "volatile": case "synchronized": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
353 |
case "native": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
354 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
355 |
// Declarations |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
356 |
case "class": case "interface": case "extends": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
357 |
case "package": case "throws": case "implements": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
358 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
359 |
// Primitive types and void |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
360 |
case "boolean": case "byte": case "char": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
361 |
case "short": case "int": case "long": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
362 |
case "float": case "double": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
363 |
case "void": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
364 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
365 |
// Control flow |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
366 |
case "if": case "else": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
367 |
case "try": case "catch": case "finally": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
368 |
case "do": case "while": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
369 |
case "for": case "continue": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
370 |
case "switch": case "case": case "default": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
371 |
case "break": case "throw": case "return": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
372 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
373 |
// Other keywords |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
374 |
case "this": case "new": case "super": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
375 |
case "import": case "instanceof": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
376 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
377 |
// Forbidden! |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
378 |
case "goto": case "const": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
379 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
380 |
// literals |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
381 |
case "null": case "true": case "false": |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
382 |
return true; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
383 |
|
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
384 |
default: |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
385 |
return false; |
425b30506f80
6415644: Make javax.lang.model.SourceVersion more informative
darcy
parents:
35351
diff
changeset
|
386 |
} |
10 | 387 |
} |
388 |
} |