Initial upgrade to Platform/JDT 3.4.1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / Annotation.java
1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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 net.sourceforge.phpdt.core.dom;
12
13 /**
14  * Abstract base class of AST nodes that represent annotations.
15  * <p>
16  * <pre>
17  * Annotation:
18  *              NormalAnnotation
19  *              MarkerAnnotation
20  *              SingleMemberAnnotation
21  * </pre>
22  * </p>
23  * @since 3.1
24  */
25 public abstract class Annotation extends Expression implements IExtendedModifier {
26         
27         /**
28          * Returns structural property descriptor for the "typeName" property
29          * of this node.
30          * 
31          * @return the property descriptor
32          */
33         abstract ChildPropertyDescriptor internalTypeNameProperty();
34
35         /**
36          * Returns structural property descriptor for the "typeName" property
37          * of this node.
38          * 
39          * @return the property descriptor
40          */
41         public final ChildPropertyDescriptor getTypeNameProperty() {
42                 return internalTypeNameProperty();
43         }
44
45         /**
46          * Creates and returns a structural property descriptor for the
47          * "typeName" property declared on the given concrete node type.
48          * 
49          * @return the property descriptor
50          */
51         static final ChildPropertyDescriptor internalTypeNamePropertyFactory(Class nodeClass) {
52                 return new ChildPropertyDescriptor(nodeClass, "typeName", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
53         }
54         
55         /**
56          * The annotation type name; lazily initialized; defaults to an unspecified,
57          * legal Java identifier.
58          */
59         Name typeName = null;
60
61         /**
62          * Creates a new AST node for an annotation node owned by the 
63          * given AST.
64          * <p>
65          * N.B. This constructor is package-private.
66          * </p>
67          * 
68          * @param ast the AST that is to own this node
69          */
70         Annotation(AST ast) {
71                 super(ast);
72         }
73         
74         /**
75          * @see IExtendedModifier#isModifier()
76          */ 
77         public boolean isModifier() {
78                 return false;
79         }
80         
81         /**
82          * @see IExtendedModifier#isAnnotation()
83          */ 
84         public boolean isAnnotation() {
85                 return true;
86         }
87
88         /**
89          * Returns the annotation type name of this annotation.
90          * 
91          * @return the annotation type name
92          */ 
93         public Name getTypeName() {
94                 if (this.typeName == null) {
95                         // lazy init must be thread-safe for readers
96                         synchronized (this) {
97                                 if (this.typeName == null) {
98                                         preLazyInit();
99                                         this.typeName = new SimpleName(this.ast);
100                                         postLazyInit(this.typeName, internalTypeNameProperty());
101                                 }
102                         }
103                 }
104                 return this.typeName;
105         }
106         
107         /**
108          * Sets the annotation type name of this annotation.
109          * 
110          * @param typeName the annotation type name
111          * @exception IllegalArgumentException if:
112          * <ul>
113          * <li>the node belongs to a different AST</li>
114          * <li>the node already has a parent</li>
115          * </ul>
116          */ 
117         public void setTypeName(Name typeName) {
118                 if (typeName == null) {
119                         throw new IllegalArgumentException();
120                 }
121                 ChildPropertyDescriptor p = internalTypeNameProperty();
122                 ASTNode oldChild = this.typeName;
123                 preReplaceChild(oldChild, typeName, p);
124                 this.typeName = typeName;
125                 postReplaceChild(oldChild, typeName, p);
126         }
127
128         /**
129          * Returns whether this is a normal annotation
130          * ({@link NormalAnnotation}).
131          * 
132          * @return <code>true</code> if this is a normal annotation,
133          *    and <code>false</code> otherwise
134          */ 
135         public boolean isNormalAnnotation() {
136                 return (this instanceof NormalAnnotation);
137         }
138
139         /**
140          * Returns whether this is a marker annotation
141          * ({@link MarkerAnnotation}).
142          * 
143          * @return <code>true</code> if this is a marker annotation,
144          *    and <code>false</code> otherwise
145          */ 
146         public boolean isMarkerAnnotation() {
147                 return (this instanceof MarkerAnnotation);
148         }
149
150         /**
151          * Returns whether this is a single member annotation.
152          * ({@link SingleMemberAnnotation}).
153          * 
154          * @return <code>true</code> if this is a single member annotation,
155          *    and <code>false</code> otherwise
156          */ 
157         public boolean isSingleMemberAnnotation() {
158                 return (this instanceof SingleMemberAnnotation);
159         }
160
161         /* (omit javadoc for this method)
162          * Method declared on ASTNode.
163          */
164         int memSize() {
165                 return BASE_NODE_SIZE + 1 * 4;
166         }
167
168         /**
169          * Resolves and returns the resolved annotation for this annotation.
170          * <p>
171          * Note that bindings (which includes resolved annotations) are generally unavailable unless
172          * requested when the AST is being built.
173          * </p>
174          * 
175          * @return the resolved annotation, or <code>null</code> if the annotation cannot be resolved
176          * @since 3.2
177          */     
178         public IAnnotationBinding resolveAnnotationBinding() {
179             return this.ast.getBindingResolver().resolveAnnotation(this);
180         }
181 }