001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 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: /**
013: * Abstract base class of AST nodes that represent annotations.
014: * <p>
015: * <pre>
016: * Annotation:
017: * NormalAnnotation
018: * MarkerAnnotation
019: * SingleMemberAnnotation
020: * </pre>
021: * </p>
022: * @since 3.1
023: */
024: public abstract class Annotation extends Expression implements
025: IExtendedModifier {
026:
027: /**
028: * Returns structural property descriptor for the "typeName" property
029: * of this node.
030: *
031: * @return the property descriptor
032: */
033: abstract ChildPropertyDescriptor internalTypeNameProperty();
034:
035: /**
036: * Returns structural property descriptor for the "typeName" property
037: * of this node.
038: *
039: * @return the property descriptor
040: */
041: public final ChildPropertyDescriptor getTypeNameProperty() {
042: return internalTypeNameProperty();
043: }
044:
045: /**
046: * Creates and returns a structural property descriptor for the
047: * "typeName" property declared on the given concrete node type.
048: *
049: * @return the property descriptor
050: */
051: static final ChildPropertyDescriptor internalTypeNamePropertyFactory(
052: Class nodeClass) {
053: return new ChildPropertyDescriptor(nodeClass,
054: "typeName", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
055: }
056:
057: /**
058: * The annotation type name; lazily initialized; defaults to an unspecified,
059: * legal Java identifier.
060: */
061: Name typeName = null;
062:
063: /**
064: * Creates a new AST node for an annotation node owned by the
065: * given AST.
066: * <p>
067: * N.B. This constructor is package-private.
068: * </p>
069: *
070: * @param ast the AST that is to own this node
071: */
072: Annotation(AST ast) {
073: super (ast);
074: }
075:
076: /**
077: * @see IExtendedModifier#isModifier()
078: */
079: public boolean isModifier() {
080: return false;
081: }
082:
083: /**
084: * @see IExtendedModifier#isAnnotation()
085: */
086: public boolean isAnnotation() {
087: return true;
088: }
089:
090: /**
091: * Returns the annotation type name of this annotation.
092: *
093: * @return the annotation type name
094: */
095: public Name getTypeName() {
096: if (this .typeName == null) {
097: // lazy init must be thread-safe for readers
098: synchronized (this ) {
099: if (this .typeName == null) {
100: preLazyInit();
101: this .typeName = new SimpleName(this .ast);
102: postLazyInit(this .typeName,
103: internalTypeNameProperty());
104: }
105: }
106: }
107: return this .typeName;
108: }
109:
110: /**
111: * Sets the annotation type name of this annotation.
112: *
113: * @param typeName the annotation type name
114: * @exception IllegalArgumentException if:
115: * <ul>
116: * <li>the node belongs to a different AST</li>
117: * <li>the node already has a parent</li>
118: * </ul>
119: */
120: public void setTypeName(Name typeName) {
121: if (typeName == null) {
122: throw new IllegalArgumentException();
123: }
124: ChildPropertyDescriptor p = internalTypeNameProperty();
125: ASTNode oldChild = this .typeName;
126: preReplaceChild(oldChild, typeName, p);
127: this .typeName = typeName;
128: postReplaceChild(oldChild, typeName, p);
129: }
130:
131: /**
132: * Returns whether this is a normal annotation
133: * ({@link NormalAnnotation}).
134: *
135: * @return <code>true</code> if this is a normal annotation,
136: * and <code>false</code> otherwise
137: */
138: public boolean isNormalAnnotation() {
139: return (this instanceof NormalAnnotation);
140: }
141:
142: /**
143: * Returns whether this is a marker annotation
144: * ({@link MarkerAnnotation}).
145: *
146: * @return <code>true</code> if this is a marker annotation,
147: * and <code>false</code> otherwise
148: */
149: public boolean isMarkerAnnotation() {
150: return (this instanceof MarkerAnnotation);
151: }
152:
153: /**
154: * Returns whether this is a single member annotation.
155: * ({@link SingleMemberAnnotation}).
156: *
157: * @return <code>true</code> if this is a single member annotation,
158: * and <code>false</code> otherwise
159: */
160: public boolean isSingleMemberAnnotation() {
161: return (this instanceof SingleMemberAnnotation);
162: }
163:
164: /* (omit javadoc for this method)
165: * Method declared on ASTNode.
166: */
167: int memSize() {
168: return BASE_NODE_SIZE + 1 * 4;
169: }
170:
171: /**
172: * Resolves and returns the resolved annotation for this annotation.
173: * <p>
174: * Note that bindings (which includes resolved annotations) are generally unavailable unless
175: * requested when the AST is being built.
176: * </p>
177: *
178: * @return the resolved annotation, or <code>null</code> if the annotation cannot be resolved
179: * @since 3.2
180: */
181: public IAnnotationBinding resolveAnnotationBinding() {
182: return this.ast.getBindingResolver().resolveAnnotation(this);
183: }
184: }
|