1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/javax/lang/model/SourceVersion.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,258 @@ 1.4 +/* 1.5 + * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package javax.lang.model; 1.30 + 1.31 +import java.util.Collections; 1.32 +import java.util.Set; 1.33 +import java.util.HashSet; 1.34 + 1.35 +/** 1.36 + * Source versions of the Java™ programming language. 1.37 + * 1.38 + * See <a 1.39 + * href="http://java.sun.com/docs/books/jls/">http://java.sun.com/docs/books/jls/</a> 1.40 + * for information on editions of <i>The Java™ Language 1.41 + * Specification</i>, including updates and clarifications. 1.42 + * 1.43 + * <p>Note that additional source version constants will be added to 1.44 + * model future releases of the language. 1.45 + * 1.46 + * @author Joseph D. Darcy 1.47 + * @author Scott Seligman 1.48 + * @author Peter von der Ahé 1.49 + * @since 1.6 1.50 + */ 1.51 +public enum SourceVersion { 1.52 + /* 1.53 + * Summary of language evoluation 1.54 + * 1.1: nested classes 1.55 + * 1.2: strictfp 1.56 + * 1.3: no changes 1.57 + * 1.4: assert 1.58 + * 1.5: annotations, generics, autoboxing, var-args... 1.59 + * 1.6: no changes 1.60 + */ 1.61 + 1.62 + /** 1.63 + * The original version. 1.64 + * 1.65 + * The language described in the first edition of <i>The 1.66 + * Java™ Language Specification</i>. 1.67 + */ 1.68 + RELEASE_0, 1.69 + 1.70 + /** 1.71 + * The version recognized by the Java Platform 1.1. 1.72 + * 1.73 + * The language is {@code RELEASE_0} <a 1.74 + * href="http://java.sun.com/docs/books/jls/first_edition/html/1.1Update.html">augmented</a> 1.75 + * with nested classes. 1.76 + */ 1.77 + RELEASE_1, 1.78 + 1.79 + /** 1.80 + * The version recognized by the Java 2 Platform, Standard Edition, 1.81 + * v 1.2. 1.82 + * 1.83 + * The language described in <i>The Java™ Language 1.84 + * Specification, Second Edition</i>, which includes the {@code 1.85 + * strictfp} modifier. 1.86 + */ 1.87 + RELEASE_2, 1.88 + 1.89 + /** 1.90 + * The version recognized by the Java 2 Platform, Standard Edition, 1.91 + * v 1.3. 1.92 + * 1.93 + * No major changes from {@code RELEASE_2}. 1.94 + */ 1.95 + RELEASE_3, 1.96 + 1.97 + /** 1.98 + * The version recognized by the Java 2 Platform, Standard Edition, 1.99 + * v 1.4. 1.100 + * 1.101 + * Added a simple assertion facility. 1.102 + */ 1.103 + RELEASE_4, 1.104 + 1.105 + /** 1.106 + * The version recognized by the Java 2 Platform, Standard 1.107 + * Edition 5.0. 1.108 + * 1.109 + * The language described in <i>The Java™ Language 1.110 + * Specification, Third Edition</i>. First release to support 1.111 + * generics, annotations, autoboxing, var-args, enhanced {@code 1.112 + * for} loop, and hexadecimal floating-point literals. 1.113 + */ 1.114 + RELEASE_5, 1.115 + 1.116 + /** 1.117 + * The version recognized by the Java Platform, Standard Edition 1.118 + * 6. 1.119 + * 1.120 + * No major changes from {@code RELEASE_5}. 1.121 + */ 1.122 + RELEASE_6, 1.123 + 1.124 + /** 1.125 + * The version recognized by the Java Platform, Standard Edition 1.126 + * 7. 1.127 + * 1.128 + * @since 1.7 1.129 + */ 1.130 + RELEASE_7; 1.131 + 1.132 + // Note that when adding constants for newer releases, the 1.133 + // behavior of latest() and latestSupported() must be updated too. 1.134 + 1.135 + /** 1.136 + * Returns the latest source version that can be modeled. 1.137 + * 1.138 + * @return the latest source version that can be modeled 1.139 + */ 1.140 + public static SourceVersion latest() { 1.141 + return RELEASE_7; 1.142 + } 1.143 + 1.144 + private static final SourceVersion latestSupported = getLatestSupported(); 1.145 + 1.146 + private static SourceVersion getLatestSupported() { 1.147 + try { 1.148 + String specVersion = System.getProperty("java.specification.version"); 1.149 + if ("1.7".equals(specVersion)) 1.150 + return RELEASE_7; 1.151 + else if ("1.6".equals(specVersion)) 1.152 + return RELEASE_6; 1.153 + } catch (SecurityException se) {} 1.154 + 1.155 + return RELEASE_5; 1.156 + } 1.157 + 1.158 + /** 1.159 + * Returns the latest source version fully supported by the 1.160 + * current execution environment. {@code RELEASE_5} or later must 1.161 + * be returned. 1.162 + * 1.163 + * @return the latest source version that is fully supported 1.164 + */ 1.165 + public static SourceVersion latestSupported() { 1.166 + return latestSupported; 1.167 + } 1.168 + 1.169 + /** 1.170 + * Returns whether or not {@code name} is a syntactically valid 1.171 + * identifier (simple name) or keyword in the latest source 1.172 + * version. The method returns {@code true} if the name consists 1.173 + * of an initial character for which {@link 1.174 + * Character#isJavaIdentifierStart(int)} returns {@code true}, 1.175 + * followed only by characters for which {@link 1.176 + * Character#isJavaIdentifierPart(int)} returns {@code true}. 1.177 + * This pattern matches regular identifiers, keywords, and the 1.178 + * literals {@code "true"}, {@code "false"}, and {@code "null"}. 1.179 + * The method returns {@code false} for all other strings. 1.180 + * 1.181 + * @param name the string to check 1.182 + * @return {@code true} if this string is a 1.183 + * syntactically valid identifier or keyword, {@code false} 1.184 + * otherwise. 1.185 + */ 1.186 + public static boolean isIdentifier(CharSequence name) { 1.187 + String id = name.toString(); 1.188 + 1.189 + if (id.length() == 0) { 1.190 + return false; 1.191 + } 1.192 + int cp = id.codePointAt(0); 1.193 + if (!Character.isJavaIdentifierStart(cp)) { 1.194 + return false; 1.195 + } 1.196 + for (int i = Character.charCount(cp); 1.197 + i < id.length(); 1.198 + i += Character.charCount(cp)) { 1.199 + cp = id.codePointAt(i); 1.200 + if (!Character.isJavaIdentifierPart(cp)) { 1.201 + return false; 1.202 + } 1.203 + } 1.204 + return true; 1.205 + } 1.206 + 1.207 + /** 1.208 + * Returns whether or not {@code name} is a syntactically valid 1.209 + * qualified name in the latest source version. Unlike {@link 1.210 + * #isIdentifier isIdentifier}, this method returns {@code false} 1.211 + * for keywords and literals. 1.212 + * 1.213 + * @param name the string to check 1.214 + * @return {@code true} if this string is a 1.215 + * syntactically valid name, {@code false} otherwise. 1.216 + * @jls3 6.2 Names and Identifiers 1.217 + */ 1.218 + public static boolean isName(CharSequence name) { 1.219 + String id = name.toString(); 1.220 + 1.221 + for(String s : id.split("\\.", -1)) { 1.222 + if (!isIdentifier(s) || isKeyword(s)) 1.223 + return false; 1.224 + } 1.225 + return true; 1.226 + } 1.227 + 1.228 + private final static Set<String> keywords; 1.229 + static { 1.230 + Set<String> s = new HashSet<String>(); 1.231 + String [] kws = { 1.232 + "abstract", "continue", "for", "new", "switch", 1.233 + "assert", "default", "if", "package", "synchronized", 1.234 + "boolean", "do", "goto", "private", "this", 1.235 + "break", "double", "implements", "protected", "throw", 1.236 + "byte", "else", "import", "public", "throws", 1.237 + "case", "enum", "instanceof", "return", "transient", 1.238 + "catch", "extends", "int", "short", "try", 1.239 + "char", "final", "interface", "static", "void", 1.240 + "class", "finally", "long", "strictfp", "volatile", 1.241 + "const", "float", "native", "super", "while", 1.242 + // literals 1.243 + "null", "true", "false" 1.244 + }; 1.245 + for(String kw : kws) 1.246 + s.add(kw); 1.247 + keywords = Collections.unmodifiableSet(s); 1.248 + } 1.249 + 1.250 + /** 1.251 + * Returns whether or not {@code s} is a keyword or literal in the 1.252 + * latest source version. 1.253 + * 1.254 + * @param s the string to check 1.255 + * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise. 1.256 + */ 1.257 + public static boolean isKeyword(CharSequence s) { 1.258 + String keywordOrLiteral = s.toString(); 1.259 + return keywords.contains(keywordOrLiteral); 1.260 + } 1.261 +}