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