Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.compiler/share/classes/javax/lang/model/SourceVersion.java
41159 views
1
/*
2
* Copyright (c) 2005, 2020, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
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
*
35
* See the appropriate edition of
36
* <cite>The Java Language Specification</cite>
37
* for information about a particular source version.
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&eacute;
45
* @since 1.6
46
*/
47
public enum SourceVersion {
48
/*
49
* Summary of language evolution
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
56
* 1.7: diamond syntax, try-with-resources, etc.
57
* 1.8: lambda expressions and default methods
58
* 9: modules, small cleanups to 1.7 and 1.8 changes
59
* 10: local-variable type inference (var)
60
* 11: local-variable syntax for lambda parameters
61
* 12: no changes (switch expressions in preview)
62
* 13: no changes (switch expressions and text blocks in preview)
63
* 14: switch expressions (pattern matching and records in
64
* preview, text blocks in preview again)
65
* 15: text blocks (records and pattern matching in preview again)
66
*/
67
68
/**
69
* The original version.
70
*
71
* The language described in
72
* <cite>The Java Language Specification, First Edition</cite>.
73
*/
74
RELEASE_0,
75
76
/**
77
* The version recognized by the Java Platform 1.1.
78
*
79
* The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to
80
* <cite>The Java Language Specification, First Edition</cite>.
81
*/
82
RELEASE_1,
83
84
/**
85
* The version recognized by the Java 2 Platform, Standard Edition,
86
* v 1.2.
87
*
88
* The language described in
89
* <cite>The Java Language Specification,
90
* Second Edition</cite>, which includes the {@code
91
* strictfp} modifier.
92
*/
93
RELEASE_2,
94
95
/**
96
* The version recognized by the Java 2 Platform, Standard Edition,
97
* v 1.3.
98
*
99
* No major changes from {@code RELEASE_2}.
100
*/
101
RELEASE_3,
102
103
/**
104
* The version recognized by the Java 2 Platform, Standard Edition,
105
* v 1.4.
106
*
107
* Added a simple assertion facility.
108
*/
109
RELEASE_4,
110
111
/**
112
* The version recognized by the Java 2 Platform, Standard
113
* Edition 5.0.
114
*
115
* The language described in
116
* <cite>The Java Language Specification,
117
* Third Edition</cite>. First release to support
118
* generics, annotations, autoboxing, var-args, enhanced {@code
119
* for} loop, and hexadecimal floating-point literals.
120
*/
121
RELEASE_5,
122
123
/**
124
* The version recognized by the Java Platform, Standard Edition
125
* 6.
126
*
127
* No major changes from {@code RELEASE_5}.
128
*/
129
RELEASE_6,
130
131
/**
132
* The version recognized by the Java Platform, Standard Edition
133
* 7.
134
*
135
* Additions in this release include, diamond syntax for
136
* constructors, {@code try}-with-resources, strings in switch,
137
* binary literals, and multi-catch.
138
* @since 1.7
139
*/
140
RELEASE_7,
141
142
/**
143
* The version recognized by the Java Platform, Standard Edition
144
* 8.
145
*
146
* Additions in this release include lambda expressions and default methods.
147
* @since 1.8
148
*/
149
RELEASE_8,
150
151
/**
152
* The version recognized by the Java Platform, Standard Edition
153
* 9.
154
*
155
* Additions in this release include modules and removal of a
156
* single underscore from the set of legal identifier names.
157
*
158
* @since 9
159
*/
160
RELEASE_9,
161
162
/**
163
* The version recognized by the Java Platform, Standard Edition
164
* 10.
165
*
166
* Additions in this release include local-variable type inference
167
* ({@code var}).
168
*
169
* @since 10
170
*/
171
RELEASE_10,
172
173
/**
174
* The version recognized by the Java Platform, Standard Edition
175
* 11.
176
*
177
* Additions in this release include local-variable syntax for
178
* lambda parameters.
179
*
180
* @since 11
181
*/
182
RELEASE_11,
183
184
/**
185
* The version recognized by the Java Platform, Standard Edition
186
* 12.
187
*
188
* @since 12
189
*/
190
RELEASE_12,
191
192
/**
193
* The version recognized by the Java Platform, Standard Edition
194
* 13.
195
*
196
* @since 13
197
*/
198
RELEASE_13,
199
200
/**
201
* The version recognized by the Java Platform, Standard Edition
202
* 14.
203
*
204
* Additions in this release include switch expressions.
205
*
206
* @since 14
207
*/
208
RELEASE_14,
209
210
/**
211
* The version recognized by the Java Platform, Standard Edition
212
* 15.
213
*
214
* Additions in this release include text blocks.
215
*
216
* @since 15
217
*/
218
RELEASE_15,
219
220
/**
221
* The version recognized by the Java Platform, Standard Edition
222
* 16.
223
*
224
* Additions in this release include pattern matching for {@code
225
* instanceof} and records.
226
*
227
* @since 16
228
*/
229
RELEASE_16,
230
231
/**
232
* The version recognized by the Java Platform, Standard Edition
233
* 17.
234
*
235
* Additions in this release include sealed classes and
236
* restoration of always-strict floating-point semantics.
237
*
238
* @since 17
239
*/
240
RELEASE_17;
241
242
// Note that when adding constants for newer releases, the
243
// behavior of latest() and latestSupported() must be updated too.
244
245
/**
246
* {@return the latest source version that can be modeled}
247
*/
248
public static SourceVersion latest() {
249
return RELEASE_17;
250
}
251
252
private static final SourceVersion latestSupported = getLatestSupported();
253
254
/*
255
* The integer version to enum constant mapping implemented by
256
* this method assumes the JEP 322: "Time-Based Release
257
* Versioning" scheme is in effect. This scheme began in JDK
258
* 10. If the JDK versioning scheme is revised, this method may
259
* need to be updated accordingly.
260
*/
261
private static SourceVersion getLatestSupported() {
262
int intVersion = Runtime.version().feature();
263
return (intVersion >= 11) ?
264
valueOf("RELEASE_" + Math.min(17, intVersion)):
265
RELEASE_10;
266
}
267
268
/**
269
* {@return the latest source version fully supported by the
270
* current execution environment} {@code RELEASE_9} or later must
271
* be returned.
272
*
273
* @apiNote This method is included alongside {@link latest} to
274
* allow identification of situations where the language model API
275
* is running on a platform version different than the latest
276
* version modeled by the API. One way that sort of situation can
277
* occur is if an IDE or similar tool is using the API to model
278
* source version <i>N</i> while running on platform version
279
* (<i>N</i>&nbsp;-&nbsp;1). Running in this configuration is
280
* supported by the API. Running an API on platform versions
281
* earlier than (<i>N</i>&nbsp;-&nbsp;1) or later than <i>N</i>
282
* may or may not work as an implementation detail. If an
283
* annotation processor was generating code to run under the
284
* current execution environment, the processor should only use
285
* platform features up to the {@code latestSupported} release,
286
* which may be earlier than the {@code latest} release.
287
*/
288
public static SourceVersion latestSupported() {
289
return latestSupported;
290
}
291
292
/**
293
* Returns whether or not {@code name} is a syntactically valid
294
* identifier (simple name) or keyword in the latest source
295
* version. The method returns {@code true} if the name consists
296
* of an initial character for which {@link
297
* Character#isJavaIdentifierStart(int)} returns {@code true},
298
* followed only by characters for which {@link
299
* Character#isJavaIdentifierPart(int)} returns {@code true}.
300
* This pattern matches regular identifiers, keywords, restricted
301
* keywords, restricted identifiers and the literals {@code "true"},
302
* {@code "false"}, {@code "null"}.
303
*
304
* The method returns {@code false} for all other strings.
305
*
306
* @param name the string to check
307
* @return {@code true} if this string is a
308
* syntactically valid identifier or keyword, {@code false}
309
* otherwise.
310
*
311
* @jls 3.8 Identifiers
312
*/
313
public static boolean isIdentifier(CharSequence name) {
314
String id = name.toString();
315
316
if (id.length() == 0) {
317
return false;
318
}
319
int cp = id.codePointAt(0);
320
if (!Character.isJavaIdentifierStart(cp)) {
321
return false;
322
}
323
for (int i = Character.charCount(cp);
324
i < id.length();
325
i += Character.charCount(cp)) {
326
cp = id.codePointAt(i);
327
if (!Character.isJavaIdentifierPart(cp)) {
328
return false;
329
}
330
}
331
return true;
332
}
333
334
/**
335
* Returns whether or not {@code name} is a syntactically valid
336
* qualified name in the latest source version.
337
*
338
* Syntactically, a qualified name is a sequence of identifiers
339
* separated by period characters ("{@code .}"). This method
340
* splits the input string into period-separated segments and
341
* applies checks to each segment in turn.
342
*
343
* Unlike {@link #isIdentifier isIdentifier}, this method returns
344
* {@code false} for keywords, boolean literals, and the null
345
* literal in any segment.
346
*
347
* This method returns {@code true} for <i>restricted
348
* keywords</i> and <i>restricted identifiers</i>.
349
*
350
* @param name the string to check
351
* @return {@code true} if this string is a
352
* syntactically valid name, {@code false} otherwise.
353
* @jls 3.9 Keywords
354
* @jls 6.2 Names and Identifiers
355
*/
356
public static boolean isName(CharSequence name) {
357
return isName(name, latest());
358
}
359
360
/**
361
* Returns whether or not {@code name} is a syntactically valid
362
* qualified name in the given source version.
363
*
364
* Syntactically, a qualified name is a sequence of identifiers
365
* separated by period characters ("{@code .}"). This method
366
* splits the input string into period-separated segments and
367
* applies checks to each segment in turn.
368
*
369
* Unlike {@link #isIdentifier isIdentifier}, this method returns
370
* {@code false} for keywords, boolean literals, and the null
371
* literal in any segment.
372
*
373
* This method returns {@code true} for <i>restricted
374
* keywords</i> and <i>restricted identifiers</i>.
375
*
376
* @param name the string to check
377
* @param version the version to use
378
* @return {@code true} if this string is a
379
* syntactically valid name, {@code false} otherwise.
380
* @jls 3.9 Keywords
381
* @jls 6.2 Names and Identifiers
382
* @since 9
383
*/
384
public static boolean isName(CharSequence name, SourceVersion version) {
385
String id = name.toString();
386
387
for(String s : id.split("\\.", -1)) {
388
if (!isIdentifier(s) || isKeyword(s, version))
389
return false;
390
}
391
return true;
392
}
393
394
/**
395
* Returns whether or not {@code s} is a keyword, boolean literal,
396
* or null literal in the latest source version.
397
* This method returns {@code false} for <i>restricted
398
* keywords</i> and <i>restricted identifiers</i>.
399
*
400
* @param s the string to check
401
* @return {@code true} if {@code s} is a keyword, or boolean
402
* literal, or null literal, {@code false} otherwise.
403
* @jls 3.9 Keywords
404
* @jls 3.10.3 Boolean Literals
405
* @jls 3.10.8 The Null Literal
406
*/
407
public static boolean isKeyword(CharSequence s) {
408
return isKeyword(s, latest());
409
}
410
411
/**
412
* Returns whether or not {@code s} is a keyword, boolean literal,
413
* or null literal in the given source version.
414
* This method returns {@code false} for <i>restricted
415
* keywords</i> and <i>restricted identifiers</i>.
416
*
417
* @param s the string to check
418
* @param version the version to use
419
* @return {@code true} if {@code s} is a keyword, or boolean
420
* literal, or null literal, {@code false} otherwise.
421
* @jls 3.9 Keywords
422
* @jls 3.10.3 Boolean Literals
423
* @jls 3.10.8 The Null Literal
424
* @since 9
425
*/
426
public static boolean isKeyword(CharSequence s, SourceVersion version) {
427
String id = s.toString();
428
switch(id) {
429
// A trip through history
430
case "strictfp":
431
return version.compareTo(RELEASE_2) >= 0;
432
433
case "assert":
434
return version.compareTo(RELEASE_4) >= 0;
435
436
case "enum":
437
return version.compareTo(RELEASE_5) >= 0;
438
439
case "_":
440
return version.compareTo(RELEASE_9) >= 0;
441
442
// case "non-sealed": can be added once it is a keyword only
443
// dependent on release and not also preview features being
444
// enabled.
445
446
// Keywords common across versions
447
448
// Modifiers
449
case "public": case "protected": case "private":
450
case "abstract": case "static": case "final":
451
case "transient": case "volatile": case "synchronized":
452
case "native":
453
454
// Declarations
455
case "class": case "interface": case "extends":
456
case "package": case "throws": case "implements":
457
458
// Primitive types and void
459
case "boolean": case "byte": case "char":
460
case "short": case "int": case "long":
461
case "float": case "double":
462
case "void":
463
464
// Control flow
465
case "if": case "else":
466
case "try": case "catch": case "finally":
467
case "do": case "while":
468
case "for": case "continue":
469
case "switch": case "case": case "default":
470
case "break": case "throw": case "return":
471
472
// Other keywords
473
case "this": case "new": case "super":
474
case "import": case "instanceof":
475
476
// Forbidden!
477
case "goto": case "const":
478
479
// literals
480
case "null": case "true": case "false":
481
return true;
482
483
default:
484
return false;
485
}
486
}
487
}
488
489