0N/A/*
553N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage javax.lang.model;
0N/A
0N/Aimport java.util.Collections;
0N/Aimport java.util.Set;
0N/Aimport java.util.HashSet;
0N/A
0N/A/**
0N/A * Source versions of the Java™ programming language.
0N/A *
971N/A * See the appropriate edition of
971N/A * <cite>The Java&trade; Language Specification</cite>
971N/A * for information about a particular source version.
0N/A *
0N/A * <p>Note that additional source version constants will be added to
0N/A * model future releases of the language.
0N/A *
0N/A * @author Joseph D. Darcy
0N/A * @author Scott Seligman
0N/A * @author Peter von der Ah&eacute;
0N/A * @since 1.6
0N/A */
0N/Apublic enum SourceVersion {
0N/A /*
0N/A * Summary of language evoluation
0N/A * 1.1: nested classes
0N/A * 1.2: strictfp
0N/A * 1.3: no changes
0N/A * 1.4: assert
0N/A * 1.5: annotations, generics, autoboxing, var-args...
0N/A * 1.6: no changes
0N/A */
0N/A
0N/A /**
0N/A * The original version.
0N/A *
971N/A * The language described in
971N/A * <cite>The Java&trade; Language Specification, First Edition</cite>.
0N/A */
0N/A RELEASE_0,
0N/A
0N/A /**
0N/A * The version recognized by the Java Platform 1.1.
0N/A *
971N/A * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to
971N/A * <cite>The Java&trade; Language Specification, First Edition</cite>.
0N/A */
0N/A RELEASE_1,
0N/A
0N/A /**
0N/A * The version recognized by the Java 2 Platform, Standard Edition,
0N/A * v 1.2.
0N/A *
971N/A * The language described in
971N/A * <cite>The Java&trade; Language Specification,
971N/A * Second Edition</cite>, which includes the {@code
0N/A * strictfp} modifier.
0N/A */
0N/A RELEASE_2,
0N/A
0N/A /**
0N/A * The version recognized by the Java 2 Platform, Standard Edition,
0N/A * v 1.3.
0N/A *
0N/A * No major changes from {@code RELEASE_2}.
0N/A */
0N/A RELEASE_3,
0N/A
0N/A /**
0N/A * The version recognized by the Java 2 Platform, Standard Edition,
0N/A * v 1.4.
0N/A *
0N/A * Added a simple assertion facility.
0N/A */
0N/A RELEASE_4,
0N/A
0N/A /**
0N/A * The version recognized by the Java 2 Platform, Standard
0N/A * Edition 5.0.
0N/A *
971N/A * The language described in
971N/A * <cite>The Java&trade; Language Specification,
971N/A * Third Edition</cite>. First release to support
0N/A * generics, annotations, autoboxing, var-args, enhanced {@code
0N/A * for} loop, and hexadecimal floating-point literals.
0N/A */
0N/A RELEASE_5,
0N/A
0N/A /**
0N/A * The version recognized by the Java Platform, Standard Edition
0N/A * 6.
0N/A *
0N/A * No major changes from {@code RELEASE_5}.
0N/A */
0N/A RELEASE_6,
0N/A
0N/A /**
0N/A * The version recognized by the Java Platform, Standard Edition
0N/A * 7.
0N/A *
0N/A * @since 1.7
0N/A */
0N/A RELEASE_7;
0N/A
0N/A // Note that when adding constants for newer releases, the
0N/A // behavior of latest() and latestSupported() must be updated too.
0N/A
0N/A /**
0N/A * Returns the latest source version that can be modeled.
0N/A *
0N/A * @return the latest source version that can be modeled
0N/A */
0N/A public static SourceVersion latest() {
0N/A return RELEASE_7;
0N/A }
0N/A
0N/A private static final SourceVersion latestSupported = getLatestSupported();
0N/A
0N/A private static SourceVersion getLatestSupported() {
0N/A try {
0N/A String specVersion = System.getProperty("java.specification.version");
0N/A if ("1.7".equals(specVersion))
0N/A return RELEASE_7;
0N/A else if ("1.6".equals(specVersion))
0N/A return RELEASE_6;
0N/A } catch (SecurityException se) {}
0N/A
0N/A return RELEASE_5;
0N/A }
0N/A
0N/A /**
0N/A * Returns the latest source version fully supported by the
0N/A * current execution environment. {@code RELEASE_5} or later must
0N/A * be returned.
0N/A *
0N/A * @return the latest source version that is fully supported
0N/A */
0N/A public static SourceVersion latestSupported() {
0N/A return latestSupported;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not {@code name} is a syntactically valid
0N/A * identifier (simple name) or keyword in the latest source
0N/A * version. The method returns {@code true} if the name consists
0N/A * of an initial character for which {@link
0N/A * Character#isJavaIdentifierStart(int)} returns {@code true},
0N/A * followed only by characters for which {@link
0N/A * Character#isJavaIdentifierPart(int)} returns {@code true}.
0N/A * This pattern matches regular identifiers, keywords, and the
0N/A * literals {@code "true"}, {@code "false"}, and {@code "null"}.
0N/A * The method returns {@code false} for all other strings.
0N/A *
0N/A * @param name the string to check
0N/A * @return {@code true} if this string is a
0N/A * syntactically valid identifier or keyword, {@code false}
0N/A * otherwise.
0N/A */
0N/A public static boolean isIdentifier(CharSequence name) {
0N/A String id = name.toString();
0N/A
0N/A if (id.length() == 0) {
0N/A return false;
0N/A }
0N/A int cp = id.codePointAt(0);
0N/A if (!Character.isJavaIdentifierStart(cp)) {
0N/A return false;
0N/A }
0N/A for (int i = Character.charCount(cp);
0N/A i < id.length();
0N/A i += Character.charCount(cp)) {
0N/A cp = id.codePointAt(i);
0N/A if (!Character.isJavaIdentifierPart(cp)) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not {@code name} is a syntactically valid
0N/A * qualified name in the latest source version. Unlike {@link
0N/A * #isIdentifier isIdentifier}, this method returns {@code false}
0N/A * for keywords and literals.
0N/A *
0N/A * @param name the string to check
0N/A * @return {@code true} if this string is a
0N/A * syntactically valid name, {@code false} otherwise.
971N/A * @jls 6.2 Names and Identifiers
0N/A */
0N/A public static boolean isName(CharSequence name) {
0N/A String id = name.toString();
0N/A
0N/A for(String s : id.split("\\.", -1)) {
0N/A if (!isIdentifier(s) || isKeyword(s))
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A private final static Set<String> keywords;
0N/A static {
0N/A Set<String> s = new HashSet<String>();
0N/A String [] kws = {
0N/A "abstract", "continue", "for", "new", "switch",
0N/A "assert", "default", "if", "package", "synchronized",
0N/A "boolean", "do", "goto", "private", "this",
0N/A "break", "double", "implements", "protected", "throw",
0N/A "byte", "else", "import", "public", "throws",
0N/A "case", "enum", "instanceof", "return", "transient",
0N/A "catch", "extends", "int", "short", "try",
0N/A "char", "final", "interface", "static", "void",
0N/A "class", "finally", "long", "strictfp", "volatile",
0N/A "const", "float", "native", "super", "while",
0N/A // literals
0N/A "null", "true", "false"
0N/A };
0N/A for(String kw : kws)
0N/A s.add(kw);
0N/A keywords = Collections.unmodifiableSet(s);
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not {@code s} is a keyword or literal in the
0N/A * latest source version.
0N/A *
0N/A * @param s the string to check
0N/A * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise.
0N/A */
0N/A public static boolean isKeyword(CharSequence s) {
0N/A String keywordOrLiteral = s.toString();
0N/A return keywords.contains(keywordOrLiteral);
0N/A }
0N/A}