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