Plan B
Programmatically looking up Java reserved words
(Warning: the solution discussed is specific to Sun's javac compiler and is not "vendor" agnostic)
Let's say during code generation, you want to make sure that you don't use java reserved words as identifiers. Trivially, you could go through the hassle of creating an Enum for yourself and manually populating it with known reserved words for that version of Java. But, why reinvent the wheel when the compiler already has that information (updated per release)? All you need to do is create an EnumSet from the Enum com.sun.tools.javac.parser.Token and look it up to know if a certain String is a java reserved word.
The only problem with this approach is that it relies upon an implementation specific class/package(com.sun.* can change without notice and do not constitute an API). But if laziness takes over, its still an option worth considering for one-off usage. Of course, I'd like to know better (implementation independent) alternatives.
Thanks to Sundar for providing pointers in the javac source.
Posted at 12:57PM Jan 21, 2007 by Bharath Ravikumar in Java | Comments[3]
Posted by Ranganath.s on January 23, 2007 at 04:40 PM IST #
Posted by Bharath R on January 23, 2007 at 06:41 PM IST #
Posted by Ranganath.S on January 24, 2007 at 02:45 PM IST #