Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/lang/model/SourceVersion.java
38890 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.lang.model;2627import java.util.Collections;28import java.util.Set;29import java.util.HashSet;3031/**32* Source versions of the Java™ programming language.33*34* See the appropriate edition of35* <cite>The Java™ Language Specification</cite>36* for information about a particular source version.37*38* <p>Note that additional source version constants will be added to39* model future releases of the language.40*41* @author Joseph D. Darcy42* @author Scott Seligman43* @author Peter von der Ahé44* @since 1.645*/46public enum SourceVersion {47/*48* Summary of language evolution49* 1.1: nested classes50* 1.2: strictfp51* 1.3: no changes52* 1.4: assert53* 1.5: annotations, generics, autoboxing, var-args...54* 1.6: no changes55* 1.7: diamond syntax, try-with-resources, etc.56* 1.8: lambda expressions and default methods57*/5859/**60* The original version.61*62* The language described in63* <cite>The Java™ Language Specification, First Edition</cite>.64*/65RELEASE_0,6667/**68* The version recognized by the Java Platform 1.1.69*70* The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to71* <cite>The Java™ Language Specification, First Edition</cite>.72*/73RELEASE_1,7475/**76* The version recognized by the Java 2 Platform, Standard Edition,77* v 1.2.78*79* The language described in80* <cite>The Java™ Language Specification,81* Second Edition</cite>, which includes the {@code82* strictfp} modifier.83*/84RELEASE_2,8586/**87* The version recognized by the Java 2 Platform, Standard Edition,88* v 1.3.89*90* No major changes from {@code RELEASE_2}.91*/92RELEASE_3,9394/**95* The version recognized by the Java 2 Platform, Standard Edition,96* v 1.4.97*98* Added a simple assertion facility.99*/100RELEASE_4,101102/**103* The version recognized by the Java 2 Platform, Standard104* Edition 5.0.105*106* The language described in107* <cite>The Java™ Language Specification,108* Third Edition</cite>. First release to support109* generics, annotations, autoboxing, var-args, enhanced {@code110* for} loop, and hexadecimal floating-point literals.111*/112RELEASE_5,113114/**115* The version recognized by the Java Platform, Standard Edition116* 6.117*118* No major changes from {@code RELEASE_5}.119*/120RELEASE_6,121122/**123* The version recognized by the Java Platform, Standard Edition124* 7.125*126* Additions in this release include, diamond syntax for127* constructors, {@code try}-with-resources, strings in switch,128* binary literals, and multi-catch.129* @since 1.7130*/131RELEASE_7,132133/**134* The version recognized by the Java Platform, Standard Edition135* 8.136*137* Additions in this release include lambda expressions and default methods.138* @since 1.8139*/140RELEASE_8;141142// Note that when adding constants for newer releases, the143// behavior of latest() and latestSupported() must be updated too.144145/**146* Returns the latest source version that can be modeled.147*148* @return the latest source version that can be modeled149*/150public static SourceVersion latest() {151return RELEASE_8;152}153154private static final SourceVersion latestSupported = getLatestSupported();155156private static SourceVersion getLatestSupported() {157try {158String specVersion = System.getProperty("java.specification.version");159160if ("1.8".equals(specVersion))161return RELEASE_8;162else if("1.7".equals(specVersion))163return RELEASE_7;164else if("1.6".equals(specVersion))165return RELEASE_6;166} catch (SecurityException se) {}167168return RELEASE_5;169}170171/**172* Returns the latest source version fully supported by the173* current execution environment. {@code RELEASE_5} or later must174* be returned.175*176* @return the latest source version that is fully supported177*/178public static SourceVersion latestSupported() {179return latestSupported;180}181182/**183* Returns whether or not {@code name} is a syntactically valid184* identifier (simple name) or keyword in the latest source185* version. The method returns {@code true} if the name consists186* of an initial character for which {@link187* Character#isJavaIdentifierStart(int)} returns {@code true},188* followed only by characters for which {@link189* Character#isJavaIdentifierPart(int)} returns {@code true}.190* This pattern matches regular identifiers, keywords, and the191* literals {@code "true"}, {@code "false"}, and {@code "null"}.192* The method returns {@code false} for all other strings.193*194* @param name the string to check195* @return {@code true} if this string is a196* syntactically valid identifier or keyword, {@code false}197* otherwise.198*/199public static boolean isIdentifier(CharSequence name) {200String id = name.toString();201202if (id.length() == 0) {203return false;204}205int cp = id.codePointAt(0);206if (!Character.isJavaIdentifierStart(cp)) {207return false;208}209for (int i = Character.charCount(cp);210i < id.length();211i += Character.charCount(cp)) {212cp = id.codePointAt(i);213if (!Character.isJavaIdentifierPart(cp)) {214return false;215}216}217return true;218}219220/**221* Returns whether or not {@code name} is a syntactically valid222* qualified name in the latest source version. Unlike {@link223* #isIdentifier isIdentifier}, this method returns {@code false}224* for keywords and literals.225*226* @param name the string to check227* @return {@code true} if this string is a228* syntactically valid name, {@code false} otherwise.229* @jls 6.2 Names and Identifiers230*/231public static boolean isName(CharSequence name) {232String id = name.toString();233234for(String s : id.split("\\.", -1)) {235if (!isIdentifier(s) || isKeyword(s))236return false;237}238return true;239}240241private final static Set<String> keywords;242static {243Set<String> s = new HashSet<String>();244String [] kws = {245"abstract", "continue", "for", "new", "switch",246"assert", "default", "if", "package", "synchronized",247"boolean", "do", "goto", "private", "this",248"break", "double", "implements", "protected", "throw",249"byte", "else", "import", "public", "throws",250"case", "enum", "instanceof", "return", "transient",251"catch", "extends", "int", "short", "try",252"char", "final", "interface", "static", "void",253"class", "finally", "long", "strictfp", "volatile",254"const", "float", "native", "super", "while",255// literals256"null", "true", "false"257};258for(String kw : kws)259s.add(kw);260keywords = Collections.unmodifiableSet(s);261}262263/**264* Returns whether or not {@code s} is a keyword or literal in the265* latest source version.266*267* @param s the string to check268* @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise.269*/270public static boolean isKeyword(CharSequence s) {271String keywordOrLiteral = s.toString();272return keywords.contains(keywordOrLiteral);273}274}275276277