KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Local type declaration statement AST node type.
19  * <p>
20  * This kind of node is used to convert a type declaration
21  * node into a statement node by wrapping it.
22  * </p>
23  * For JLS2:
24  * <pre>
25  * TypeDeclarationStatement:
26  * TypeDeclaration
27  * </pre>
28  * For JLS3, the kinds of type declarations grew to include enum declarations:
29  * <pre>
30  * TypeDeclarationStatement:
31  * TypeDeclaration
32  * EnumDeclaration
33  * </pre>
34  * Although allowed at the AST, not all arrangements of AST nodes are meaningful;
35  * in particular, only class and enum declarations are meaningful in the context of
36  * a block.
37  *
38  * @since 2.0
39  */

40 public class TypeDeclarationStatement extends Statement {
41     
42     /**
43      * The "typeDeclaration" structural property of this node type (JLS2 API only).
44      * @since 3.0
45      */

46     public static final ChildPropertyDescriptor TYPE_DECLARATION_PROPERTY =
47         new ChildPropertyDescriptor(TypeDeclarationStatement.class, "typeDeclaration", TypeDeclaration.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
48

49     /**
50      * The "declaration" structural property of this node type (added in JLS3 API).
51      * @since 3.1
52      */

53     public static final ChildPropertyDescriptor DECLARATION_PROPERTY =
54         new ChildPropertyDescriptor(TypeDeclarationStatement.class, "declaration", AbstractTypeDeclaration.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
55

56     /**
57      * A list of property descriptors (element type:
58      * {@link StructuralPropertyDescriptor}),
59      * or null if uninitialized.
60      * @since 3.0
61      */

62     private static final List JavaDoc PROPERTY_DESCRIPTORS_2_0;
63     
64     /**
65      * A list of property descriptors (element type:
66      * {@link StructuralPropertyDescriptor}),
67      * or null if uninitialized.
68      * @since 3.1
69      */

70     private static final List JavaDoc PROPERTY_DESCRIPTORS_3_0;
71     
72     static {
73         List JavaDoc propertyList = new ArrayList JavaDoc(2);
74         createPropertyList(TypeDeclarationStatement.class, propertyList);
75         addProperty(TYPE_DECLARATION_PROPERTY, propertyList);
76         PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList);
77         
78         propertyList = new ArrayList JavaDoc(2);
79         createPropertyList(TypeDeclarationStatement.class, propertyList);
80         addProperty(DECLARATION_PROPERTY, propertyList);
81         PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList);
82     }
83
84     /**
85      * Returns a list of structural property descriptors for this node type.
86      * Clients must not modify the result.
87      *
88      * @param apiLevel the API level; one of the
89      * <code>AST.JLS*</code> constants
90
91      * @return a list of property descriptors (element type:
92      * {@link StructuralPropertyDescriptor})
93      * @since 3.0
94      */

95     public static List JavaDoc propertyDescriptors(int apiLevel) {
96         if (apiLevel == AST.JLS2_INTERNAL) {
97             return PROPERTY_DESCRIPTORS_2_0;
98         } else {
99             return PROPERTY_DESCRIPTORS_3_0;
100         }
101     }
102             
103     /**
104      * The type declaration; lazily initialized; defaults to a unspecified,
105      * but legal, type declaration. In JLS2, corresponds to TYPE_DECLARATION_PROPERTY.
106      * After JLS2, corresponds to DECLARATION_PROPERTY.
107      * @see #typeDeclProperty
108      */

109     private AbstractTypeDeclaration typeDecl = null;
110     
111     /**
112      * The child property stored on the <code>typeDecl</code> instance variable.
113      * In JLS2, corresponds to TYPE_DECLARATION_PROPERTY. After JLS2, corresponds to
114      * DECLARATION_PROPERTY.
115      *
116      * @return the property corresponding to the <code>typeDecl</code> instance variable;
117      * never <code>null</code>
118      */

119     private ChildPropertyDescriptor typeDeclProperty () {
120         if (getAST().apiLevel() == AST.JLS2_INTERNAL) {
121             return TYPE_DECLARATION_PROPERTY;
122         } else {
123             return DECLARATION_PROPERTY;
124         }
125     }
126
127
128     /**
129      * Creates a new unparented local type declaration statement node owned
130      * by the given AST. By default, the local type declaration is an
131      * unspecified, but legal, type declaration.
132      * <p>
133      * N.B. This constructor is package-private.
134      * </p>
135      *
136      * @param ast the AST that is to own this node
137      */

138     TypeDeclarationStatement(AST ast) {
139         super(ast);
140     }
141
142     /* (omit javadoc for this method)
143      * Method declared on ASTNode.
144      * @since 3.0
145      */

146     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
147         return propertyDescriptors(apiLevel);
148     }
149     
150     /* (omit javadoc for this method)
151      * Method declared on ASTNode.
152      */

