EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/AssertStatement.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 * Assert statement AST node type.
22 *
23 * <pre>
24 * AssertStatement:
25 *    <b>assert</b> Expression [ <b>:</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")
32public class AssertStatement 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(AssertStatement.class"expression"Expression.classMANDATORYCYCLE_RISK); //$NON-NLS-1$
40
41    /**
42     * The "message" structural property of this node type (child type: {@link Expression}).
43     * @since 3.0
44     */
45    public static final ChildPropertyDescriptor MESSAGE_PROPERTY =
46        new ChildPropertyDescriptor(AssertStatement.class"message"Expression.classOPTIONALCYCLE_RISK); //$NON-NLS-1$
47
48    /**
49     * A list of property descriptors (element type:
50     * {@link StructuralPropertyDescriptor}),
51     * or null if uninitialized.
52     */
53    private static final List PROPERTY_DESCRIPTORS;
54
55    static {
56        List properyList = new ArrayList(3);
57        createPropertyList(AssertStatement.classproperyList);
58        addProperty(EXPRESSION_PROPERTYproperyList);
59        addProperty(MESSAGE_PROPERTYproperyList);
60        PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
61    }
62
63    /**
64     * Returns a list of structural property descriptors for this node type.
65     * Clients must not modify the result.
66     *
67     * @param apiLevel the API level; one of the
68     * <code>AST.JLS*</code> constants
69
70     * @return a list of property descriptors (element type:
71     * {@link StructuralPropertyDescriptor})
72     * @since 3.0
73     */
74    public static List propertyDescriptors(int apiLevel) {
75        return PROPERTY_DESCRIPTORS;
76    }
77
78    /**
79     * The expression; lazily initialized; defaults to a unspecified, but legal,
80     * expression.
81     */
82    private Expression expression = null;
83
84    /**
85     * The message expression; <code>null</code> for none; defaults to none.
86     */
87    private Expression optionalMessageExpression = null;
88
89    /**
90     * Creates a new unparented assert statement node owned by the given
91     * AST. By default, the assert statement has an unspecified, but legal,
92     * expression, and not message expression.
93     * <p>
94     * N.B. This constructor is package-private.
95     * </p>
96     *
97     * @param ast the AST that is to own this node
98     */
99    AssertStatement(AST ast) {
100        super(ast);
101    }
102
103    @Override
104    final List internalStructuralPropertiesForType(int apiLevel) {
105        return propertyDescriptors(apiLevel);
106    }
107
108    @Override
109    final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor propertyboolean getASTNode child) {
110        if (property == EXPRESSION_PROPERTY) {
111            if (get) {
112                return getExpression();
113            } else {
114                setExpression((Expressionchild);
115                return null;
116            }
117        }
118        if (property == MESSAGE_PROPERTY) {
119            if (get) {
120                return getMessage();
121            } else {
122                setMessage((Expressionchild);
123                return null;
124            }
125        }
126        // allow default implementation to flag the error
127        return super.internalGetSetChildProperty(propertygetchild);
128    }
129
130    @Override
131    final int getNodeType0() {
132        return ASSERT_STATEMENT;
133    }
134
135    @Override
136    ASTNode clone0(AST target) {
137        AssertStatement result = new AssertStatement(target);
138        result.setSourceRange(getStartPosition(), getLength());
139        result.copyLeadingComment(this);
140        result.setExpression(
141            (ExpressionASTNode.copySubtree(targetgetExpression()));
142        result.setMessage(
143            (ExpressionASTNode.copySubtree(targetgetMessage()));
144        return result;
145    }
146
147    @Override
148    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
149        // dispatch to correct overloaded match method
150        return matcher.match(this, other);
151    }
152
153    @Override
154    void accept0(ASTVisitor visitor) {
155        boolean visitChildren = visitor.visit(this);
156        if (visitChildren) {
157            // visit children in normal left to right reading order
158            acceptChild(visitorgetExpression());
159            acceptChild(visitorgetMessage());
160        }
161        visitor.endVisit(this);
162    }
163
164    /**
165     * Returns the first expression of this assert statement.
166     *
167     * @return the expression node
168     */
169    public Expression getExpression() {
170        if (this.expression == null) {
171            // lazy init must be thread-safe for readers
172            synchronized (this) {
173                if (this.expression == null) {
174                    preLazyInit();
175                    this.expression = new SimpleName(this.ast);
176                    postLazyInit(this.expressionEXPRESSION_PROPERTY);
177                }
178            }
179        }
180        return this.expression;
181    }
182
183    /**
184     * Sets the first expression of this assert statement.
185     *
186     * @param expression the new expression node
187     * @exception IllegalArgumentException if:
188     * <ul>
189     * <li>the node belongs to a different AST</li>
190     * <li>the node already has a parent</li>
191     * <li>a cycle in would be created</li>
192     * </ul>
193     */
194    public void setExpression(Expression expression) {
195        if (expression == null) {
196            throw new IllegalArgumentException();
197        }
198        // an AssertStatement may occur inside an Expression - must check cycles
199        ASTNode oldChild = this.expression;
200        preReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
201        this.expression = expression;
202        postReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
203    }
204
205    /**
206     * Returns the message expression of this assert statement, or
207     * <code>null</code> if there is none.
208     *
209     * @return the message expression node, or <code>null</code> if there
210     *    is none
211     */
212    public Expression getMessage() {
213        return this.optionalMessageExpression;
214    }
215
216    /**
217     * Sets or clears the message expression of this assert statement.
218     *
219     * @param expression the message expression node, or <code>null</code> if
220     *    there is none
221     * @exception IllegalArgumentException if:
222     * <ul>
223     * <li>the node belongs to a different AST</li>
224     * <li>the node already has a parent</li>
225     * <li>a cycle in would be created</li>
226     * </ul>
227     */
228    public void setMessage(Expression expression) {
229        // an AsertStatement may occur inside an Expression - must check cycles
230        ASTNode oldChild = this.optionalMessageExpression;
231        preReplaceChild(oldChildexpressionMESSAGE_PROPERTY);
232        this.optionalMessageExpression = expression;
233        postReplaceChild(oldChildexpressionMESSAGE_PROPERTY);
234    }
235
236    @Override
237    int memSize() {
238        return super.memSize() + 2 * 4;
239    }
240
241    @Override
242    int treeSize() {
243        return
244            memSize()
245            + (this.expression == null ? 0 : getExpression().treeSize())
246            + (this.optionalMessageExpression == null ? 0 : getMessage().treeSize());
247
248    }
249}
250
251
MembersX
AssertStatement:Block:properyList
AssertStatement:internalGetSetChildProperty
AssertStatement:setExpression:Block:oldChild
AssertStatement:AssertStatement
AssertStatement:setMessage
AssertStatement:propertyDescriptors
AssertStatement:internalStructuralPropertiesForType
AssertStatement:setExpression
AssertStatement:setMessage:Block:oldChild
AssertStatement:subtreeMatch0
AssertStatement:memSize
AssertStatement:getExpression
AssertStatement:treeSize
AssertStatement:getNodeType0
AssertStatement:optionalMessageExpression
AssertStatement:PROPERTY_DESCRIPTORS
AssertStatement:getMessage
AssertStatement:MESSAGE_PROPERTY
AssertStatement:expression
AssertStatement:accept0:Block:visitChildren
AssertStatement:accept0
AssertStatement:clone0
AssertStatement:EXPRESSION_PROPERTY
AssertStatement:clone0:Block:result
Members
X