Source code: javax/ide/model/java/source/tree/ClassT.java
1 /*
2 * @(#)ClassT.java
3 */
4
5 package javax.ide.model.java.source.tree;
6
7 import java.util.Collection;
8 import java.util.LinkedHashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 /**
14 * A type declaration. These include: "class" type, "interface" type,
15 * "enum" type, "annotation" type. <p/>
16 *
17 * @author Andy Yu
18 * */
19 public interface ClassT
20 extends MemberT, HasNameT, BlockElementT
21 {
22 // ----------------------------------------------------------------------
23
24 public static final ClassT[] EMPTY_ARRAY = new ClassT[ 0 ];
25
26
27 // ----------------------------------------------------------------------
28
29 /**
30 * Identifies what kind of class declaration this is.
31 *
32 * @return The JavaConstants.TY_* constant for this class.
33 */
34 public ClassKind getTypeKind();
35
36 /**
37 * Attempts to change the type of class this is.
38 * <pre>
39 * TY_CLASS: "class".
40 * TY_ENUM: "enum".
41 * TY_INTERFACE: "interface".
42 * TY_ANNOTATE: "@interface".
43 */
44 public void setTypeKind( ClassKind typeKind );
45
46 /**
47 * True if this is a class or an enum type. Backward
48 * compatible. Checks against the presence of ACC_INTERFACE.
49 *
50 * @return True if this is a class or enum type. False if this is an
51 * interface or annotation type.
52 */
53 public boolean isClass();
54
55 /**
56 * True if this is an interface or an annotation type. Backward
57 * compatible. Checks for the presence of ACC_INTERFACE.
58 *
59 * @return True if this is an interface or annotation type. False if
60 * this is a class or enum type.
61 */
62 public boolean isInterface();
63
64 /**
65 * True if this is an enum type. Checks for the presence of
66 * ACC_ENUM.
67 *
68 * @return True if this is an enum type. False otherwise.
69 */
70 public boolean isEnum();
71
72 /**
73 * True if this is an annotation type. Checks for the presence of
74 * ACC_ANNOTATION.
75 *
76 * @return True if this is an annotation type. False otherwise.
77 */
78 public boolean isAnnotation();
79
80
81 // ----------------------------------------------------------------------
82
83 /**
84 * Gets the owning package declaration, null if none..
85 * This amounts to calling getOwningSourceFile().getPackage().
86 *
87 * @return The package declaration element.
88 */
89 public PackageT getPackageDeclaration();
90
91 /**
92 * Gets the owning package name, empty if none.
93 *
94 * Provided for convenience. This amounts to calling
95 * getOwningSourceFile().getPackageName().
96 */
97 public String getPackageName();
98
99
100 // ----------------------------------------------------------------------
101
102 /**
103 * True if this is an inner class. Member, local, and anonymous classes
104 * are all considered to be inner classes.
105 *
106 * @return True if this is an inner class. False otherwise.
107 */
108 public boolean isInnerClass();
109
110 /**
111 * @return True if this is a top-level class or a member class.
112 * False otherwise.
113 */
114 public boolean isExported();
115
116 /**
117 * True if this is an anonymous class declaration.
118 *
119 * @return True if this is an anonymous class.
120 */
121 public boolean isAnonymous();
122
123
124 // ----------------------------------------------------------------------
125
126 /**
127 * Gets the superclass declaration. On a "class" type, this will be
128 * the "extends" clause.
129 *
130 * @return The super clause belonging to this class. Always non-null.
131 * If there is no superclass clause, then a synthetic one is returned.
132 */
133 public SuperclassT getSuperclassClause();
134
135 /**
136 * Gets the interfaces declaration. On a "class" or "enum" type, this will
137 * be the "implements" clause. On an "interface" type, this will be the
138 * "extends" clause.
139 *
140 * @return The super clause belonging to this class. Always non-null.
141 * If there is no interfaces clause, then a synthetic one is returned.
142 */
143 public InterfacesT getInterfacesClause();
144
145 /**
146 * Gets the class body.
147 *
148 * @return The class body belonging to this class. Always non-null.
149 * If there is no class body, then a synthetic one is returned. Note:
150 * It is a compile error if there is no class body.
151 */
152 public ClassBodyT getClassBody();
153
154
155 // ----------------------------------------------------------------------
156
157 /**
158 * Gets the ordered list of type parameters declared on this class.
159 * Remember, type parameters are not inherited by subclasses (and
160 * subinterfaces). <p/>
161 *
162 * For classes, syntax is "javadoc mods class name <ty_params> {}".
163 *
164 * @return The list of type parameters. <p/>
165 *
166 * List of TypeParameterTs.
167 */
168 public List getTypeParameters();
169
170 /**
171 * Gets the declared superclass. On a "class" type, this will be the
172 * "extends" type.
173 *
174 * @return The explicitly declared superclass. Will be non-null if a
175 * superclass is declared, even if the superclass cannot be legally
176 * resolved. Will be null if no superclass is declared regardless of
177 * the implicit superclass declaration.
178 */
179 public TypeReferenceT getSuperclass();
180
181 /**
182 * Attempts to set the declared base class of this class.
183 *
184 * @throws UnsupportedOperationException if this class may not have a
185 * superclass.
186 * @throws IllegalStateException if the input element is already linked.
187 */
188 public void setSuperclass( TypeReferenceT superclass );
189
190 /**
191 * Gets the list of declared interfaces. Do not confuse this with
192 * inner classes that are "interface" types. The name was chosen to be
193 * consistent with JavaType and java/lang/Class. <p/>
194 *
195 * On a "class" or "enum" type, this will be the "implements" types. On an
196 * "interface" type, this will be the "extends" types.
197 *
198 * @return The list of explicitly declared interfaces. Will have an element
199 * for each super interface that is declared, even if the super interface
200 * cannot be legally resolved. <p/>
201 *
202 * List of TypeReferenceTs.
203 */
204 public List getInterfaces();
205
206 /**
207 * Gets the list of declared member declarations.
208 *
209 * @return All declared members of this class, not including synthetic
210 * members (e.g. default constructor). This includes: methods, constuctors,
211 * field declarations, enum constant declarations, member classes, and
212 * class initializers. <p/>
213 *
214 * List of MemberTs.
215 */
216 public List getDeclaredMembers();
217
218 /**
219 * Gets the list of declared field declarations, including enum
220 * constant declarations.
221 *
222 * @return All declared field declarations, not including synthetic
223 * ones (e.g. "this$0"). This includes enum constant declarations. <p/>
224 *
225 * List of FieldDeclTs.
226 */
227 public List getDeclaredFieldDeclarations();
228
229 /**
230 * Gets the collection of declared field variables, including
231 * enum constant variables.
232 *
233 * @return All declared field variables, not including synthetic
234 * ones (e.g. "this$0"). This includes enum constant variables. <p/>
235 *
236 * Collection of FieldVariableTs.
237 */
238 public Collection getDeclaredFieldVariables();
239
240 /**
241 * Gets the matching declared field variable, null if none.
242 *
243 * @param name The field name to match. Require non-null.
244 *
245 * @return The first matching source-model declared field. Null if none.
246 */
247 public FieldVariableT getDeclaredFieldVariable( String name );
248
249 /**
250 * Gets the list of declared method (but not constructor)
251 * declarations.
252 *
253 * @return All declared methods, not including constructors and not
254 * including synthetic ones (e.g. "values()", "<clinit>()"). <p/>
255 *
256 * List of MethodTs.
257 */
258 public List getDeclaredMethods();
259
260 /**
261 * Gets the collection of matching methods (but not constructor)
262 * declarations. This list will not be filtered according to method
263 * signature. In other words, if there are multiple methods listed
264 * with the same signature, this list will include them all.
265 *
266 * @param name The method name to match. Require non-null.
267 *
268 * @return All matching declared methods, but not constructors and not
269 * synthetic ones (e.g. "values()", "<clinit>()"). <p/>
270 *
271 * Collection of MethodTs.
272 */
273 public Collection getDeclaredMethods( String name );
274
275 /**
276 * Gets the matching method declaration, null if none. This list will
277 * not be filtered according to method signature. In other words, if
278 * multiple methods are listed with the same signature, this list will
279 * include them all.
280 *
281 * @param name Require non-null.
282 * @param targetTypes Null indicates an empty parameter list.
283 *
284 * @return The first matching declared source-model method (but not
285 * constructors). Null if none.
286 */
287 // public MethodT getDeclaredMethod( String name, JavaType[] targetTypes );
288
289 /**
290 * Gets the list of declared constructor declarations.
291 *
292 * @return All declared constructor declarations, not including
293 * synthetic ones. <p/>
294 *
295 * List of MethodTs.
296 */
297 public List getDeclaredConstructors();
298
299 /**
300 * Gets the matching declared constructor declaration, null if none.
301 *
302 * @param targetTypes Null indicates an empty parameter list.
303 *
304 * @return The first matching declared source-model constructor.
305 * Null if none.
306 */
307 // public MethodT getDeclaredConstructor( JavaType[] targetTypes );
308
309 /**
310 * Gets the list of member class declarations. To be clear, this
311 * list does not include local or anonymous classes.
312 *
313 * @return All declared member classes. <p/>
314 *
315 * List of ClassTs.
316 */
317 public List getDeclaredClasses();
318
319 /**
320 * Gets the matching member class declarations. To be clear, this will
321 * not return a local or anonymous class.
322 *
323 * @param name The class name to match.
324 *
325 * @return The first matching member class.
326 */
327 public ClassT getDeclaredClasses( String name );
328
329 /**
330 * Gets the list of declared class initializers. To be clear,
331 * this list does NOT include the implicit block elements generated
332 * for member variable initializers.
333 *
334 * @return All initializers declared in this class, not including
335 * synthetic ones. <p/>
336 *
337 * List of ClassInitializerTs.
338 */
339 public List getDeclaredInitializers();
340
341
342 // ----------------------------------------------------------------------
343
344 /**
345 * Gets the owning member declaration, null if none. <p/>
346 *
347 * The Java VM spec 3rd ed requires that local and anonymous classes
348 * have an attribute specifying the enclosing method. At a source level,
349 * we'll return the owning member. <p/>
350 *
351 * @return The directly enclosing member. If the directly enclosing
352 * member is a class, then this will return the same thing as
353 * getOwningClass(). If this is a top-level class, this will return null.
354 */
355 public MemberT getOwningMember();
356
357
358 // ----------------------------------------------------------------------
359
360 /**
361 * An enumeration identifying which kind of type declaration this is.
362 */
363 public static final class ClassKind
364 {
365 /** This ClassT is of the "class" type, not an enum type. */
366 public static final int CLASS_TYPE = 0;
367
368 /** This ClassT is of the "interface" type, not an annotation type. */
369 public static final int INTERFACE_TYPE = 1;
370
371 /** This ClassT is of the "enum" type. */
372 public static final int ENUM_TYPE = 2;
373
374 /** This ClassT is of the "annotation" type. */
375 public static final int ANNOTATION_TYPE = 3;
376
377
378
379 // ----------------------------------------------------------------------
380
381 private final int ordinal;
382
383 private final String name;
384
385 private ClassKind(int ordinal, String name)
386 {
387 this.ordinal = ordinal;
388 this.name = name;
389 }
390
391 // ----------------------------------------------------------------------
392
393 // Begin enum compatibility section.
394
395 public String name()
396 {
397 return name;
398 }
399
400 public String toString()
401 {
402 return name();
403 }
404
405 public int ordinal()
406 {
407 return ordinal;
408 }
409
410 public int hashCode()
411 {
412 return ordinal();
413 }
414
415 public int compareTo(ClassKind other)
416 {
417 return ordinal() - other.ordinal();
418 }
419
420 public boolean equals(Object other)
421 {
422 if (other instanceof ClassKind)
423 {
424 final ClassKind tk = (ClassKind) other;
425 return ordinal() == tk.ordinal();
426 }
427
428 return false;
429 }
430
431 public Class getDeclaringClass()
432 {
433 return ClassKind.class;
434 }
435
436
437 // ----------------------------------------------------------------------
438
439 private static final Map values = new LinkedHashMap();
440
441 private static final String[] CLASS_names =
442 {
443 "CLASS_TYPE",
444 "INTERFACE_TYPE",
445 "ENUM_TYPE",
446 "ANNOTATION_TYPE",
447 };
448
449 static
450 {
451 for ( int i = 0; i < 4; i++ )
452 {
453 final String name = CLASS_names[i];
454 values.put( name, new ClassKind(i, name ) );
455 }
456 }
457
458 public static ClassKind valueOf(int ordinal)
459 {
460 return valueOf(null, CLASS_names[ordinal]);
461 }
462
463 public static ClassKind valueOf(Class ignored, String name)
464 {
465 return (ClassKind) values.get(name);
466 }
467
468 public static ClassKind[] values()
469 {
470 final Set entries = values.entrySet();
471 return (ClassKind[])entries.toArray(new ClassKind[entries.size()]);
472 }
473
474
475 // ----------------------------------------------------------------------
476
477 }
478 }