1 /* 2 * Copyright (c) 2005, 2018, 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 */ 25 26 package javax.lang.model; 27 28 import java.util.Collections; 29 import java.util.Set; 30 import java.util.HashSet; 31 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 * 9: modules, small cleanups to 1.7 and 1.8 changes 59 * 10: local-variable type inference (var) 60 * 11: local-variable syntax for lambda parameters 61 * 12: TBD 62 */ 63 64 /** 65 * The original version. 66 * 67 * The language described in 68 * <cite>The Java™ Language Specification, First Edition</cite>. 69 */ 70 RELEASE_0, 71 72 /** 73 * The version recognized by the Java Platform 1.1. 74 * 75 * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to 76 * <cite>The Java™ Language Specification, First Edition</cite>. 77 */ 78 RELEASE_1, 79 80 /** 81 * The version recognized by the Java 2 Platform, Standard Edition, 82 * v 1.2. 83 * 84 * The language described in 85 * <cite>The Java™ Language Specification, 86 * Second Edition</cite>, which includes the {@code 87 * strictfp} modifier. 88 */ 89 RELEASE_2, 90 91 /** 92 * The version recognized by the Java 2 Platform, Standard Edition, 93 * v 1.3. 94 * 95 * No major changes from {@code RELEASE_2}. 96 */ 97 RELEASE_3, 98 99 /** 100 * The version recognized by the Java 2 Platform, Standard Edition, 101 * v 1.4. 102 * 103 * Added a simple assertion facility. 104 */ 105 RELEASE_4, 106 107 /** 108 * The version recognized by the Java 2 Platform, Standard 109 * Edition 5.0. 110 * 111 * The language described in 112 * <cite>The Java™ Language Specification, 113 * Third Edition</cite>. First release to support 114 * generics, annotations, autoboxing, var-args, enhanced {@code 115 * for} loop, and hexadecimal floating-point literals. 116 */ 117 RELEASE_5, 118 119 /** 120 * The version recognized by the Java Platform, Standard Edition 121 * 6. 122 * 123 * No major changes from {@code RELEASE_5}. 124 */ 125 RELEASE_6, 126 127 /** 128 * The version recognized by the Java Platform, Standard Edition 129 * 7. 130 * 131 * Additions in this release include, diamond syntax for 132 * constructors, {@code try}-with-resources, strings in switch, 133 * binary literals, and multi-catch. 134 * @since 1.7 135 */ 136 RELEASE_7, 137 138 /** 139 * The version recognized by the Java Platform, Standard Edition 140 * 8. 141 * 142 * Additions in this release include lambda expressions and default methods. 143 * @since 1.8 144 */ 145 RELEASE_8, 146 147 /** 148 * The version recognized by the Java Platform, Standard Edition 149 * 9. 150 * 151 * Additions in this release include modules and removal of a 152 * single underscore from the set of legal identifier names. 153 * 154 * @since 9 155 */ 156 RELEASE_9, 157 158 /** 159 * The version recognized by the Java Platform, Standard Edition 160 * 10. 161 * 162 * Additions in this release include local-variable type inference 163 * ({@code var}). 164 * 165 * @since 10 166 */ 167 RELEASE_10, 168 169 /** 170 * The version recognized by the Java Platform, Standard Edition 171 * 11. 172 * 173 * Additions in this release include local-variable syntax for 174 * lambda parameters. 175 * 176 * @since 11 177 */ 178 RELEASE_11, 179 180 /** 181 * The version recognized by the Java Platform, Standard Edition 182 * 12. 183 * 184 * @since 12 185 */ 186 RELEASE_12; 187 188 // Note that when adding constants for newer releases, the 189 // behavior of latest() and latestSupported() must be updated too. 190 191 /** 192 * Returns the latest source version that can be modeled. 193 * 194 * @return the latest source version that can be modeled 195 */ 196 public static SourceVersion latest() { 197 return RELEASE_12; 198 } 199 200 private static final SourceVersion latestSupported = getLatestSupported(); 201 202 private static SourceVersion getLatestSupported() { 203 try { 204 String specVersion = System.getProperty("java.specification.version"); 205 206 switch (specVersion) { 207 case "12": 208 return RELEASE_12; 209 case "11": 210 return RELEASE_11; 211 case "10": 212 return RELEASE_10; 213 case "9": 214 return RELEASE_9; 215 case "1.8": 216 return RELEASE_8; 217 case "1.7": 218 return RELEASE_7; 219 case "1.6": 220 return RELEASE_6; 221 } 222 } catch (SecurityException se) {} 223 224 return RELEASE_5; 225 } 226 227 /** 228 * Returns the latest source version fully supported by the 229 * current execution environment. {@code RELEASE_5} or later must 230 * be returned. 231 * 232 * @return the latest source version that is fully supported 233 */ 234 public static SourceVersion latestSupported() { 235 return latestSupported; 236 } 237 238 /** 239 * Returns whether or not {@code name} is a syntactically valid 240 * identifier (simple name) or keyword in the latest source 241 * version. The method returns {@code true} if the name consists 242 * of an initial character for which {@link 243 * Character#isJavaIdentifierStart(int)} returns {@code true}, 244 * followed only by characters for which {@link 245 * Character#isJavaIdentifierPart(int)} returns {@code true}. 246 * This pattern matches regular identifiers, keywords, restricted 247 * keywords, and the literals {@code "true"}, {@code "false"}, 248 * {@code "null"}, and {@code "var"}. 249 * 250 * The method returns {@code false} for all other strings. 251 * 252 * @param name the string to check 253 * @return {@code true} if this string is a 254 * syntactically valid identifier or keyword, {@code false} 255 * otherwise. 256 */ 257 public static boolean isIdentifier(CharSequence name) { 258 String id = name.toString(); 259 260 if (id.length() == 0) { 261 return false; 262 } 263 int cp = id.codePointAt(0); 264 if (!Character.isJavaIdentifierStart(cp)) { 265 return false; 266 } 267 for (int i = Character.charCount(cp); 268 i < id.length(); 269 i += Character.charCount(cp)) { 270 cp = id.codePointAt(i); 271 if (!Character.isJavaIdentifierPart(cp)) { 272 return false; 273 } 274 } 275 return true; 276 } 277 278 /** 279 * Returns whether or not {@code name} is a syntactically valid 280 * qualified name in the latest source version. Unlike {@link 281 * #isIdentifier isIdentifier}, this method returns {@code false} 282 * for keywords, boolean literals, and the null literal. 283 * 284 * This method returns {@code true} for <i>restricted 285 * keywords</i> and {@code "var"}. 286 * 287 * @param name the string to check 288 * @return {@code true} if this string is a 289 * syntactically valid name, {@code false} otherwise. 290 * @jls 3.9 Keywords 291 * @jls 6.2 Names and Identifiers 292 */ 293 public static boolean isName(CharSequence name) { 294 return isName(name, latest()); 295 } 296 297 /** 298 * Returns whether or not {@code name} is a syntactically valid 299 * qualified name in the given source version. Unlike {@link 300 * #isIdentifier isIdentifier}, this method returns {@code false} 301 * for keywords, boolean literals, and the null literal. 302 * 303 * This method returns {@code true} for <i>restricted 304 * keywords</i> and {@code "var"}. 305 * 306 * @param name the string to check 307 * @param version the version to use 308 * @return {@code true} if this string is a 309 * syntactically valid name, {@code false} otherwise. 310 * @jls 3.9 Keywords 311 * @jls 6.2 Names and Identifiers 312 * @since 9 313 */ 314 public static boolean isName(CharSequence name, SourceVersion version) { 315 String id = name.toString(); 316 317 for(String s : id.split("\\.", -1)) { 318 if (!isIdentifier(s) || isKeyword(s, version)) 319 return false; 320 } 321 return true; 322 } 323 324 /** 325 * Returns whether or not {@code s} is a keyword, boolean literal, 326 * or null literal in the latest source version. 327 * This method returns {@code false} for <i>restricted 328 * keywords</i> and {@code "var"}. 329 * 330 * @param s the string to check 331 * @return {@code true} if {@code s} is a keyword, or boolean 332 * literal, or null literal, {@code false} otherwise. 333 * @jls 3.9 Keywords 334 * @jls 3.10.3 Boolean Literals 335 * @jls 3.10.7 The Null Literal 336 */ 337 public static boolean isKeyword(CharSequence s) { 338 return isKeyword(s, latest()); 339 } 340 341 /** 342 * Returns whether or not {@code s} is a keyword, boolean literal, 343 * or null literal in the given source version. 344 * This method returns {@code false} for <i>restricted 345 * keywords</i> and {@code "var"}. 346 * 347 * @param s the string to check 348 * @param version the version to use 349 * @return {@code true} if {@code s} is a keyword, or boolean 350 * literal, or null literal, {@code false} otherwise. 351 * @jls 3.9 Keywords 352 * @jls 3.10.3 Boolean Literals 353 * @jls 3.10.7 The Null Literal 354 * @since 9 355 */ 356 public static boolean isKeyword(CharSequence s, SourceVersion version) { 357 String id = s.toString(); 358 switch(id) { 359 // A trip through history 360 case "strictfp": 361 return version.compareTo(RELEASE_2) >= 0; 362 363 case "assert": 364 return version.compareTo(RELEASE_4) >= 0; 365 366 case "enum": 367 return version.compareTo(RELEASE_5) >= 0; 368 369 case "_": 370 return version.compareTo(RELEASE_9) >= 0; 371 372 // Keywords common across versions 373 374 // Modifiers 375 case "public": case "protected": case "private": 376 case "abstract": case "static": case "final": 377 case "transient": case "volatile": case "synchronized": 378 case "native": 379 380 // Declarations 381 case "class": case "interface": case "extends": 382 case "package": case "throws": case "implements": 383 384 // Primitive types and void 385 case "boolean": case "byte": case "char": 386 case "short": case "int": case "long": 387 case "float": case "double": 388 case "void": 389 390 // Control flow 391 case "if": case "else": 392 case "try": case "catch": case "finally": 393 case "do": case "while": 394 case "for": case "continue": 395 case "switch": case "case": case "default": 396 case "break": case "throw": case "return": 397 398 // Other keywords 399 case "this": case "new": case "super": 400 case "import": case "instanceof": 401 402 // Forbidden! 403 case "goto": case "const": 404 405 // literals 406 case "null": case "true": case "false": 407 return true; 408 409 default: 410 return false; 411 } 412 } 413 }