removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / AnnotationTypeMemberDeclaration.java
1 /*******************************************************************************
2  * Copyright (c) 2004, 2008 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 net.sourceforge.phpdt.core.dom;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 /**
17  * Annotation type member declaration AST node type (added in JLS3 API).
18  * <pre>
19  * AnnotationTypeMemberDeclaration:
20  *   [ Javadoc ] { ExtendedModifier }
21  *       Type Identifier <b>(</b> <b>)</b> [ <b>default</b> Expression ] <b>;</b>
22  * </pre>
23  * <p>
24  * Note that annotation type member declarations are only meaningful as
25  * elements of {@link AnnotationTypeDeclaration#bodyDeclarations()}.
26  * </p>
27  * <p>
28  * When a Javadoc comment is present, the source
29  * range begins with the first character of the "/**" comment delimiter.
30  * When there is no Javadoc comment, the source range begins with the first
31  * character of the first modifier keyword (if modifiers), 
32  * or the first character of the member type (no modifiers). 
33  * The source range extends through the last character of the
34  * ";" token.
35  * </p>
36  * 
37  * @since 3.1
38  * @noinstantiate This class is not intended to be instantiated by clients.
39  */
40 public class AnnotationTypeMemberDeclaration extends BodyDeclaration {
41         
42         /**
43          * The "javadoc" structural property of this node type.
44          */
45         public static final ChildPropertyDescriptor JAVADOC_PROPERTY = 
46                 internalJavadocPropertyFactory(AnnotationTypeMemberDeclaration.class);
47
48         /**
49          * The "modifiers" structural property of this node type.
50          */
51         public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = 
52                 internalModifiers2PropertyFactory(AnnotationTypeMemberDeclaration.class);
53         
54         /**
55          * The "name" structural property of this node type.
56          */
57         public static final ChildPropertyDescriptor NAME_PROPERTY = 
58                 new ChildPropertyDescriptor(AnnotationTypeMemberDeclaration.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
59
60         /**
61          * The "type" structural property of this node type.
62          */
63         public static final ChildPropertyDescriptor TYPE_PROPERTY = 
64                 new ChildPropertyDescriptor(AnnotationTypeMemberDeclaration.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
65
66         /**
67          * The "default" structural property of this node type.
68          */
69         public static final ChildPropertyDescriptor DEFAULT_PROPERTY = 
70                 new ChildPropertyDescriptor(AnnotationTypeMemberDeclaration.class, "default", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
71
72         /**
73          * A list of property descriptors (element type: 
74          * {@link StructuralPropertyDescriptor}),
75          * or null if uninitialized.
76          */
77         private static final List PROPERTY_DESCRIPTORS;
78         
79         static {
80                 List properyList = new ArrayList(6);
81                 createPropertyList(AnnotationTypeMemberDeclaration.class, properyList);
82                 addProperty(JAVADOC_PROPERTY, properyList);
83                 addProperty(MODIFIERS2_PROPERTY, properyList);
84                 addProperty(NAME_PROPERTY, properyList);
85                 addProperty(TYPE_PROPERTY, properyList);
86                 addProperty(DEFAULT_PROPERTY, properyList);
87                 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
88         }
89
90         /**
91          * Returns a list of structural property descriptors for this node type.
92          * Clients must not modify the result.
93          * 
94          * @param apiLevel the API level; one of the
95          * <code>AST.JLS*</code> constants
96          * @return a list of property descriptors (element type: 
97          * {@link StructuralPropertyDescriptor})
98          */
99         public static List propertyDescriptors(int apiLevel) {
100                 return PROPERTY_DESCRIPTORS;
101         }
102                                                 
103         /**
104          * The member name; lazily initialized; defaults to an unspecified,
105          * legal Java identifier.
106          */
107         private SimpleName memberName = null;
108
109         /**
110          * The member type; lazily initialized; defaults to int.
111          */
112         private Type memberType = null;
113         
114         /**
115          * The optional default expression; <code>null</code> for none; defaults to none.
116          */
117         private Expression optionalDefaultValue = null;
118         
119         /**
120          * Creates a new AST node for an annotation type member declaration owned 
121          * by the given AST. By default, the declaration is for a member of an
122          * unspecified, but legal, name; no modifiers; no javadoc;
123          * an unspecified value type; and no default value.
124          * <p>
125          * N.B. This constructor is package-private; all subclasses must be 
126          * declared in the same package; clients are unable to declare 
127          * additional subclasses.
128          * </p>
129          * 
130          * @param ast the AST that is to own this node
131          */
132         AnnotationTypeMemberDeclaration(AST ast) {
133                 super(ast);
134             unsupportedIn2();
135         }
136
137         /* (omit javadoc for this method)
138          * Method declared on ASTNode.
139          */
140         final List internalStructuralPropertiesForType(int apiLevel) {
141                 return propertyDescriptors(apiLevel);
142         }
143         
144         /* (omit javadoc for this method)
145          * Method declared on ASTNode.
146          */
147         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
148                 if (property == JAVADOC_PROPERTY) {
149                         if (get) {
150                                 return getJavadoc();
151                         } else {
152                                 setJavadoc((Javadoc) child);
153                                 return null;
154                         }
155                 }
156                 if (property == NAME_PROPERTY) {
157                         if (get) {
158                                 return getName();
159                         } else {
160                                 setName((SimpleName) child);
161                                 return null;
162                         }
163                 }
164                 if (property == NAME_PROPERTY) {
165                         if (get) {
166                                 return getName();
167                         } else {
168                                 setName((SimpleName) child);
169                                 return null;
170                         }
171                 }
172                 if (property == TYPE_PROPERTY) {
173                         if (get) {
174                                 return getType();
175                         } else {
176                                 setType((Type) child);
177                                 return null;
178                         }
179                 }
180                 if (property == DEFAULT_PROPERTY) {
181                         if (get) {
182                                 return getDefault();
183                         } else {
184                                 setDefault((Expression) child);
185                                 return null;
186                         }
187                 }
188                 // allow default implementation to flag the error
189                 return super.internalGetSetChildProperty(property, get, child);
190         }
191         
192         /* (omit javadoc for this method)
193          * Method declared on ASTNode.
194          */
195         final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
196                 if (property == MODIFIERS2_PROPERTY) {
197                         return modifiers();
198                 }
199                 // allow default implementation to flag the error
200                 return super.internalGetChildListProperty(property);
201         }
202         
203         /* (omit javadoc for this method)
204          * Method declared on BodyDeclaration.
205          */
206         final ChildPropertyDescriptor internalJavadocProperty() {
207                 return JAVADOC_PROPERTY;
208         }
209
210         /* (omit javadoc for this method)
211          * Method declared on BodyDeclaration.
212          */
213         final ChildListPropertyDescriptor internalModifiers2Property() {
214                 return MODIFIERS2_PROPERTY;
215         }
216
217         /* (omit javadoc for this method)
218          * Method declared on BodyDeclaration.
219          */
220         final SimplePropertyDescriptor internalModifiersProperty() {
221                 // this property will not be asked for (node type did not exist in JLS2)
222                 return null;
223         }
224
225         /* (omit javadoc for this method)
226          * Method declared on ASTNode.
227          */
228         final int getNodeType0() {
229                 return ANNOTATION_TYPE_MEMBER_DECLARATION;
230         }
231
232         /* (omit javadoc for this method)
233          * Method declared on ASTNode.
234          */
235         ASTNode clone0(AST target) {
236                 AnnotationTypeMemberDeclaration result = new AnnotationTypeMemberDeclaration(target);
237                 result.setSourceRange(this.getStartPosition(), this.getLength());
238                 result.setJavadoc(
239                         (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
240                 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
241                 result.setType((Type) ASTNode.copySubtree(target, getType()));
242                 result.setName((SimpleName) getName().clone(target));
243                 result.setDefault((Expression) ASTNode.copySubtree(target, getDefault()));
244                 return result;
245         }
246
247         /* (omit javadoc for this method)
248          * Method declared on ASTNode.
249          */
250         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
251                 // dispatch to correct overloaded match method
252                 return matcher.match(this, other);
253         }
254         
255         /* (omit javadoc for this method)
256          * Method declared on ASTNode.
257          */
258         void accept0(ASTVisitor visitor) {
259                 boolean visitChildren = visitor.visit(this);
260                 if (visitChildren) {
261                         // visit children in normal left to right reading order
262                         acceptChild(visitor, getJavadoc());
263                         acceptChildren(visitor, this.modifiers);
264                         acceptChild(visitor, getType());
265                         acceptChild(visitor, getName());
266                         acceptChild(visitor, getDefault());
267                 }
268                 visitor.endVisit(this);
269         }
270         
271         /**
272          * Returns the name of the annotation type member declared in this declaration.
273          * 
274          * @return the member name node
275          */ 
276         public SimpleName getName() {
277                 if (this.memberName == null) {
278                         // lazy init must be thread-safe for readers
279                         synchronized (this) {
280                                 if (this.memberName == null) {
281                                         preLazyInit();
282                                         this.memberName = new SimpleName(this.ast);
283                                         postLazyInit(this.memberName, NAME_PROPERTY);
284                                 }
285                         }
286                 }
287                 return this.memberName;
288         }
289         
290         /**
291          * Sets the name of the annotation type member declared in this declaration to the
292          * given name.
293          * 
294          * @param memberName the new member name
295          * @exception IllegalArgumentException if:
296          * <ul>
297          * <li>the node belongs to a different AST</li>
298          * <li>the node already has a parent</li>
299          * </ul>
300          */ 
301         public void setName(SimpleName memberName) {
302                 if (memberName == null) {
303                         throw new IllegalArgumentException();
304                 }
305                 ASTNode oldChild = this.memberName;
306                 preReplaceChild(oldChild, memberName, NAME_PROPERTY);
307                 this.memberName = memberName;
308                 postReplaceChild(oldChild, memberName, NAME_PROPERTY);
309         }
310
311         /**
312          * Returns the type of the annotation type member declared in this 
313          * declaration.
314          * 
315          * @return the type of the member
316          */ 
317         public Type getType() {
318                 if (this.memberType == null) {
319                         // lazy init must be thread-safe for readers
320                         synchronized (this) {
321                                 if (this.memberType == null) {
322                                         preLazyInit();
323                                         this.memberType = this.ast.newPrimitiveType(PrimitiveType.INT);
324                                         postLazyInit(this.memberType, TYPE_PROPERTY);
325                                 }
326                         }
327                 }
328                 return this.memberType;
329         }
330
331         /**
332          * Sets the type of the annotation type member declared in this declaration
333          * to the given type.
334          * 
335          * @param type the new member type
336          * @exception IllegalArgumentException if:
337          * <ul>
338          * <li>the node belongs to a different AST</li>
339          * <li>the node already has a parent</li>
340          * </ul>
341          */ 
342         public void setType(Type type) {
343                 if (type == null) {
344                         throw new IllegalArgumentException();
345                 }
346                 ASTNode oldChild = this.memberType;
347                 preReplaceChild(oldChild, type, TYPE_PROPERTY);
348                 this.memberType = type;
349                 postReplaceChild(oldChild, type, TYPE_PROPERTY);
350         }
351
352         /**
353          * Returns the default value of this annotation type member, or 
354          * <code>null</code> if there is none.
355          * 
356          * @return the expression node, or <code>null</code> if there is none
357          */ 
358         public Expression getDefault() {
359                 return this.optionalDefaultValue;
360         }
361         
362         /**
363          * Sets or clears the default value of this annotation type member.
364          * 
365          * @param defaultValue the expression node, or <code>null</code> if 
366          *    there is none
367          * @exception IllegalArgumentException if:
368          * <ul>
369          * <li>the node belongs to a different AST</li>
370          * <li>the node already has a parent</li>
371          * <li>a cycle in would be created</li>
372          * </ul>
373          */ 
374         public void setDefault(Expression defaultValue) {
375                 // a AnnotationTypeMemberDeclaration may occur inside an Expression - must check cycles
376                 ASTNode oldChild = this.optionalDefaultValue;
377                 preReplaceChild(oldChild, defaultValue, DEFAULT_PROPERTY);
378                 this.optionalDefaultValue = defaultValue;
379                 postReplaceChild(oldChild, defaultValue, DEFAULT_PROPERTY);
380         }
381         
382         /**
383          * Resolves and returns the binding for the annotation type member declared
384          * in this declaration.
385          * <p>
386          * Note that bindings are generally unavailable unless requested when the
387          * AST is being built.
388          * </p>
389          * 
390          * @return the binding, or <code>null</code> if the binding cannot be 
391          *    resolved
392          */     
393         public IMethodBinding resolveBinding() {
394                 return this.ast.getBindingResolver().resolveMember(this);
395         }
396
397         /* (omit javadoc for this method)
398          * Method declared on ASTNode.
399          */
400         int memSize() {
401                 return super.memSize() + 3 * 4;
402         }
403         
404         /* (omit javadoc for this method)
405          * Method declared on ASTNode.
406          */
407         int treeSize() {
408                 return
409                         memSize()
410                         + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
411                         + this.modifiers.listSize()
412                         + (this.memberName == null ? 0 : getName().treeSize())
413                         + (this.memberType == null ? 0 : getType().treeSize())
414                         + (this.optionalDefaultValue == null ? 0 : getDefault().treeSize());
415         }
416 }
417