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 | |
15 | package org.eclipse.jdt.core.dom; |
16 | |
17 | import java.util.ArrayList; |
18 | import java.util.List; |
19 | |
20 | /** |
21 | * Expression statement AST node type. |
22 | * <p> |
23 | * This kind of node is used to convert an expression (<code>Expression</code>) |
24 | * into a statement (<code>Statement</code>) by wrapping it. |
25 | * </p> |
26 | * <pre> |
27 | * ExpressionStatement: |
28 | * StatementExpression <b>;</b> |
29 | * </pre> |
30 | * |
31 | * @since 2.0 |
32 | * @noinstantiate This class is not intended to be instantiated by clients. |
33 | */ |
34 | @SuppressWarnings("rawtypes") |
35 | public class ExpressionStatement extends Statement { |
36 | |
37 | /** |
38 | * The "expression" structural property of this node type (child type: {@link Expression}). |
39 | * @since 3.0 |
40 | */ |
41 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
42 | new ChildPropertyDescriptor(ExpressionStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
43 | |
44 | /** |
45 | * A list of property descriptors (element type: |
46 | * {@link StructuralPropertyDescriptor}), |
47 | * or null if uninitialized. |
48 | */ |
49 | private static final List PROPERTY_DESCRIPTORS; |
50 | |
51 | static { |
52 | List properyList = new ArrayList(2); |
53 | createPropertyList(ExpressionStatement.class, properyList); |
54 | addProperty(EXPRESSION_PROPERTY, properyList); |
55 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
56 | } |
57 | |
58 | /** |
59 | * Returns a list of structural property descriptors for this node type. |
60 | * Clients must not modify the result. |
61 | * |
62 | * @param apiLevel the API level; one of the |
63 | * <code>AST.JLS*</code> constants |
64 | |
65 | * @return a list of property descriptors (element type: |
66 | * {@link StructuralPropertyDescriptor}) |
67 | * @since 3.0 |
68 | */ |
69 | public static List propertyDescriptors(int apiLevel) { |
70 | return PROPERTY_DESCRIPTORS; |
71 | } |
72 | |
73 | /** |
74 | * The expression; lazily initialized; defaults to a unspecified, but legal, |
75 | * expression. |
76 | */ |
77 | private Expression expression = null; |
78 | |
79 | /** |
80 | * Creates a new unparented expression statement node owned by the given |
81 | * AST. By default, the expression statement is unspecified, but legal, |
82 | * method invocation expression. |
83 | * <p> |
84 | * N.B. This constructor is package-private. |
85 | * </p> |
86 | * |
87 | * @param ast the AST that is to own this node |
88 | */ |
89 | ExpressionStatement(AST ast) { |
90 | super(ast); |
91 | } |
92 | |
93 | @Override |
94 | final List internalStructuralPropertiesForType(int apiLevel) { |
95 | return propertyDescriptors(apiLevel); |
96 | } |
97 | |
98 | @Override |
99 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
100 | if (property == EXPRESSION_PROPERTY) { |
101 | if (get) { |
102 | return getExpression(); |
103 | } else { |
104 | setExpression((Expression) child); |
105 | return null; |
106 | } |
107 | } |
108 | // allow default implementation to flag the error |
109 | return super.internalGetSetChildProperty(property, get, child); |
110 | } |
111 | |
112 | @Override |
113 | final int getNodeType0() { |
114 | return EXPRESSION_STATEMENT; |
115 | } |
116 | |
117 | @Override |
118 | ASTNode clone0(AST target) { |
119 | ExpressionStatement result = new ExpressionStatement(target); |
120 | result.setSourceRange(getStartPosition(), getLength()); |
121 | result.copyLeadingComment(this); |
122 | result.setExpression((Expression) getExpression().clone(target)); |
123 | return result; |
124 | } |
125 | |
126 | @Override |
127 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
128 | // dispatch to correct overloaded match method |
129 | return matcher.match(this, other); |
130 | } |
131 | |
132 | @Override |
133 | void accept0(ASTVisitor visitor) { |
134 | boolean visitChildren = visitor.visit(this); |
135 | if (visitChildren) { |
136 | acceptChild(visitor, getExpression()); |
137 | } |
138 | visitor.endVisit(this); |
139 | } |
140 | |
141 | /** |
142 | * Returns the expression of this expression statement. |
143 | * |
144 | * @return the expression node |
145 | */ |
146 | public Expression getExpression() { |
147 | if (this.expression == null) { |
148 | // lazy init must be thread-safe for readers |
149 | synchronized (this) { |
150 | if (this.expression == null) { |
151 | preLazyInit(); |
152 | this.expression = new MethodInvocation(this.ast); |
153 | postLazyInit(this.expression, EXPRESSION_PROPERTY); |
154 | } |
155 | } |
156 | } |
157 | return this.expression; |
158 | } |
159 | |
160 | /** |
161 | * Sets the expression of this expression statement. |
162 | * |
163 | * @param expression the new expression node |
164 | * @exception IllegalArgumentException if: |
165 | * <ul> |
166 | * <li>the node belongs to a different AST</li> |
167 | * <li>the node already has a parent</li> |
168 | * <li>a cycle in would be created</li> |
169 | * </ul> |
170 | */ |
171 | public void setExpression(Expression expression) { |
172 | if (expression == null) { |
173 | throw new IllegalArgumentException(); |
174 | } |
175 | ASTNode oldChild = this.expression; |
176 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
177 | this.expression = expression; |
178 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
179 | } |
180 | |
181 | @Override |
182 | int memSize() { |
183 | return super.memSize() + 1 * 4; |
184 | } |
185 | |
186 | @Override |
187 | int treeSize() { |
188 | return |
189 | memSize() |
190 | + (this.expression == null ? 0 : getExpression().treeSize()); |
191 | } |
192 | } |
193 | |
194 |
Members