Get a list of all reserved Java keys

I am looking for a way to get all stored keywords in Java into some kind of data structure. For example: "for, while, if, else, int, double, etc."

I need to do a name check on a string to be specific, I need to make sure that it doesn't match any java keywords.

Is there any way to get all keywords into one data structure? or do I just need to create a regular expression string with all these keywords in it: "for | while | if | ..." and try and match my string with it?

thanks

+6
source share
4 answers

From axis.apache.org

Basically, pre- sort your keywords and store them in an array using Arrays.binarySearch for your keyword for good'ol O ( logn ) complexity

import java.util.Arrays; public class MainDemo { static final String keywords[] = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" }; public static boolean isJavaKeyword(String keyword) { return (Arrays.binarySearch(keywords, keyword) >= 0); } //Main method public static void main(String[] args) { System.out.println(isJavaKeyword("void")); } } 

Output:

true


On the other hand, as users of @typeracer, @ Holger suggested in the comments, you can use SourceVersion.isKeyword("void") , which uses the javax.lang.model.SourceVersion library structure and the Hashset data structure inside and keeps the list updated for you.

+11
source

There is no direct API method. Alternatively, you take them in an array and check the entered keyword matches in the array, if the keywords are.

  public static String[] keys= { "new",..... } 

then

  for (int i = 0; i < keys.length; i++) { if (input.equals(keys[i])) { // TO DO } } 
+3
source

OK So, since there is no automatic way to do this, I will create a text file consisting of all keywords: List of Java keywords

And then, at runtime, navigate through the file by inserting each keyword into an array or arraylist (or regex string) and test the use of this data structure when I verify the name is correct.

Thank you all

+1
source

I am surprised that no one has yet suggested javax.lang.model.SourceVersion , because it was actually with Java 1.6.

If you need to check if some string is a reserved keyword, you can simply call:

 SourceVersion.isKeyword(str) 

And if you really need a complete list of reserved keywords, you can get it from the source code of this class:

 private final static Set<String> keywords; static { Set<String> s = new HashSet<String>(); String [] kws = { "abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", // literals "null", "true", "false" }; for(String kw : kws) s.add(kw); keywords = Collections.unmodifiableSet(s); } 

Note : the above source code is taken from Java 1.8 , so do not just copy and paste from this post if you are using a different version of Java. Actually, it's probably not a good idea to copy this at all - they made the field private for a good reason - you probably don't want to update it for every new release of Java. But if you absolutely need it, copy it from the source code to your JDK distribution, remembering that you may have to manually update it later.

+1
source

Source: https://habr.com/ru/post/970949/


All Articles