153     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
154         if (property == TYPE_DECLARATION_PROPERTY) {
155             if (get) {
156                 return getTypeDeclaration();
157             } else {
158                 setTypeDeclaration((TypeDeclaration) child);
159                 return null;
160             }
161         }
162         if (property == DECLARATION_PROPERTY) {
163             if (get) {
164                 return getDeclaration();
165             } else {
166                 setDeclaration((AbstractTypeDeclaration) child);
167                 return null;
168             }
169         }
170         // allow default implementation to flag the error
171
return super.internalGetSetChildProperty(property, get, child);
172     }
173     
174     /* (omit javadoc for this method)
175      * Method declared on ASTNode.
176      */

177     final int getNodeType0() {
178         return TYPE_DECLARATION_STATEMENT;
179     }
180
181     /* (omit javadoc for this method)
182      * Method declared on ASTNode.
183      */

184     ASTNode clone0(AST target) {
185         TypeDeclarationStatement result =
186             new TypeDeclarationStatement(target);
187         result.setSourceRange(this.getStartPosition(), this.getLength());
188         result.copyLeadingComment(this);
189         result.setDeclaration(
190             (AbstractTypeDeclaration) getDeclaration().clone(target));
191         return result;
192     }
193     
194     /* (omit javadoc for this method)
195      * Method declared on ASTNode.
196      */

197     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
198         // dispatch to correct overloaded match method
199
return matcher.match(this, other);
200     }
201
202     /* (omit javadoc for this method)
203      * Method declared on ASTNode.
204      */

205     void accept0(ASTVisitor visitor) {
206         boolean visitChildren = visitor.visit(this);
207         if (visitChildren) {
208             acceptChild(visitor, getDeclaration());
209         }
210         visitor.endVisit(this);
211     }
212     
213     /**
214      * Returns the abstract type declaration of this local type declaration
215      * statement (added in JLS3 API).
216      *
217      * @return the type declaration node
218      * @since 3.1
219      */

220     public AbstractTypeDeclaration getDeclaration() {
221         if (this.typeDecl == null) {
222             // lazy init must be thread-safe for readers
223
synchronized (this) {
224                 if (this.typeDecl == null) {
225                     preLazyInit();
226                     this.typeDecl = new TypeDeclaration(this.ast);
227                     postLazyInit(this.typeDecl, typeDeclProperty());
228                 }
229             }
230         }
231         return this.typeDecl;
232     }
233         
234     /**
235      * Sets the abstract type declaration of this local type declaration
236      * statement (added in JLS3 API).
237      *
238      * @param decl the type declaration node
239      * @exception IllegalArgumentException if:
240      * <ul>
241      * <li>the node belongs to a different AST</li>
242      * <li>the node already has a parent</li>
243      * <li>a cycle in would be created</li>
244      * </ul>
245      * @since 3.1
246      */

