< prev index next >

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

Print this page




  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: no changes (switch expressions were in preview)
  62      *  13: no changes (switch expressions and text blocks in preview)
  63      *  14: TBD
  64      */
  65 
  66     /**
  67      * The original version.
  68      *
  69      * The language described in
  70      * <cite>The Java&trade; Language Specification, First Edition</cite>.
  71      */
  72     RELEASE_0,
  73 
  74     /**
  75      * The version recognized by the Java Platform 1.1.
  76      *
  77      * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to
  78      * <cite>The Java&trade; Language Specification, First Edition</cite>.
  79      */
  80     RELEASE_1,
  81 
  82     /**
  83      * The version recognized by the Java 2 Platform, Standard Edition,


 176      * lambda parameters.
 177      *
 178      * @since 11
 179      */
 180      RELEASE_11,
 181 
 182     /**
 183      * The version recognized by the Java Platform, Standard Edition
 184      * 12.
 185      *
 186      * @since 12
 187      */
 188      RELEASE_12,
 189 
 190     /**
 191      * The version recognized by the Java Platform, Standard Edition
 192      * 13.
 193      *
 194      * @since 13
 195      */
 196      RELEASE_13,
 197 
 198     /**
 199      * The version recognized by the Java Platform, Standard Edition
 200      * 14.
 201      *
 202      * @since 14
 203      */
 204      RELEASE_14;
 205 
 206     // Note that when adding constants for newer releases, the
 207     // behavior of latest() and latestSupported() must be updated too.
 208 
 209     /**
 210      * Returns the latest source version that can be modeled.
 211      *
 212      * @return the latest source version that can be modeled
 213      */
 214     public static SourceVersion latest() {
 215         return RELEASE_14;
 216     }
 217 
 218     private static final SourceVersion latestSupported = getLatestSupported();
 219 
 220     /*
 221      * The integer version to enum constant mapping implemented by
 222      * this method assumes the JEP 322: "Time-Based Release
 223      * Versioning" scheme is in effect. This scheme began in JDK
 224      * 10. If the JDK versioning scheme is revised, this method may
 225      * need to be updated accordingly.
 226      */
 227     private static SourceVersion getLatestSupported() {
 228         int intVersion = Runtime.version().feature();
 229         return (intVersion >= 11) ?
 230             valueOf("RELEASE_" + Math.min(14, intVersion)):
 231             RELEASE_10;
 232     }
 233 
 234     /**
 235      * Returns the latest source version fully supported by the
 236      * current execution environment.  {@code RELEASE_9} or later must
 237      * be returned.
 238      *
 239      * @apiNote This method is included alongside {@link latest} to
 240      * allow identification of situations where the language model API
 241      * is running on a platform version different than the latest
 242      * version modeled by the API. One way that sort of situation can
 243      * occur is if an IDE or similar tool is using the API to model
 244      * source version <i>N</i> while running on platform version
 245      * (<i>N</i>&nbsp;-&nbsp;1). Running in this configuration is
 246      * supported by the API. Running an API on platform versions
 247      * earlier than (<i>N</i>&nbsp;-&nbsp;1) or later than <i>N</i>
 248      * may or may not work as an implementation detail. If an
 249      * annotation processor was generating code to run under the
 250      * current execution environment, the processor should only use
 251      * platform features up to the {@code latestSupported} release,
 252      * which may be earlier than the {@code latest} release.
 253      *
 254      * @return the latest source version that is fully supported
 255      */
 256     public static SourceVersion latestSupported() {
 257         return latestSupported;
 258     }
 259 
 260     /**
 261      * Returns whether or not {@code name} is a syntactically valid
 262      * identifier (simple name) or keyword in the latest source
 263      * version.  The method returns {@code true} if the name consists
 264      * of an initial character for which {@link
 265      * Character#isJavaIdentifierStart(int)} returns {@code true},
 266      * followed only by characters for which {@link
 267      * Character#isJavaIdentifierPart(int)} returns {@code true}.
 268      * This pattern matches regular identifiers, keywords, restricted
 269      * keywords, restricted identifiers and the literals {@code "true"},
 270      * {@code "false"}, {@code "null"}.
 271      *
 272      * The method returns {@code false} for all other strings.
 273      *
 274      * @param name the string to check
 275      * @return {@code true} if this string is a
 276      * syntactically valid identifier or keyword, {@code false}
 277      * otherwise.
 278      */
 279     public static boolean isIdentifier(CharSequence name) {
 280         String id = name.toString();
 281 
 282         if (id.length() == 0) {
 283             return false;
 284         }
 285         int cp = id.codePointAt(0);
 286         if (!Character.isJavaIdentifierStart(cp)) {
 287             return false;
 288         }
 289         for (int i = Character.charCount(cp);
 290                 i < id.length();
 291                 i += Character.charCount(cp)) {
 292             cp = id.codePointAt(i);
 293             if (!Character.isJavaIdentifierPart(cp)) {
 294                 return false;
 295             }
 296         }
 297         return true;
 298     }
 299 
 300     /**
 301      * Returns whether or not {@code name} is a syntactically valid
 302      * qualified name in the latest source version.  Unlike {@link
 303      * #isIdentifier isIdentifier}, this method returns {@code false}
 304      * for keywords, boolean literals, and the null literal.
 305      *
 306      * This method returns {@code true} for <i>restricted
 307      * keywords</i> and <i>restricted identifiers</i>
 308      *
 309      * @param name the string to check
 310      * @return {@code true} if this string is a
 311      * syntactically valid name, {@code false} otherwise.
 312      * @jls 3.9 Keywords
 313      * @jls 6.2 Names and Identifiers
 314      */
 315     public static boolean isName(CharSequence name) {
 316         return isName(name, latest());
 317     }
 318 
 319     /**
 320      * Returns whether or not {@code name} is a syntactically valid
 321      * qualified name in the given source version.  Unlike {@link
 322      * #isIdentifier isIdentifier}, this method returns {@code false}
 323      * for keywords, boolean literals, and the null literal.
 324      *
 325      * This method returns {@code true} for <i>restricted
 326      * keywords</i> and <i>restricted identifiers</i>
 327      *
 328      * @param name the string to check
 329      * @param version the version to use
 330      * @return {@code true} if this string is a
 331      * syntactically valid name, {@code false} otherwise.
 332      * @jls 3.9 Keywords
 333      * @jls 6.2 Names and Identifiers
 334      * @since 9
 335      */
 336     public static boolean isName(CharSequence name, SourceVersion version) {
 337         String id = name.toString();
 338 
 339         for(String s : id.split("\\.", -1)) {
 340             if (!isIdentifier(s) || isKeyword(s, version))
 341                 return false;
 342         }
 343         return true;
 344     }
 345 
 346     /**
 347      * Returns whether or not {@code s} is a keyword, boolean literal,
 348      * or null literal in the latest source version.
 349      * This method returns {@code false} for <i>restricted
 350      * keywords</i> and <i>restricted identifiers</i>.
 351      *
 352      * @param s the string to check
 353      * @return {@code true} if {@code s} is a keyword, or boolean
 354      * literal, or null literal, {@code false} otherwise.
 355      * @jls 3.9 Keywords
 356      * @jls 3.10.3 Boolean Literals
 357      * @jls 3.10.7 The Null Literal
 358      */
 359     public static boolean isKeyword(CharSequence s) {
 360         return isKeyword(s, latest());
 361     }
 362 
 363     /**
 364      * Returns whether or not {@code s} is a keyword, boolean literal,
 365      * or null literal in the given source version.
 366      * This method returns {@code false} for <i>restricted
 367      * keywords</i> and <i>restricted identifiers</i>.
 368      *
 369      * @param s the string to check
 370      * @param version the version to use
 371      * @return {@code true} if {@code s} is a keyword, or boolean
 372      * literal, or null literal, {@code false} otherwise.
 373      * @jls 3.9 Keywords
 374      * @jls 3.10.3 Boolean Literals
 375      * @jls 3.10.7 The Null Literal
 376      * @since 9
 377      */
 378     public static boolean isKeyword(CharSequence s, SourceVersion version) {
 379         String id = s.toString();
 380         switch(id) {
 381             // A trip through history
 382         case "strictfp":
 383             return version.compareTo(RELEASE_2) >= 0;
 384 
 385         case "assert":
 386             return version.compareTo(RELEASE_4) >= 0;
 387 




  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: no changes (switch expressions in preview)
  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,


 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     /*
 212      * The integer version to enum constant mapping implemented by
 213      * this method assumes the JEP 322: "Time-Based Release
 214      * Versioning" scheme is in effect. This scheme began in JDK
 215      * 10. If the JDK versioning scheme is revised, this method may
 216      * need to be updated accordingly.
 217      */
 218     private static SourceVersion getLatestSupported() {
 219         int intVersion = Runtime.version().feature();
 220         return (intVersion >= 11) ?
 221             valueOf("RELEASE_" + Math.min(13, intVersion)):
 222             RELEASE_10;
 223     }
 224 
 225     /**
 226      * Returns the latest source version fully supported by the
 227      * current execution environment.  {@code RELEASE_9} or later must
 228      * be returned.
 229      *
 230      * @apiNote This method is included alongside {@link latest} to
 231      * allow identification of situations where the language model API
 232      * is running on a platform version different than the latest
 233      * version modeled by the API. One way that sort of situation can
 234      * occur is if an IDE or similar tool is using the API to model
 235      * source version <i>N</i> while running on platform version
 236      * (<i>N</i>&nbsp;-&nbsp;1). Running in this configuration is
 237      * supported by the API. Running an API on platform versions
 238      * earlier than (<i>N</i>&nbsp;-&nbsp;1) or later than <i>N</i>
 239      * may or may not work as an implementation detail. If an
 240      * annotation processor was generating code to run under the
 241      * current execution environment, the processor should only use
 242      * platform features up to the {@code latestSupported} release,
 243      * which may be earlier than the {@code latest} release.
 244      *
 245      * @return the latest source version that is fully supported
 246      */
 247     public static SourceVersion latestSupported() {
 248         return latestSupported;
 249     }
 250 
 251     /**
 252      * Returns whether or not {@code name} is a syntactically valid
 253      * identifier (simple name) or keyword in the latest source
 254      * version.  The method returns {@code true} if the name consists
 255      * of an initial character for which {@link
 256      * Character#isJavaIdentifierStart(int)} returns {@code true},
 257      * followed only by characters for which {@link
 258      * Character#isJavaIdentifierPart(int)} returns {@code true}.
 259      * This pattern matches regular identifiers, keywords, restricted
 260      * keywords, and the literals {@code "true"}, {@code "false"},
 261      * {@code "null"}, and {@code "var"}.
 262      *
 263      * The method returns {@code false} for all other strings.
 264      *
 265      * @param name the string to check
 266      * @return {@code true} if this string is a
 267      * syntactically valid identifier or keyword, {@code false}
 268      * otherwise.
 269      */
 270     public static boolean isIdentifier(CharSequence name) {
 271         String id = name.toString();
 272 
 273         if (id.length() == 0) {
 274             return false;
 275         }
 276         int cp = id.codePointAt(0);
 277         if (!Character.isJavaIdentifierStart(cp)) {
 278             return false;
 279         }
 280         for (int i = Character.charCount(cp);
 281                 i < id.length();
 282                 i += Character.charCount(cp)) {
 283             cp = id.codePointAt(i);
 284             if (!Character.isJavaIdentifierPart(cp)) {
 285                 return false;
 286             }
 287         }
 288         return true;
 289     }
 290 
 291     /**
 292      * Returns whether or not {@code name} is a syntactically valid
 293      * qualified name in the latest source version.  Unlike {@link
 294      * #isIdentifier isIdentifier}, this method returns {@code false}
 295      * for keywords, boolean literals, and the null literal.
 296      *
 297      * This method returns {@code true} for <i>restricted
 298      * keywords</i> and {@code "var"}.
 299      *
 300      * @param name the string to check
 301      * @return {@code true} if this string is a
 302      * syntactically valid name, {@code false} otherwise.
 303      * @jls 3.9 Keywords
 304      * @jls 6.2 Names and Identifiers
 305      */
 306     public static boolean isName(CharSequence name) {
 307         return isName(name, latest());
 308     }
 309 
 310     /**
 311      * Returns whether or not {@code name} is a syntactically valid
 312      * qualified name in the given source version.  Unlike {@link
 313      * #isIdentifier isIdentifier}, this method returns {@code false}
 314      * for keywords, boolean literals, and the null literal.
 315      *
 316      * This method returns {@code true} for <i>restricted
 317      * keywords</i> and {@code "var"}.
 318      *
 319      * @param name the string to check
 320      * @param version the version to use
 321      * @return {@code true} if this string is a
 322      * syntactically valid name, {@code false} otherwise.
 323      * @jls 3.9 Keywords
 324      * @jls 6.2 Names and Identifiers
 325      * @since 9
 326      */
 327     public static boolean isName(CharSequence name, SourceVersion version) {
 328         String id = name.toString();
 329 
 330         for(String s : id.split("\\.", -1)) {
 331             if (!isIdentifier(s) || isKeyword(s, version))
 332                 return false;
 333         }
 334         return true;
 335     }
 336 
 337     /**
 338      * Returns whether or not {@code s} is a keyword, boolean literal,
 339      * or null literal in the latest source version.
 340      * This method returns {@code false} for <i>restricted
 341      * keywords</i> and {@code "var"}.
 342      *
 343      * @param s the string to check
 344      * @return {@code true} if {@code s} is a keyword, or boolean
 345      * literal, or null literal, {@code false} otherwise.
 346      * @jls 3.9 Keywords
 347      * @jls 3.10.3 Boolean Literals
 348      * @jls 3.10.7 The Null Literal
 349      */
 350     public static boolean isKeyword(CharSequence s) {
 351         return isKeyword(s, latest());
 352     }
 353 
 354     /**
 355      * Returns whether or not {@code s} is a keyword, boolean literal,
 356      * or null literal in the given source version.
 357      * This method returns {@code false} for <i>restricted
 358      * keywords</i> and {@code "var"}.
 359      *
 360      * @param s the string to check
 361      * @param version the version to use
 362      * @return {@code true} if {@code s} is a keyword, or boolean
 363      * literal, or null literal, {@code false} otherwise.
 364      * @jls 3.9 Keywords
 365      * @jls 3.10.3 Boolean Literals
 366      * @jls 3.10.7 The Null Literal
 367      * @since 9
 368      */
 369     public static boolean isKeyword(CharSequence s, SourceVersion version) {
 370         String id = s.toString();
 371         switch(id) {
 372             // A trip through history
 373         case "strictfp":
 374             return version.compareTo(RELEASE_2) >= 0;
 375 
 376         case "assert":
 377             return version.compareTo(RELEASE_4) >= 0;
 378 


< prev index next >