Thu, 27 Jun 2013 11:46:23 -0700
8019308: Add descriptions of Java SE 7 and 8 language changes to SourceVersion
Reviewed-by: jjg
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 2005, 2013, 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 | * |
jjh@972 | 35 | * See the appropriate edition of |
jjh@972 | 36 | * <cite>The Java™ Language Specification</cite> |
jjh@972 | 37 | * for information about a particular source version. |
duke@1 | 38 | * |
duke@1 | 39 | * <p>Note that additional source version constants will be added to |
duke@1 | 40 | * model future releases of the language. |
duke@1 | 41 | * |
duke@1 | 42 | * @author Joseph D. Darcy |
duke@1 | 43 | * @author Scott Seligman |
duke@1 | 44 | * @author Peter von der Ahé |
duke@1 | 45 | * @since 1.6 |
duke@1 | 46 | */ |
duke@1 | 47 | public enum SourceVersion { |
duke@1 | 48 | /* |
jjg@1521 | 49 | * Summary of language evolution |
duke@1 | 50 | * 1.1: nested classes |
duke@1 | 51 | * 1.2: strictfp |
duke@1 | 52 | * 1.3: no changes |
duke@1 | 53 | * 1.4: assert |
duke@1 | 54 | * 1.5: annotations, generics, autoboxing, var-args... |
duke@1 | 55 | * 1.6: no changes |
darcy@1867 | 56 | * 1.7: diamond syntax, try-with-resources, etc. |
darcy@1867 | 57 | * 1.8: lambda expressions and default methods |
duke@1 | 58 | */ |
duke@1 | 59 | |
duke@1 | 60 | /** |
duke@1 | 61 | * The original version. |
duke@1 | 62 | * |
jjh@972 | 63 | * The language described in |
jjh@972 | 64 | * <cite>The Java™ Language Specification, First Edition</cite>. |
duke@1 | 65 | */ |
duke@1 | 66 | RELEASE_0, |
duke@1 | 67 | |
duke@1 | 68 | /** |
duke@1 | 69 | * The version recognized by the Java Platform 1.1. |
duke@1 | 70 | * |
jjh@972 | 71 | * The language is {@code RELEASE_0} augmented with nested classes as described in the 1.1 update to |
jjh@972 | 72 | * <cite>The Java™ Language Specification, First Edition</cite>. |
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 | * |
jjh@972 | 80 | * The language described in |
jjh@972 | 81 | * <cite>The Java™ Language Specification, |
jjh@972 | 82 | * Second Edition</cite>, which includes the {@code |
duke@1 | 83 | * strictfp} modifier. |
duke@1 | 84 | */ |
duke@1 | 85 | RELEASE_2, |
duke@1 | 86 | |
duke@1 | 87 | /** |
duke@1 | 88 | * The version recognized by the Java 2 Platform, Standard Edition, |
duke@1 | 89 | * v 1.3. |
duke@1 | 90 | * |
duke@1 | 91 | * No major changes from {@code RELEASE_2}. |
duke@1 | 92 | */ |
duke@1 | 93 | RELEASE_3, |
duke@1 | 94 | |
duke@1 | 95 | /** |
duke@1 | 96 | * The version recognized by the Java 2 Platform, Standard Edition, |
duke@1 | 97 | * v 1.4. |
duke@1 | 98 | * |
duke@1 | 99 | * Added a simple assertion facility. |
duke@1 | 100 | */ |
duke@1 | 101 | RELEASE_4, |
duke@1 | 102 | |
duke@1 | 103 | /** |
duke@1 | 104 | * The version recognized by the Java 2 Platform, Standard |
duke@1 | 105 | * Edition 5.0. |
duke@1 | 106 | * |
jjh@972 | 107 | * The language described in |
jjh@972 | 108 | * <cite>The Java™ Language Specification, |
jjh@972 | 109 | * Third Edition</cite>. First release to support |
duke@1 | 110 | * generics, annotations, autoboxing, var-args, enhanced {@code |
duke@1 | 111 | * for} loop, and hexadecimal floating-point literals. |
duke@1 | 112 | */ |
duke@1 | 113 | RELEASE_5, |
duke@1 | 114 | |
duke@1 | 115 | /** |
duke@1 | 116 | * The version recognized by the Java Platform, Standard Edition |
duke@1 | 117 | * 6. |
duke@1 | 118 | * |
duke@1 | 119 | * No major changes from {@code RELEASE_5}. |
duke@1 | 120 | */ |
duke@1 | 121 | RELEASE_6, |
duke@1 | 122 | |
duke@1 | 123 | /** |
duke@1 | 124 | * The version recognized by the Java Platform, Standard Edition |
duke@1 | 125 | * 7. |
duke@1 | 126 | * |
darcy@1867 | 127 | * Additions in this release include, diamond syntax for |
darcy@1867 | 128 | * constructors, {@code try}-with-resources, strings in switch, |
darcy@1867 | 129 | * binary literals, and multi-catch. |
duke@1 | 130 | * @since 1.7 |
duke@1 | 131 | */ |
darcy@1042 | 132 | RELEASE_7, |
darcy@1042 | 133 | |
darcy@1042 | 134 | /** |
darcy@1042 | 135 | * The version recognized by the Java Platform, Standard Edition |
darcy@1042 | 136 | * 8. |
darcy@1042 | 137 | * |
darcy@1867 | 138 | * Additions in this release include lambda expressions and default methods. |
darcy@1042 | 139 | * @since 1.8 |
darcy@1042 | 140 | */ |
darcy@1042 | 141 | RELEASE_8; |
duke@1 | 142 | |
duke@1 | 143 | // Note that when adding constants for newer releases, the |
duke@1 | 144 | // behavior of latest() and latestSupported() must be updated too. |
duke@1 | 145 | |
duke@1 | 146 | /** |
duke@1 | 147 | * Returns the latest source version that can be modeled. |
duke@1 | 148 | * |
duke@1 | 149 | * @return the latest source version that can be modeled |
duke@1 | 150 | */ |
duke@1 | 151 | public static SourceVersion latest() { |
darcy@1042 | 152 | return RELEASE_8; |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | private static final SourceVersion latestSupported = getLatestSupported(); |
duke@1 | 156 | |
duke@1 | 157 | private static SourceVersion getLatestSupported() { |
duke@1 | 158 | try { |
duke@1 | 159 | String specVersion = System.getProperty("java.specification.version"); |
darcy@1042 | 160 | |
darcy@1042 | 161 | if ("1.8".equals(specVersion)) |
darcy@1042 | 162 | return RELEASE_8; |
darcy@1042 | 163 | else if("1.7".equals(specVersion)) |
duke@1 | 164 | return RELEASE_7; |
darcy@1042 | 165 | else if("1.6".equals(specVersion)) |
duke@1 | 166 | return RELEASE_6; |
duke@1 | 167 | } catch (SecurityException se) {} |
duke@1 | 168 | |
duke@1 | 169 | return RELEASE_5; |
duke@1 | 170 | } |
duke@1 | 171 | |
duke@1 | 172 | /** |
duke@1 | 173 | * Returns the latest source version fully supported by the |
duke@1 | 174 | * current execution environment. {@code RELEASE_5} or later must |
duke@1 | 175 | * be returned. |
duke@1 | 176 | * |
duke@1 | 177 | * @return the latest source version that is fully supported |
duke@1 | 178 | */ |
duke@1 | 179 | public static SourceVersion latestSupported() { |
duke@1 | 180 | return latestSupported; |
duke@1 | 181 | } |
duke@1 | 182 | |
duke@1 | 183 | /** |
duke@1 | 184 | * Returns whether or not {@code name} is a syntactically valid |
duke@1 | 185 | * identifier (simple name) or keyword in the latest source |
duke@1 | 186 | * version. The method returns {@code true} if the name consists |
duke@1 | 187 | * of an initial character for which {@link |
duke@1 | 188 | * Character#isJavaIdentifierStart(int)} returns {@code true}, |
duke@1 | 189 | * followed only by characters for which {@link |
duke@1 | 190 | * Character#isJavaIdentifierPart(int)} returns {@code true}. |
duke@1 | 191 | * This pattern matches regular identifiers, keywords, and the |
duke@1 | 192 | * literals {@code "true"}, {@code "false"}, and {@code "null"}. |
duke@1 | 193 | * The method returns {@code false} for all other strings. |
duke@1 | 194 | * |
duke@1 | 195 | * @param name the string to check |
duke@1 | 196 | * @return {@code true} if this string is a |
duke@1 | 197 | * syntactically valid identifier or keyword, {@code false} |
duke@1 | 198 | * otherwise. |
duke@1 | 199 | */ |
duke@1 | 200 | public static boolean isIdentifier(CharSequence name) { |
duke@1 | 201 | String id = name.toString(); |
duke@1 | 202 | |
duke@1 | 203 | if (id.length() == 0) { |
duke@1 | 204 | return false; |
duke@1 | 205 | } |
duke@1 | 206 | int cp = id.codePointAt(0); |
duke@1 | 207 | if (!Character.isJavaIdentifierStart(cp)) { |
duke@1 | 208 | return false; |
duke@1 | 209 | } |
duke@1 | 210 | for (int i = Character.charCount(cp); |
duke@1 | 211 | i < id.length(); |
duke@1 | 212 | i += Character.charCount(cp)) { |
duke@1 | 213 | cp = id.codePointAt(i); |
duke@1 | 214 | if (!Character.isJavaIdentifierPart(cp)) { |
duke@1 | 215 | return false; |
duke@1 | 216 | } |
duke@1 | 217 | } |
duke@1 | 218 | return true; |
duke@1 | 219 | } |
duke@1 | 220 | |
duke@1 | 221 | /** |
duke@1 | 222 | * Returns whether or not {@code name} is a syntactically valid |
duke@1 | 223 | * qualified name in the latest source version. Unlike {@link |
duke@1 | 224 | * #isIdentifier isIdentifier}, this method returns {@code false} |
duke@1 | 225 | * for keywords and literals. |
duke@1 | 226 | * |
duke@1 | 227 | * @param name the string to check |
duke@1 | 228 | * @return {@code true} if this string is a |
duke@1 | 229 | * syntactically valid name, {@code false} otherwise. |
jjh@972 | 230 | * @jls 6.2 Names and Identifiers |
duke@1 | 231 | */ |
duke@1 | 232 | public static boolean isName(CharSequence name) { |
duke@1 | 233 | String id = name.toString(); |
duke@1 | 234 | |
duke@1 | 235 | for(String s : id.split("\\.", -1)) { |
duke@1 | 236 | if (!isIdentifier(s) || isKeyword(s)) |
duke@1 | 237 | return false; |
duke@1 | 238 | } |
duke@1 | 239 | return true; |
duke@1 | 240 | } |
duke@1 | 241 | |
duke@1 | 242 | private final static Set<String> keywords; |
duke@1 | 243 | static { |
duke@1 | 244 | Set<String> s = new HashSet<String>(); |
duke@1 | 245 | String [] kws = { |
duke@1 | 246 | "abstract", "continue", "for", "new", "switch", |
duke@1 | 247 | "assert", "default", "if", "package", "synchronized", |
duke@1 | 248 | "boolean", "do", "goto", "private", "this", |
duke@1 | 249 | "break", "double", "implements", "protected", "throw", |
duke@1 | 250 | "byte", "else", "import", "public", "throws", |
duke@1 | 251 | "case", "enum", "instanceof", "return", "transient", |
duke@1 | 252 | "catch", "extends", "int", "short", "try", |
duke@1 | 253 | "char", "final", "interface", "static", "void", |
duke@1 | 254 | "class", "finally", "long", "strictfp", "volatile", |
duke@1 | 255 | "const", "float", "native", "super", "while", |
duke@1 | 256 | // literals |
duke@1 | 257 | "null", "true", "false" |
duke@1 | 258 | }; |
duke@1 | 259 | for(String kw : kws) |
duke@1 | 260 | s.add(kw); |
duke@1 | 261 | keywords = Collections.unmodifiableSet(s); |
duke@1 | 262 | } |
duke@1 | 263 | |
duke@1 | 264 | /** |
duke@1 | 265 | * Returns whether or not {@code s} is a keyword or literal in the |
duke@1 | 266 | * latest source version. |
duke@1 | 267 | * |
duke@1 | 268 | * @param s the string to check |
duke@1 | 269 | * @return {@code true} if {@code s} is a keyword or literal, {@code false} otherwise. |
duke@1 | 270 | */ |
duke@1 | 271 | public static boolean isKeyword(CharSequence s) { |
duke@1 | 272 | String keywordOrLiteral = s.toString(); |
duke@1 | 273 | return keywords.contains(keywordOrLiteral); |
duke@1 | 274 | } |
duke@1 | 275 | } |