author | jjh |
Wed, 13 Apr 2011 11:35:43 -0700 | |
changeset 9303 | eae35c201e19 |
parent 5520 | 86e4b9a9da40 |
child 10180 | b293c1f36ac4 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2005, 2006, 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 |
*/ |
|
127 |
RELEASE_7; |
|
128 |
||
129 |
// Note that when adding constants for newer releases, the |
|
130 |
// behavior of latest() and latestSupported() must be updated too. |
|
131 |
||
132 |
/** |
|
133 |
* Returns the latest source version that can be modeled. |
|
134 |
* |
|
135 |
* @return the latest source version that can be modeled |
|
136 |
*/ |
|
137 |
public static SourceVersion latest() { |
|
138 |
return RELEASE_7; |
|
139 |
} |
|
140 |
||
141 |
private static final SourceVersion latestSupported = getLatestSupported(); |
|
142 |
||
143 |
private static SourceVersion getLatestSupported() { |
|
144 |
try { |
|
145 |
String specVersion = System.getProperty("java.specification.version"); |
|
146 |
if ("1.7".equals(specVersion)) |
|
147 |
return RELEASE_7; |
|
148 |
else if ("1.6".equals(specVersion)) |
|
149 |
return RELEASE_6; |
|
150 |
} catch (SecurityException se) {} |
|
151 |
||
152 |
return RELEASE_5; |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Returns the latest source version fully supported by the |
|
157 |
* current execution environment. {@code RELEASE_5} or later must |
|
158 |
* be returned. |
|
159 |
* |
|
160 |
* @return the latest source version that is fully supported |
|
161 |
*/ |
|
162 |
public static SourceVersion latestSupported() { |
|
163 |
return latestSupported; |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Returns whether or not {@code name} is a syntactically valid |
|
168 |
* identifier (simple name) or keyword in the latest source |
|
169 |
* version. The method returns {@code true} if the name consists |
|
170 |
* of an initial character for which {@link |
|
171 |
* Character#isJavaIdentifierStart(int)} returns {@code true}, |
|
172 |
* followed only by characters for which {@link |
|
173 |
* Character#isJavaIdentifierPart(int)} returns {@code true}. |
|
174 |
* This pattern matches regular identifiers, keywords, and the |
|
175 |
* literals {@code "true"}, {@code "false"}, and {@code "null"}. |
|
176 |
* The method returns {@code false} for all other strings. |
|
177 |
* |
|
178 |
* @param name the string to check |
|
179 |
* @return {@code true} if this string is a |
|
180 |
* syntactically valid identifier or keyword, {@code false} |
|
181 |
* otherwise. |
|
182 |
*/ |
|
183 |
public static boolean isIdentifier(CharSequence name) { |
|
184 |
String id = name.toString(); |
|
185 |
||
186 |
if (id.length() == 0) { |
|
187 |
return false; |
|
188 |
} |
|
189 |
int cp = id.codePointAt(0); |
|
190 |
if (!Character.isJavaIdentifierStart(cp)) { |
|
191 |
return false; |
|
192 |
} |
|
193 |
for (int i = Character.charCount(cp); |
|
194 |
i < id.length(); |
|
195 |
i += Character.charCount(cp)) { |
|
196 |
cp = id.codePointAt(i); |
|
197 |
if (!Character.isJavaIdentifierPart(cp)) { |
|
198 |
return false; |
|
199 |
} |
|
200 |
} |
|
201 |
return true; |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Returns whether or not {@code name} is a syntactically valid |
|
206 |
* qualified name in the latest source version. Unlike {@link |
|
207 |
* #isIdentifier isIdentifier}, this method returns {@code false} |
|
208 |
* for keywords and literals. |
|
209 |
* |
|
210 |
* @param name the string to check |
|
211 |
* @return {@code true} if this string is a |
|
212 |
* 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
|
213 |
* @jls 6.2 Names and Identifiers |
10 | 214 |
*/ |
215 |
public static boolean isName(CharSequence name) { |
|
216 |
String id = name.toString(); |
|
217 |
||
218 |
for(String s : id.split("\\.", -1)) { |
|
219 |
if (!isIdentifier(s) || isKeyword(s)) |
|
220 |
return false; |
|
221 |
} |
|
222 |
return true; |
|
223 |
} |
|
224 |
||
225 |
private final static Set<String> keywords; |
|
226 |
static { |
|
227 |
Set<String> s = new HashSet<String>(); |
|
228 |
String [] kws = { |
|
229 |
"abstract", "continue", "for", "new", "switch", |
|
230 |
"assert", "default", "if", "package", "synchronized", |
|
231 |
"boolean", "do", "goto", "private", "this", |
|
232 |
"break", "double", "implements", "protected", "throw", |
|
233 |
"byte", "else", "import", "public", "throws", |
|
234 |
"case", "enum", "instanceof", "return", "transient", |
|
235 |
"catch", "extends", "int", "short", "try", |
|
236 |
"char", "final", "interface", "static", "void", |
|
237 |
"class", "finally", "long", "strictfp", "volatile", |
|
238 |
"const", "float", "native", "super", "while", |
|
239 |
// literals |
|
240 |
"null", "true", "false" |
|
241 |
}; |
|
242 |
for(String kw : kws) |
|
243 |
s.add(kw); |
|
244 |
keywords = Collections.unmodifiableSet(s); |
|
245 |
} |
|
246 |
||
247 |
/** |
|
248 |
* Returns whether or not {@code s} is a keyword or literal in the |
|
249 |
* latest source version. |
|
250 |
* |
|
251 |
* @param s the string to check |
|
252 |
* @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise. |
|
253 |
*/ |
|
254 |
public static boolean isKeyword(CharSequence s) { |
|
255 |
String keywordOrLiteral = s.toString(); |
|
256 |
return keywords.contains(keywordOrLiteral); |
|
257 |
} |
|
258 |
} |