KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > AbstractTypeDeclaration


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core.dom;
12
13 import java.util.List JavaDoc;
14
15 /**
16  * Abstract subclass for type declaration, enum declaration,
17  * and annotation type declaration AST node types.
18  * <pre>
19  * AbstractTypeDeclaration:
20  * TypeDeclaration
21  * EnumDeclaration
22  * AnnotationTypeDeclaration
23  * </pre>
24  *
25  * @since 3.0
26  */

27 public abstract class AbstractTypeDeclaration extends BodyDeclaration {
28     
29     /**
30      * The type name; lazily initialized; defaults to a unspecified,
31      * legal Java class identifier.
32      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
33      */

34     SimpleName typeName = null;
35
36     /**
37      * The body declarations (element type: <code>BodyDeclaration</code>).
38      * Defaults to an empty list.
39      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
40      */

41     ASTNode.NodeList bodyDeclarations;
42
43     /**
44      * Returns structural property descriptor for the "bodyDeclarations" property
45      * of this node.
46      *
47      * @return the property descriptor
48      */

49     abstract ChildListPropertyDescriptor internalBodyDeclarationsProperty();
50
51     /**
52      * Returns structural property descriptor for the "bodyDeclarations" property
53      * of this node.
54      *
55      * @return the property descriptor
56      * @since 3.1
57      */

58     public final ChildListPropertyDescriptor getBodyDeclarationsProperty() {
59         return internalBodyDeclarationsProperty();
60     }
61
62     /**
63      * Returns structural property descriptor for the "name" property
64      * of this node.
65      *
66      * @return the property descriptor
67      */

68     abstract ChildPropertyDescriptor internalNameProperty();
69     
70     /**
71      * Returns structural property descriptor for the "name" property
72      * of this node.
73      *
74      * @return the property descriptor
75      * @since 3.1
76      */

77     public final ChildPropertyDescriptor getNameProperty() {
78         return internalNameProperty();
79     }
80     
81     /**
82      * Creates and returns a structural property descriptor for the
83      * "bodyDeclaration" property declared on the given concrete node type.
84      *
85      * @return the property descriptor
86      */

87     static final ChildListPropertyDescriptor internalBodyDeclarationPropertyFactory(Class JavaDoc nodeClass) {
88         return new ChildListPropertyDescriptor(nodeClass, "bodyDeclarations", BodyDeclaration.class, CYCLE_RISK); //$NON-NLS-1$
89
}
90     
91     /**
92      * Creates and returns a structural property descriptor for the
93      * "name" property declared on the given concrete node type.
94      *
95      * @return the property descriptor
96      */

97     static final ChildPropertyDescriptor internalNamePropertyFactory(Class JavaDoc nodeClass) {
98         return new ChildPropertyDescriptor(nodeClass, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
99
}
100     
101     /**
102      * Creates a new AST node for an abstract type declaration owned by the given
103      * AST.
104      * <p>
105      * N.B. This constructor is package-private; all subclasses must be
106      * declared in the same package; clients are unable to declare
107      * additional subclasses.
108      * </p>
109      *
110      * @param ast the AST that is to own this node
111      */

112     AbstractTypeDeclaration(AST ast) {
113         super(ast);
114         this.bodyDeclarations = new ASTNode.NodeList(internalBodyDeclarationsProperty());
115     }
116
117     /**
118      * Returns the name of the type declared in this type declaration.
119      *
120      * @return the type name node
121      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
122      */

123     public SimpleName getName() {
124         if (this.typeName == null) {
125             // lazy init must be thread-safe for readers
126
synchronized (this) {
127                 if (this.typeName == null) {
128                     preLazyInit();
129                     this.typeName = new SimpleName(this.ast);
130                     postLazyInit(this.typeName, internalNameProperty());
131                 }
132             }
133         }
134         return this.typeName;
135     }
136         
137     /**
138      * Sets the name of the type declared in this type declaration to the
139      * given name.
140      *
141      * @param typeName the new type name
142      * @exception IllegalArgumentException if:
143      * <ul>
144      * <li>the node belongs to a different AST</li>
145      * <li>the node already has a parent</li>
146      * </ul>
147      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
148      */

149     public void setName(SimpleName typeName) {
150         if (typeName == null) {
151             throw new IllegalArgumentException JavaDoc();
152         }
153         ChildPropertyDescriptor p = internalNameProperty();
154         ASTNode oldChild = this.typeName;
155         preReplaceChild(oldChild, typeName, p);
156         this.typeName = typeName;
157         postReplaceChild(oldChild, typeName, p);
158     }
159
160     /**
161      * Returns the live ordered list of body declarations of this type
162      * declaration.
163      *
164      * @return the live list of body declarations
165      * (element type: <code>BodyDeclaration</code>)
166      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
167      */

168     public List JavaDoc bodyDeclarations() {
169         return this.bodyDeclarations;
170     }
171     
172     /**
173      * Returns whether this type declaration is a package member (that is,
174      * a top-level type).
175      * <p>
176      * Note that this is a convenience method that simply checks whether
177      * this node's parent is a compilation unit node.
178      * </p>
179      *
180      * @return <code>true</code> if this type declaration is a child of
181      * a compilation unit node, and <code>false</code> otherwise
182      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
183      */

184     public boolean isPackageMemberTypeDeclaration() {
185         ASTNode parent = getParent();
186         return (parent instanceof CompilationUnit);
187     }
188
189     /**
190      * Returns whether this type declaration is a type member.
191      * <p>
192      * Note that this is a convenience method that simply checks whether
193      * this node's parent is a type declaration node or an anonymous
194      * class declaration.
195      * </p>
196      *
197      * @return <code>true</code> if this type declaration is a child of
198      * a type declaration node or an anonymous class declaration node,
199      * and <code>false</code> otherwise
200      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
201      */

202     public boolean isMemberTypeDeclaration() {
203         ASTNode parent = getParent();
204         return (parent instanceof AbstractTypeDeclaration)
205             || (parent instanceof AnonymousClassDeclaration);
206     }
207
208     /**
209      * Returns whether this type declaration is a local type.
210      * <p>
211      * Note that this is a convenience method that simply checks whether
212      * this node's parent is a type declaration statement node.
213      * </p>
214      *
215      * @return <code>true</code> if this type declaration is a child of
216      * a type declaration statement node, and <code>false</code> otherwise
217      * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
218      */

219     public boolean isLocalTypeDeclaration() {
220         ASTNode parent = getParent();
221         return (parent instanceof TypeDeclarationStatement);
222     }
223     
224     /**
225      * Resolves and returns the binding for the type declared in this type
226      * declaration.
227      * <p>
228      * Note that bindings are generally unavailable unless requested when the
229      * AST is being built.
230      * </p>
231      *
232      * @return the binding, or <code>null</code> if the binding cannot be
233      * resolved
234      * @since 3.1 Declared in 3.0 on the individual subclasses.
235      */

236     public final ITypeBinding resolveBinding() {
237         return internalResolveBinding();
238     }
239     
240     /**
241      * Resolves and returns the binding for the type declared in this type
242      * declaration. This method must be implemented by subclasses.
243      *
244      * @return the binding, or <code>null</code> if the binding cannot be
245      * resolved
246      */

247     abstract ITypeBinding internalResolveBinding();
248     
249     /* (omit javadoc for this method)
250      * Method declared on ASTNode.
251      */

252     int memSize() {
253         return super.memSize() + 2 * 4;
254     }
255     
256 }
257
Popular Tags