KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.core.dom;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Single member annotation node (added in JLS3 API). The single member annotation
18  * "@foo(bar)" is equivalent to the normal annotation "@foo(value=bar)".
19  * <p>
20  * <pre>
21  * SingleMemberAnnotation:
22  * <b>@</b> TypeName <b>(</b> Expression <b>)</b>
23  * </pre>
24  * Within annotations, only certain kinds of expressions are meaningful,
25  * including other annotations.
26  * </p>
27  *
28  * @since 3.1
29  */

30 public final class SingleMemberAnnotation extends Annotation {
31     
32     /**
33      * The "typeName" structural property of this node type.
34      */

35     public static final ChildPropertyDescriptor TYPE_NAME_PROPERTY =
36         internalTypeNamePropertyFactory(SingleMemberAnnotation.class);
37
38     /**
39      * The "value" structural property of this node type.
40      */

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

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

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

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

75     private Expression value = null;
76
77     /**
78      * Creates a new unparented normal annotation node owned
79      * by the given AST. By default, the annotation has an
80      * unspecified type name and an unspecified value.
81      * <p>
82      * N.B. This constructor is package-private.
83      * </p>
84      *
85      * @param ast the AST that is to own this node
86      */

87     SingleMemberAnnotation(AST ast) {
88         super(ast);
89         unsupportedIn2();
90     }
91
92     /* (omit javadoc for this method)
93      * Method declared on ASTNode.
94      */

95     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
96         return propertyDescriptors(apiLevel);
97     }
98     
99     /* (omit javadoc for this method)
100      * Method declared on ASTNode.
101      */

102     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
103         if (property == TYPE_NAME_PROPERTY) {
104             if (get) {
105                 return getTypeName();
106             } else {
107                 setTypeName((Name) child);
108                 return null;
109             }
110         }
111         if (property == VALUE_PROPERTY) {
112             if (get) {
113                 return getValue();
114             } else {
115                 setValue((Expression) child);
116                 return null;
117             }
118         }
119         // allow default implementation to flag the error
120
return super.internalGetSetChildProperty(property, get, child);
121     }
122     
123     /* (omit javadoc for this method)
124      * Method declared on BodyDeclaration.
125      */

126     final ChildPropertyDescriptor internalTypeNameProperty() {
127         return TYPE_NAME_PROPERTY;
128     }
129
130     /* (omit javadoc for this method)
131      * Method declared on ASTNode.
132      */

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

140     ASTNode clone0(AST target) {
141         SingleMemberAnnotation result = new SingleMemberAnnotation(target);
142         result.setSourceRange(this.getStartPosition(), this.getLength());
143         result.setTypeName((Name) ASTNode.copySubtree(target, getTypeName()));
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, getTypeName());
164             acceptChild(visitor, getValue());
165         }
166         visitor.endVisit(this);
167     }
168     
169     /**
170      * Returns the value of this annotation.
171      *
172      * @return the value node
173      */

174     public Expression getValue() {
175         if (this.value == null) {
176             // lazy init must be thread-safe for readers
177
synchronized (this) {
178                 if (this.value == null) {
179                     preLazyInit();
180                     this.value = new SimpleName(this.ast);
181                     postLazyInit(this.value, VALUE_PROPERTY);
182                 }
183             }
184         }
185         return this.value;
186     }
187         
188     /**
189      * Sets the value of this annotation.
190      *
191      * @param value the new value
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      * <li>a cycle in would be created</li>
197      * </ul>
198      */

199     public void setValue(Expression value) {
200         if (value == null) {
201             throw new IllegalArgumentException JavaDoc();
202         }
203         ASTNode oldChild = this.value;
204         preReplaceChild(oldChild, value, VALUE_PROPERTY);
205         this.value = value;
206         postReplaceChild(oldChild, value, VALUE_PROPERTY);
207     }
208
209     /* (omit javadoc for this method)
210      * Method declared on ASTNode.
211      */

212     int memSize() {
213         return super.memSize() + 1 * 4;
214     }
215     
216     /* (omit javadoc for this method)
217      * Method declared on ASTNode.
218      */

219     int treeSize() {
220         return
221             memSize()
222             + (this.typeName == null ? 0 : getTypeName().treeSize())
223             + (this.value == null ? 0 : getValue().treeSize());
224     }
225 }
226
Popular Tags