author | darcy |
Thu, 27 Jun 2013 11:46:23 -0700 | |
changeset 18660 | d8332e6f4fab |
parent 15385 | ee1eebe7e210 |
child 22159 | 682da512ec17 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
15385 | 2 |
* Copyright (c) 2005, 2013, 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 |
10 | 58 |
*/ |
59 |
||
60 |
/** |
|
61 |
* The original version. |
|
62 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
63 |
* 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
|
64 |
* <cite>The Java™ Language Specification, First Edition</cite>. |
10 | 65 |
*/ |
66 |
RELEASE_0, |
|
67 |
||
68 |
/** |
|
69 |
* The version recognized by the Java Platform 1.1. |
|
70 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
71 |
* 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
|
72 |
* <cite>The Java™ Language Specification, First Edition</cite>. |
10 | 73 |
*/ |
74 |
RELEASE_1, |
|
75 |
||
76 |
/** |
|
77 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
78 |
* v 1.2. |
|
79 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
80 |
* 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
|
81 |
* <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
|
82 |
* Second Edition</cite>, which includes the {@code |
10 | 83 |
* strictfp} modifier. |
84 |
*/ |
|
85 |
RELEASE_2, |
|
86 |
||
87 |
/** |
|
88 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
89 |
* v 1.3. |
|
90 |
* |
|
91 |
* No major changes from {@code RELEASE_2}. |
|
92 |
*/ |
|
93 |
RELEASE_3, |
|
94 |
||
95 |
/** |
|
96 |
* The version recognized by the Java 2 Platform, Standard Edition, |
|
97 |
* v 1.4. |
|
98 |
* |
|
99 |
* Added a simple assertion facility. |
|
100 |
*/ |
|
101 |
RELEASE_4, |
|
102 |
||
103 |
/** |
|
104 |
* The version recognized by the Java 2 Platform, Standard |
|
105 |
* Edition 5.0. |
|
106 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
107 |
* 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
|
108 |
* <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
|
109 |
* Third Edition</cite>. First release to support |
10 | 110 |
* generics, annotations, autoboxing, var-args, enhanced {@code |
111 |
* for} loop, and hexadecimal floating-point literals. |
|
112 |
*/ |
|
113 |
RELEASE_5, |
|
114 |
||
115 |
/** |
|
116 |
* The version recognized by the Java Platform, Standard Edition |
|
117 |
* 6. |
|
118 |
* |
|
119 |
* No major changes from {@code RELEASE_5}. |
|
120 |
*/ |
|
121 |
RELEASE_6, |
|
122 |
||
123 |
/** |
|
124 |
* The version recognized by the Java Platform, Standard Edition |
|
125 |
* 7. |
|
126 |
* |
|
18660
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
127 |
* 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
|
128 |
* 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
|
129 |
* binary literals, and multi-catch. |
10 | 130 |
* @since 1.7 |
131 |
*/ |
|
10180 | 132 |
RELEASE_7, |
133 |
||
134 |
/** |
|
135 |
* The version recognized by the Java Platform, Standard Edition |
|
136 |
* 8. |
|
137 |
* |
|
18660
d8332e6f4fab
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents:
15385
diff
changeset
|
138 |
* Additions in this release include lambda expressions and default methods. |
10180 | 139 |
* @since 1.8 |
140 |
*/ |
|
141 |
RELEASE_8; |
|
10 | 142 |
|
143 |
// Note that when adding constants for newer releases, the |
|
144 |
// behavior of latest() and latestSupported() must be updated too. |
|
145 |
||
146 |
/** |
|
147 |
* Returns the latest source version that can be modeled. |
|
148 |
* |
|
149 |
* @return the latest source version that can be modeled |
|
150 |
*/ |
|
151 |
public static SourceVersion latest() { |
|
10180 | 152 |
return RELEASE_8; |
10 | 153 |
} |
154 |
||
155 |
private static final SourceVersion latestSupported = getLatestSupported(); |
|
156 |
||
157 |
private static SourceVersion getLatestSupported() { |
|
158 |
try { |
|
159 |
String specVersion = System.getProperty("java.specification.version"); |
|
10180 | 160 |
|
161 |
if ("1.8".equals(specVersion)) |
|
162 |
return RELEASE_8; |
|
163 |
else if("1.7".equals(specVersion)) |
|
10 | 164 |
return RELEASE_7; |
10180 | 165 |
else if("1.6".equals(specVersion)) |
10 | 166 |
return RELEASE_6; |
167 |
} catch (SecurityException se) {} |
|
168 |
||
169 |
return RELEASE_5; |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Returns the latest source version fully supported by the |
|
174 |
* current execution environment. {@code RELEASE_5} or later must |
|
175 |
* be returned. |
|
176 |
* |
|
177 |
* @return the latest source version that is fully supported |
|
178 |
*/ |
|
179 |
public static SourceVersion latestSupported() { |
|
180 |
return latestSupported; |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Returns whether or not {@code name} is a syntactically valid |
|
185 |
* identifier (simple name) or keyword in the latest source |
|
186 |
* version. The method returns {@code true} if the name consists |
|
187 |
* of an initial character for which {@link |
|
188 |
* Character#isJavaIdentifierStart(int)} returns {@code true}, |
|
189 |
* followed only by characters for which {@link |
|
190 |
* Character#isJavaIdentifierPart(int)} returns {@code true}. |
|
191 |
* This pattern matches regular identifiers, keywords, and the |
|
192 |
* literals {@code "true"}, {@code "false"}, and {@code "null"}. |
|
193 |
* The method returns {@code false} for all other strings. |
|
194 |
* |
|
195 |
* @param name the string to check |
|
196 |
* @return {@code true} if this string is a |
|
197 |
* syntactically valid identifier or keyword, {@code false} |
|
198 |
* otherwise. |
|
199 |
*/ |
|
200 |
public static boolean isIdentifier(CharSequence name) { |
|
201 |
String id = name.toString(); |
|
202 |
||
203 |
if (id.length() == 0) { |
|
204 |
return false; |
|
205 |
} |
|
206 |
int cp = id.codePointAt(0); |
|
207 |
if (!Character.isJavaIdentifierStart(cp)) { |
|
208 |
return false; |
|
209 |
} |
|
210 |
for (int i = Character.charCount(cp); |
|
211 |
i < id.length(); |
|
212 |
i += Character.charCount(cp)) { |
|
213 |
cp = id.codePointAt(i); |
|
214 |
if (!Character.isJavaIdentifierPart(cp)) { |
|
215 |
return false; |
|
216 |
} |
|
217 |
} |
|
218 |
return true; |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Returns whether or not {@code name} is a syntactically valid |
|
223 |
* qualified name in the latest source version. Unlike {@link |
|
224 |
* #isIdentifier isIdentifier}, this method returns {@code false} |
|
225 |
* for keywords and literals. |
|
226 |
* |
|
227 |
* @param name the string to check |
|
228 |
* @return {@code true} if this string is a |
|
229 |
* syntactically valid name, {@code false} otherwise. |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
5520
diff
changeset
|
230 |
* @jls 6.2 Names and Identifiers |
10 | 231 |
*/ |
232 |
public static boolean isName(CharSequence name) { |
|
233 |
String id = name.toString(); |
|
234 |
||
235 |
for(String s : id.split("\\.", -1)) { |
|
236 |
if (!isIdentifier(s) || isKeyword(s)) |
|
237 |
return false; |
|
238 |
} |
|
239 |
return true; |
|
240 |
} |
|
241 |
||
242 |
private final static Set<String> keywords; |
|
243 |
static { |
|
244 |
Set<String> s = new HashSet<String>(); |
|
245 |
String [] kws = { |
|
246 |
"abstract", "continue", "for", "new", "switch", |
|
247 |
"assert", "default", "if", "package", "synchronized", |
|
248 |
"boolean", "do", "goto", "private", "this", |
|
249 |
"break", "double", "implements", "protected", "throw", |
|
250 |
"byte", "else", "import", "public", "throws", |
|
251 |
"case", "enum", "instanceof", "return", "transient", |
|
252 |
"catch", "extends", "int", "short", "try", |
|
253 |
"char", "final", "interface", "static", "void", |
|
254 |
"class", "finally", "long", "strictfp", "volatile", |
|
255 |
"const", "float", "native", "super", "while", |
|
256 |
// literals |
|
257 |
"null", "true", "false" |
|
258 |
}; |
|
259 |
for(String kw : kws) |
|
260 |
s.add(kw); |
|
261 |
keywords = Collections.unmodifiableSet(s); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Returns whether or not {@code s} is a keyword or literal in the |
|
266 |
* latest source version. |
|
267 |
* |
|
268 |
* @param s the string to check |
|
269 |
* @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise. |
|
270 |
*/ |
|
271 |
public static boolean isKeyword(CharSequence s) { |
|
272 |
String keywordOrLiteral = s.toString(); |
|
273 |
return keywords.contains(keywordOrLiteral); |
|
274 |
} |
|
275 |
} |