EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/VariableDeclarationFragment.java
1/*******************************************************************************
2 * Copyright (c) 2000, 2013 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
15package org.eclipse.jdt.core.dom;
16
17import java.util.ArrayList;
18import java.util.List;
19
20/**
21 * Variable declaration fragment AST node type, used in field declarations,
22 * local variable declarations, <code>ForStatement</code> initializers,
23 * and <code>LambdaExpression</code> parameters.
24 * In contrast to <code>SingleVariableDeclaration</code>, fragments are
25 * missing the modifiers and the type; these are either located in the fragment's
26 * parent node, or inferred (for lambda parameters).
27 *
28 * <pre>
29 * VariableDeclarationFragment:
30 *    Identifier { Dimension } [ <b>=</b> Expression ]
31 * </pre>
32 * @since 2.0
33 * @noinstantiate This class is not intended to be instantiated by clients.
34 */
35@SuppressWarnings("rawtypes")
36public class VariableDeclarationFragment extends VariableDeclaration {
37
38    /**
39     * The "name" structural property of this node type (child type: {@link SimpleName}).
40     * @since 3.0
41     */
42    public static final ChildPropertyDescriptor NAME_PROPERTY =
43            internalNamePropertyFactory(VariableDeclarationFragment.class);
44
45    /**
46     * The "extraDimensions" structural property of this node type (type: {@link Integer}) (below JLS8 only).
47     *
48     * @since 3.0
49     * @deprecated in JLS8 and later, use {@link VariableDeclarationFragment#EXTRA_DIMENSIONS2_PROPERTY} instead.
50     */
51    public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY =
52            internalExtraDimensionsPropertyFactory(VariableDeclarationFragment.class);
53
54    /**
55     * The "extraDimensions2" structural property of this node type (element type: {@link Dimension}) (added in JLS8 API).
56     * @since 3.10
57     */
58    public static final ChildListPropertyDescriptor EXTRA_DIMENSIONS2_PROPERTY =
59            internalExtraDimensions2PropertyFactory(VariableDeclarationFragment.class);
60
61    /**
62     * The "initializer" structural property of this node type (child type: {@link Expression}).
63     * @since 3.0
64     */
65    public static final ChildPropertyDescriptor INITIALIZER_PROPERTY =
66            internalInitializerPropertyFactory(VariableDeclarationFragment.class);
67
68    /**
69     * A list of property descriptors (element type:
70     * {@link StructuralPropertyDescriptor}),
71     * or null if uninitialized.
72     * @since 3.0
73     */
74    private static final List PROPERTY_DESCRIPTORS;
75
76    /**
77     * A list of property descriptors (element type:
78     * {@link StructuralPropertyDescriptor}),
79     * or null if uninitialized.
80     * @since 3.10
81     */
82    private static final List PROPERTY_DESCRIPTORS_8_0;
83
84    static {
85        List propertyList = new ArrayList(4);
86        createPropertyList(VariableDeclarationFragment.classpropertyList);
87        addProperty(NAME_PROPERTYpropertyList);
88        addProperty(EXTRA_DIMENSIONS_PROPERTYpropertyList);
89        addProperty(INITIALIZER_PROPERTYpropertyList);
90        PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
91
92        propertyList = new ArrayList(4);
93        createPropertyList(VariableDeclarationFragment.classpropertyList);
94        addProperty(NAME_PROPERTYpropertyList);
95        addProperty(EXTRA_DIMENSIONS2_PROPERTYpropertyList);
96        addProperty(INITIALIZER_PROPERTYpropertyList);
97        PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList);
98    }
99
100    /**
101     * Returns a list of structural property descriptors for this node type.
102     * Clients must not modify the result.
103     *
104     * @param apiLevel the API level; one of the
105     * <code>AST.JLS*</code> constants
106     * @return a list of property descriptors (element type:
107     * {@link StructuralPropertyDescriptor})
108     * @since 3.0
109     */
110    public static List propertyDescriptors(int apiLevel) {
111        if (apiLevel >= AST.JLS8_INTERNAL) {
112            return PROPERTY_DESCRIPTORS_8_0;
113        } else {
114            return PROPERTY_DESCRIPTORS;
115        }
116    }
117
118    /**
119     * Creates a new AST node for a variable declaration fragment owned by the
120     * given AST. By default, the variable declaration has: an unspecified
121     * (but legal) variable name, no initializer, and no extra array dimensions.
122     * <p>
123     * N.B. This constructor is package-private.
124     * </p>
125     *
126     * @param ast the AST that is to own this node
127     */
128    VariableDeclarationFragment(AST ast) {
129        super(ast);
130    }
131
132    @Override
133    final ChildPropertyDescriptor internalNameProperty() {
134        return NAME_PROPERTY;
135    }
136
137    @Override
138    final SimplePropertyDescriptor internalExtraDimensionsProperty() {
139        return EXTRA_DIMENSIONS_PROPERTY;
140    }
141
142    @Override
143    final ChildListPropertyDescriptor internalExtraDimensions2Property() {
144        return EXTRA_DIMENSIONS2_PROPERTY;
145    }
146
147    @Override
148    final ChildPropertyDescriptor internalInitializerProperty() {
149        return INITIALIZER_PROPERTY;
150    }
151
152    @Override
153    final List internalStructuralPropertiesForType(int apiLevel) {
154        return propertyDescriptors(apiLevel);
155    }
156
157    @Override
158    final int internalGetSetIntProperty(SimplePropertyDescriptor propertyboolean getint value) {
159        if (property == EXTRA_DIMENSIONS_PROPERTY) {
160            if (get) {
161                return getExtraDimensions();
162            } else {
163                internalSetExtraDimensions(value);
164                return 0;
165            }
166        }
167        // allow default implementation to flag the error
168        return super.internalGetSetIntProperty(propertygetvalue);
169    }
170
171    @Override
172    final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor propertyboolean getASTNode child) {
173        if (property == NAME_PROPERTY) {
174            if (get) {
175                return getName();
176            } else {
177                setName((SimpleNamechild);
178                return null;
179            }
180        }
181        if (property == INITIALIZER_PROPERTY) {
182            if (get) {
183                return getInitializer();
184            } else {
185                setInitializer((Expressionchild);
186                return null;
187            }
188        }
189        // allow default implementation to flag the error
190        return super.internalGetSetChildProperty(propertygetchild);
191    }
192
193    @Override
194    final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
195        if (property == EXTRA_DIMENSIONS2_PROPERTY) {
196            return extraDimensions();
197        }
198        // allow default implementation to flag the error
199        return super.internalGetChildListProperty(property);
200    }
201
202    @Override
203    final int getNodeType0() {
204        return VARIABLE_DECLARATION_FRAGMENT;
205    }
206
207    @Override
208    ASTNode clone0(AST target) {
209        VariableDeclarationFragment result = new VariableDeclarationFragment(target);
210        result.setSourceRange(getStartPosition(), getLength());
211        result.setName((SimpleNamegetName().clone(target));
212        if (this.ast.apiLevel >= AST.JLS8_INTERNAL) {
213            result.extraDimensions().addAll(
214                    ASTNode.copySubtrees(targetextraDimensions()));
215        } else {
216            result.internalSetExtraDimensions(getExtraDimensions());
217        }
218        result.setInitializer(
219            (ExpressionASTNode.copySubtree(targetgetInitializer()));
220        return result;
221    }
222
223    @Override
224    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
225        // dispatch to correct overloaded match method
226        return matcher.match(this, other);
227    }
228
229    @Override
230    void accept0(ASTVisitor visitor) {
231        boolean visitChildren = visitor.visit(this);
232        if (visitChildren) {
233            // visit children in normal left to right reading order
234            acceptChild(visitorgetName());
235            if (this.ast.apiLevel >= AST.JLS8_INTERNAL) {
236                acceptChildren(visitor, this.extraDimensions);
237            }
238            acceptChild(visitorgetInitializer());
239        }
240        visitor.endVisit(this);
241    }
242
243    @Override
244    int memSize() {
245        // treat Operator as free
246        return BASE_NODE_SIZE + 4 * 4;
247    }
248
249    @Override
250    int treeSize() {
251        return
252            memSize()
253            + (this.variableName == null ? 0 : getName().treeSize())
254            + (this.extraDimensions == null ? 0 : this.extraDimensions.listSize())
255            + (this.optionalInitializer == null ? 0 : getInitializer().treeSize());
256    }
257}
258
MembersX
VariableDeclarationFragment:internalExtraDimensions2Property
VariableDeclarationFragment:treeSize
VariableDeclarationFragment:EXTRA_DIMENSIONS2_PROPERTY
VariableDeclarationFragment:clone0:Block:result
VariableDeclarationFragment:internalGetSetIntProperty
VariableDeclarationFragment:Block:propertyList
VariableDeclarationFragment:internalExtraDimensionsProperty
VariableDeclarationFragment:internalInitializerProperty
VariableDeclarationFragment:accept0
VariableDeclarationFragment:internalStructuralPropertiesForType
VariableDeclarationFragment:PROPERTY_DESCRIPTORS_8_0
VariableDeclarationFragment:clone0
VariableDeclarationFragment:internalNameProperty
VariableDeclarationFragment:INITIALIZER_PROPERTY
VariableDeclarationFragment:accept0:Block:visitChildren
VariableDeclarationFragment:VariableDeclarationFragment
VariableDeclarationFragment:internalGetChildListProperty
VariableDeclarationFragment:getNodeType0
VariableDeclarationFragment:propertyDescriptors
VariableDeclarationFragment:EXTRA_DIMENSIONS_PROPERTY
VariableDeclarationFragment:memSize
VariableDeclarationFragment:internalGetSetChildProperty
VariableDeclarationFragment:subtreeMatch0
VariableDeclarationFragment:NAME_PROPERTY
VariableDeclarationFragment:PROPERTY_DESCRIPTORS
Members
X