EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/CastExpression.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 * Cast expression AST node type.
22 *
23 * <pre>
24 * CastExpression:
25 *    <b>(</b> Type <b>)</b> Expression
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 CastExpression extends Expression {
33
34    /**
35     * The "type" structural property of this node type (child type: {@link Type}).
36     * @since 3.0
37     */
38    public static final ChildPropertyDescriptor TYPE_PROPERTY =
39        new ChildPropertyDescriptor(CastExpression.class"type"Type.classMANDATORYNO_CYCLE_RISK); //$NON-NLS-1$
40
41    /**
42     * The "expression" structural property of this node type (child type: {@link Expression}).
43     * @since 3.0
44     */
45    public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
46        new ChildPropertyDescriptor(CastExpression.class"expression"Expression.classMANDATORYCYCLE_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(CastExpression.classproperyList);
58        addProperty(TYPE_PROPERTYproperyList);
59        addProperty(EXPRESSION_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     * @return a list of property descriptors (element type:
70     * {@link StructuralPropertyDescriptor})
71     * @since 3.0
72     */
73    public static List propertyDescriptors(int apiLevel) {
74        return PROPERTY_DESCRIPTORS;
75    }
76
77    /**
78     * The type; lazily initialized; defaults to a unspecified,
79     * legal type.
80     */
81    private Type type = null;
82
83    /**
84     * The expression; lazily initialized; defaults to a unspecified, but legal,
85     * expression.
86     */
87    private Expression expression = null;
88
89    /**
90     * Creates a new AST node for a cast expression owned by the given
91     * AST. By default, the type and expression are unspecified (but legal).
92     * <p>
93     * N.B. This constructor is package-private.
94     * </p>
95     *
96     * @param ast the AST that is to own this node
97     */
98    CastExpression(AST ast) {
99        super(ast);
100    }
101
102    @Override
103    final List internalStructuralPropertiesForType(int apiLevel) {
104        return propertyDescriptors(apiLevel);
105    }
106
107    @Override
108    final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor propertyboolean getASTNode child) {
109        if (property == EXPRESSION_PROPERTY) {
110            if (get) {
111                return getExpression();
112            } else {
113                setExpression((Expressionchild);
114                return null;
115            }
116        }
117        if (property == TYPE_PROPERTY) {
118            if (get) {
119                return getType();
120            } else {
121                setType((Typechild);
122                return null;
123            }
124        }
125        // allow default implementation to flag the error
126        return super.internalGetSetChildProperty(propertygetchild);
127    }
128
129    @Override
130    final int getNodeType0() {
131        return CAST_EXPRESSION;
132    }
133
134    @Override
135    ASTNode clone0(AST target) {
136        CastExpression result = new CastExpression(target);
137        result.setSourceRange(getStartPosition(), getLength());
138        result.setType((TypegetType().clone(target));
139        result.setExpression((ExpressiongetExpression().clone(target));
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(visitorgetType());
155            acceptChild(visitorgetExpression());
156        }
157        visitor.endVisit(this);
158    }
159
160    /**
161     * Returns the type in this cast expression.
162     *
163     * @return the type
164     */
165    public Type getType() {
166        if (this.type == null) {
167            // lazy init must be thread-safe for readers
168            synchronized (this) {
169                if (this.type == null) {
170                    preLazyInit();
171                    this.type = this.ast.newPrimitiveType(PrimitiveType.INT);
172                    postLazyInit(this.typeTYPE_PROPERTY);
173                }
174            }
175        }
176        return this.type;
177    }
178
179    /**
180     * Sets the type in this cast expression to the given type.
181     *
182     * @param type the new type
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     * </ul>
188     */
189    public void setType(Type type) {
190        if (type == null) {
191            throw new IllegalArgumentException();
192        }
193        ASTNode oldChild = this.type;
194        preReplaceChild(oldChildtypeTYPE_PROPERTY);
195        this.type = type;
196        postReplaceChild(oldChildtypeTYPE_PROPERTY);
197    }
198
199    /**
200     * Returns the expression of this cast expression.
201     *
202     * @return the expression node
203     */
204    public Expression getExpression() {
205        if (this.expression == null) {
206            // lazy init must be thread-safe for readers
207            synchronized (this) {
208                if (this.expression == null) {
209                    preLazyInit();
210                    this.expression = new SimpleName(this.ast);
211                    postLazyInit(this.expressionEXPRESSION_PROPERTY);
212                }
213            }
214        }
215        return this.expression;
216    }
217
218    /**
219     * Sets the expression of this cast expression.
220     *
221     * @param expression the new expression node
222     * @exception IllegalArgumentException if:
223     * <ul>
224     * <li>the node belongs to a different AST</li>
225     * <li>the node already has a parent</li>
226     * <li>a cycle in would be created</li>
227     * </ul>
228     */
229    public void setExpression(Expression expression) {
230        if (expression == null) {
231            throw new IllegalArgumentException();
232        }
233        ASTNode oldChild = this.expression;
234        preReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
235        this.expression = expression;
236        postReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
237    }
238
239    @Override
240    int memSize() {
241        // treat Code as free
242        return BASE_NODE_SIZE + 2 * 4;
243    }
244
245    @Override
246    int treeSize() {
247        return
248            memSize()
249            + (this.expression == null ? 0 : getExpression().treeSize())
250            + (this.type == null ? 0 : getType().treeSize());
251    }
252}
253
MembersX
CastExpression:expression
CastExpression:Block:properyList
CastExpression:type
CastExpression:clone0
CastExpression:accept0
CastExpression:internalStructuralPropertiesForType
CastExpression:TYPE_PROPERTY
CastExpression:propertyDescriptors
CastExpression:getNodeType0
CastExpression:getExpression
CastExpression:setExpression:Block:oldChild
CastExpression:internalGetSetChildProperty
CastExpression:treeSize
CastExpression:subtreeMatch0
CastExpression:setType:Block:oldChild
CastExpression:getType
CastExpression:setType
CastExpression:setExpression
CastExpression:PROPERTY_DESCRIPTORS
CastExpression:CastExpression
CastExpression:accept0:Block:visitChildren
CastExpression:clone0:Block:result
CastExpression:EXPRESSION_PROPERTY
CastExpression:memSize
Members
X