src/share/classes/javax/lang/model/SourceVersion.java

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 1042
defdd98cb7ce
child 1867
065f8cb7bd89
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

duke@1 1 /*
jjg@1521 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package javax.lang.model;
duke@1 27
duke@1 28 import java.util.Collections;
duke@1 29 import java.util.Set;
duke@1 30 import java.util.HashSet;
duke@1 31
duke@1 32 /**
duke@1 33 * Source versions of the Java™ programming language.
duke@1 34 *
jjh@972 35 * See the appropriate edition of
jjh@972 36 * <cite>The Java&trade; Language Specification</cite>
jjh@972 37 * for information about a particular source version.
duke@1 38 *
duke@1 39 * <p>Note that additional source version constants will be added to
duke@1 40 * model future releases of the language.
duke@1 41 *
duke@1 42 * @author Joseph D. Darcy
duke@1 43 * @author Scott Seligman
duke@1 44 * @author Peter von der Ah&eacute;
duke@1 45 * @since 1.6
duke@1 46 */
duke@1 47 public enum SourceVersion {
duke@1 48 /*
jjg@1521 49 * Summary of language evolution
duke@1 50 * 1.1: nested classes
duke@1 51 * 1.2: strictfp
duke@1 52 * 1.3: no changes
duke@1 53 * 1.4: assert
duke@1 54 * 1.5: annotations, generics, autoboxing, var-args...
duke@1 55 * 1.6: no changes
duke@1 56 */
duke@1 57
duke@1 58 /**
duke@1 59 * The original version.
duke@1 60 *
jjh@972 61 * The language described in
jjh@972 62 * <cite>The Java&trade; Language Specification, First Edition</cite>.
duke@1 63 */
duke@1 64 RELEASE_0,
duke@1 65
duke@1 66 /**
duke@1 67 * The version recognized by the Java Platform 1.1.
duke@1 68 *
jjh@972 69 * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to
jjh@972 70 * <cite>The Java&trade; Language Specification, First Edition</cite>.
duke@1 71 */
duke@1 72 RELEASE_1,
duke@1 73
duke@1 74 /**
duke@1 75 * The version recognized by the Java 2 Platform, Standard Edition,
duke@1 76 * v 1.2.
duke@1 77 *
jjh@972 78 * The language described in
jjh@972 79 * <cite>The Java&trade; Language Specification,
jjh@972 80 * Second Edition</cite>, which includes the {@code
duke@1 81 * strictfp} modifier.
duke@1 82 */
duke@1 83 RELEASE_2,
duke@1 84
duke@1 85 /**
duke@1 86 * The version recognized by the Java 2 Platform, Standard Edition,
duke@1 87 * v 1.3.
duke@1 88 *
duke@1 89 * No major changes from {@code RELEASE_2}.
duke@1 90 */
duke@1 91 RELEASE_3,
duke@1 92
duke@1 93 /**
duke@1 94 * The version recognized by the Java 2 Platform, Standard Edition,
duke@1 95 * v 1.4.
duke@1 96 *
duke@1 97 * Added a simple assertion facility.
duke@1 98 */
duke@1 99 RELEASE_4,
duke@1 100
duke@1 101 /**
duke@1 102 * The version recognized by the Java 2 Platform, Standard
duke@1 103 * Edition 5.0.
duke@1 104 *
jjh@972 105 * The language described in
jjh@972 106 * <cite>The Java&trade; Language Specification,
jjh@972 107 * Third Edition</cite>. First release to support
duke@1 108 * generics, annotations, autoboxing, var-args, enhanced {@code
duke@1 109 * for} loop, and hexadecimal floating-point literals.
duke@1 110 */
duke@1 111 RELEASE_5,
duke@1 112
duke@1 113 /**
duke@1 114 * The version recognized by the Java Platform, Standard Edition
duke@1 115 * 6.
duke@1 116 *
duke@1 117 * No major changes from {@code RELEASE_5}.
duke@1 118 */
duke@1 119 RELEASE_6,
duke@1 120
duke@1 121 /**
duke@1 122 * The version recognized by the Java Platform, Standard Edition
duke@1 123 * 7.
duke@1 124 *
duke@1 125 * @since 1.7
duke@1 126 */
darcy@1042 127 RELEASE_7,
darcy@1042 128
darcy@1042 129 /**
darcy@1042 130 * The version recognized by the Java Platform, Standard Edition
darcy@1042 131 * 8.
darcy@1042 132 *
darcy@1042 133 * @since 1.8
darcy@1042 134 */
darcy@1042 135 RELEASE_8;
duke@1 136
duke@1 137 // Note that when adding constants for newer releases, the
duke@1 138 // behavior of latest() and latestSupported() must be updated too.
duke@1 139
duke@1 140 /**
duke@1 141 * Returns the latest source version that can be modeled.
duke@1 142 *
duke@1 143 * @return the latest source version that can be modeled
duke@1 144 */
duke@1 145 public static SourceVersion latest() {
darcy@1042 146 return RELEASE_8;
duke@1 147 }
duke@1 148
duke@1 149 private static final SourceVersion latestSupported = getLatestSupported();
duke@1 150
duke@1 151 private static SourceVersion getLatestSupported() {
duke@1 152 try {
duke@1 153 String specVersion = System.getProperty("java.specification.version");
darcy@1042 154
darcy@1042 155 if ("1.8".equals(specVersion))
darcy@1042 156 return RELEASE_8;
darcy@1042 157 else if("1.7".equals(specVersion))
duke@1 158 return RELEASE_7;
darcy@1042 159 else if("1.6".equals(specVersion))
duke@1 160 return RELEASE_6;
duke@1 161 } catch (SecurityException se) {}
duke@1 162
duke@1 163 return RELEASE_5;
duke@1 164 }
duke@1 165
duke@1 166 /**
duke@1 167 * Returns the latest source version fully supported by the
duke@1 168 * current execution environment. {@code RELEASE_5} or later must
duke@1 169 * be returned.
duke@1 170 *
duke@1 171 * @return the latest source version that is fully supported
duke@1 172 */
duke@1 173 public static SourceVersion latestSupported() {
duke@1 174 return latestSupported;
duke@1 175 }
duke@1 176
duke@1 177 /**
duke@1 178 * Returns whether or not {@code name} is a syntactically valid
duke@1 179 * identifier (simple name) or keyword in the latest source
duke@1 180 * version. The method returns {@code true} if the name consists
duke@1 181 * of an initial character for which {@link
duke@1 182 * Character#isJavaIdentifierStart(int)} returns {@code true},
duke@1 183 * followed only by characters for which {@link
duke@1 184 * Character#isJavaIdentifierPart(int)} returns {@code true}.
duke@1 185 * This pattern matches regular identifiers, keywords, and the
duke@1 186 * literals {@code "true"}, {@code "false"}, and {@code "null"}.
duke@1 187 * The method returns {@code false} for all other strings.
duke@1 188 *
duke@1 189 * @param name the string to check
duke@1 190 * @return {@code true} if this string is a
duke@1 191 * syntactically valid identifier or keyword, {@code false}
duke@1 192 * otherwise.
duke@1 193 */
duke@1 194 public static boolean isIdentifier(CharSequence name) {
duke@1 195 String id = name.toString();
duke@1 196
duke@1 197 if (id.length() == 0) {
duke@1 198 return false;
duke@1 199 }
duke@1 200 int cp = id.codePointAt(0);
duke@1 201 if (!Character.isJavaIdentifierStart(cp)) {
duke@1 202 return false;
duke@1 203 }
duke@1 204 for (int i = Character.charCount(cp);
duke@1 205 i < id.length();
duke@1 206 i += Character.charCount(cp)) {
duke@1 207 cp = id.codePointAt(i);
duke@1 208 if (!Character.isJavaIdentifierPart(cp)) {
duke@1 209 return false;
duke@1 210 }
duke@1 211 }
duke@1 212 return true;
duke@1 213 }
duke@1 214
duke@1 215 /**
duke@1 216 * Returns whether or not {@code name} is a syntactically valid
duke@1 217 * qualified name in the latest source version. Unlike {@link
duke@1 218 * #isIdentifier isIdentifier}, this method returns {@code false}
duke@1 219 * for keywords and literals.
duke@1 220 *
duke@1 221 * @param name the string to check
duke@1 222 * @return {@code true} if this string is a
duke@1 223 * syntactically valid name, {@code false} otherwise.
jjh@972 224 * @jls 6.2 Names and Identifiers
duke@1 225 */
duke@1 226 public static boolean isName(CharSequence name) {
duke@1 227 String id = name.toString();
duke@1 228
duke@1 229 for(String s : id.split("\\.", -1)) {
duke@1 230 if (!isIdentifier(s) || isKeyword(s))
duke@1 231 return false;
duke@1 232 }
duke@1 233 return true;
duke@1 234 }
duke@1 235
duke@1 236 private final static Set<String> keywords;
duke@1 237 static {
duke@1 238 Set<String> s = new HashSet<String>();
duke@1 239 String [] kws = {
duke@1 240 "abstract", "continue", "for", "new", "switch",
duke@1 241 "assert", "default", "if", "package", "synchronized",
duke@1 242 "boolean", "do", "goto", "private", "this",
duke@1 243 "break", "double", "implements", "protected", "throw",
duke@1 244 "byte", "else", "import", "public", "throws",
duke@1 245 "case", "enum", "instanceof", "return", "transient",
duke@1 246 "catch", "extends", "int", "short", "try",
duke@1 247 "char", "final", "interface", "static", "void",
duke@1 248 "class", "finally", "long", "strictfp", "volatile",
duke@1 249 "const", "float", "native", "super", "while",
duke@1 250 // literals
duke@1 251 "null", "true", "false"
duke@1 252 };
duke@1 253 for(String kw : kws)
duke@1 254 s.add(kw);
duke@1 255 keywords = Collections.unmodifiableSet(s);
duke@1 256 }
duke@1 257
duke@1 258 /**
duke@1 259 * Returns whether or not {@code s} is a keyword or literal in the
duke@1 260 * latest source version.
duke@1 261 *
duke@1 262 * @param s the string to check
duke@1 263 * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise.
duke@1 264 */
duke@1 265 public static boolean isKeyword(CharSequence s) {
duke@1 266 String keywordOrLiteral = s.toString();
duke@1 267 return keywords.contains(keywordOrLiteral);
duke@1 268 }
duke@1 269 }

mercurial