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