Is there any package to store the set of java keywords

后端 未结 2 496
北恋
北恋 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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题