JDK-8194523 : Update javax.lang.model.SourceVersion for "var" name
  • Type: CSR
  • Component: core-libs
  • Sub-Component: javax.lang.model
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 10
  • Submitted: 2018-01-03
  • Updated: 2018-01-09
  • Resolved: 2018-01-04
Related Reports
CSR :  
Description
Summary
-------

The specifications of the name-related predicates in `SourceVersion` are updated to explicitly describe their handling of the string `"var"`.

Problem
-------

Local-variable type inference adds special handling to the name `"var"` in some contexts and `SourceVersion` should be updated accordingly.

Solution
--------

Treat `"var"` similarly to restricted keywords, that is, without any complicated checking. Fully precise determination of whether or not `"var"` can be used as a name requires both version-dependent and context-dependent checking.

Specification
-------------

public static boolean isIdentifier(CharSequence name):

    --- old/src/java.compiler/share/classes/javax/lang/model/SourceVersion.java	2018-01-02 19:10:45.130122929 -0800
    +++ new/src/java.compiler/share/classes/javax/lang/model/SourceVersion.java	2018-01-02 19:10:44.806122915 -0800
    @@ -230,8 +230,9 @@
          * followed only by characters for which {@link
          * Character#isJavaIdentifierPart(int)} returns {@code true}.
          * This pattern matches regular identifiers, keywords, restricted
    -     * keywords, and the literals {@code "true"}, {@code "false"}, and
    -     * {@code "null"}.
    +     * keywords, and the literals {@code "true"}, {@code "false"},
    +     * {@code "null"}, and {@code "var"}.
    +     *
          * The method returns {@code false} for all other strings.
          *
          * @param name the string to check


public static boolean isName(CharSequence name):

    @@ -265,8 +266,9 @@
          * qualified name in the latest source version.  Unlike {@link
          * #isIdentifier isIdentifier}, this method returns {@code false}
          * for keywords, boolean literals, and the null literal.
    +     *
          * This method returns {@code true} for <i>restricted
    -     * keywords</i>.
    +     * keywords</i> and {@code "var"}.
          *
          * @param name the string to check
          * @return {@code true} if this string is a

public static boolean isName(CharSequence name, SourceVersion version):

    @@ -283,8 +285,9 @@
          * qualified name in the given source version.  Unlike {@link
          * #isIdentifier isIdentifier}, this method returns {@code false}
          * for keywords, boolean literals, and the null literal.
    +     *
          * This method returns {@code true} for <i>restricted
    -     * keywords</i>.
    +     * keywords</i> and {@code "var"}.
          *
          * @param name the string to check
          * @param version the version to use

 public static boolean isKeyword(CharSequence s):

    @@ -308,7 +311,7 @@
          * Returns whether or not {@code s} is a keyword, boolean literal,
          * or null literal in the latest source version.
          * This method returns {@code false} for <i>restricted
    -     * keywords</i>.
    +     * keywords</i> and {@code "var"}.
          *
          * @param s the string to check
          * @return {@code true} if {@code s} is a keyword, or boolean

public static boolean isKeyword(CharSequence s, SourceVersion version):

    @@ -325,7 +328,7 @@
          * Returns whether or not {@code s} is a keyword, boolean literal,
          * or null literal in the given source version.
          * This method returns {@code false} for <i>restricted
    -     * keywords</i>.
    +     * keywords</i> and {@code "var"}.
          *
          * @param s the string to check
          * @param version the version to use


Comments
Thanks for clarifications.
09-01-2018

The defined treatment of "var" is independent of version, just as for restricted keywords.
08-01-2018

If i understand correctly, even if version >=10, isName("var") will return true. ? But the compiler treat this("var") separately based on the version and context. Something like restricted keywords?
08-01-2018

Krushnareddy, under this proposal the string "var" does not receive version-specific handling.
05-01-2018

The comment section of isName(CharSequence name, SourceVersion version): This method returns {@code true} for <i>restricted - * keywords</i>. + * keywords</i> and {@code "var"}. It might not be the case if the SourceVersion is 10 where "var" is treated separately. Hope this will be stated explicitly.
05-01-2018

Moving to approved.
04-01-2018

Style suggestion ... if it is worth inserting a blank line in the raw comment text, consider also adding <p> to create a similar vertical space in the generated API docs.
03-01-2018