001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * Annotation type member declaration AST node type (added in JLS3 API).
017: * <pre>
018: * AnnotationTypeMemberDeclaration:
019: * [ Javadoc ] { ExtendedModifier }
020: * Type Identifier <b>(</b> <b>)</b> [ <b>default</b> Expression ] <b>;</b>
021: * </pre>
022: * <p>
023: * Note that annotation type member declarations are only meaningful as
024: * elements of {@link AnnotationTypeDeclaration#bodyDeclarations()}.
025: * </p>
026: * <p>
027: * When a Javadoc comment is present, the source
028: * range begins with the first character of the "/**" comment delimiter.
029: * When there is no Javadoc comment, the source range begins with the first
030: * character of the first modifier keyword (if modifiers),
031: * or the first character of the member type (no modifiers).
032: * The source range extends through the last character of the
033: * ";" token.
034: * </p>
035: *
036: * @since 3.1
037: */
038: public class AnnotationTypeMemberDeclaration extends BodyDeclaration {
039:
040: /**
041: * The "javadoc" structural property of this node type.
042: */
043: public static final ChildPropertyDescriptor JAVADOC_PROPERTY = internalJavadocPropertyFactory(AnnotationTypeMemberDeclaration.class);
044:
045: /**
046: * The "modifiers" structural property of this node type.
047: */
048: public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = internalModifiers2PropertyFactory(AnnotationTypeMemberDeclaration.class);
049:
050: /**
051: * The "name" structural property of this node type.
052: */
053: public static final ChildPropertyDescriptor NAME_PROPERTY = new ChildPropertyDescriptor(
054: AnnotationTypeMemberDeclaration.class,
055: "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
056:
057: /**
058: * The "type" structural property of this node type.
059: */
060: public static final ChildPropertyDescriptor TYPE_PROPERTY = new ChildPropertyDescriptor(
061: AnnotationTypeMemberDeclaration.class,
062: "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
063:
064: /**
065: * The "default" structural property of this node type.
066: */
067: public static final ChildPropertyDescriptor DEFAULT_PROPERTY = new ChildPropertyDescriptor(
068: AnnotationTypeMemberDeclaration.class,
069: "default", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
070:
071: /**
072: * A list of property descriptors (element type:
073: * {@link StructuralPropertyDescriptor}),
074: * or null if uninitialized.
075: */
076: private static final List PROPERTY_DESCRIPTORS;
077:
078: static {
079: List properyList = new ArrayList(6);
080: createPropertyList(AnnotationTypeMemberDeclaration.class,
081: properyList);
082: addProperty(JAVADOC_PROPERTY, properyList);
083: addProperty(MODIFIERS2_PROPERTY, properyList);
084: addProperty(NAME_PROPERTY, properyList);
085: addProperty(TYPE_PROPERTY, properyList);
086: addProperty(DEFAULT_PROPERTY, properyList);
087: PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
088: }
089:
090: /**
091: * Returns a list of structural property descriptors for this node type.
092: * Clients must not modify the result.
093: *
094: * @param apiLevel the API level; one of the
095: * <code>AST.JLS*</code> constants
096: * @return a list of property descriptors (element type:
097: * {@link StructuralPropertyDescriptor})
098: */
099: 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(
148: ChildPropertyDescriptor property, boolean get, ASTNode child) {
149: if (property == JAVADOC_PROPERTY) {
150: if (get) {
151: return getJavadoc();
152: } else {
153: setJavadoc((Javadoc) child);
154: return null;
155: }
156: }
157: if (property == NAME_PROPERTY) {
158: if (get) {
159: return getName();
160: } else {
161: setName((SimpleName) child);
162: return null;
163: }
164: }
165: if (property == NAME_PROPERTY) {
166: if (get) {
167: return getName();
168: } else {
169: setName((SimpleName) child);
170: return null;
171: }
172: }
173: if (property == TYPE_PROPERTY) {
174: if (get) {
175: return getType();
176: } else {
177: setType((Type) child);
178: return null;
179: }
180: }
181: if (property == DEFAULT_PROPERTY) {
182: if (get) {
183: return getDefault();
184: } else {
185: setDefault((Expression) child);
186: return null;
187: }
188: }
189: // allow default implementation to flag the error
190: return super .internalGetSetChildProperty(property, get, child);
191: }
192:
193: /* (omit javadoc for this method)
194: * Method declared on ASTNode.
195: */
196: final List internalGetChildListProperty(
197: ChildListPropertyDescriptor property) {
198: if (property == MODIFIERS2_PROPERTY) {
199: return modifiers();
200: }
201: // allow default implementation to flag the error
202: return super .internalGetChildListProperty(property);
203: }
204:
205: /* (omit javadoc for this method)
206: * Method declared on BodyDeclaration.
207: */
208: final ChildPropertyDescriptor internalJavadocProperty() {
209: return JAVADOC_PROPERTY;
210: }
211:
212: /* (omit javadoc for this method)
213: * Method declared on BodyDeclaration.
214: */
215: final ChildListPropertyDescriptor internalModifiers2Property() {
216: return MODIFIERS2_PROPERTY;
217: }
218:
219: /* (omit javadoc for this method)
220: * Method declared on BodyDeclaration.
221: */
222: final SimplePropertyDescriptor internalModifiersProperty() {
223: // this property will not be asked for (node type did not exist in JLS2)
224: return null;
225: }
226:
227: /* (omit javadoc for this method)
228: * Method declared on ASTNode.
229: */
230: final int getNodeType0() {
231: return ANNOTATION_TYPE_MEMBER_DECLARATION;
232: }
233:
234: /* (omit javadoc for this method)
235: * Method declared on ASTNode.
236: */
237: ASTNode clone0(AST target) {
238: AnnotationTypeMemberDeclaration result = new AnnotationTypeMemberDeclaration(
239: target);
240: result
241: .setSourceRange(this .getStartPosition(), this
242: .getLength());
243: result.setJavadoc((Javadoc) ASTNode.copySubtree(target,
244: getJavadoc()));
245: result.modifiers().addAll(
246: ASTNode.copySubtrees(target, modifiers()));
247: result.setType((Type) ASTNode.copySubtree(target, getType()));
248: result.setName((SimpleName) getName().clone(target));
249: result.setDefault((Expression) ASTNode.copySubtree(target,
250: getDefault()));
251: return result;
252: }
253:
254: /* (omit javadoc for this method)
255: * Method declared on ASTNode.
256: */
257: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
258: // dispatch to correct overloaded match method
259: return matcher.match(this , other);
260: }
261:
262: /* (omit javadoc for this method)
263: * Method declared on ASTNode.
264: */
265: void accept0(ASTVisitor visitor) {
266: boolean visitChildren = visitor.visit(this );
267: if (visitChildren) {
268: // visit children in normal left to right reading order
269: acceptChild(visitor, getJavadoc());
270: acceptChildren(visitor, this .modifiers);
271: acceptChild(visitor, getType());
272: acceptChild(visitor, getName());
273: acceptChild(visitor, getDefault());
274: }
275: visitor.endVisit(this );
276: }
277:
278: /**
279: * Returns the name of the annotation type member declared in this declaration.
280: *
281: * @return the member name node
282: */
283: public SimpleName getName() {
284: if (this .memberName == null) {
285: // lazy init must be thread-safe for readers
286: synchronized (this ) {
287: if (this .memberName == null) {
288: preLazyInit();
289: this .memberName = new SimpleName(this .ast);
290: postLazyInit(this .memberName, NAME_PROPERTY);
291: }
292: }
293: }
294: return this .memberName;
295: }
296:
297: /**
298: * Sets the name of the annotation type member declared in this declaration to the
299: * given name.
300: *
301: * @param memberName the new member name
302: * @exception IllegalArgumentException if:
303: * <ul>
304: * <li>the node belongs to a different AST</li>
305: * <li>the node already has a parent</li>
306: * </ul>
307: */
308: public void setName(SimpleName memberName) {
309: if (memberName == null) {
310: throw new IllegalArgumentException();
311: }
312: ASTNode oldChild = this .memberName;
313: preReplaceChild(oldChild, memberName, NAME_PROPERTY);
314: this .memberName = memberName;
315: postReplaceChild(oldChild, memberName, NAME_PROPERTY);
316: }
317:
318: /**
319: * Returns the type of the annotation type member declared in this
320: * declaration.
321: *
322: * @return the type of the member
323: */
324: public Type getType() {
325: if (this .memberType == null) {
326: // lazy init must be thread-safe for readers
327: synchronized (this ) {
328: if (this .memberType == null) {
329: preLazyInit();
330: this .memberType = this .ast
331: .newPrimitiveType(PrimitiveType.INT);
332: postLazyInit(this .memberType, TYPE_PROPERTY);
333: }
334: }
335: }
336: return this .memberType;
337: }
338:
339: /**
340: * Sets the type of the annotation type member declared in this declaration
341: * to the given type.
342: *
343: * @param type the new member type
344: * @exception IllegalArgumentException if:
345: * <ul>
346: * <li>the node belongs to a different AST</li>
347: * <li>the node already has a parent</li>
348: * </ul>
349: */
350: public void setType(Type type) {
351: if (type == null) {
352: throw new IllegalArgumentException();
353: }
354: ASTNode oldChild = this .memberType;
355: preReplaceChild(oldChild, type, TYPE_PROPERTY);
356: this .memberType = type;
357: postReplaceChild(oldChild, type, TYPE_PROPERTY);
358: }
359:
360: /**
361: * Returns the default value of this annotation type member, or
362: * <code>null</code> if there is none.
363: *
364: * @return the expression node, or <code>null</code> if there is none
365: */
366: public Expression getDefault() {
367: return this .optionalDefaultValue;
368: }
369:
370: /**
371: * Sets or clears the default value of this annotation type member.
372: *
373: * @param defaultValue the expression node, or <code>null</code> if
374: * there is none
375: * @exception IllegalArgumentException if:
376: * <ul>
377: * <li>the node belongs to a different AST</li>
378: * <li>the node already has a parent</li>
379: * <li>a cycle in would be created</li>
380: * </ul>
381: */
382: public void setDefault(Expression defaultValue) {
383: // a AnnotationTypeMemberDeclaration may occur inside an Expression - must check cycles
384: ASTNode oldChild = this .optionalDefaultValue;
385: preReplaceChild(oldChild, defaultValue, DEFAULT_PROPERTY);
386: this .optionalDefaultValue = defaultValue;
387: postReplaceChild(oldChild, defaultValue, DEFAULT_PROPERTY);
388: }
389:
390: /**
391: * Resolves and returns the binding for the annotation type member declared
392: * in this declaration.
393: * <p>
394: * Note that bindings are generally unavailable unless requested when the
395: * AST is being built.
396: * </p>
397: *
398: * @return the binding, or <code>null</code> if the binding cannot be
399: * resolved
400: */
401: public IMethodBinding resolveBinding() {
402: return this .ast.getBindingResolver().resolveMember(this );
403: }
404:
405: /* (omit javadoc for this method)
406: * Method declared on ASTNode.
407: */
408: int memSize() {
409: return super .memSize() + 3 * 4;
410: }
411:
412: /* (omit javadoc for this method)
413: * Method declared on ASTNode.
414: */
415: int treeSize() {
416: return memSize()
417: + (this .optionalDocComment == null ? 0 : getJavadoc()
418: .treeSize())
419: + this .modifiers.listSize()
420: + (this .memberName == null ? 0 : getName().treeSize())
421: + (this .memberType == null ? 0 : getType().treeSize())
422: + (this .optionalDefaultValue == null ? 0 : getDefault()
423: .treeSize());
424: }
425: }
|