Tue, 25 May 2010 15:54:51 -0700
6943119: Rebrand source copyright notices
Reviewed-by: darcy
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package javax.lang.model; |
duke@1 | 27 | |
duke@1 | 28 | import java.util.Collections; |
duke@1 | 29 | import java.util.Set; |
duke@1 | 30 | import java.util.HashSet; |
duke@1 | 31 | |
duke@1 | 32 | /** |
duke@1 | 33 | * Source versions of the Java™ programming language. |
duke@1 | 34 | * |
duke@1 | 35 | * See <a |
duke@1 | 36 | * href="http://java.sun.com/docs/books/jls/">http://java.sun.com/docs/books/jls/</a> |
duke@1 | 37 | * for information on editions of <i>The Java™ Language |
duke@1 | 38 | * Specification</i>, including updates and clarifications. |
duke@1 | 39 | * |
duke@1 | 40 | * <p>Note that additional source version constants will be added to |
duke@1 | 41 | * model future releases of the language. |
duke@1 | 42 | * |
duke@1 | 43 | * @author Joseph D. Darcy |
duke@1 | 44 | * @author Scott Seligman |
duke@1 | 45 | * @author Peter von der Ahé |
duke@1 | 46 | * @since 1.6 |
duke@1 | 47 | */ |
duke@1 | 48 | public enum SourceVersion { |
duke@1 | 49 | /* |
duke@1 | 50 | * Summary of language evoluation |
duke@1 | 51 | * 1.1: nested classes |
duke@1 | 52 | * 1.2: strictfp |
duke@1 | 53 | * 1.3: no changes |
duke@1 | 54 | * 1.4: assert |
duke@1 | 55 | * 1.5: annotations, generics, autoboxing, var-args... |
duke@1 | 56 | * 1.6: no changes |
duke@1 | 57 | */ |
duke@1 | 58 | |
duke@1 | 59 | /** |
duke@1 | 60 | * The original version. |
duke@1 | 61 | * |
duke@1 | 62 | * The language described in the first edition of <i>The |
duke@1 | 63 | * Java™ Language Specification</i>. |
duke@1 | 64 | */ |
duke@1 | 65 | RELEASE_0, |
duke@1 | 66 | |
duke@1 | 67 | /** |
duke@1 | 68 | * The version recognized by the Java Platform 1.1. |
duke@1 | 69 | * |
duke@1 | 70 | * The language is {@code RELEASE_0} <a |
duke@1 | 71 | * href="http://java.sun.com/docs/books/jls/first_edition/html/1.1Update.html">augmented</a> |
duke@1 | 72 | * with nested classes. |
duke@1 | 73 | */ |
duke@1 | 74 | RELEASE_1, |
duke@1 | 75 | |
duke@1 | 76 | /** |
duke@1 | 77 | * The version recognized by the Java 2 Platform, Standard Edition, |
duke@1 | 78 | * v 1.2. |
duke@1 | 79 | * |
duke@1 | 80 | * The language described in <i>The Java™ Language |
duke@1 | 81 | * Specification, Second Edition</i>, which includes the {@code |
duke@1 | 82 | * strictfp} modifier. |
duke@1 | 83 | */ |
duke@1 | 84 | RELEASE_2, |
duke@1 | 85 | |
duke@1 | 86 | /** |
duke@1 | 87 | * The version recognized by the Java 2 Platform, Standard Edition, |
duke@1 | 88 | * v 1.3. |
duke@1 | 89 | * |
duke@1 | 90 | * No major changes from {@code RELEASE_2}. |
duke@1 | 91 | */ |
duke@1 | 92 | RELEASE_3, |
duke@1 | 93 | |
duke@1 | 94 | /** |
duke@1 | 95 | * The version recognized by the Java 2 Platform, Standard Edition, |
duke@1 | 96 | * v 1.4. |
duke@1 | 97 | * |
duke@1 | 98 | * Added a simple assertion facility. |
duke@1 | 99 | */ |
duke@1 | 100 | RELEASE_4, |
duke@1 | 101 | |
duke@1 | 102 | /** |
duke@1 | 103 | * The version recognized by the Java 2 Platform, Standard |
duke@1 | 104 | * Edition 5.0. |
duke@1 | 105 | * |
duke@1 | 106 | * The language described in <i>The Java™ Language |
duke@1 | 107 | * Specification, Third Edition</i>. First release to support |
duke@1 | 108 | * generics, annotations, autoboxing, var-args, enhanced {@code |
duke@1 | 109 | * for} loop, and hexadecimal floating-point literals. |
duke@1 | 110 | */ |
duke@1 | 111 | RELEASE_5, |
duke@1 | 112 | |
duke@1 | 113 | /** |
duke@1 | 114 | * The version recognized by the Java Platform, Standard Edition |
duke@1 | 115 | * 6. |
duke@1 | 116 | * |
duke@1 | 117 | * No major changes from {@code RELEASE_5}. |
duke@1 | 118 | */ |
duke@1 | 119 | RELEASE_6, |
duke@1 | 120 | |
duke@1 | 121 | /** |
duke@1 | 122 | * The version recognized by the Java Platform, Standard Edition |
duke@1 | 123 | * 7. |
duke@1 | 124 | * |
duke@1 | 125 | * @since 1.7 |
duke@1 | 126 | */ |
duke@1 | 127 | RELEASE_7; |
duke@1 | 128 | |
duke@1 | 129 | // Note that when adding constants for newer releases, the |
duke@1 | 130 | // behavior of latest() and latestSupported() must be updated too. |
duke@1 | 131 | |
duke@1 | 132 | /** |
duke@1 | 133 | * Returns the latest source version that can be modeled. |
duke@1 | 134 | * |
duke@1 | 135 | * @return the latest source version that can be modeled |
duke@1 | 136 | */ |
duke@1 | 137 | public static SourceVersion latest() { |
duke@1 | 138 | return RELEASE_7; |
duke@1 | 139 | } |
duke@1 | 140 | |
duke@1 | 141 | private static final SourceVersion latestSupported = getLatestSupported(); |
duke@1 | 142 | |
duke@1 | 143 | private static SourceVersion getLatestSupported() { |
duke@1 | 144 | try { |
duke@1 | 145 | String specVersion = System.getProperty("java.specification.version"); |
duke@1 | 146 | if ("1.7".equals(specVersion)) |
duke@1 | 147 | return RELEASE_7; |
duke@1 | 148 | else if ("1.6".equals(specVersion)) |
duke@1 | 149 | return RELEASE_6; |
duke@1 | 150 | } catch (SecurityException se) {} |
duke@1 | 151 | |
duke@1 | 152 | return RELEASE_5; |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | /** |
duke@1 | 156 | * Returns the latest source version fully supported by the |
duke@1 | 157 | * current execution environment. {@code RELEASE_5} or later must |
duke@1 | 158 | * be returned. |
duke@1 | 159 | * |
duke@1 | 160 | * @return the latest source version that is fully supported |
duke@1 | 161 | */ |
duke@1 | 162 | public static SourceVersion latestSupported() { |
duke@1 | 163 | return latestSupported; |
duke@1 | 164 | } |
duke@1 | 165 | |
duke@1 | 166 | /** |
duke@1 | 167 | * Returns whether or not {@code name} is a syntactically valid |
duke@1 | 168 | * identifier (simple name) or keyword in the latest source |
duke@1 | 169 | * version. The method returns {@code true} if the name consists |
duke@1 | 170 | * of an initial character for which {@link |
duke@1 | 171 | * Character#isJavaIdentifierStart(int)} returns {@code true}, |
duke@1 | 172 | * followed only by characters for which {@link |
duke@1 | 173 | * Character#isJavaIdentifierPart(int)} returns {@code true}. |
duke@1 | 174 | * This pattern matches regular identifiers, keywords, and the |
duke@1 | 175 | * literals {@code "true"}, {@code "false"}, and {@code "null"}. |
duke@1 | 176 | * The method returns {@code false} for all other strings. |
duke@1 | 177 | * |
duke@1 | 178 | * @param name the string to check |
duke@1 | 179 | * @return {@code true} if this string is a |
duke@1 | 180 | * syntactically valid identifier or keyword, {@code false} |
duke@1 | 181 | * otherwise. |
duke@1 | 182 | */ |
duke@1 | 183 | public static boolean isIdentifier(CharSequence name) { |
duke@1 | 184 | String id = name.toString(); |
duke@1 | 185 | |
duke@1 | 186 | if (id.length() == 0) { |
duke@1 | 187 | return false; |
duke@1 | 188 | } |
duke@1 | 189 | int cp = id.codePointAt(0); |
duke@1 | 190 | if (!Character.isJavaIdentifierStart(cp)) { |
duke@1 | 191 | return false; |
duke@1 | 192 | } |
duke@1 | 193 | for (int i = Character.charCount(cp); |
duke@1 | 194 | i < id.length(); |
duke@1 | 195 | i += Character.charCount(cp)) { |
duke@1 | 196 | cp = id.codePointAt(i); |
duke@1 | 197 | if (!Character.isJavaIdentifierPart(cp)) { |
duke@1 | 198 | return false; |
duke@1 | 199 | } |
duke@1 | 200 | } |
duke@1 | 201 | return true; |
duke@1 | 202 | } |
duke@1 | 203 | |
duke@1 | 204 | /** |
duke@1 | 205 | * Returns whether or not {@code name} is a syntactically valid |
duke@1 | 206 | * qualified name in the latest source version. Unlike {@link |
duke@1 | 207 | * #isIdentifier isIdentifier}, this method returns {@code false} |
duke@1 | 208 | * for keywords and literals. |
duke@1 | 209 | * |
duke@1 | 210 | * @param name the string to check |
duke@1 | 211 | * @return {@code true} if this string is a |
duke@1 | 212 | * syntactically valid name, {@code false} otherwise. |
duke@1 | 213 | * @jls3 6.2 Names and Identifiers |
duke@1 | 214 | */ |
duke@1 | 215 | public static boolean isName(CharSequence name) { |
duke@1 | 216 | String id = name.toString(); |
duke@1 | 217 | |
duke@1 | 218 | for(String s : id.split("\\.", -1)) { |
duke@1 | 219 | if (!isIdentifier(s) || isKeyword(s)) |
duke@1 | 220 | return false; |
duke@1 | 221 | } |
duke@1 | 222 | return true; |
duke@1 | 223 | } |
duke@1 | 224 | |
duke@1 | 225 | private final static Set<String> keywords; |
duke@1 | 226 | static { |
duke@1 | 227 | Set<String> s = new HashSet<String>(); |
duke@1 | 228 | String [] kws = { |
duke@1 | 229 | "abstract", "continue", "for", "new", "switch", |
duke@1 | 230 | "assert", "default", "if", "package", "synchronized", |
duke@1 | 231 | "boolean", "do", "goto", "private", "this", |
duke@1 | 232 | "break", "double", "implements", "protected", "throw", |
duke@1 | 233 | "byte", "else", "import", "public", "throws", |
duke@1 | 234 | "case", "enum", "instanceof", "return", "transient", |
duke@1 | 235 | "catch", "extends", "int", "short", "try", |
duke@1 | 236 | "char", "final", "interface", "static", "void", |
duke@1 | 237 | "class", "finally", "long", "strictfp", "volatile", |
duke@1 | 238 | "const", "float", "native", "super", "while", |
duke@1 | 239 | // literals |
duke@1 | 240 | "null", "true", "false" |
duke@1 | 241 | }; |
duke@1 | 242 | for(String kw : kws) |
duke@1 | 243 | s.add(kw); |
duke@1 | 244 | keywords = Collections.unmodifiableSet(s); |
duke@1 | 245 | } |
duke@1 | 246 | |
duke@1 | 247 | /** |
duke@1 | 248 | * Returns whether or not {@code s} is a keyword or literal in the |
duke@1 | 249 | * latest source version. |
duke@1 | 250 | * |
duke@1 | 251 | * @param s the string to check |
duke@1 | 252 | * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise. |
duke@1 | 253 | */ |
duke@1 | 254 | public static boolean isKeyword(CharSequence s) { |
duke@1 | 255 | String keywordOrLiteral = s.toString(); |
duke@1 | 256 | return keywords.contains(keywordOrLiteral); |
duke@1 | 257 | } |
duke@1 | 258 | } |