KickJava   Java API By Example, From Geeks To Geeks.

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


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.JavaModelPackage;
24 import org.netbeans.jmi.javamodel.MethodInvocation;
25 import org.netbeans.jmi.javamodel.PrimaryExpression;
26 import org.netbeans.jmi.javamodel.Type;
27 import org.netbeans.jmi.javamodel.Method;
28 import org.netbeans.jmi.javamodel.MethodInvocationClass;
29 import org.netbeans.lib.java.parser.ASTree;
30 import org.netbeans.lib.java.parser.ASTreeTypes;
31 import org.netbeans.mdr.storagemodel.StorableObject;
32 import org.netbeans.modules.javacore.parser.ASTProvider;
33
34
35 /**
36  *
37  * @author Martin Matula
38  */

39 public abstract class MethodInvocationImpl extends InvocationImpl implements MethodInvocation {
40     private boolean hasSuper = false;
41     private PrimaryExpression parentClass;
42     
43     /** Creates a new instance of MethodInvocationImpl */
44     public MethodInvocationImpl(StorableObject o) {
45         super(o);
46     }
47     
48     public void setParentClass(PrimaryExpression expression) {
49         objectChanged(CHANGED_PARENT_CLASS);
50         changeChild(getParentClass(), expression);
51         this.parentClass = expression;
52     }
53     
54     public PrimaryExpression getParentClass() {
55         if (!childrenInited) {
56             initChildren();
57         }
58         return parentClass;
59     }
60     
61     public boolean getHasSuper() {
62         if (isChanged(CHANGED_HAS_SUPER)) {
63             return hasSuper;
64         } else {
65             return getASTree().getSubTrees()[1] != null;
66         }
67     }
68     
69     public void setHasSuper(boolean hasSuper) {
70         objectChanged(CHANGED_HAS_SUPER);
71         this.hasSuper = hasSuper;
72     }
73     
74     public List getChildren() {
75         List list = new ArrayList();
76         addIfNotNull(list, getParentClass());
77         list.addAll(super.getChildren());
78         return list;
79     }
80     
81     protected void initChildren() {
82         childrenInited = false;
83         ASTree tree = getASTree();
84         if (tree != null) {
85             ASTree[] parts = tree.getSubTrees();
86             parentClass = (PrimaryExpression) initOrCreate(parentClass, parts[0]);
87             parameters = createChildrenList(parameters, "parameters", parts[4], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); // NOI18N
88
}
89         childrenInited = true;
90     }
91     
92      /**
93      * Returns the value of reference type.
94      * @return Value of reference type.
95      */

96     public Type getType() {
97         Object JavaDoc semInfo = getParser().getSemanticInfo(getASTree(), this);
98         
99         if (semInfo instanceof Method)
100             return ((Method)semInfo).getType();
101         return null;
102     }
103     
104     String JavaDoc getRawText() {
105         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
106         boolean hasSuper = getHasSuper ();
107         StatementImpl parentClass = (StatementImpl) getParentClass();
108         List params = getParameters();
109         String JavaDoc methodName = getName();
110         if (parentClass != null) {
111             buf.append(parentClass.getSourceText());
112             buf.append("."); // NOI18N
113
}
114         if (hasSuper) {
115             buf.append("super."); // NOI18N
116
}
117         buf.append(methodName);
118         // trees
119
ASTree tree = getASTree();
120         if (tree != null && tree.getSubTrees()[3] != null) {
121             IndentUtil.printTokenWithAllAround(tree.getSubTrees()[3].getLastToken()+1, getASTree().getFirstToken(), this, buf);
122         } else {
123             formatElementPart(STMT_OPEN_BRACKET, buf);
124         }
125         Iterator iter = params.iterator();
126         while (iter.hasNext()) {
127             StatementImpl par = (StatementImpl) iter.next();
128             buf.append(par.getSourceText());
129             if (iter.hasNext()) {
130                 ASTree t = par.getASTree();
131                 if (t != null) {
132                     int commaToken = t.getLastToken() + 1;
133                     IndentUtil.printTokenWithAllAround(commaToken, getASTree().getFirstToken(), this, buf);
134                 } else {
135                     formatElementPart(MetadataElement.COMMA, buf);
136                 }
137             }
138         }
139         if (tree != null) {
140             IndentUtil.printTokenWithAllAround(tree.getLastToken(), getASTree().getFirstToken(), this, buf);
141         } else {
142             formatElementPart(STMT_CLOSE_BRACKET, buf);
143         }
144         return buf.toString();
145     }
146
147     public void getDiff(List diff) {
148         ASTProvider parser = getParser();
149         ASTree tree = getASTree();
150         ASTree[] children = tree.getSubTrees();
151         
152         if (isChanged(CHANGED_HAS_SUPER)) {
153             int endOffset = parser.getToken(children[3].getFirstToken()-1).getEndOffset();
154             int startOffset = children[0]!=null?parser.getToken(children[1].getLastToken()).getEndOffset():endOffset;
155             diff.add(new DiffElement(startOffset, endOffset, isHasSuper()?"super":"")); //NOI18N
156
}
157         
158         MetadataElement parentClass = (MetadataElement) getParentClass();
159         if (isChanged(CHANGED_PARENT_CLASS) || (parentClass != null && parentClass.isChanged())) {
160             getChildDiff(diff, parser, children[0], parentClass, CHANGED_PARENT_CLASS);
161             //replaceNode(diff, parser, tree, getSourceText(), 0, null);
162
}
163         if (isChanged(CHANGED_NAME)) {
164             replaceNode(diff, parser, children[3], getName(), 0, null);
165         }
166         getCollectionDiff(diff, parser, CHANGED_PARAMETERS, children[4], ASTreeTypes.ARGUMENT_LIST, getParameters(),
167             parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(MetadataElement.COMMA));
168 }
169     
170     void setData(String JavaDoc name, List parameters, PrimaryExpression parentClass, boolean hasSuper) {
171         setData(name, parameters);
172         changeChild(null, parentClass);
173         this.parentClass = parentClass;
174         this.hasSuper = hasSuper;
175     }
176
177     protected ASTree getNameAST() {
178         return getASTree().getSubTrees()[3];
179     }
180
181     protected void _delete() {
182         // --- delete components -------------------------------------------
183
if (childrenInited) {
184             deleteChild(parentClass);
185         }
186         // --- delete links -----------------------------------------------
187
// no links to delete
188
// --- call super ---------------------------------------
189
super._delete();
190     }
191     
192     public void replaceChild(Element oldElement,Element newElement) {
193         if (childrenInited) {
194             if (oldElement.equals(parentClass)) {
195                 setParentClass((PrimaryExpression)newElement);
196             } else
197                 super.replaceChild(oldElement,newElement);
198         }
199     }
200     
201     public Element duplicate(JavaModelPackage targetExtent) {
202         return targetExtent.getMethodInvocation().createMethodInvocation(
203                 getName(),
204                 duplicateList(getParameters(), targetExtent),
205                 (PrimaryExpression) duplicateElement(getParentClass(), targetExtent),
206                 getHasSuper()
207                );
208     }
209
210     protected String JavaDoc getIndentation() {
211         return ((MetadataElement) refImmediateComposite()).getIndentation();
212     }
213 }
214
Popular Tags