KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > ForEachStatementImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javacore.jmiimpl.javamodel;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.jmi.javamodel.Element;
25 import org.netbeans.jmi.javamodel.Expression;
26 import org.netbeans.jmi.javamodel.ForEachStatement;
27 import org.netbeans.jmi.javamodel.JavaModelPackage;
28 import org.netbeans.jmi.javamodel.Parameter;
29 import org.netbeans.jmi.javamodel.Statement;
30 import org.netbeans.lib.java.parser.*;
31 import org.netbeans.mdr.storagemodel.StorableObject;
32 import org.netbeans.modules.javacore.parser.ASTProvider;
33 import org.netbeans.modules.javacore.parser.ParameterInfo;
34
35 /**
36  * Instance of elements represent enhanced for loop in java source, which is
37  * specified in JSR201 and supported by JDK 1.5.
38  *
39  * <code>
40  * Collection<String> c = ...;
41  * for (String s : c)
42  * body;
43  * </code>
44  *
45  * @see ForStatementImpl
46  *
47  * @author Pavel Flaska
48  */

49 public abstract class ForEachStatementImpl extends StatementImpl implements ForEachStatement {
50
51     // contains body (body can be represented by statement or statement block)
52
private Statement body = null;
53     // contains parameter, i.e. String s from the example above
54
private Parameter parameter = null;
55     // contains element which implements Iterable interface, i.e. c element
56
// from the example above
57
private Expression iterable = null;
58
59     /**
60      * Creates a new handler for a specified storable.
61      *
62      * @param o storable for which the handler is created.
63      */

64     public ForEachStatementImpl(StorableObject o) {
65         super(o);
66     }
67
68     /**
69      * Returns a statement representing body of the enhanced for loop.
70      *
71      * @return statement representing the body (often StatementBlock instance)
72      */

73     public Statement getBody() {
74         if (!childrenInited) {
75             initChildren();
76         }
77         return body;
78     }
79
80     /**
81      * Sets a new body for the enhanced for loop.
82      *
83      * @param body a new statement or a new statement block representing
84      * body
85      */

86     public void setBody(Statement body) {
87         objectChanged(CHANGED_BODY);
88         changeChild(getBody(), body);
89         this.body = body;
90     }
91
92     /**
93      * Returns a parameter representing object from the iterator.
94      * From the example above it is 'String s' text.
95      *
96      * @return a parameter object
97      */

98     public Parameter getParameter() {
99         if (!childrenInited) {
100             initChildren();
101         }
102         return parameter;
103     }
104
105     /**
106      * Sets a new parameter for the enhanced for loop.
107      * From the example above it is replacement of 'String s' text.
108      *
109      * @param a parameter object
110      */

111     public void setParameter(Parameter newValue) {
112         objectChanged(CHANGED_PARAMETER);
113         changeChild(getParameter(), parameter);
114         this.parameter = parameter;
115     }
116
117     /**
118      * Returns an iterable, i.e. class implementing iterable interface.
119      * From the example above it is 'c' text in for statement.
120      *
121      * @return iterable element
122      */

123     public Expression getIterable() {
124         if (!childrenInited) {
125             initChildren();
126         }
127         return iterable;
128     }
129
130     /**
131      * Sets an iterable, i.e. class use for iterating the collection.
132      * From the example above it is replacement of 'c' text in for statement.
133      *
134      * @param an interable
135      */

136     public void setIterable(Expression newValue) {
137         objectChanged(CHANGED_ITERABLE);
138         changeChild(getIterable(), iterable);
139         this.iterable = iterable;
140     }
141
142     public List JavaDoc getChildren() {
143         List JavaDoc list = new ArrayList JavaDoc(3);
144         list.add(getParameter());
145         list.add(getIterable());
146         list.add(getBody());
147         return list;
148     }
149
150     // generate source-text representation of element
151
String JavaDoc getRawText() {
152         // create helping local variables (cast them to impl to allow
153
// to call getSourceText() on them.
154
StatementImpl body = (StatementImpl) getBody();
155         ParameterImpl parameter = (ParameterImpl) getParameter();
156         ExpressionImpl iterable = (ExpressionImpl) getIterable();
157         
158         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
159         formatElementPart(FOR_KEYWORD, buf, isNew());
160         formatElementPart(STMT_OPEN_BRACKET, buf);
161         buf.append(parameter.getSourceText());
162         formatElementPart(COND_EXPR_COLON, buf);
163         buf.append(iterable.getSourceText());
164         formatElementPart(STMT_CLOSE_BRACKET, buf);
165         buf.append(body.getSourceText());
166         
167         return buf.toString();
168     }
169
170     // generate differencies from the changes
171
public void getDiff(List JavaDoc diff) {
172         ASTProvider parser = getParser();
173         ASTree tree = getASTree();
174         ASTree[] children = tree.getSubTrees();
175         
176         getChildDiff(diff, parser, children[0], (MetadataElement) getParameter(), CHANGED_PARAMETER);
177         getChildDiff(diff, parser, children[1], (MetadataElement) getIterable(), CHANGED_BODY);
178         getChildDiff(diff, parser, children[2], (MetadataElement) getBody(), CHANGED_BODY);
179     }
180
181     protected void _delete() {
182         // --- delete components ----------------------------------------------
183
if (childrenInited) {
184             deleteChild(body);
185             deleteChild(parameter);
186             deleteChild(iterable);
187         }
188         // --- delete links ---------------------------------------------------
189
// no links to delete
190
// --- call super -----------------------------------------------------
191
super._delete();
192     }
193     
194     /**
195      * Initialize elements (children) of this element.
196      */

197     protected void initChildren() {
198         childrenInited = false;
199         ASTree tree = getASTree();
200         if (tree != null) {
201             ASTree[] parts = tree.getSubTrees();
202             // parameter init
203
ASTree parameterAST = parts[0];
204             ParameterInfo paramInfo=(ParameterInfo)getParser().getSemanticInfo(parameterAST, this);
205             if (parameter == null) {
206                 parameter = (Parameter)createElement(paramInfo);
207                 changeChild(null, parameter);
208             } else {
209                 ((SemiPersistentElement)parameter).updatePersistent(paramInfo);
210                 ((SemiPersistentElement)parameter).setElementInfo(paramInfo);
211             }
212             // iterable init
213
iterable = (Expression) initOrCreate(iterable, parts[1]);
214             // body init
215
body = (Statement) initOrCreate(body, parts[2]);
216         }
217         childrenInited = true;
218     }
219
220     void setData(Statement body, Parameter parameter, Expression iterable) {
221         // body
222
changeChild(null, body);
223         this.body = body;
224         // parameter
225
changeChild(null, parameter);
226         this.parameter = parameter;
227         // iterable
228
changeChild(null, iterable);
229         this.iterable = iterable;
230     }
231     
232     public Element duplicate(JavaModelPackage targetExtent) {
233         return targetExtent.getForEachStatement().createForEachStatement(
234                 (Statement) duplicateElement(getBody(), targetExtent),
235                 (Parameter) duplicateElement(getParameter(), targetExtent),
236                 (Expression) duplicateElement(getIterable(), targetExtent)
237                );
238     }
239
240 }
241
Popular Tags