1 /*******************************************************************************
2 * Copyright (c) 2004, 2007 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core.dom;
13 import java.util.List;
16 * Abstract subclass for type declaration, enum declaration,
17 * and annotation type declaration AST node types.
19 * AbstractTypeDeclaration:
22 * AnnotationTypeDeclaration
27 public abstract class AbstractTypeDeclaration extends BodyDeclaration {
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>)
34 SimpleName typeName = null;
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>)
41 ASTNode.NodeList bodyDeclarations;
44 * Returns structural property descriptor for the "bodyDeclarations" property
47 * @return the property descriptor
49 abstract ChildListPropertyDescriptor internalBodyDeclarationsProperty();
52 * Returns structural property descriptor for the "bodyDeclarations" property
55 * @return the property descriptor
58 public final ChildListPropertyDescriptor getBodyDeclarationsProperty() {
59 return internalBodyDeclarationsProperty();
63 * Returns structural property descriptor for the "name" property
66 * @return the property descriptor
68 abstract ChildPropertyDescriptor internalNameProperty();
71 * Returns structural property descriptor for the "name" property
74 * @return the property descriptor
77 public final ChildPropertyDescriptor getNameProperty() {
78 return internalNameProperty();
82 * Creates and returns a structural property descriptor for the
83 * "bodyDeclaration" property declared on the given concrete node type.
85 * @return the property descriptor
87 static final ChildListPropertyDescriptor internalBodyDeclarationPropertyFactory(Class nodeClass) {
88 return new ChildListPropertyDescriptor(nodeClass, "bodyDeclarations", BodyDeclaration.class, CYCLE_RISK); //$NON-NLS-1$
92 * Creates and returns a structural property descriptor for the
93 * "name" property declared on the given concrete node type.
95 * @return the property descriptor
97 static final ChildPropertyDescriptor internalNamePropertyFactory(Class nodeClass) {
98 return new ChildPropertyDescriptor(nodeClass, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
102 * Creates a new AST node for an abstract type declaration owned by the given
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.
110 * @param ast the AST that is to own this node
112 AbstractTypeDeclaration(AST ast) {
114 this.bodyDeclarations = new ASTNode.NodeList(internalBodyDeclarationsProperty());
118 * Returns the name of the type declared in this type declaration.
120 * @return the type name node
121 * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
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) {
129 this.typeName = new SimpleName(this.ast);
130 postLazyInit(this.typeName, internalNameProperty());
134 return this.typeName;
138 * Sets the name of the type declared in this type declaration to the
141 * @param typeName the new type name
142 * @exception IllegalArgumentException if:
144 * <li>the node belongs to a different AST</li>
145 * <li>the node already has a parent</li>
147 * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
149 public void setName(SimpleName typeName) {
150 if (typeName == null) {
151 throw new IllegalArgumentException();
153 ChildPropertyDescriptor p = internalNameProperty();
154 ASTNode oldChild = this.typeName;
155 preReplaceChild(oldChild, typeName, p);
156 this.typeName = typeName;
157 postReplaceChild(oldChild, typeName, p);
161 * Returns the live ordered list of body declarations of this type
164 * @return the live list of body declarations
165 * (element type: <code>BodyDeclaration</code>)
166 * @since 2.0 (originally declared on <code>TypeDeclaration</code>)
168 public List bodyDeclarations() {
169 return this.bodyDeclarations;
173 * Returns whether this type declaration is a package member (that is,
176 * Note that this is a convenience method that simply checks whether
177 * this node's parent is a compilation unit node.
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>)
184 public boolean isPackageMemberTypeDeclaration() {
185 ASTNode parent = getParent();
186 return (parent instanceof CompilationUnit);
190 * Returns whether this type declaration is a type member.
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
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>)
202 public boolean isMemberTypeDeclaration() {
203 ASTNode parent = getParent();
204 return (parent instanceof AbstractTypeDeclaration)
205 || (parent instanceof AnonymousClassDeclaration);
209 * Returns whether this type declaration is a local type.
211 * Note that this is a convenience method that simply checks whether
212 * this node's parent is a type declaration statement node.
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>)
219 public boolean isLocalTypeDeclaration() {
220 ASTNode parent = getParent();
221 return (parent instanceof TypeDeclarationStatement);
225 * Resolves and returns the binding for the type declared in this type
228 * Note that bindings are generally unavailable unless requested when the
229 * AST is being built.
232 * @return the binding, or <code>null</code> if the binding cannot be
234 * @since 3.1 Declared in 3.0 on the individual subclasses.
236 public final ITypeBinding resolveBinding() {
237 return internalResolveBinding();
241 * Resolves and returns the binding for the type declared in this type
242 * declaration. This method must be implemented by subclasses.
244 * @return the binding, or <code>null</code> if the binding cannot be
247 abstract ITypeBinding internalResolveBinding();
249 /* (omit javadoc for this method)
250 * Method declared on ASTNode.
253 return super.memSize() + 2 * 4;