langtools/src/share/classes/javax/lang/model/SourceVersion.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 22159 682da512ec17
child 22169 4be6dc44489b
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
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
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * The original version.
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 5520
diff changeset
    63
     * 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
    64
     * <cite>The Java&trade; Language Specification, First Edition</cite>.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    RELEASE_0,
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * The version recognized by the Java Platform 1.1.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 5520
diff changeset
    71
     * 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
    72
     * <cite>The Java&trade; Language Specification, First Edition</cite>.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    RELEASE_1,
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * The version recognized by the Java 2 Platform, Standard Edition,
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * v 1.2.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 5520
diff changeset
    80
     * 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
    81
     * <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
    82
     * Second Edition</cite>, which includes the {@code
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * strictfp} modifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    RELEASE_2,
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * The version recognized by the Java 2 Platform, Standard Edition,
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * v 1.3.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * No major changes from {@code RELEASE_2}.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    RELEASE_3,
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     * The version recognized by the Java 2 Platform, Standard Edition,
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     * v 1.4.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * Added a simple assertion facility.
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    RELEASE_4,
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * The version recognized by the Java 2 Platform, Standard
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     * Edition 5.0.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 5520
diff changeset
   107
     * 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
   108
     * <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
   109
     * Third Edition</cite>.  First release to support
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * generics, annotations, autoboxing, var-args, enhanced {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * for} loop, and hexadecimal floating-point literals.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    RELEASE_5,
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * The version recognized by the Java Platform, Standard Edition
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * 6.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * No major changes from {@code RELEASE_5}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    RELEASE_6,
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * The version recognized by the Java Platform, Standard Edition
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * 7.
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     *
18660
d8332e6f4fab 8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents: 15385
diff changeset
   127
     * 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
   128
     * 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
   129
     * binary literals, and multi-catch.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     * @since 1.7
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     */
10180
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   132
    RELEASE_7,
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   133
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   134
    /**
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   135
     * The version recognized by the Java Platform, Standard Edition
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   136
     * 8.
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   137
     *
18660
d8332e6f4fab 8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
darcy
parents: 15385
diff changeset
   138
     * Additions in this release include lambda expressions and default methods.
10180
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   139
     * @since 1.8
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   140
     */
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   141
    RELEASE_8;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    // Note that when adding constants for newer releases, the
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    // behavior of latest() and latestSupported() must be updated too.
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     * Returns the latest source version that can be modeled.
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     * @return the latest source version that can be modeled
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    public static SourceVersion latest() {
10180
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   152
        return RELEASE_8;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    private static final SourceVersion latestSupported = getLatestSupported();
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    private static SourceVersion getLatestSupported() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            String specVersion = System.getProperty("java.specification.version");
10180
b293c1f36ac4 7025784: Add SourceVersion.RELEASE_8
darcy
parents: 9303
diff changeset
   160
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   161
            switch (specVersion) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   162
                case "1.8":
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   163
                    return RELEASE_8;
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   164
                case "1.7":
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   165
                    return RELEASE_7;
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   166
                case "1.6":
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   167
                    return RELEASE_6;
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 18660
diff changeset
   168
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        } catch (SecurityException se) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        return RELEASE_5;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     * Returns the latest source version fully supported by the
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * current execution environment.  {@code RELEASE_5} or later must
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * be returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     * @return the latest source version that is fully supported
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public static SourceVersion latestSupported() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        return latestSupported;
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 whether or not {@code name} is a syntactically valid
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     * identifier (simple name) or keyword in the latest source
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     * version.  The method returns {@code true} if the name consists
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     * of an initial character for which {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     * Character#isJavaIdentifierStart(int)} returns {@code true},
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     * followed only by characters for which {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     * Character#isJavaIdentifierPart(int)} returns {@code true}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     * This pattern matches regular identifiers, keywords, and the
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     * literals {@code "true"}, {@code "false"}, and {@code "null"}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     * The method returns {@code false} for all other strings.
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     * @param name the string to check
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     * @return {@code true} if this string is a
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     * syntactically valid identifier or keyword, {@code false}
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    public static boolean isIdentifier(CharSequence name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        String id = name.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        if (id.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        int cp = id.codePointAt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        if (!Character.isJavaIdentifierStart(cp)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        for (int i = Character.charCount(cp);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                i < id.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                i += Character.charCount(cp)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            cp = id.codePointAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            if (!Character.isJavaIdentifierPart(cp)) {
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
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     *  Returns whether or not {@code name} is a syntactically valid
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     *  qualified name in the latest source version.  Unlike {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
     *  #isIdentifier isIdentifier}, this method returns {@code false}
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
     *  for keywords and literals.
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     * @param name the string to check
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     * @return {@code true} if this string is a
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     * 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
   232
     * @jls 6.2 Names and Identifiers
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    public static boolean isName(CharSequence name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        String id = name.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        for(String s : id.split("\\.", -1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            if (!isIdentifier(s) || isKeyword(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    private final static Set<String> keywords;
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    static {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   246
        Set<String> s = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        String [] kws = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            "abstract", "continue",     "for",          "new",          "switch",
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            "assert",   "default",      "if",           "package",      "synchronized",
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            "boolean",  "do",           "goto",         "private",      "this",
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            "break",    "double",       "implements",   "protected",    "throw",
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            "byte",     "else",         "import",       "public",       "throws",
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            "case",     "enum",         "instanceof",   "return",       "transient",
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            "catch",    "extends",      "int",          "short",        "try",
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            "char",     "final",        "interface",    "static",       "void",
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            "class",    "finally",      "long",         "strictfp",     "volatile",
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            "const",    "float",        "native",       "super",        "while",
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            // literals
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            "null",     "true",         "false"
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        for(String kw : kws)
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            s.add(kw);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        keywords = Collections.unmodifiableSet(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     *  Returns whether or not {@code s} is a keyword or literal in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     *  latest source version.
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     * @param s the string to check
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    public static boolean isKeyword(CharSequence s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        String keywordOrLiteral = s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        return keywords.contains(keywordOrLiteral);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
}