summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew John Hughes <gnu_andrew@member.fsf.org>2013-02-18 23:13:15 +0000
committerAndrew John Hughes <gnu_andrew@member.fsf.org>2013-02-18 23:13:15 +0000
commitb004826deb64618f4eebcbc21abed78319082abc (patch)
tree1905d22800ac70921a943e6716717256ff14d02c
parent3c5bd6b4cac5eb7c0efef59ff7402e309c6241a0 (diff)
downloadclasspath-b004826deb64618f4eebcbc21abed78319082abc.tar.gz
Add missing methods in javax.lang.model.SourceVersion.
2013-02-18 Andrew John Hughes <gnu_andrew@member.fsf.org> * javax/lang/model/SourceVersion.java, (KEYWORDS): Add array containing language keywords. (isKeyword(CharSequence)): Implemented. (isName(CharSequence)): Likewise. Signed-off-by: Andrew John Hughes <gnu_andrew@member.fsf.org>
-rw-r--r--ChangeLog7
-rw-r--r--javax/lang/model/SourceVersion.java64
2 files changed, 71 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index c5bb0454b..906a439bd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2013-02-18 Andrew John Hughes <gnu_andrew@member.fsf.org>
+ * javax/lang/model/SourceVersion.java,
+ (KEYWORDS): Add array containing language keywords.
+ (isKeyword(CharSequence)): Implemented.
+ (isName(CharSequence)): Likewise.
+
+2013-02-18 Andrew John Hughes <gnu_andrew@member.fsf.org>
+
* javax/annotation/processing/SupportedSourceVersion.java:
Implemented.
(value()): Implemented.
diff --git a/javax/lang/model/SourceVersion.java b/javax/lang/model/SourceVersion.java
index e42889dd3..5c3d040be 100644
--- a/javax/lang/model/SourceVersion.java
+++ b/javax/lang/model/SourceVersion.java
@@ -37,6 +37,8 @@ exception statement from your version. */
package javax.lang.model;
+import java.util.Arrays;
+
/**
* Source versions of the Java programming language.
* Note that this will be extended with additional
@@ -55,6 +57,22 @@ public enum SourceVersion
/** Java 5 */ RELEASE_5,
/** Java 6 */ RELEASE_6;
+ /** List of language keywords, as specified in 3.9 of the Java
+ * Language Specification. Please keep sorted. */
+ private static final String[] KEYWORDS = { "abstract", "assert", "boolean",
+ "break", "byte", "case", "catch",
+ "char", "class", "const", "continue",
+ "default", "do", "double", "else",
+ "enum", "extends", "final", "finally",
+ "float", "for", "if", "goto", "implements",
+ "import", "instanceof", "int", "interface",
+ "long", "native", "new", "package", "private",
+ "protected", "public", "return", "short",
+ "static", "strictfp", "super", "switch",
+ "synchronized", "this", "throw", "throws",
+ "transient", "try", "void", "volatile",
+ "while" };
+
/**
* Returns true if {@code name} is a syntactically valid identifier or
* keyword in the latest version of the language. That is, this
@@ -100,4 +118,50 @@ public enum SourceVersion
return RELEASE_5;
}
+ /**
+ * Returns true if the specified character sequence
+ * represents a keyword or one of the literals:
+ * {@code "null"}, {@code "true"} or {@code "false"}.
+ *
+ * @param string the string to check.
+ * @return true if {@code string} represents a valid keyword or literal.
+ */
+ public static boolean isKeyword(CharSequence string)
+ {
+ if (Arrays.binarySearch(KEYWORDS, string) >= 0)
+ return true;
+ if (string.equals("true") || string.equals("false") ||
+ string.equals("null"))
+ return true;
+ return false;
+ }
+
+ /**
+ * Returns true if the specified character sequence
+ * represents a qualified name such as {@code "java.lang.Object"}
+ * or {@code "String"}. Unlike {@link #isIdentifier(CharSequence)},
+ * this method does not return {@code true} for keywords
+ * and literals.
+ *
+ * @param string the string to check.
+ * @return true if {@code string} represents a valid qualified name.
+ */
+ public static boolean isName(CharSequence string)
+ {
+ int dotLocation = -1;
+ int length = string.length();
+
+ // Find a '.' if there is one
+ for (int a = 0; a < length; ++a)
+ if (string.charAt(a) == '.')
+ dotLocation = a;
+
+ if (dotLocation == -1)
+ return isIdentifier(string) && !isKeyword(string);
+
+ CharSequence identifier = string.subSequence(dotLocation + 1, length);
+ return (isName(string.subSequence(0, dotLocation)) &&
+ (isIdentifier(identifier) && !isKeyword(identifier)));
+ }
+
}