1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2004, 2019 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | package org.eclipse.jdt.core.dom; |
15 | |
16 | import java.util.ArrayList; |
17 | import java.util.List; |
18 | |
19 | /** |
20 | * Single member annotation node (added in JLS3 API). The single member annotation |
21 | * "@foo(bar)" is equivalent to the normal annotation "@foo(value=bar)". |
22 | * <pre> |
23 | * SingleMemberAnnotation: |
24 | * <b>@</b> TypeName <b>(</b> Expression <b>)</b> |
25 | * </pre> |
26 | * <p> |
27 | * Within annotations, only certain kinds of expressions are meaningful, |
28 | * including other annotations. |
29 | * </p> |
30 | * |
31 | * @since 3.1 |
32 | * @noinstantiate This class is not intended to be instantiated by clients. |
33 | */ |
34 | @SuppressWarnings("rawtypes") |
35 | public final class SingleMemberAnnotation extends Annotation { |
36 | |
37 | /** |
38 | * The "typeName" structural property of this node type (child type: {@link Name}). |
39 | */ |
40 | public static final ChildPropertyDescriptor TYPE_NAME_PROPERTY = |
41 | internalTypeNamePropertyFactory(SingleMemberAnnotation.class); |
42 | |
43 | /** |
44 | * The "value" structural property of this node type (child type: {@link Expression}). |
45 | */ |
46 | public static final ChildPropertyDescriptor VALUE_PROPERTY = |
47 | new ChildPropertyDescriptor(SingleMemberAnnotation.class, "value", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
48 | |
49 | /** |
50 | * A list of property descriptors (element type: |
51 | * {@link StructuralPropertyDescriptor}), |
52 | * or null if uninitialized. |
53 | */ |
54 | private static final List PROPERTY_DESCRIPTORS; |
55 | |
56 | static { |
57 | List propertyList = new ArrayList(3); |
58 | createPropertyList(SingleMemberAnnotation.class, propertyList); |
59 | addProperty(TYPE_NAME_PROPERTY, propertyList); |
60 | addProperty(VALUE_PROPERTY, propertyList); |
61 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
62 | } |
63 | |
64 | /** |
65 | * Returns a list of structural property descriptors for this node type. |
66 | * Clients must not modify the result. |
67 | * |
68 | * @param apiLevel the API level; one of the AST.JLS* constants |
69 | * @return a list of property descriptors (element type: |
70 | * {@link StructuralPropertyDescriptor}) |
71 | */ |
72 | public static List propertyDescriptors(int apiLevel) { |
73 | return PROPERTY_DESCRIPTORS; |
74 | } |
75 | |
76 | /** |
77 | * The value; lazily initialized; defaults to a unspecified, but legal, |
78 | * expression. |
79 | */ |
80 | private Expression value = null; |
81 | |
82 | /** |
83 | * Creates a new unparented normal annotation node owned |
84 | * by the given AST. By default, the annotation has an |
85 | * unspecified type name and an unspecified value. |
86 | * <p> |
87 | * N.B. This constructor is package-private. |
88 | * </p> |
89 | * |
90 | * @param ast the AST that is to own this node |
91 | */ |
92 | SingleMemberAnnotation(AST ast) { |
93 | super(ast); |
94 | unsupportedIn2(); |
95 | } |
96 | |
97 | @Override |
98 | final List internalStructuralPropertiesForType(int apiLevel) { |
99 | return propertyDescriptors(apiLevel); |
100 | } |
101 | |
102 | @Override |
103 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
104 | if (property == TYPE_NAME_PROPERTY) { |
105 | if (get) { |
106 | return getTypeName(); |
107 | } else { |
108 | setTypeName((Name) child); |
109 | return null; |
110 | } |
111 | } |
112 | if (property == VALUE_PROPERTY) { |
113 | if (get) { |
114 | return getValue(); |
115 | } else { |
116 | setValue((Expression) child); |
117 | return null; |
118 | } |
119 | } |
120 | // allow default implementation to flag the error |
121 | return super.internalGetSetChildProperty(property, get, child); |
122 | } |
123 | |
124 | @Override |
125 | final ChildPropertyDescriptor internalTypeNameProperty() { |
126 | return TYPE_NAME_PROPERTY; |
127 | } |
128 | |
129 | @Override |
130 | final int getNodeType0() { |
131 | return SINGLE_MEMBER_ANNOTATION; |
132 | } |
133 | |
134 | @Override |
135 | ASTNode clone0(AST target) { |
136 | SingleMemberAnnotation result = new SingleMemberAnnotation(target); |
137 | result.setSourceRange(getStartPosition(), getLength()); |
138 | result.setTypeName((Name) ASTNode.copySubtree(target, getTypeName())); |
139 | result.setValue((Expression) ASTNode.copySubtree(target, getValue())); |
140 | return result; |
141 | } |
142 | |
143 | @Override |
144 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
145 | // dispatch to correct overloaded match method |
146 | return matcher.match(this, other); |
147 | } |
148 | |
149 | @Override |
150 | void accept0(ASTVisitor visitor) { |
151 | boolean visitChildren = visitor.visit(this); |
152 | if (visitChildren) { |
153 | // visit children in normal left to right reading order |
154 | acceptChild(visitor, getTypeName()); |
155 | acceptChild(visitor, getValue()); |
156 | } |
157 | visitor.endVisit(this); |
158 | } |
159 | |
160 | /** |
161 | * Returns the value of this annotation. |
162 | * |
163 | * @return the value node |
164 | */ |
165 | public Expression getValue() { |
166 | if (this.value == null) { |
167 | // lazy init must be thread-safe for readers |
168 | synchronized (this) { |
169 | if (this.value == null) { |
170 | preLazyInit(); |
171 | this.value = new SimpleName(this.ast); |
172 | postLazyInit(this.value, VALUE_PROPERTY); |
173 | } |
174 | } |
175 | } |
176 | return this.value; |
177 | } |
178 | |
179 | /** |
180 | * Sets the value of this annotation. |
181 | * |
182 | * @param value the new value |
183 | * @exception IllegalArgumentException if: |
184 | * <ul> |
185 | * <li>the node belongs to a different AST</li> |
186 | * <li>the node already has a parent</li> |
187 | * <li>a cycle in would be created</li> |
188 | * </ul> |
189 | */ |
190 | public void setValue(Expression value) { |
191 | if (value == null) { |
192 | throw new IllegalArgumentException(); |
193 | } |
194 | ASTNode oldChild = this.value; |
195 | preReplaceChild(oldChild, value, VALUE_PROPERTY); |
196 | this.value = value; |
197 | postReplaceChild(oldChild, value, VALUE_PROPERTY); |
198 | } |
199 | |
200 | @Override |
201 | int memSize() { |
202 | return super.memSize() + 1 * 4; |
203 | } |
204 | |
205 | @Override |
206 | int treeSize() { |
207 | return |
208 | memSize() |
209 | + (this.typeName == null ? 0 : getTypeName().treeSize()) |
210 | + (this.value == null ? 0 : getValue().treeSize()); |
211 | } |
212 | } |
213 |
Members