Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
1 /*
2 * Copyright (c) 2005, 2013, 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 */
26 package javax.lang.model;
28 import java.util.Collections;
29 import java.util.Set;
30 import java.util.HashSet;
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é
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 */
60 /**
61 * The original version.
62 *
63 * The language described in
64 * <cite>The Java™ Language Specification, First Edition</cite>.
65 */
66 RELEASE_0,
68 /**
69 * The version recognized by the Java Platform 1.1.
70 *
71 * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to
72 * <cite>The Java™ Language Specification, First Edition</cite>.
73 */
74 RELEASE_1,
76 /**
77 * The version recognized by the Java 2 Platform, Standard Edition,
78 * v 1.2.
79 *
80 * The language described in
81 * <cite>The Java™ Language Specification,
82 * Second Edition</cite>, which includes the {@code
83 * strictfp} modifier.
84 */
85 RELEASE_2,
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,
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,
103 /**
104 * The version recognized by the Java 2 Platform, Standard
105 * Edition 5.0.
106 *
107 * The language described in
108 * <cite>The Java™ Language Specification,
109 * Third Edition</cite>. First release to support
110 * generics, annotations, autoboxing, var-args, enhanced {@code
111 * for} loop, and hexadecimal floating-point literals.
112 */
113 RELEASE_5,
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,
123 /**
124 * The version recognized by the Java Platform, Standard Edition
125 * 7.
126 *
127 * Additions in this release include, diamond syntax for
128 * constructors, {@code try}-with-resources, strings in switch,
129 * binary literals, and multi-catch.
130 * @since 1.7
131 */
132 RELEASE_7,
134 /**
135 * The version recognized by the Java Platform, Standard Edition
136 * 8.
137 *
138 * Additions in this release include lambda expressions and default methods.
139 * @since 1.8
140 */
141 RELEASE_8;
143 // Note that when adding constants for newer releases, the
144 // behavior of latest() and latestSupported() must be updated too.
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() {
152 return RELEASE_8;
153 }
155 private static final SourceVersion latestSupported = getLatestSupported();
157 private static SourceVersion getLatestSupported() {
158 try {
159 String specVersion = System.getProperty("java.specification.version");
161 if ("1.8".equals(specVersion))
162 return RELEASE_8;
163 else if("1.7".equals(specVersion))
164 return RELEASE_7;
165 else if("1.6".equals(specVersion))
166 return RELEASE_6;
167 } catch (SecurityException se) {}
169 return RELEASE_5;
170 }
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 }
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();
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 }
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.
230 * @jls 6.2 Names and Identifiers
231 */
232 public static boolean isName(CharSequence name) {
233 String id = name.toString();
235 for(String s : id.split("\\.", -1)) {
236 if (!isIdentifier(s) || isKeyword(s))
237 return false;
238 }
239 return true;
240 }
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 }
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 }