8187982: Update SourceVersion to mention restricted keywords

Reviewed-by: mcimadamore
This commit is contained in:
Joe Darcy 2017-09-27 14:23:41 -07:00
parent 24d81d3f2e
commit 5d2a4c319a
3 changed files with 39 additions and 4 deletions

View File

@ -216,8 +216,9 @@ public enum SourceVersion {
* Character#isJavaIdentifierStart(int)} returns {@code true},
* followed only by characters for which {@link
* Character#isJavaIdentifierPart(int)} returns {@code true}.
* This pattern matches regular identifiers, keywords, and the
* literals {@code "true"}, {@code "false"}, and {@code "null"}.
* This pattern matches regular identifiers, keywords, restricted
* keywords, and the literals {@code "true"}, {@code "false"}, and
* {@code "null"}.
* The method returns {@code false} for all other strings.
*
* @param name the string to check
@ -251,10 +252,13 @@ public enum SourceVersion {
* qualified name in the latest source version. Unlike {@link
* #isIdentifier isIdentifier}, this method returns {@code false}
* for keywords, boolean literals, and the null literal.
* This method returns {@code true} for <i>restricted
* keywords</i>.
*
* @param name the string to check
* @return {@code true} if this string is a
* syntactically valid name, {@code false} otherwise.
* @jls 3.9 Keywords
* @jls 6.2 Names and Identifiers
*/
public static boolean isName(CharSequence name) {
@ -266,11 +270,14 @@ public enum SourceVersion {
* qualified name in the given source version. Unlike {@link
* #isIdentifier isIdentifier}, this method returns {@code false}
* for keywords, boolean literals, and the null literal.
* This method returns {@code true} for <i>restricted
* keywords</i>.
*
* @param name the string to check
* @param version the version to use
* @return {@code true} if this string is a
* syntactically valid name, {@code false} otherwise.
* @jls 3.9 Keywords
* @jls 6.2 Names and Identifiers
* @since 9
*/
@ -287,6 +294,8 @@ public enum SourceVersion {
/**
* Returns whether or not {@code s} is a keyword, boolean literal,
* or null literal in the latest source version.
* This method returns {@code false} for <i>restricted
* keywords</i>.
*
* @param s the string to check
* @return {@code true} if {@code s} is a keyword, or boolean
@ -302,6 +311,8 @@ public enum SourceVersion {
/**
* Returns whether or not {@code s} is a keyword, boolean literal,
* or null literal in the given source version.
* This method returns {@code false} for <i>restricted
* keywords</i>.
*
* @param s the string to check
* @param version the version to use

View File

@ -87,7 +87,7 @@ public interface ModuleElement extends Element, QualifiedNameable {
*
* @return {@code true} if this is an open module and {@code
* false} otherwise
*/ // TODO: add @jls to unnamed module section
*/
boolean isOpen();
/**
@ -96,7 +96,9 @@ public interface ModuleElement extends Element, QualifiedNameable {
*
* @return {@code true} if this is an unnamed module and {@code
* false} otherwise
*/ // TODO: add @jls to unnamed module section
*
* @jls 7.7.5 Unnamed Modules
*/
boolean isUnnamed();
/**

View File

@ -41,6 +41,7 @@ public class TestSourceVersion {
public static void main(String... args) {
testLatestSupported();
testVersionVaryingKeywords();
testRestrictedKeywords();
}
private static void testLatestSupported() {
@ -74,6 +75,27 @@ public class TestSourceVersion {
}
}
private static void testRestrictedKeywords() {
// Restricted keywords are not full keywords
/*
* JLS 3.9
* " A further ten character sequences are restricted
* keywords: open, module, requires, transitive, exports,
* opens, to, uses, provides, and with"
*/
Set<String> restrictedKeywords =
Set.of("open", "module", "requires", "transitive", "exports",
"opens", "to", "uses", "provides", "with");
for(String key : restrictedKeywords) {
for(SourceVersion version : SourceVersion.values()) {
check(false, isKeyword(key, version), "keyword", version);
check(true, isName(key, version), "name", version);
}
}
}
private static void check(boolean result, boolean expected,
String message, SourceVersion version) {
if (result != expected) {