KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > tlbimp > Escape


1 package com4j.tlbimp;
2
3 import java.util.Collections JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Set JavaDoc;
6
7 /**
8  * Escapes the Java reserved words.
9  *
10  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
11  */

12 class Escape {
13     /**
14      * Set of all the Java keywords.
15      */

16     private static final Set JavaDoc reservedWords;
17
18     /**
19      * Escapes the identifier if necessary.
20      */

21     public static String JavaDoc escape( String JavaDoc identifier ) {
22         if(reservedWords.contains(identifier))
23             return '_'+identifier;
24         else
25             return identifier;
26     }
27
28     static {
29         // see http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#229308
30
String JavaDoc[] words = new String JavaDoc[] {
31             "abstract",
32             "assert",
33             "boolean",
34             "break",
35             "byte",
36             "case",
37             "catch",
38             "char",
39             "class",
40             "const",
41             "continue",
42             "default",
43             "do",
44             "double",
45             "else",
46             "enum",
47             "extends",
48             "final",
49             "finally",
50             "float",
51             "for",
52             "goto",
53             "if",
54             "implements",
55             "import",
56             "instanceof",
57             "int",
58             "interface",
59             "long",
60             "native",
61             "new",
62             "package",
63             "private",
64             "protected",
65             "public",
66             "return",
67             "short",
68             "static",
69             "strictfp",
70             "super",
71             "switch",
72             "synchronized",
73             "this",
74             "throw",
75             "throws",
76             "transient",
77             "try",
78             "void",
79             "volatile",
80             "while"
81         };
82         HashSet JavaDoc s = new HashSet JavaDoc();
83         for( String JavaDoc w : words )
84             s.add(w);
85         reservedWords = Collections.unmodifiableSet(s);
86     }
87 }
88
Popular Tags