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

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 1042
defdd98cb7ce
child 1867
065f8cb7bd89
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

     1 /*
     2  * Copyright (c) 2005, 2013, 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  */
    26 package javax.lang.model;
    28 import java.util.Collections;
    29 import java.util.Set;
    30 import java.util.HashSet;
    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      */
    58     /**
    59      * The original version.
    60      *
    61      * The language described in
    62      * <cite>The Java&trade; Language Specification, First Edition</cite>.
    63      */
    64     RELEASE_0,
    66     /**
    67      * The version recognized by the Java Platform 1.1.
    68      *
    69      * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to
    70      * <cite>The Java&trade; Language Specification, First Edition</cite>.
    71      */
    72     RELEASE_1,
    74     /**
    75      * The version recognized by the Java 2 Platform, Standard Edition,
    76      * v 1.2.
    77      *
    78      * The language described in
    79      * <cite>The Java&trade; Language Specification,
    80      * Second Edition</cite>, which includes the {@code
    81      * strictfp} modifier.
    82      */
    83     RELEASE_2,
    85     /**
    86      * The version recognized by the Java 2 Platform, Standard Edition,
    87      * v 1.3.
    88      *
    89      * No major changes from {@code RELEASE_2}.
    90      */
    91     RELEASE_3,
    93     /**
    94      * The version recognized by the Java 2 Platform, Standard Edition,
    95      * v 1.4.
    96      *
    97      * Added a simple assertion facility.
    98      */
    99     RELEASE_4,
   101     /**
   102      * The version recognized by the Java 2 Platform, Standard
   103      * Edition 5.0.
   104      *
   105      * The language described in
   106      * <cite>The Java&trade; Language Specification,
   107      * Third Edition</cite>.  First release to support
   108      * generics, annotations, autoboxing, var-args, enhanced {@code
   109      * for} loop, and hexadecimal floating-point literals.
   110      */
   111     RELEASE_5,
   113     /**
   114      * The version recognized by the Java Platform, Standard Edition
   115      * 6.
   116      *
   117      * No major changes from {@code RELEASE_5}.
   118      */
   119     RELEASE_6,
   121     /**
   122      * The version recognized by the Java Platform, Standard Edition
   123      * 7.
   124      *
   125      * @since 1.7
   126      */
   127     RELEASE_7,
   129     /**
   130      * The version recognized by the Java Platform, Standard Edition
   131      * 8.
   132      *
   133      * @since 1.8
   134      */
   135     RELEASE_8;
   137     // Note that when adding constants for newer releases, the
   138     // behavior of latest() and latestSupported() must be updated too.
   140     /**
   141      * Returns the latest source version that can be modeled.
   142      *
   143      * @return the latest source version that can be modeled
   144      */
   145     public static SourceVersion latest() {
   146         return RELEASE_8;
   147     }
   149     private static final SourceVersion latestSupported = getLatestSupported();
   151     private static SourceVersion getLatestSupported() {
   152         try {
   153             String specVersion = System.getProperty("java.specification.version");
   155             if ("1.8".equals(specVersion))
   156                 return RELEASE_8;
   157             else if("1.7".equals(specVersion))
   158                 return RELEASE_7;
   159             else if("1.6".equals(specVersion))
   160                 return RELEASE_6;
   161         } catch (SecurityException se) {}
   163         return RELEASE_5;
   164     }
   166     /**
   167      * Returns the latest source version fully supported by the
   168      * current execution environment.  {@code RELEASE_5} or later must
   169      * be returned.
   170      *
   171      * @return the latest source version that is fully supported
   172      */
   173     public static SourceVersion latestSupported() {
   174         return latestSupported;
   175     }
   177     /**
   178      * Returns whether or not {@code name} is a syntactically valid
   179      * identifier (simple name) or keyword in the latest source
   180      * version.  The method returns {@code true} if the name consists
   181      * of an initial character for which {@link
   182      * Character#isJavaIdentifierStart(int)} returns {@code true},
   183      * followed only by characters for which {@link
   184      * Character#isJavaIdentifierPart(int)} returns {@code true}.
   185      * This pattern matches regular identifiers, keywords, and the
   186      * literals {@code "true"}, {@code "false"}, and {@code "null"}.
   187      * The method returns {@code false} for all other strings.
   188      *
   189      * @param name the string to check
   190      * @return {@code true} if this string is a
   191      * syntactically valid identifier or keyword, {@code false}
   192      * otherwise.
   193      */
   194     public static boolean isIdentifier(CharSequence name) {
   195         String id = name.toString();
   197         if (id.length() == 0) {
   198             return false;
   199         }
   200         int cp = id.codePointAt(0);
   201         if (!Character.isJavaIdentifierStart(cp)) {
   202             return false;
   203         }
   204         for (int i = Character.charCount(cp);
   205                 i < id.length();
   206                 i += Character.charCount(cp)) {
   207             cp = id.codePointAt(i);
   208             if (!Character.isJavaIdentifierPart(cp)) {
   209                 return false;
   210             }
   211         }
   212         return true;
   213     }
   215     /**
   216      *  Returns whether or not {@code name} is a syntactically valid
   217      *  qualified name in the latest source version.  Unlike {@link
   218      *  #isIdentifier isIdentifier}, this method returns {@code false}
   219      *  for keywords and literals.
   220      *
   221      * @param name the string to check
   222      * @return {@code true} if this string is a
   223      * syntactically valid name, {@code false} otherwise.
   224      * @jls 6.2 Names and Identifiers
   225      */
   226     public static boolean isName(CharSequence name) {
   227         String id = name.toString();
   229         for(String s : id.split("\\.", -1)) {
   230             if (!isIdentifier(s) || isKeyword(s))
   231                 return false;
   232         }
   233         return true;
   234     }
   236     private final static Set<String> keywords;
   237     static {
   238         Set<String> s = new HashSet<String>();
   239         String [] kws = {
   240             "abstract", "continue",     "for",          "new",          "switch",
   241             "assert",   "default",      "if",           "package",      "synchronized",
   242             "boolean",  "do",           "goto",         "private",      "this",
   243             "break",    "double",       "implements",   "protected",    "throw",
   244             "byte",     "else",         "import",       "public",       "throws",
   245             "case",     "enum",         "instanceof",   "return",       "transient",
   246             "catch",    "extends",      "int",          "short",        "try",
   247             "char",     "final",        "interface",    "static",       "void",
   248             "class",    "finally",      "long",         "strictfp",     "volatile",
   249             "const",    "float",        "native",       "super",        "while",
   250             // literals
   251             "null",     "true",         "false"
   252         };
   253         for(String kw : kws)
   254             s.add(kw);
   255         keywords = Collections.unmodifiableSet(s);
   256     }
   258     /**
   259      *  Returns whether or not {@code s} is a keyword or literal in the
   260      *  latest source version.
   261      *
   262      * @param s the string to check
   263      * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise.
   264      */
   265     public static boolean isKeyword(CharSequence s) {
   266         String keywordOrLiteral = s.toString();
   267         return keywords.contains(keywordOrLiteral);
   268     }
   269 }

mercurial