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