KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > ThrowStatement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Throw statement AST node type.
19  *
20  * <pre>
21  * ThrowStatement:
22  * <b>throw</b> Expression <b>;</b>
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class ThrowStatement extends Statement {
28             
29     /**
30      * The "expression" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
34         new ChildPropertyDescriptor(ThrowStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * A list of property descriptors (element type:
38      * {@link StructuralPropertyDescriptor}),
39      * or null if uninitialized.
40      */

41     private static final List JavaDoc PROPERTY_DESCRIPTORS;
42     
43     static {
44         List JavaDoc propertyList = new ArrayList JavaDoc(2);
45         createPropertyList(ThrowStatement.class, propertyList);
46         addProperty(EXPRESSION_PROPERTY, propertyList);
47         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
48     }
49
50     /**
51      * Returns a list of structural property descriptors for this node type.
52      * Clients must not modify the result.
53      *
54      * @param apiLevel the API level; one of the
55      * <code>AST.JLS*</code> constants
56
57      * @return a list of property descriptors (element type:
58      * {@link StructuralPropertyDescriptor})
59      * @since 3.0
60      */

61     public static List JavaDoc propertyDescriptors(int apiLevel) {
62         return PROPERTY_DESCRIPTORS;
63     }
64             
65     /**
66      * The expression; lazily initialized; defaults to a unspecified, but legal,
67      * expression.
68      */

69     private Expression expression = null;
70
71     /**
72      * Creates a new unparented throw statement node owned by the given
73      * AST. By default, the throw statement has an unspecified, but legal,
74      * expression.
75      * <p>
76      * N.B. This constructor is package-private.
77      * </p>
78      *
79      * @param ast the AST that is to own this node
80      */

81     ThrowStatement(AST ast) {
82         super(ast);
83     }
84
85     /* (omit javadoc for this method)
86      * Method declared on ASTNode.
87      */

88     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
89         return propertyDescriptors(apiLevel);
90     }
91     
92     /* (omit javadoc for this method)
93      * Method declared on ASTNode.
94      */

95     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
96         if (property == EXPRESSION_PROPERTY) {
97             if (get) {
98                 return getExpression();
99             } else {
100                 setExpression((Expression) child);
101                 return null;
102             }
103         }
104         // allow default implementation to flag the error
105
return super.internalGetSetChildProperty(property, get, child);
106     }
107     
108     /* (omit javadoc for this method)
109      * Method declared on ASTNode.
110      */

111     final int getNodeType0() {
112         return THROW_STATEMENT;
113     }
114
115     /* (omit javadoc for this method)
116      * Method declared on ASTNode.
117      */

118     ASTNode clone0(AST target) {
119         ThrowStatement result = new ThrowStatement(target);
120         result.setSourceRange(this.getStartPosition(), this.getLength());
121         result.copyLeadingComment(this);
122         result.setExpression((Expression) getExpression().clone(target));
123         return result;
124     }
125     
126     /* (omit javadoc for this method)
127      * Method declared on ASTNode.
128      */

129     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
130         // dispatch to correct overloaded match method
131
return matcher.match(this, other);
132     }
133
134     /* (omit javadoc for this method)
135      * Method declared on ASTNode.
136      */

137     void accept0(ASTVisitor visitor) {
138         boolean visitChildren = visitor.visit(this);
139         if (visitChildren) {
140             acceptChild(visitor, getExpression());
141         }
142         visitor.endVisit(this);
143     }
144     
145     /**
146      * Returns the expression of this throw statement.
147      *
148      * @return the expression node
149      */

150     public Expression getExpression() {
151         if (this.expression == null) {
152             // lazy init must be thread-safe for readers
153
synchronized (this) {
154                 if (this.expression == null) {
155                     preLazyInit();
156                     this.expression = new SimpleName(this.ast);
157                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
158                 }
159             }
160         }
161         return this.expression;
162     }
163         
164     /**
165      * Sets the expression of this throw statement.
166      *
167      * @param expression the new expression node
168      * @exception IllegalArgumentException if:
169      * <ul>
170      * <li>the node belongs to a different AST</li>
171      * <li>the node already has a parent</li>
172      * <li>a cycle in would be created</li>
173      * </ul>
174      */

175     public void setExpression(Expression expression) {
176         if (expression == null) {
177             throw new IllegalArgumentException JavaDoc();
178         }
179         ASTNode oldChild = this.expression;
180         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
181         this.expression = expression;
182         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
183     }
184     
185     /* (omit javadoc for this method)
186      * Method declared on ASTNode.
187      */

188     int memSize() {
189         return super.memSize() + 1 * 4;
190     }
191     
192     /* (omit javadoc for this method)
193      * Method declared on ASTNode.
194      */

195     int treeSize() {
196         return
197             memSize()
198             + (this.expression == null ? 0 : getExpression().treeSize());
199     }
200 }
201
Popular Tags