aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.lang.model; aoqi@0: aoqi@0: import java.util.Collections; aoqi@0: import java.util.Set; aoqi@0: import java.util.HashSet; aoqi@0: aoqi@0: /** aoqi@0: * Source versions of the Java™ programming language. aoqi@0: * aoqi@0: * See the appropriate edition of aoqi@0: * The Java™ Language Specification aoqi@0: * for information about a particular source version. aoqi@0: * aoqi@0: *

Note that additional source version constants will be added to aoqi@0: * model future releases of the language. aoqi@0: * aoqi@0: * @author Joseph D. Darcy aoqi@0: * @author Scott Seligman aoqi@0: * @author Peter von der Ahé aoqi@0: * @since 1.6 aoqi@0: */ aoqi@0: public enum SourceVersion { aoqi@0: /* aoqi@0: * Summary of language evolution aoqi@0: * 1.1: nested classes aoqi@0: * 1.2: strictfp aoqi@0: * 1.3: no changes aoqi@0: * 1.4: assert aoqi@0: * 1.5: annotations, generics, autoboxing, var-args... aoqi@0: * 1.6: no changes aoqi@0: * 1.7: diamond syntax, try-with-resources, etc. aoqi@0: * 1.8: lambda expressions and default methods aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * The original version. aoqi@0: * aoqi@0: * The language described in aoqi@0: * The Java™ Language Specification, First Edition. aoqi@0: */ aoqi@0: RELEASE_0, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java Platform 1.1. aoqi@0: * aoqi@0: * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to aoqi@0: * The Java™ Language Specification, First Edition. aoqi@0: */ aoqi@0: RELEASE_1, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java 2 Platform, Standard Edition, aoqi@0: * v 1.2. aoqi@0: * aoqi@0: * The language described in aoqi@0: * The Java™ Language Specification, aoqi@0: * Second Edition, which includes the {@code aoqi@0: * strictfp} modifier. aoqi@0: */ aoqi@0: RELEASE_2, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java 2 Platform, Standard Edition, aoqi@0: * v 1.3. aoqi@0: * aoqi@0: * No major changes from {@code RELEASE_2}. aoqi@0: */ aoqi@0: RELEASE_3, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java 2 Platform, Standard Edition, aoqi@0: * v 1.4. aoqi@0: * aoqi@0: * Added a simple assertion facility. aoqi@0: */ aoqi@0: RELEASE_4, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java 2 Platform, Standard aoqi@0: * Edition 5.0. aoqi@0: * aoqi@0: * The language described in aoqi@0: * The Java™ Language Specification, aoqi@0: * Third Edition. First release to support aoqi@0: * generics, annotations, autoboxing, var-args, enhanced {@code aoqi@0: * for} loop, and hexadecimal floating-point literals. aoqi@0: */ aoqi@0: RELEASE_5, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java Platform, Standard Edition aoqi@0: * 6. aoqi@0: * aoqi@0: * No major changes from {@code RELEASE_5}. aoqi@0: */ aoqi@0: RELEASE_6, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java Platform, Standard Edition aoqi@0: * 7. aoqi@0: * aoqi@0: * Additions in this release include, diamond syntax for aoqi@0: * constructors, {@code try}-with-resources, strings in switch, aoqi@0: * binary literals, and multi-catch. aoqi@0: * @since 1.7 aoqi@0: */ aoqi@0: RELEASE_7, aoqi@0: aoqi@0: /** aoqi@0: * The version recognized by the Java Platform, Standard Edition aoqi@0: * 8. aoqi@0: * aoqi@0: * Additions in this release include lambda expressions and default methods. aoqi@0: * @since 1.8 aoqi@0: */ aoqi@0: RELEASE_8; aoqi@0: aoqi@0: // Note that when adding constants for newer releases, the aoqi@0: // behavior of latest() and latestSupported() must be updated too. aoqi@0: aoqi@0: /** aoqi@0: * Returns the latest source version that can be modeled. aoqi@0: * aoqi@0: * @return the latest source version that can be modeled aoqi@0: */ aoqi@0: public static SourceVersion latest() { aoqi@0: return RELEASE_8; aoqi@0: } aoqi@0: aoqi@0: private static final SourceVersion latestSupported = getLatestSupported(); aoqi@0: aoqi@0: private static SourceVersion getLatestSupported() { aoqi@0: try { aoqi@0: String specVersion = System.getProperty("java.specification.version"); aoqi@0: aoqi@0: if ("1.8".equals(specVersion)) aoqi@0: return RELEASE_8; aoqi@0: else if("1.7".equals(specVersion)) aoqi@0: return RELEASE_7; aoqi@0: else if("1.6".equals(specVersion)) aoqi@0: return RELEASE_6; aoqi@0: } catch (SecurityException se) {} aoqi@0: aoqi@0: return RELEASE_5; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the latest source version fully supported by the aoqi@0: * current execution environment. {@code RELEASE_5} or later must aoqi@0: * be returned. aoqi@0: * aoqi@0: * @return the latest source version that is fully supported aoqi@0: */ aoqi@0: public static SourceVersion latestSupported() { aoqi@0: return latestSupported; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns whether or not {@code name} is a syntactically valid aoqi@0: * identifier (simple name) or keyword in the latest source aoqi@0: * version. The method returns {@code true} if the name consists aoqi@0: * of an initial character for which {@link aoqi@0: * Character#isJavaIdentifierStart(int)} returns {@code true}, aoqi@0: * followed only by characters for which {@link aoqi@0: * Character#isJavaIdentifierPart(int)} returns {@code true}. aoqi@0: * This pattern matches regular identifiers, keywords, and the aoqi@0: * literals {@code "true"}, {@code "false"}, and {@code "null"}. aoqi@0: * The method returns {@code false} for all other strings. aoqi@0: * aoqi@0: * @param name the string to check aoqi@0: * @return {@code true} if this string is a aoqi@0: * syntactically valid identifier or keyword, {@code false} aoqi@0: * otherwise. aoqi@0: */ aoqi@0: public static boolean isIdentifier(CharSequence name) { aoqi@0: String id = name.toString(); aoqi@0: aoqi@0: if (id.length() == 0) { aoqi@0: return false; aoqi@0: } aoqi@0: int cp = id.codePointAt(0); aoqi@0: if (!Character.isJavaIdentifierStart(cp)) { aoqi@0: return false; aoqi@0: } aoqi@0: for (int i = Character.charCount(cp); aoqi@0: i < id.length(); aoqi@0: i += Character.charCount(cp)) { aoqi@0: cp = id.codePointAt(i); aoqi@0: if (!Character.isJavaIdentifierPart(cp)) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns whether or not {@code name} is a syntactically valid aoqi@0: * qualified name in the latest source version. Unlike {@link aoqi@0: * #isIdentifier isIdentifier}, this method returns {@code false} aoqi@0: * for keywords and literals. aoqi@0: * aoqi@0: * @param name the string to check aoqi@0: * @return {@code true} if this string is a aoqi@0: * syntactically valid name, {@code false} otherwise. aoqi@0: * @jls 6.2 Names and Identifiers aoqi@0: */ aoqi@0: public static boolean isName(CharSequence name) { aoqi@0: String id = name.toString(); aoqi@0: aoqi@0: for(String s : id.split("\\.", -1)) { aoqi@0: if (!isIdentifier(s) || isKeyword(s)) aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: private final static Set keywords; aoqi@0: static { aoqi@0: Set s = new HashSet(); aoqi@0: String [] kws = { aoqi@0: "abstract", "continue", "for", "new", "switch", aoqi@0: "assert", "default", "if", "package", "synchronized", aoqi@0: "boolean", "do", "goto", "private", "this", aoqi@0: "break", "double", "implements", "protected", "throw", aoqi@0: "byte", "else", "import", "public", "throws", aoqi@0: "case", "enum", "instanceof", "return", "transient", aoqi@0: "catch", "extends", "int", "short", "try", aoqi@0: "char", "final", "interface", "static", "void", aoqi@0: "class", "finally", "long", "strictfp", "volatile", aoqi@0: "const", "float", "native", "super", "while", aoqi@0: // literals aoqi@0: "null", "true", "false" aoqi@0: }; aoqi@0: for(String kw : kws) aoqi@0: s.add(kw); aoqi@0: keywords = Collections.unmodifiableSet(s); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns whether or not {@code s} is a keyword or literal in the aoqi@0: * latest source version. aoqi@0: * aoqi@0: * @param s the string to check aoqi@0: * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise. aoqi@0: */ aoqi@0: public static boolean isKeyword(CharSequence s) { aoqi@0: String keywordOrLiteral = s.toString(); aoqi@0: return keywords.contains(keywordOrLiteral); aoqi@0: } aoqi@0: }