Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0db79569560853893252f872968d844e96cae2fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) 2004, 2019 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.dom;

/**
 * Abstract base class of AST nodes that represent annotations.
 * <pre>
 * Annotation:
 *		NormalAnnotation
 *		MarkerAnnotation
 *		SingleMemberAnnotation
 * </pre>
 * @since 3.1
 */
@SuppressWarnings("rawtypes")
public abstract class Annotation extends Expression implements IExtendedModifier {

	/**
	 * Returns structural property descriptor for the "typeName" property
	 * of this node (child type: {@link Name}).
	 *
	 * @return the property descriptor
	 */
	abstract ChildPropertyDescriptor internalTypeNameProperty();

	/**
	 * Returns structural property descriptor for the "typeName" property
	 * of this node (child type: {@link Name}).
	 *
	 * @return the property descriptor
	 */
	public final ChildPropertyDescriptor getTypeNameProperty() {
		return internalTypeNameProperty();
	}

	/**
	 * Creates and returns a structural property descriptor for the
	 * "typeName" property declared on the given concrete node type (child type: {@link Name}).
	 *
	 * @return the property descriptor
	 */
	static final ChildPropertyDescriptor internalTypeNamePropertyFactory(Class nodeClass) {
		return new ChildPropertyDescriptor(nodeClass, "typeName", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
	}

	/**
	 * The annotation type name; lazily initialized; defaults to an unspecified,
	 * legal Java identifier.
	 */
	Name typeName = null;

	/**
	 * Creates a new AST node for an annotation node owned by the
	 * given AST.
	 * <p>
	 * N.B. This constructor is package-private.
	 * </p>
	 *
	 * @param ast the AST that is to own this node
	 */
	Annotation(AST ast) {
		super(ast);
	}

	/**
	 * @see IExtendedModifier#isModifier()
	 */
	@Override
	public boolean isModifier() {
		return false;
	}

	/**
	 * @see IExtendedModifier#isAnnotation()
	 */
	@Override
	public boolean isAnnotation() {
		return true;
	}

	/**
	 * Returns the annotation type name of this annotation.
	 *
	 * @return the annotation type name
	 */
	public Name getTypeName() {
		if (this.typeName == null) {
			// lazy init must be thread-safe for readers
			synchronized (this) {
				if (this.typeName == null) {
					preLazyInit();
					this.typeName = new SimpleName(this.ast);
					postLazyInit(this.typeName, internalTypeNameProperty());
				}
			}
		}
		return this.typeName;
	}

	/**
	 * Sets the annotation type name of this annotation.
	 *
	 * @param typeName the annotation type name
	 * @exception IllegalArgumentException if:
	 * <ul>
	 * <li>the node belongs to a different AST</li>
	 * <li>the node already has a parent</li>
	 * </ul>
	 */
	public void setTypeName(Name typeName) {
		if (typeName == null) {
			throw new IllegalArgumentException();
		}
		ChildPropertyDescriptor p = internalTypeNameProperty();
		ASTNode oldChild = this.typeName;
		preReplaceChild(oldChild, typeName, p);
		this.typeName = typeName;
		postReplaceChild(oldChild, typeName, p);
	}

	/**
	 * Returns whether this is a normal annotation
	 * ({@link NormalAnnotation}).
	 *
	 * @return <code>true</code> if this is a normal annotation,
	 *    and <code>false</code> otherwise
	 */
	public boolean isNormalAnnotation() {
		return (this instanceof NormalAnnotation);
	}

	/**
	 * Returns whether this is a marker annotation
	 * ({@link MarkerAnnotation}).
	 *
	 * @return <code>true</code> if this is a marker annotation,
	 *    and <code>false</code> otherwise
	 */
	public boolean isMarkerAnnotation() {
		return (this instanceof MarkerAnnotation);
	}

	/**
	 * Returns whether this is a single member annotation.
	 * ({@link SingleMemberAnnotation}).
	 *
	 * @return <code>true</code> if this is a single member annotation,
	 *    and <code>false</code> otherwise
	 */
	public boolean isSingleMemberAnnotation() {
		return (this instanceof SingleMemberAnnotation);
	}

	@Override
	int memSize() {
		return BASE_NODE_SIZE + 1 * 4;
	}

	/**
	 * Resolves and returns the resolved annotation for this annotation.
	 * <p>
	 * Note that bindings (which includes resolved annotations) are generally unavailable unless
	 * requested when the AST is being built.
	 * </p>
	 *
	 * @return the resolved annotation, or <code>null</code> if the annotation cannot be resolved
	 * @since 3.2
	 */
	public IAnnotationBinding resolveAnnotationBinding() {
	    return this.ast.getBindingResolver().resolveAnnotation(this);
	}
}

Back to the top