KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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  */

31 public class MemberValuePair extends ASTNode {
32     
33     /**
34      * The "name" structural property of this node type.
35      */

36     public static final ChildPropertyDescriptor NAME_PROPERTY =
37         new ChildPropertyDescriptor(MemberValuePair.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
38

39     /**
40      * The "value" structural property of this node type.
41      */

42     public static final ChildPropertyDescriptor VALUE_PROPERTY =
43         new ChildPropertyDescriptor(MemberValuePair.class, "value", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
44

45     /**
46      * A list of property descriptors (element type:
47      * {@link StructuralPropertyDescriptor}),
48      * or null if uninitialized.
49      */

50     private static final List JavaDoc PROPERTY_DESCRIPTORS;
51     
52     static {
53         List JavaDoc propertyList = new ArrayList JavaDoc(3);
54         createPropertyList(MemberValuePair.class, propertyList);
55         addProperty(NAME_PROPERTY, propertyList);
56         addProperty(VALUE_PROPERTY, propertyList);
57         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
58     }
59
60     /**
61      * Returns a list of structural property descriptors for this node type.
62      * Clients must not modify the result.
63      *
64      * @param apiLevel the API level; one of the AST.JLS* constants
65      * @return a list of property descriptors (element type:
66      * {@link StructuralPropertyDescriptor})
67      */

68     public static List JavaDoc propertyDescriptors(int apiLevel) {
69         return PROPERTY_DESCRIPTORS;
70     }
71                         
72     /**
73      * The member name; lazily initialized; defaults to a unspecified,
74      * legal name.
75      */

76     private SimpleName name = null;
77
78     /**
79      * The value; lazily initialized; defaults to a unspecified,
80      * legal expression.
81      */

82     private Expression value = null;
83
84     /**
85      * Creates a new AST node for a member value pair owned by the given
86      * AST. By default, the node has an unspecified (but legal) member
87      * name and value.
88      * <p>
89      * N.B. This constructor is package-private.
90      * </p>
91      *
92      * @param ast the AST that is to own this node
93      */

94     MemberValuePair(AST ast) {
95         super(ast);
96         unsupportedIn2();
97     }
98
99     /* (omit javadoc for this method)
100      * Method declared on ASTNode.
101      */

102     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
103         return propertyDescriptors(apiLevel);
104     }
105     
106     /* (omit javadoc for this method)
107      * Method declared on ASTNode.
108      */

109     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
110         if (property == NAME_PROPERTY) {
111             if (get) {
112                 return getName();
113             } else {
114                 setName((SimpleName) child);
115                 return null;
116             }
117         }
118         if (property == VALUE_PROPERTY) {
119             if (get) {
120                 return getValue();
121             } else {
122                 setValue((Expression) child);
123                 return null;
124             }
125         }
126         // allow default implementation to flag the error
127
return super.internalGetSetChildProperty(property, get, child);
128     }
129     
130     /* (omit javadoc for this method)
131      * Method declared on ASTNode.
132      */

133     final int getNodeType0() {
134         return MEMBER_VALUE_PAIR;
135     }
136
137     /* (omit javadoc for this method)
138      * Method declared on ASTNode.
139      */

140     ASTNode clone0(AST target) {
141         MemberValuePair result = new MemberValuePair(target);
142         result.setSourceRange(this.getStartPosition(), this.getLength());
143         result.setName((SimpleName) ASTNode.copySubtree(target, getName()));
144         result.setValue((Expression) ASTNode.copySubtree(target, getValue()));
145         return result;
146     }
147
148     /* (omit javadoc for this method)
149      * Method declared on ASTNode.
150      */

151     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
152         // dispatch to correct overloaded match method
153
return matcher.match(this, other);
154     }
155     
156     /* (omit javadoc for this method)
157      * Method declared on ASTNode.
158      */

159     void accept0(ASTVisitor visitor) {
160         boolean visitChildren = visitor.visit(this);
161         if (visitChildren) {
162             // visit children in normal left to right reading order
163
acceptChild(visitor, getName());
164             acceptChild(visitor, getValue());
165         }
166         visitor.endVisit(this);
167     }
168     
169     /**
170      * Returns the member name.
171      *
172      * @return the member name node
173      */

174     public SimpleName getName() {
175         if (this.name == null) {
176             // lazy init must be thread-safe for readers
177
synchronized (this) {
178                 if (this.name == null) {
179                     preLazyInit();
180                     this.name = new SimpleName(this.ast);
181                     postLazyInit(this.name, NAME_PROPERTY);
182                 }
183             }
184         }
185         return this.name;
186     }
187     
188     /**
189      * Sets the member name.
190      *
191      * @param name the member name node
192      * @exception IllegalArgumentException if:
193      * <ul>
194      * <li>the node belongs to a different AST</li>
195      * <li>the node already has a parent</li>
196      * </ul>
197      */

198     public void setName(SimpleName name) {
199         if (name == null) {
200             throw new IllegalArgumentException JavaDoc();
201         }
202         ASTNode oldChild = this.name;
203         preReplaceChild(oldChild, name, NAME_PROPERTY);
204         this.name = name;
205         postReplaceChild(oldChild, name, NAME_PROPERTY);
206     }
207
208     /**
209      * Returns the value expression.
210      *
211      * @return the value expression
212      */

213     public Expression getValue() {
214         if (this.value == null) {
215             // lazy init must be thread-safe for readers
216
synchronized (this) {
217                 if (this.value == null) {
218                     preLazyInit();
219                     this.value= new SimpleName(this.ast);
220                     postLazyInit(this.value, VALUE_PROPERTY);
221                 }
222             }
223         }
224         return this.value;
225     }
226
227     /**
228      * Sets the value of this pair.
229      *
230      * @param value the new value
231      * @exception IllegalArgumentException if:
232      * <ul>
233      * <li>the node belongs to a different AST</li>
234      * <li>the node already has a parent</li>
235      * <li>a cycle in would be created</li>
236      * </ul>
237      */

238     public void setValue(Expression value) {
239         if (value == null) {
240             throw new IllegalArgumentException JavaDoc();
241         }
242         ASTNode oldChild = this.value;
243         preReplaceChild(oldChild, value, VALUE_PROPERTY);
244         this.value = value;
245         postReplaceChild(oldChild, value, VALUE_PROPERTY);
246     }
247
248     /* (omit javadoc for this method)
249      * Method declared on ASTNode.
250      */

251     int memSize() {
252         return BASE_NODE_SIZE + 2 * 4;
253     }
254     
255     /* (omit javadoc for this method)
256      * Method declared on ASTNode.
257      */

258     int treeSize() {
259         return
260             memSize()
261             + (this.name == null ? 0 : getName().treeSize())
262             + (this.value == null ? 0 : getValue().treeSize());
263     }
264 }
265
Popular Tags