removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / MemberValuePair.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
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 /**
18  * Member value pair node (added in JLS3 API). Member value pairs appear in annotations.
19  * <p>
20  * <pre>
21  * MemberValuePair:
22  *   SimpleName <b>=</b> Expression
23  * </pre>
24  * Within annotations, only certain kinds of expressions are meaningful,
25  * including other annotations.
26  * </p>
27  *
28  * @see NormalAnnotation
29  * @since 3.1
30  * @noinstantiate This class is not intended to be instantiated by clients.
31  */
32 public class MemberValuePair extends ASTNode {
33         
34         /**
35          * The "name" structural property of this node type.
36          */
37         public static final ChildPropertyDescriptor NAME_PROPERTY = 
38                 new ChildPropertyDescriptor(MemberValuePair.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
39
40         /**
41          * The "value" structural property of this node type.
42          */
43         public static final ChildPropertyDescriptor VALUE_PROPERTY = 
44                 new ChildPropertyDescriptor(MemberValuePair.class, "value", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
45
46         /**
47          * A list of property descriptors (element type: 
48          * {@link StructuralPropertyDescriptor}),
49          * or null if uninitialized.
50          */
51         private static final List PROPERTY_DESCRIPTORS;
52         
53         static {
54                 List propertyList = new ArrayList(3);
55                 createPropertyList(MemberValuePair.class, propertyList);
56                 addProperty(NAME_PROPERTY, propertyList);
57                 addProperty(VALUE_PROPERTY, propertyList);
58                 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
59         }
60
61         /**
62          * Returns a list of structural property descriptors for this node type.
63          * Clients must not modify the result.
64          * 
65          * @param apiLevel the API level; one of the AST.JLS* constants
66          * @return a list of property descriptors (element type: 
67          * {@link StructuralPropertyDescriptor})
68          */
69         public static List propertyDescriptors(int apiLevel) {
70                 return PROPERTY_DESCRIPTORS;
71         }
72                                                 
73         /**
74          * The member name; lazily initialized; defaults to a unspecified,
75          * legal name.
76          */
77         private SimpleName name = null;
78
79         /**
80          * The value; lazily initialized; defaults to a unspecified,
81          * legal expression.
82          */
83         private Expression value = null;
84         
85         /**
86          * Creates a new AST node for a member value pair owned by the given 
87          * AST. By default, the node has an unspecified (but legal) member
88          * name and value.
89          * <p>
90          * N.B. This constructor is package-private.
91          * </p>
92          * 
93          * @param ast the AST that is to own this node
94          */
95         MemberValuePair(AST ast) {
96                 super(ast);
97             unsupportedIn2();
98         }
99
100         /* (omit javadoc for this method)
101          * Method declared on ASTNode.
102          */
103         final List internalStructuralPropertiesForType(int apiLevel) {
104                 return propertyDescriptors(apiLevel);
105         }
106         
107         /* (omit javadoc for this method)
108          * Method declared on ASTNode.
109          */
110         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
111                 if (property == NAME_PROPERTY) {
112                         if (get) {
113                                 return getName();
114                         } else {
115                                 setName((SimpleName) child);
116                                 return null;
117                         }
118                 }
119                 if (property == VALUE_PROPERTY) {
120                         if (get) {
121                                 return getValue();
122                         } else {
123                                 setValue((Expression) child);
124                                 return null;
125                         }
126                 }
127                 // allow default implementation to flag the error
128                 return super.internalGetSetChildProperty(property, get, child);
129         }
130         
131         /* (omit javadoc for this method)
132          * Method declared on ASTNode.
133          */
134         final int getNodeType0() {
135                 return MEMBER_VALUE_PAIR;
136         }
137
138         /* (omit javadoc for this method)
139          * Method declared on ASTNode.
140          */
141         ASTNode clone0(AST target) {
142                 MemberValuePair result = new MemberValuePair(target);
143                 result.setSourceRange(this.getStartPosition(), this.getLength());
144                 result.setName((SimpleName) ASTNode.copySubtree(target, getName()));
145                 result.setValue((Expression) ASTNode.copySubtree(target, getValue()));
146                 return result;
147         }
148
149         /* (omit javadoc for this method)
150          * Method declared on ASTNode.
151          */
152         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
153                 // dispatch to correct overloaded match method
154                 return matcher.match(this, other);
155         }
156         
157         /* (omit javadoc for this method)
158          * Method declared on ASTNode.
159          */
160         void accept0(ASTVisitor visitor) {
161                 boolean visitChildren = visitor.visit(this);
162                 if (visitChildren) {
163                         // visit children in normal left to right reading order
164                         acceptChild(visitor, getName());
165                         acceptChild(visitor, getValue());
166                 }
167                 visitor.endVisit(this);
168         }
169         
170         /**
171          * Returns the member name.
172          * 
173          * @return the member name node
174          */ 
175         public SimpleName getName() {
176                 if (this.name == null) {
177                         // lazy init must be thread-safe for readers
178                         synchronized (this) {
179                                 if (this.name == null) {
180                                         preLazyInit();
181                                         this.name = new SimpleName(this.ast);
182                                         postLazyInit(this.name, NAME_PROPERTY);
183                                 }
184                         }
185                 }
186                 return this.name;
187         }
188         
189         /**
190          * Resolves and returns the member value pair binding for this member value pair.
191          * <p>
192          * Note that bindings are generally unavailable unless requested when the
193          * AST is being built.
194          * </p>
195          * 
196          * @return the binding, or <code>null</code> if the binding cannot be 
197          *    resolved
198          * @since 3.2
199          */     
200         public final IMemberValuePairBinding resolveMemberValuePairBinding() {
201                 return this.ast.getBindingResolver().resolveMemberValuePair(this);
202         }
203
204         /**
205          * Sets the member name.
206          * 
207          * @param name the member name node
208          * @exception IllegalArgumentException if:
209          * <ul>
210          * <li>the node belongs to a different AST</li>
211          * <li>the node already has a parent</li>
212          * </ul>
213          */ 
214         public void setName(SimpleName name) {
215                 if (name == null) {
216                         throw new IllegalArgumentException();
217                 }
218                 ASTNode oldChild = this.name;
219                 preReplaceChild(oldChild, name, NAME_PROPERTY);
220                 this.name = name;
221                 postReplaceChild(oldChild, name, NAME_PROPERTY);
222         }
223
224         /**
225          * Returns the value expression.
226          * 
227          * @return the value expression
228          */ 
229         public Expression getValue() {
230                 if (this.value == null) {
231                         // lazy init must be thread-safe for readers
232                         synchronized (this) {
233                                 if (this.value == null) {
234                                         preLazyInit();
235                                         this.value= new SimpleName(this.ast);
236                                         postLazyInit(this.value, VALUE_PROPERTY);
237                                 }
238                         }
239                 }
240                 return this.value;
241         }
242
243         /**
244          * Sets the value of this pair.
245          * 
246          * @param value the new value
247          * @exception IllegalArgumentException if:
248          * <ul>
249          * <li>the node belongs to a different AST</li>
250          * <li>the node already has a parent</li>
251          * <li>a cycle in would be created</li>
252          * </ul>
253          */ 
254         public void setValue(Expression value) {
255                 if (value == null) {
256                         throw new IllegalArgumentException();
257                 }
258                 ASTNode oldChild = this.value;
259                 preReplaceChild(oldChild, value, VALUE_PROPERTY);
260                 this.value = value;
261                 postReplaceChild(oldChild, value, VALUE_PROPERTY);
262         }
263
264         /* (omit javadoc for this method)
265          * Method declared on ASTNode.
266          */
267         int memSize() {
268                 return BASE_NODE_SIZE + 2 * 4;
269         }
270         
271         /* (omit javadoc for this method)
272          * Method declared on ASTNode.
273          */
274         int treeSize() {
275                 return 
276                         memSize()
277                         + (this.name == null ? 0 : getName().treeSize())
278                         + (this.value == null ? 0 : getValue().treeSize());
279         }
280 }