Source code: javax/ide/model/java/source/tree/TreeKind.java
1 /*
2 * @(#)TreeKind.java
3 */
4
5 package javax.ide.model.java.source.tree;
6 import java.util.LinkedHashMap;
7 import java.util.Map;
8 import java.util.Set;
9
10 /**
11 * The TreeKind enumeration identifies each individual kind of
12 * Tree. <p/>
13 *
14 * For code-readability, all constants have been prefixed as TREE_*.
15 * Constants for subinterfaces of StatementT have been prefixed as
16 * TREE_STMT_*. Constants for subinterfaces of ExpressionT have
17 * been prefixed as TREE_EXPR_*. Constants for subinterfaces of
18 * DocT have been prefixed as TREE_DOC_*. <p/>
19 *
20 * Though the doc structure is not part of the Java Language
21 * specification (moreover, there is no specification of doc
22 * structure), doc comment constants have been included because
23 * refactoring features may need to access and mutate doc comment
24 * contents. <p/>
25 *
26 * In this version, this class is 1.4 compatible. In a later version,
27 * it will be redone as an enum. <p/>
28 *
29 * @author Andy Yu
30 */
31 public final class TreeKind
32 {
33 // ----------------------------------------------------------------------
34
35 public static final int TREE_invalid = 0;
36
37 public static final int TREE_base = 1;
38
39 /** An annotation. AnnotationT. */
40 public static final int TREE_ANNOTATION = TREE_base + 0;
41
42 /** A code block. BlockT. */
43 public static final int TREE_BLOCK = TREE_base + 1;
44
45 /** A body of a class declaration. ClassBodyT. */
46 public static final int TREE_CLASS_BODY = TREE_base + 2;
47
48 /** A class, enum, interface, or annotation type declaration. ClassT. */
49 public static final int TREE_CLASS_D = TREE_base + 3;
50
51 /** An initializer in a class declaration. ClassInitializerT. */
52 public static final int TREE_CLASS_INITIALIZER = TREE_base + 4;
53
54 /** A constructor declaration. MethodT. */
55 public static final int TREE_CONSTRUCTOR_D = TREE_base + 5;
56
57 /** An enum constant declaration. FieldDeclT. */
58 public static final int TREE_ENUM_CONSTANT_D = TREE_base + 6;
59
60 /** A field declaration that is not for an enum constant. FieldDeclT. */
61 public static final int TREE_FIELD_D = TREE_base + 7;
62
63 /** A field variable. FieldVariableT. */
64 public static final int TREE_FIELD_VARIABLE = TREE_base + 8;
65
66 /** A source file (a compilation unit). FileT. */
67 public static final int TREE_FILE = TREE_base + 9;
68
69 /** A formal parameter. FormalParameterT. */
70 public static final int TREE_FORMAL_PARAMETER = TREE_base + 10;
71
72 /** A formal parameter list. FormalParameterListT. */
73 public static final int TREE_FORMAL_PARAMETER_LIST = TREE_base + 11;
74
75 /** An import declaration. ImportT. */
76 public static final int TREE_IMPORT_D = TREE_base + 12;
77
78 /** An interfaces clause for a class declaration. On a "class"
79 * declaration, this will be the "implements" clause. On an
80 * "interface" declaration, this will be the "extends" clause. <p/>
81 *
82 * The name "interfaces" was taken from reflection. <p/>
83 *
84 * @see java.lang.Class#getInterfaces()
85 InterfacesT. */
86 public static final int TREE_INTERFACES_D = TREE_base + 13;
87
88 /** A local variable. LocalVariableT. */
89 public static final int TREE_LOCAL_VARIABLE = TREE_base + 14;
90
91 /** A local variable declaration. LocalVariableDeclT. */
92 public static final int TREE_LOCAL_VARIABLE_D = TREE_base + 15;
93
94 /** A method declaration that is not for a constructor. MethodT. */
95 public static final int TREE_METHOD_D = TREE_base + 16;
96
97 /** Declaration modifiers. ModifiersT. */
98 public static final int TREE_MODIFIERS = TREE_base + 17;
99
100 /** A name, either a simple name or a qualified name. NameT. */
101 public static final int TREE_NAME = TREE_base + 18;
102
103 /** A package declaration. PackageT. */
104 public static final int TREE_PACKAGE_D = TREE_base + 19;
105
106 /** A statement label. StatementLabelT. */
107 public static final int TREE_STATEMENT_LABEL = TREE_base + 20;
108
109 /** A super-class clause for a class declaration. On a "class"
110 * declaration, this will be the "extends" clause.
111 *
112 * The name "superclass" was taken from reflection. <p/>
113 *
114 * @see java.lang.Class#getSuperclass()
115 SuperclassT. */
116 public static final int TREE_SUPERCLASS = TREE_base + 21;
117
118 /** A switch case label, either case or default. SwitchLabelT. */
119 public static final int TREE_SWITCH_LABEL = TREE_base + 22;
120
121 /** A throws clause for a method declaration. ThrowsT. */
122 public static final int TREE_THROWS = TREE_base + 23;
123
124 /** A type reference. TypeReferenceT. */
125 public static final int TREE_TYPE_REFERENCE = TREE_base + 24;
126
127 /** A type argument in a type reference. TypeArgumentT. */
128 public static final int TREE_TYPE_ARGUMENT = TREE_base + 25;
129
130 /** A type parameter declaration. TypeParameterT. */
131 public static final int TREE_TYPE_PARAMETER = TREE_base + 26;
132
133
134 // ----------------------------------------------------------------------
135 // STateMenT kinds.
136
137 /** An assert statement. AssertStatementT. */
138 public static final int TREE_STMT_ASSERT = TREE_base + 27;
139
140 /** A statement wrapping a block.BlockStatementT. */
141 public static final int TREE_STMT_BLOCK = TREE_base + 28;
142
143 /** A break statement. BreakStatementT. */
144 public static final int TREE_STMT_BREAK = TREE_base + 29;
145
146 /** A catch clause in a try statement. CatchClauseT. */
147 public static final int TREE_STMT_CATCH = TREE_base + 30;
148
149 /** A continue statement. ContinueStatementT. */
150 public static final int TREE_STMT_CONTINUE = TREE_base + 31;
151
152 /** A do-while statement. DoStatementT. */
153 public static final int TREE_STMT_DO = TREE_base + 32;
154
155 /** An else clause in an if statement. ElseClauseT. */
156 public static final int TREE_STMT_ELSE = TREE_base + 33;
157
158 /** An empty statement. EmptyStatementT. */
159 public static final int TREE_STMT_EMPTY = TREE_base + 34;
160
161 /** A statement wrapping an expression. ExpressionStatementT. */
162 public static final int TREE_STMT_EXPRESSION = TREE_base + 35;
163
164 /** A finally clause in a try statement. FinallyClauseT. */
165 public static final int TREE_STMT_FINALLY = TREE_base + 36;
166
167 /** A for statement, all variants. ForStatementT. */
168 public static final int TREE_STMT_FOR = TREE_base + 37;
169
170 /** An if statement. IfStatementT. */
171 public static final int TREE_STMT_IF = TREE_base + 38;
172
173 /** A return statement. ReturnStatementT. */
174 public static final int TREE_STMT_RETURN = TREE_base + 39;
175
176 /** A switch statement. SwitchStatementT. */
177 public static final int TREE_STMT_SWITCH = TREE_base + 40;
178
179 /** A synchronized statement. SynchStatementT. */
180 public static final int TREE_STMT_SYNCH = TREE_base + 41;
181
182 /** A throw statement. ThrowStatementT. */
183 public static final int TREE_STMT_THROW = TREE_base + 42;
184
185 /** A try statement. TryStatementT. */
186 public static final int TREE_STMT_TRY = TREE_base + 43;
187
188 /** A while statement. WhileStatementT. */
189 public static final int TREE_STMT_WHILE = TREE_base + 44;
190
191
192 // ----------------------------------------------------------------------
193 // EXPRession kinds.
194
195 /** An expression wrapping an annotation. Used only in annotations. AnnotationExpressionT. */
196 public static final int TREE_EXPR_ANNOTATION = TREE_base + 45;
197
198 /** An array access expression. ArrayAccessExpressionT. */
199 public static final int TREE_EXPR_ARRAY_ACCESS = TREE_base + 46;
200
201 /** An array creator expression. NewArrayExpressionT. */
202 public static final int TREE_EXPR_ARRAY_CREATOR = TREE_base + 47;
203
204 /** An expression for an assignment operation. AssignmentExpressionT. */
205 public static final int TREE_EXPR_ASSIGNMENT = TREE_base + 48;
206
207 /** A class creator expression. NewClassExpressionT. */
208 public static final int TREE_EXPR_CLASS_CREATOR = TREE_base + 49;
209
210 /** An expression for an identifier selector. DotExpressionT. */
211 public static final int TREE_EXPR_DOT = TREE_base + 50;
212
213 /** An identifier expression. IdentifierExpressionT. */
214 public static final int TREE_EXPR_IDENTIFIER = TREE_base + 51;
215
216 /** An expression for an infix operation. InfixExpressionT. */
217 public static final int TREE_EXPR_INFIX = TREE_base + 52;
218
219 /** A list of expressions. May be an argument list or an array constant. ListExpressionT. */
220 public static final int TREE_EXPR_LIST = TREE_base + 53;
221
222 /** A literal. LiteralExpressionT. */
223 public static final int TREE_EXPR_LITERAL = TREE_base + 54;
224
225 /** A method call. MethodCallExpressionT. */
226 public static final int TREE_EXPR_METHOD_CALL = TREE_base + 55;
227
228 /** An expression for the conditional operator. QuestionExpressionT. */
229 public static final int TREE_EXPR_QUESTION = TREE_base + 56;
230
231 /** An expression wrapping a type reference. TypeExpressionT. */
232 public static final int TREE_EXPR_TYPE = TREE_base + 57;
233
234 /** An expression for a typecast operation. TypecastExpressionT. */
235 public static final int TREE_EXPR_TYPECAST = TREE_base + 58;
236
237 /** An expression for a prefix or postfix operation. Also
238 * includes keyword selectors. UnaryExpressionT. */
239 public static final int TREE_EXPR_UNARY = TREE_base + 59;
240
241 /** An expression wrapping another expression. Will be either a
242 * parenthesis wrapper or a bracket wrapper. WrapperExpressionT. */
243 public static final int TREE_EXPR_WRAPPER = TREE_base + 60;
244
245
246 // ----------------------------------------------------------------------
247 // DOCumentation kinds.
248
249 /** A doc comment. DocCommentT. */
250 public static final int TREE_DOC_COMMENT = TREE_base + 61;
251
252 /** An inline doc tag. DocTagT. */
253 public static final int TREE_DOC_TAG = TREE_base + 62;
254
255 ;
256
257
258 // ----------------------------------------------------------------------
259
260 public static final int TREE_STMT_base = TREE_STMT_ASSERT;
261 public static final int TREE_STMT_max = TREE_STMT_WHILE + 1;
262 public static final int TREE_EXPR_base = TREE_EXPR_ANNOTATION;
263 public static final int TREE_EXPR_max = TREE_EXPR_WRAPPER + 1;
264 public static final int TREE_max = TREE_base + 63;
265
266
267 // ----------------------------------------------------------------------
268
269 private final int ordinal;
270
271 private final String name;
272
273 private final Class treeClass;
274
275 private TreeKind(int ordinal, String name, Class treeClass)
276 {
277 this.ordinal = ordinal;
278 this.name = name;
279 this.treeClass = treeClass;
280 }
281
282 public Class getTreeClass()
283 {
284 return treeClass;
285 }
286
287 // ----------------------------------------------------------------------
288
289 // Begin enum compatibility section.
290
291 public String name()
292 {
293 return name;
294 }
295
296 public String toString()
297 {
298 return name();
299 }
300
301 public int ordinal()
302 {
303 return ordinal;
304 }
305
306 public int hashCode()
307 {
308 return ordinal();
309 }
310
311 public int compareTo(TreeKind other)
312 {
313 return ordinal() - other.ordinal();
314 }
315
316 public boolean equals(Object other)
317 {
318 if (other instanceof TreeKind)
319 {
320 final TreeKind tk = (TreeKind) other;
321 return ordinal() == tk.ordinal();
322 }
323
324 return false;
325 }
326
327 public Class getDeclaringClass()
328 {
329 return TreeKind.class;
330 }
331
332
333 // ----------------------------------------------------------------------
334
335 private static final Map values = new LinkedHashMap();
336
337 private static final String[] TREE_names =
338 {
339 "TREE_ANNOTATION",
340 "TREE_BLOCK",
341 "TREE_CLASS_BODY",
342 "TREE_CLASS_D",
343 "TREE_CLASS_INITIALIZER",
344 "TREE_CONSTRUCTOR_D",
345 "TREE_ENUM_CONSTANT_D",
346 "TREE_FIELD_D",
347 "TREE_FIELD_VARIABLE",
348 "TREE_FILE",
349 "TREE_FORMAL_PARAMETER",
350 "TREE_FORMAL_PARAMETER_LIST",
351 "TREE_IMPORT_D",
352 "TREE_INTERFACES_D",
353 "TREE_LOCAL_VARIABLE",
354 "TREE_LOCAL_VARIABLE_D",
355 "TREE_METHOD_D",
356 "TREE_MODIFIERS",
357 "TREE_NAME",
358 "TREE_PACKAGE_D",
359 "TREE_STATEMENT_LABEL",
360 "TREE_SUPERCLASS",
361 "TREE_SWITCH_LABEL",
362 "TREE_THROWS",
363 "TREE_TYPE_REFERENCE",
364 "TREE_TYPE_ARGUMENT",
365 "TREE_TYPE_PARAMETER",
366 "TREE_STMT_ASSERT",
367 "TREE_STMT_BLOCK",
368 "TREE_STMT_BREAK",
369 "TREE_STMT_CATCH",
370 "TREE_STMT_CONTINUE",
371 "TREE_STMT_DO",
372 "TREE_STMT_ELSE",
373 "TREE_STMT_EMPTY",
374 "TREE_STMT_EXPRESSION",
375 "TREE_STMT_FINALLY",
376 "TREE_STMT_FOR",
377 "TREE_STMT_IF",
378 "TREE_STMT_RETURN",
379 "TREE_STMT_SWITCH",
380 "TREE_STMT_SYNCH",
381 "TREE_STMT_THROW",
382 "TREE_STMT_TRY",
383 "TREE_STMT_WHILE",
384 "TREE_EXPR_ANNOTATION",
385 "TREE_EXPR_ARRAY_ACCESS",
386 "TREE_EXPR_ARRAY_CREATOR",
387 "TREE_EXPR_ASSIGNMENT",
388 "TREE_EXPR_CLASS_CREATOR",
389 "TREE_EXPR_DOT",
390 "TREE_EXPR_IDENTIFIER",
391 "TREE_EXPR_INFIX",
392 "TREE_EXPR_LIST",
393 "TREE_EXPR_LITERAL",
394 "TREE_EXPR_METHOD_CALL",
395 "TREE_EXPR_QUESTION",
396 "TREE_EXPR_TYPE",
397 "TREE_EXPR_TYPECAST",
398 "TREE_EXPR_UNARY",
399 "TREE_EXPR_WRAPPER",
400 "TREE_DOC_COMMENT",
401 "TREE_DOC_TAG",
402 };
403
404 private static final Class[] TREE_classes =
405 {
406 AnnotationT.class,
407 BlockT.class,
408 ClassBodyT.class,
409 ClassT.class,
410 ClassInitializerT.class,
411 MethodT.class,
412 FieldDeclT.class,
413 FieldDeclT.class,
414 FieldVariableT.class,
415 FileT.class,
416 FormalParameterT.class,
417 FormalParameterListT.class,
418 ImportT.class,
419 InterfacesT.class,
420 LocalVariableT.class,
421 LocalVariableDeclT.class,
422 MethodT.class,
423 ModifiersT.class,
424 NameT.class,
425 PackageT.class,
426 StatementLabelT.class,
427 SuperclassT.class,
428 SwitchLabelT.class,
429 ThrowsT.class,
430 TypeReferenceT.class,
431 TypeArgumentT.class,
432 TypeParameterT.class,
433 AssertStatementT.class,
434 BlockStatementT.class,
435 BreakStatementT.class,
436 CatchClauseT.class,
437 ContinueStatementT.class,
438 DoStatementT.class,
439 ElseClauseT.class,
440 EmptyStatementT.class,
441 ExpressionStatementT.class,
442 FinallyClauseT.class,
443 ForStatementT.class,
444 IfStatementT.class,
445 ReturnStatementT.class,
446 SwitchStatementT.class,
447 SynchStatementT.class,
448 ThrowStatementT.class,
449 TryStatementT.class,
450 WhileStatementT.class,
451
452 AnnotationExpressionT.class,
453 ArrayAccessExpressionT.class,
454 NewArrayExpressionT.class,
455 AssignmentExpressionT.class,
456 NewClassExpressionT.class,
457 DotExpressionT.class,
458 IdentifierExpressionT.class,
459 InfixExpressionT.class,
460 ListExpressionT.class,
461 LiteralExpressionT.class,
462 MethodCallExpressionT.class,
463 QuestionExpressionT.class,
464 TypeExpressionT.class,
465 TypecastExpressionT.class,
466 UnaryExpressionT.class,
467 WrapperExpressionT.class,
468
469 DocCommentT.class,
470 DocTagT.class,
471 };
472
473 static
474 {
475 for ( int i = TREE_base; i < TREE_max; i++ )
476 {
477 final String name = TREE_names[ i - TREE_base ];
478 values.put( name, new TreeKind(i, name, TREE_classes[ i - TREE_base ]));
479 }
480 }
481
482 public static TreeKind valueOf(int ordinal)
483 {
484 return valueOf(null, TREE_names[ordinal]);
485 }
486
487 public static TreeKind valueOf(Class ignored, String name)
488 {
489 return (TreeKind) values.get(name);
490 }
491
492 public static TreeKind[] values()
493 {
494 final Set entries = values.entrySet();
495 return (TreeKind[]) entries.toArray(new TreeKind[entries.size()]);
496 }
497
498
499 // ----------------------------------------------------------------------
500 }