1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Parenthesized expression AST node type.
21 * ParenthesizedExpression:
22 * <b>(</b> Expression <b>)</b>
26 * @noinstantiate This class is not intended to be instantiated by clients.
28 public class ParenthesizedExpression extends Expression {
31 * The "expression" structural property of this node type.
34 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
35 new ChildPropertyDescriptor(ParenthesizedExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
38 * A list of property descriptors (element type:
39 * {@link StructuralPropertyDescriptor}),
40 * or null if uninitialized.
42 private static final List PROPERTY_DESCRIPTORS;
45 List propertyList = new ArrayList(2);
46 createPropertyList(ParenthesizedExpression.class, propertyList);
47 addProperty(EXPRESSION_PROPERTY, propertyList);
48 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
52 * Returns a list of structural property descriptors for this node type.
53 * Clients must not modify the result.
55 * @param apiLevel the API level; one of the
56 * <code>AST.JLS*</code> constants
57 * @return a list of property descriptors (element type:
58 * {@link StructuralPropertyDescriptor})
61 public static List propertyDescriptors(int apiLevel) {
62 return PROPERTY_DESCRIPTORS;
66 * The expression; lazily initialized; defaults to a unspecified, but legal,
69 private Expression expression = null;
72 * Creates a new unparented parenthesized expression node owned by the given
73 * AST. By default, the parenthesized expression has an unspecified, but
76 * N.B. This constructor is package-private.
79 * @param ast the AST that is to own this node
81 ParenthesizedExpression(AST ast) {
85 /* (omit javadoc for this method)
86 * Method declared on ASTNode.
88 final List internalStructuralPropertiesForType(int apiLevel) {
89 return propertyDescriptors(apiLevel);
92 /* (omit javadoc for this method)
93 * Method declared on ASTNode.
95 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
96 if (property == EXPRESSION_PROPERTY) {
98 return getExpression();
100 setExpression((Expression) child);
104 // allow default implementation to flag the error
105 return super.internalGetSetChildProperty(property, get, child);
108 /* (omit javadoc for this method)
109 * Method declared on ASTNode.
111 final int getNodeType0() {
112 return PARENTHESIZED_EXPRESSION;
115 /* (omit javadoc for this method)
116 * Method declared on ASTNode.
118 ASTNode clone0(AST target) {
119 ParenthesizedExpression result = new ParenthesizedExpression(target);
120 result.setSourceRange(this.getStartPosition(), this.getLength());
121 result.setExpression((Expression) getExpression().clone(target));
125 /* (omit javadoc for this method)
126 * Method declared on ASTNode.
128 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
129 // dispatch to correct overloaded match method
130 return matcher.match(this, other);
133 /* (omit javadoc for this method)
134 * Method declared on ASTNode.
136 void accept0(ASTVisitor visitor) {
137 boolean visitChildren = visitor.visit(this);
139 acceptChild(visitor, getExpression());
141 visitor.endVisit(this);
145 * Returns the expression of this parenthesized expression.
147 * @return the expression node
149 public Expression getExpression() {
150 if (this.expression == null) {
151 // lazy init must be thread-safe for readers
152 synchronized (this) {
153 if (this.expression == null) {
155 this.expression = new SimpleName(this.ast);
156 postLazyInit(this.expression, EXPRESSION_PROPERTY);
160 return this.expression;
164 * Sets the expression of this parenthesized expression.
166 * @param expression the new expression node
167 * @exception IllegalArgumentException if:
169 * <li>the node belongs to a different AST</li>
170 * <li>the node already has a parent</li>
171 * <li>a cycle in would be created</li>
174 public void setExpression(Expression expression) {
175 if (expression == null) {
176 throw new IllegalArgumentException();
178 ASTNode oldChild = this.expression;
179 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
180 this.expression = expression;
181 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
184 /* (omit javadoc for this method)
185 * Method declared on ASTNode.
188 return BASE_NODE_SIZE + 1 * 4;
191 /* (omit javadoc for this method)
192 * Method declared on ASTNode.
197 + (this.expression == null ? 0 : getExpression().treeSize());