EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/SingleMemberAnnotation.java
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 *******************************************************************************/
14package org.eclipse.jdt.core.dom;
15
16import java.util.ArrayList;
17import 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")
35public 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.classMANDATORYCYCLE_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.classpropertyList);
59        addProperty(TYPE_NAME_PROPERTYpropertyList);
60        addProperty(VALUE_PROPERTYpropertyList);
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 propertyboolean getASTNode child) {
104        if (property == TYPE_NAME_PROPERTY) {
105            if (get) {
106                return getTypeName();
107            } else {
108                setTypeName((Namechild);
109                return null;
110            }
111        }
112        if (property == VALUE_PROPERTY) {
113            if (get) {
114                return getValue();
115            } else {
116                setValue((Expressionchild);
117                return null;
118            }
119        }
120        // allow default implementation to flag the error
121        return super.internalGetSetChildProperty(propertygetchild);
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((NameASTNode.copySubtree(targetgetTypeName()));
139        result.setValue((ExpressionASTNode.copySubtree(targetgetValue()));
140        return result;
141    }
142
143    @Override
144    final boolean subtreeMatch0(ASTMatcher matcherObject 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(visitorgetTypeName());
155            acceptChild(visitorgetValue());
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.valueVALUE_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(oldChildvalueVALUE_PROPERTY);
196        this.value = value;
197        postReplaceChild(oldChildvalueVALUE_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
MembersX
SingleMemberAnnotation:value
SingleMemberAnnotation:clone0
SingleMemberAnnotation:SingleMemberAnnotation
SingleMemberAnnotation:treeSize
SingleMemberAnnotation:TYPE_NAME_PROPERTY
SingleMemberAnnotation:memSize
SingleMemberAnnotation:Block:propertyList
SingleMemberAnnotation:subtreeMatch0
SingleMemberAnnotation:setValue
SingleMemberAnnotation:getValue
SingleMemberAnnotation:VALUE_PROPERTY
SingleMemberAnnotation:PROPERTY_DESCRIPTORS
SingleMemberAnnotation:internalStructuralPropertiesForType
SingleMemberAnnotation:internalTypeNameProperty
SingleMemberAnnotation:internalGetSetChildProperty
SingleMemberAnnotation:accept0:Block:visitChildren
SingleMemberAnnotation:getNodeType0
SingleMemberAnnotation:setValue:Block:oldChild
SingleMemberAnnotation:propertyDescriptors
SingleMemberAnnotation:clone0:Block:result
SingleMemberAnnotation:accept0
Members
X