247     public void setDeclaration(AbstractTypeDeclaration decl) {
248         if (decl == null) {
249             throw new IllegalArgumentException JavaDoc();
250         }
251         // a TypeDeclarationStatement may occur inside an
252
// TypeDeclaration - must check cycles
253
ASTNode oldChild = this.typeDecl;
254         ChildPropertyDescriptor typeDeclProperty = typeDeclProperty();
255         preReplaceChild(oldChild, decl, typeDeclProperty);
256         this.typeDecl= decl;
257         postReplaceChild(oldChild, decl, typeDeclProperty);
258     }
259     
260     /**
261      * Returns the type declaration of this local type declaration
262      * statement (JLS2 API only).
263      *
264      * @return the type declaration node
265      * @exception UnsupportedOperationException if this operation is used in
266      * an AST later than JLS2
267      * @deprecated In the JLS3 API, this method is replaced by
268      * {@link #getDeclaration()}, which returns <code>AbstractTypeDeclaration</code>
269      * instead of <code>TypeDeclaration</code>.
270      */

271     public TypeDeclaration getTypeDeclaration() {
272         return internalGetTypeDeclaration();
273     }
274     
275     /**
276      * Internal synonym for deprecated method. Used to avoid
277      * deprecation warnings.
278      * @since 3.1
279      */

280     /*package*/ final TypeDeclaration internalGetTypeDeclaration() {
281         supportedOnlyIn2();
282         return (TypeDeclaration) getDeclaration();
283     }
284         
285     /**
286      * Sets the type declaration of this local type declaration
287      * statement (JLS2 API only).
288      *
289      * @param decl the type declaration node
290      * @exception IllegalArgumentException if:
291      * <ul>
292      * <li>the node belongs to a different AST</li>
293      * <li>the node already has a parent</li>
294      * <li>a cycle in would be created</li>
295      * </ul>
296      * @exception UnsupportedOperationException if this operation is used in
297      * an AST later than JLS2
298      * @deprecated In the JLS3 API, this method is replaced by
299      * {@link #setDeclaration(AbstractTypeDeclaration)} which takes
300      * <code>AbstractTypeDeclaration</code> instead of
301      * <code>TypeDeclaration</code>.
302      */

303     public void setTypeDeclaration(TypeDeclaration decl) {
304         internalSetTypeDeclaration(decl);
305     }
306     
307     /**
308      * Internal synonym for deprecated method. Used to avoid
309      * deprecation warnings.
310      * @since 3.1
311      */

312     /*package*/ final void internalSetTypeDeclaration(TypeDeclaration decl) {
313         supportedOnlyIn2();
314         // forward to non-deprecated replacement method
315
setDeclaration(decl);
316     }
317     
318     /**
319      * Resolves and returns the binding for the class or interface declared in
320      * this type declaration statement.
321      * <p>
322      * Note that bindings are generally unavailable unless requested when the
323      * AST is being built.
324      * </p>
325      *
326      * @return the binding, or <code>null</code> if the binding cannot be
327      * resolved
328      */

329     public ITypeBinding resolveBinding() {
330         // forward request to the wrapped type declaration
331
AbstractTypeDeclaration d = getDeclaration();
332         if (d instanceof TypeDeclaration) {
333             return ((TypeDeclaration) d).resolveBinding();
334         } else if (d instanceof AnnotationTypeDeclaration) {
335             return ((AnnotationTypeDeclaration) d).resolveBinding();
336         } else {
337             // shouldn't happen
338
return null;
339         }
340     }
341     
342     /* (omit javadoc for this method)
343      * Method declared on ASTNode.
344      */

345     int memSize() {
346         return super.memSize() + 1 * 4;
347     }
348     
349     /* (omit javadoc for this method)
350      * Method declared on ASTNode.
351      */

352     int treeSize() {
353         return
354             memSize()
355             + (this.typeDecl == null ? 0 : getDeclaration().treeSize());
356     }
357 }
358
359
Popular Tags