Is there any package to store the set of java keywords

后端 未结 2 495
北恋
北恋 2021-01-21 19:25

Where All the Keywords are stored in java? Is there any package to store the set of keywords(predefined thing) Because these are not related to any \"Classes(capital C), but \"c

相关标签:
2条回答
  • 2021-01-21 20:13

    In theory, there is no need for the Java runtime environment, where your compiled code runs, to keep the keywords. Once compiled, all the code becomes bytecode. A private method, for example, would be marked by a flag rather than the string of letters p-r-i-v-a-t-e. A for loop would be represented by some byte code that sets the initial value of the variable, performs the operation, increments the value, checks the condition, and jumps back to the beginning of the operation. No need for the three letters f-o-r to be stored anywhere for this.

    However, standard Java does have a class in the runtime library which contains all of the Java keywords - the SourceVersion enum in the javax.lang.model package. This enum has a method which accepts a CharSequence and checks whether it is a keyword in the latest release of Java (the running version, obviously).

    The javax.lang.model package is offered for purposes of modeling the Java language. For example, it's being used by programs that process annotations or by programs that themselves compile Java programs.

    The SourceVersion enum stores all the keywords, but it doesn't make them available to you. The API merely allows you to check if a string is a keyword or not.

    0 讨论(0)
  • 2021-01-21 20:29

    It is doubtful that all keywords are stored somewhere in the java runtime library, as they are not needed there.
    Where they are needed, however, is in the compiler.

    After giving the JDK langtools repo a quick look, I found that:

    • com.sun.tools.javac.antlr.Java.g4 (A text file)
      Contains a list of keywords:

      // §3.9 Keywords
      
      ABSTRACT : 'abstract';
      ASSERT : 'assert';
      BOOLEAN : 'boolean';
      BREAK : 'break';
      BYTE : 'byte';
      CASE : 'case';
      CATCH : 'catch';
      CHAR : 'char';
      CLASS : 'class';
      CONST : 'const';
      CONTINUE : 'continue';
      DEFAULT : 'default';
      DO : 'do';
      DOUBLE : 'double';
      ELSE : 'else';
      ENUM : 'enum';
      EXTENDS : 'extends';
      FINAL : 'final';
      FINALLY : 'finally';
      FLOAT : 'float';
      FOR : 'for';
      IF : 'if';
      GOTO : 'goto';
      IMPLEMENTS : 'implements';
      IMPORT : 'import';
      INSTANCEOF : 'instanceof';
      INT : 'int';
      INTERFACE : 'interface';
      LONG : 'long';
      NATIVE : 'native';
      NEW : 'new';
      PACKAGE : 'package';
      PRIVATE : 'private';
      PROTECTED : 'protected';
      PUBLIC : 'public';
      RETURN : 'return';
      SHORT : 'short';
      STATIC : 'static';
      STRICTFP : 'strictfp';
      SUPER : 'super';
      SWITCH : 'switch';
      SYNCHRONIZED : 'synchronized';
      THIS : 'this';
      THROW : 'throw';
      THROWS : 'throws';
      TRANSIENT : 'transient';
      TRY : 'try';
      VOID : 'void';
      VOLATILE : 'volatile';
      WHILE : 'while';
      

      I'm unsure whether that file is also included in the distributed javac, and thus could be retrieved at run time.

    • com.sun.tools.javac.parser.Tokens (A normal java class)
      Contains a subclass enum TokenKind, which holds all tokens.
      While these could definitely be retrieved at run time, they also include operators, an end-of-file representation, etc., so you'd have to filter them first.
    0 讨论(0)
提交回复
热议问题