KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.*;
22 import org.netbeans.jmi.javamodel.Element;
23 import org.netbeans.jmi.javamodel.Expression;
24 import org.netbeans.jmi.javamodel.JavaModelPackage;
25 import org.netbeans.jmi.javamodel.ReturnStatement;
26 import org.netbeans.lib.java.parser.ASTree;
27 import org.netbeans.mdr.storagemodel.StorableObject;
28 import org.netbeans.modules.javacore.parser.ASTProvider;
29
30 /**
31  *
32  * @author Martin Matula
33  */

34 public abstract class ReturnStatementImpl extends StatementImpl implements ReturnStatement {
35     private Expression expression = null;
36     
37     /** Creates a new instance of ReturnStatementImpl */
38     public ReturnStatementImpl(StorableObject o) {
39         super(o);
40     }
41     
42     public void setExpression(Expression expression) {
43         objectChanged(CHANGED_EXPRESSION);
44         changeChild(getExpression(), expression);
45         this.expression = expression;
46     }
47     
48     public Expression getExpression() {
49         if (!childrenInited) {
50             initChildren();
51         }
52         return expression;
53     }
54     
55     public List getChildren() {
56         List list = new ArrayList(1);
57         addIfNotNull(list, getExpression());
58         return list;
59     }
60     
61     protected void initChildren() {
62         childrenInited = false;
63         ASTree tree = getASTree();
64         if (tree != null) {
65             expression = (Expression) initOrCreate(expression, tree.getSubTrees()[0]);
66         }
67         childrenInited = true;
68     }
69     
70     String JavaDoc getRawText() {
71         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
72         StatementImpl expr = (StatementImpl) getExpression();
73         buf.append("return"); // NOI18N
74
if (expr != null) {
75             buf.append(' ').append(expr.getSourceText());
76         }
77         buf.append(';');
78         return buf.toString();
79     }
80
81     public void getDiff(List diff) {
82         ASTProvider parser = getParser();
83         ASTree tree = getASTree();
84         ASTree[] children = tree.getSubTrees();
85
86         int pos = parser.getToken(tree.getFirstToken()).getEndOffset();
87         getChildDiff(diff, parser, children[0], (MetadataElement) getExpression(), CHANGED_EXPRESSION, pos, " "); // NOI18N
88
}
89     
90     void setData(Expression expression) {
91         changeChild(null, expression);
92         this.expression = expression;
93     }
94
95     protected void _delete() {
96         // --- delete components -------------------------------------------
97
if (childrenInited) {
98             deleteChild(expression);
99         }
100         // --- delete links -----------------------------------------------
101
// no links to delete
102
// --- call super ---------------------------------------
103
super._delete();
104     }
105     
106     public void replaceChild(Element oldElement,Element newElement) {
107         if (childrenInited) {
108             if (oldElement.equals(expression)) {
109                 setExpression((Expression)newElement);
110             }
111         }
112     }
113     
114     public Element duplicate(JavaModelPackage targetExtent) {
115         return targetExtent.getReturnStatement().createReturnStatement((Expression) duplicateElement(getExpression(), targetExtent));
116     }
117 }
118
Popular Tags