1 | /* |
2 | * Copyright (C) Christian Schulte, 2012-253 |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * o Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * o Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in |
14 | * the documentation and/or other materials provided with the |
15 | * distribution. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
19 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * |
28 | * $JOMC: JavaLanguage.java 4962 2014-09-06 23:58:48Z schulte $ |
29 | * |
30 | */ |
31 | package org.jomc.model; |
32 | |
33 | import java.util.HashSet; |
34 | import java.util.Set; |
35 | |
36 | /** |
37 | * Java language support. |
38 | * |
39 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
40 | * @version $JOMC: JavaLanguage.java 4962 2014-09-06 23:58:48Z schulte $ |
41 | * @since 1.4 |
42 | */ |
43 | class JavaLanguage |
44 | { |
45 | |
46 | /** Creates a new {@code JavaLanguage} instance. */ |
47 | JavaLanguage() |
48 | { |
49 | super(); |
50 | } |
51 | |
52 | /** |
53 | * The Java Language Specification - Java SE 7 Edition - Chapter 18. Syntax |
54 | * <pre> |
55 | * BasicType: |
56 | * byte |
57 | * short |
58 | * char |
59 | * int |
60 | * long |
61 | * float |
62 | * double |
63 | * </pre> |
64 | */ |
65 | static final Set<String> BASIC_TYPES = new HashSet<String>( 8 ); |
66 | |
67 | /** |
68 | * The Java Language Specification - Java SE 7 Edition - Chapter 3.10.3. Boolean Literals |
69 | * <pre> |
70 | * BooleanLiteral: one of |
71 | * true false |
72 | * </pre> |
73 | */ |
74 | static final Set<String> BOOLEAN_LITERALS = new HashSet<String>( 2 ); |
75 | |
76 | /** |
77 | * The Java Language Specification - Java SE 7 Edition - Chapter 3.9. Keywords |
78 | * <pre> |
79 | * Keyword: one of |
80 | * abstract continue for new switch |
81 | * assert default if package synchronized |
82 | * boolean do goto private this |
83 | * break double implements protected throw |
84 | * byte else import public throws |
85 | * case enum instanceof return transient |
86 | * catch extends int short try |
87 | * char final interface static void |
88 | * class finally long strictfp volatile |
89 | * const float native super while |
90 | * </pre> |
91 | */ |
92 | static final Set<String> KEYWORDS = new HashSet<String>( 50 ); |
93 | |
94 | /** |
95 | * The Java Language Specification - Java SE 7 Edition - Chapter 3.10.7. The Null Literal |
96 | * <pre> |
97 | * NullLiteral: |
98 | * null |
99 | * </pre> |
100 | */ |
101 | static final String NULL_LITERAL = "null"; |
102 | |
103 | static |
104 | { |
105 | // JLS - Java SE 7 Edition - Chapter 18. Syntax - BasicType |
106 | BASIC_TYPES.add( "boolean" ); |
107 | BASIC_TYPES.add( "byte" ); |
108 | BASIC_TYPES.add( "char" ); |
109 | BASIC_TYPES.add( "double" ); |
110 | BASIC_TYPES.add( "float" ); |
111 | BASIC_TYPES.add( "short" ); |
112 | BASIC_TYPES.add( "int" ); |
113 | BASIC_TYPES.add( "long" ); |
114 | |
115 | // JLS - Java SE 7 Edition - 3.10.3. Boolean Literals |
116 | BOOLEAN_LITERALS.add( "true" ); |
117 | BOOLEAN_LITERALS.add( "false" ); |
118 | |
119 | // JLS - Java SE 7 Edition - Chapter 3. Lexical Structure - 3.9. Keywords |
120 | KEYWORDS.add( "abstract" ); |
121 | KEYWORDS.add( "assert" ); |
122 | KEYWORDS.add( "boolean" ); |
123 | KEYWORDS.add( "break" ); |
124 | KEYWORDS.add( "byte" ); |
125 | KEYWORDS.add( "case" ); |
126 | KEYWORDS.add( "catch" ); |
127 | KEYWORDS.add( "char" ); |
128 | KEYWORDS.add( "class" ); |
129 | KEYWORDS.add( "const" ); |
130 | KEYWORDS.add( "continue" ); |
131 | KEYWORDS.add( "default" ); |
132 | KEYWORDS.add( "do" ); |
133 | KEYWORDS.add( "double" ); |
134 | KEYWORDS.add( "else" ); |
135 | KEYWORDS.add( "enum" ); |
136 | KEYWORDS.add( "extends" ); |
137 | KEYWORDS.add( "final" ); |
138 | KEYWORDS.add( "finally" ); |
139 | KEYWORDS.add( "float" ); |
140 | KEYWORDS.add( "for" ); |
141 | KEYWORDS.add( "if" ); |
142 | KEYWORDS.add( "goto" ); |
143 | KEYWORDS.add( "implements" ); |
144 | KEYWORDS.add( "import" ); |
145 | KEYWORDS.add( "instanceof" ); |
146 | KEYWORDS.add( "int" ); |
147 | KEYWORDS.add( "interface" ); |
148 | KEYWORDS.add( "long" ); |
149 | KEYWORDS.add( "native" ); |
150 | KEYWORDS.add( "new" ); |
151 | KEYWORDS.add( "package" ); |
152 | KEYWORDS.add( "private" ); |
153 | KEYWORDS.add( "protected" ); |
154 | KEYWORDS.add( "public" ); |
155 | KEYWORDS.add( "return" ); |
156 | KEYWORDS.add( "short" ); |
157 | KEYWORDS.add( "static" ); |
158 | KEYWORDS.add( "strictfp" ); |
159 | KEYWORDS.add( "super" ); |
160 | KEYWORDS.add( "switch" ); |
161 | KEYWORDS.add( "synchronized" ); |
162 | KEYWORDS.add( "this" ); |
163 | KEYWORDS.add( "throw" ); |
164 | KEYWORDS.add( "throws" ); |
165 | KEYWORDS.add( "transient" ); |
166 | KEYWORDS.add( "try" ); |
167 | KEYWORDS.add( "void" ); |
168 | KEYWORDS.add( "volatile" ); |
169 | KEYWORDS.add( "while" ); |
170 | } |
171 | |
172 | /** |
173 | * Tests whether a given string is case-insensitively equal to a Java keyword. |
174 | * |
175 | * @param s The string to test. |
176 | * |
177 | * @return {@code true}, if {@code s} is case-insensitively equal to a Java keyword; {@code false}, if not. |
178 | */ |
179 | static boolean isKeyword( final String s ) |
180 | { |
181 | for ( final String keyword : KEYWORDS ) |
182 | { |
183 | if ( keyword.equalsIgnoreCase( s ) ) |
184 | { |
185 | return true; |
186 | } |
187 | } |
188 | |
189 | return false; |
190 | } |
191 | |
192 | /** |
193 | * Tests whether a given string is case-insensitively equal to a Java boolean literal. |
194 | * |
195 | * @param s The string to test. |
196 | * |
197 | * @return {@code true}, if {@code s} is case-insensitively equal to a Java boolean literal; {@code false}, if not. |
198 | */ |
199 | static boolean isBooleanLiteral( final String s ) |
200 | { |
201 | for ( final String literal : BOOLEAN_LITERALS ) |
202 | { |
203 | if ( literal.equalsIgnoreCase( s ) ) |
204 | { |
205 | return true; |
206 | } |
207 | } |
208 | |
209 | return false; |
210 | } |
211 | |
212 | /** |
213 | * Tests whether a given string is case-insensitively equal to the Java {@code null} literal. |
214 | * |
215 | * @param s The string to test. |
216 | * |
217 | * @return {@code true}, if {@code s} is case-insensitively equal to the Java ·{@code null} literal; {@code false}, |
218 | * if not. |
219 | */ |
220 | static boolean isNullLiteral( final String s ) |
221 | { |
222 | return NULL_LITERAL.equalsIgnoreCase( s ); |
223 | } |
224 | |
225 | } |