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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial