KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > lang > model > SourceVersion


1 /*
2  * @(#)SourceVersion.java 1.6 06/08/15
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.lang.model;
9
10 import java.util.Collections JavaDoc;
11 import java.util.Set JavaDoc;
12 import java.util.HashSet JavaDoc;
13
14 /**
15  * Source versions of the Java™ programming language.
16  *
17  * See <a
18  * HREF="http://java.sun.com/docs/books/jls/">http://java.sun.com/docs/books/jls/</a>
19  * for information on editions of <i>The Java&trade; Language
20  * Specification</i>, including updates and clarifications.
21  *
22  * <p>Note that additional source version constants will be added to
23  * model future releases of the language.
24  *
25  * @author Joseph D. Darcy
26  * @author Scott Seligman
27  * @author Peter von der Ah&eacute;
28  * @version 1.6 06/08/15
29  * @since 1.6
30  */

31 public enum SourceVersion {
32     /*
33      * Summary of language evoluation
34      * 1.1: nested classes
35      * 1.2: strictfp
36      * 1.3: no changes
37      * 1.4: assert
38      * 1.5: annotations, generics, autoboxing, var-args...
39      * 1.6: no changes
40      */

41
42     /**
43      * The original version.
44      *
45      * The language described in the first edition of <i>The
46      * Java&trade; Language Specification</i>.
47      */

48     RELEASE_0,
49
50     /**
51      * The version recognized by the Java Platform 1.1.
52      *
53      * The language is {@code RELEASE_0} <a
54      * HREF="http://java.sun.com/docs/books/jls/first_edition/html/1.1Update.html">augmented</a>
55      * with nested classes.
56      */

57     RELEASE_1,
58
59     /**
60      * The version recognized by the Java 2 Platform, Standard Edition,
61      * v 1.2.
62      *
63      * The language described in <i>The Java&trade; Language
64      * Specification, Second Edition</i>, which includes the {@code
65      * strictfp} modifier.
66      */

67     RELEASE_2,
68
69     /**
70      * The version recognized by the Java 2 Platform, Standard Edition,
71      * v 1.3.
72      *
73      * No major changes from {@code RELEASE_2}.
74      */

75     RELEASE_3,
76
77     /**
78      * The version recognized by the Java 2 Platform, Standard Edition,
79      * v 1.4.
80      *
81      * Added a simple assertion facility.
82      */

83     RELEASE_4,
84
85     /**
86      * The version recognized by the Java 2 Platform, Standard
87      * Edition 5.0.
88      *
89      * The language described in <i>The Java&trade; Language
90      * Specification, Third Edition</i>. First release to support
91      * generics, annotations, autoboxing, var-args, enhanced {@code
92      * for} loop, and hexadecimal floating-point literals.
93      */

94     RELEASE_5,
95
96     /**
97      * The version recognized by the Java Platform, Standard Edition
98      * 6.
99      *
100      * No major changes from {@code RELEASE_5}.
101      */

102     RELEASE_6;
103
104
105     /**
106      * Returns the latest source version that can be modeled.
107      *
108      * @return the latest source version that can be modeled
109      */

110     public static SourceVersion latest() {
111     return RELEASE_6;
112     }
113
114     /**
115      * Returns the latest source version fully supported by the
116      * current execution environment. {@code RELEASE_5} or later must
117      * be returned.
118      *
119      * @return the latest source version that is fully supported
120      */

121     public static SourceVersion latestSupported() {
122     return RELEASE_6;
123     }
124
125     /**
126      * Returns whether or not {@code name} is a syntactically valid
127      * identifier (simple name) or keyword in the latest source
128      * version. The method returns {@code true} if the name consists
129      * of an initial character for which {@link
130      * Character#isJavaIdentifierStart(int)} returns {@code true},
131      * followed only by characters for which {@link
132      * Character#isJavaIdentifierPart(int)} returns {@code true}.
133      * This pattern matches regular identifiers, keywords, and the
134      * literals {@code "true"}, {@code "false"}, and {@code "null"}.
135      * The method returns {@code false} for all other strings.
136      *
137      * @param name the string to check
138      * @return {@code true} if this string is a
139      * syntactically valid identifier or keyword, {@code false}
140      * otherwise.
141      */

142     public static boolean isIdentifier(CharSequence JavaDoc name) {
143     String JavaDoc id = name.toString();
144     
145     if (id.length() == 0) {
146         return false;
147     }
148     int cp = id.codePointAt(0);
149         if (!Character.isJavaIdentifierStart(cp)) {
150         return false;
151     }
152         for (int i = Character.charCount(cp);
153         i < id.length();
154         i += Character.charCount(cp)) {
155         cp = id.codePointAt(i);
156             if (!Character.isJavaIdentifierPart(cp)) {
157         return false;
158         }
159         }
160         return true;
161     }
162
163     /**
164      * Returns whether or not {@code name} is a syntactically valid
165      * qualified name in the latest source version. Unlike {@link
166      * #isIdentifier isIdentifier}, this method returns {@code false}
167      * for keywords and literals.
168      *
169      * @param name the string to check
170      * @return {@code true} if this string is a
171      * syntactically valid name, {@code false} otherwise.
172      * @jls3 6.2 Names and Identifiers
173      */

174     public static boolean isName(CharSequence JavaDoc name) {
175     String JavaDoc id = name.toString();
176     
177     for(String JavaDoc s : id.split("\\.", -1)) {
178         if (!isIdentifier(s) || isKeyword(s))
179         return false;
180     }
181     return true;
182     }
183
184     private final static Set JavaDoc<String JavaDoc> keywords;
185     static {
186     Set JavaDoc<String JavaDoc> s = new HashSet JavaDoc<String JavaDoc>();
187     String JavaDoc [] kws = {
188         "abstract", "continue", "for", "new", "switch",
189         "assert", "default", "if", "package", "synchronized",
190         "boolean", "do", "goto", "private", "this",
191         "break", "double", "implements", "protected", "throw",
192         "byte", "else", "import", "public", "throws",
193         "case", "enum", "instanceof", "return", "transient",
194         "catch", "extends", "int", "short", "try",
195         "char", "final", "interface", "static", "void",
196         "class", "finally", "long", "strictfp", "volatile",
197         "const", "float", "native", "super", "while",
198         // literals
199
"null", "true", "false"
200     };
201     for(String JavaDoc kw : kws)
202         s.add(kw);
203     keywords = Collections.unmodifiableSet(s);
204     }
205
206     /**
207      * Returns whether or not {@code s} is a keyword or literal in the
208      * latest source version.
209      *
210      * @param s the string to check
211      * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise.
212      */

213     public static boolean isKeyword(CharSequence JavaDoc s) {
214     String JavaDoc keywordOrLiteral = s.toString();
215     return keywords.contains(keywordOrLiteral);
216     }
217 }
218
Popular Tags