KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import org.netbeans.jmi.javamodel.*;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.lib.java.parser.ASTreeTypes;
25 import org.netbeans.mdr.storagemodel.StorableObject;
26 import org.netbeans.modules.javacore.internalapi.JavaModelUtil;
27 import org.netbeans.modules.javacore.parser.ASTProvider;
28 import org.netbeans.modules.javacore.parser.ClassInfo;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32
33 /**
34  *
35  * @author Martin Matula
36  */

37 public abstract class NewClassExpressionImpl extends InvocationImpl implements NewClassExpression {
38     private ClassDefinition classDefinition;
39     private PrimaryExpression enclosingClass;
40     private MultipartId className;
41
42     /** Creates a new instance of NewClassExpressionImpl */
43     public NewClassExpressionImpl(StorableObject o) {
44         super(o);
45     }
46     
47     public void setEnclosingClass(PrimaryExpression expression) {
48         objectChanged(CHANGED_ENCLOSING_CLASS);
49         changeChild(getEnclosingClass(), expression);
50         this.enclosingClass = expression;
51     }
52     
53     public PrimaryExpression getEnclosingClass() {
54         if (!childrenInited) {
55             initChildren();
56         }
57         return enclosingClass;
58     }
59     
60     public Type getType() {
61         if (isChanged(CHANGED_TYPE))
62             return type;
63         else {
64             return (Type) getParser().getSemanticInfo(getNameAST(), this);
65         }
66     }
67     
68     public void setType(Type type) {
69         this.type = type;
70         objectChanged(CHANGED_TYPE);
71     }
72     
73     public ClassDefinition getClassDefinition() {
74         if (!childrenInited) {
75             initChildren();
76         }
77         return classDefinition;
78     }
79     
80     public void setClassDefinition(ClassDefinition classDefinition) {
81         objectChanged(CHANGED_CLASS_DEFINITION);
82         changeChild(getClassDefinition(), classDefinition);
83         this.classDefinition = classDefinition;
84     }
85     
86     public MultipartId getClassName() {
87         if (!childrenInited) {
88             initChildren();
89         }
90         return className;
91     }
92
93     public void setClassName(MultipartId className) {
94         objectChanged(CHANGED_CLASS_NAME);
95         changeChild(getClassName(), className);
96         this.className = className;
97     }
98
99     protected ASTree getNameAST() {
100         return getASTree().getSubTrees()[2];
101     }
102
103     public List JavaDoc getChildren() {
104         List JavaDoc list = new ArrayList JavaDoc(6);
105         addIfNotNull(list, getEnclosingClass());
106         addIfNotNull(list, getClassName());
107         list.addAll(super.getChildren());
108         addIfNotNull(list, getClassDefinition());
109         return list;
110     }
111     
112     protected void initChildren() {
113         childrenInited = false;
114         ASTree tree = getASTree();
115         if (tree != null) {
116             ASTree[] parts = tree.getSubTrees();
117             enclosingClass = (PrimaryExpression) initOrCreate(enclosingClass, parts[0]);
118             className = (MultipartId) initOrCreate(className, parts[2]);
119             parameters = createChildrenList(parameters, "parameters", parts[3], ASTreeTypes.ARGUMENT_LIST, CHANGED_PARAMETERS, false); // NOI18N
120
ClassInfo newClassInfo = null;
121             if (parts[4] != null) {
122                 newClassInfo = (ClassInfo)getParser().getSemanticInfo(parts[4], this);
123             }
124             if (classDefinition != null) {
125                 if (newClassInfo == null) {
126                     changeChild(classDefinition, null);
127                     classDefinition.refDelete();
128                     classDefinition = null;
129                 } else {
130                     ClassDefinitionImpl defImpl = (ClassDefinitionImpl)classDefinition;
131                     
132                     defImpl.updatePersistent(newClassInfo);
133                     defImpl.setElementInfo(newClassInfo);
134                 }
135             } else if (newClassInfo != null) {
136                 SemiPersistentElement spe=(SemiPersistentElement)JavaModelUtil.getDeclaringFeature(this);
137                 classDefinition = (ClassDefinition)spe.createElement(newClassInfo);
138                 changeChild(null, classDefinition);
139             }
140         }
141         childrenInited = true;
142     }
143
144     public String JavaDoc getSourceText() {
145         // delegate
146
return getRawText();
147     }
148     
149     String JavaDoc getRawText() {
150         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
151         if (enclosingClass != null) {
152             //print enclosing class if it exist
153
buf.append(((MetadataElement) enclosingClass).getSourceText());
154             buf.append("."); // NOI18N
155
}
156         buf.append("new "); // NOI18N
157
MultipartId className = getClassName();
158         if (className != null) {
159             buf.append(((MetadataElement) className).getSourceText());
160         } else {
161             buf.append(getName());
162         }
163         formatElementPart(PAR_OPEN_BRACKET, buf);
164         Iterator JavaDoc iter = getParameters().iterator();
165         while (iter.hasNext()) {
166             //print parameters separated by commas
167
StatementImpl par = (StatementImpl) iter.next();
168             buf.append(par.getSourceText());
169             if (iter.hasNext()) {
170                 formatElementPart(MetadataElement.COMMA, buf);
171             }
172         }
173         formatElementPart(PAR_CLOSE_BRACKET, buf);
174         
175         ClassDefinition classDefinition = getClassDefinition();
176         if (classDefinition != null) {
177             //print class definition
178
buf.append(((ClassDefinitionImpl) classDefinition).getSourceText());
179         }
180         return buf.toString();
181     }
182     
183     private static final int ENCLOSING = 0;
184     private static final int NAME = 2;
185     private static final int PARAMS = 3;
186     private static final int BODY = 4;
187
188     public void getDiff(List JavaDoc diffList) {
189         ASTProvider parser = getParser();
190         ASTree[] children = getASTree().getSubTrees();
191         
192         //getChildDiff(diffList, parser, children[ENCLOSING], (MetadataElement) getEnclosingClass(), CHANGED_ENCLOSING_CLASS);
193
//[TODO] following statement could be probably somehow replaced by getChildDiff, but it does not work for me now
194
if (isChanged(CHANGED_ENCLOSING_CLASS) || ((enclosingClass != null) && ((MetadataElement) enclosingClass).isChanged())) {
195             if (children[ENCLOSING] != null) {
196                 if (enclosingClass != null) {
197                     //change
198
((MetadataElement) enclosingClass).getDiff(diffList);
199                 } else {
200                     //delete
201
diffList.add( new DiffElement(
202                     parser.getToken(children[ENCLOSING].getFirstToken()).getStartOffset(),
203                     parser.getToken(children[ENCLOSING].getLastToken()+1).getEndOffset(),
204                     "")
205                     );
206                 }
207             } else {
208                 //print enclosing class and "."
209
int startPos = parser.getToken(children[NAME].getFirstToken()-1).getStartOffset();
210                 diffList.add(new DiffElement(startPos, startPos, ((MetadataElement) enclosingClass).getSourceText() + ".")); // NOI18N
211
}
212         }
213         getChildDiff(diffList, parser, children[NAME], (MetadataElement) getClassName(), CHANGED_CLASS_NAME);
214
215         int endToken = children[PARAMS] != null ? children[PARAMS].getLastToken() : children[NAME].getLastToken() + 1;
216         int endOffset = parser.getToken(endToken).getEndOffset();
217         getCollectionDiff(diffList, parser, CHANGED_PARAMETERS, children[PARAMS], ASTreeTypes.ARGUMENT_LIST, getParameters(), endOffset, formatElementPart(MetadataElement.COMMA));
218         int startToken = getASTree().getLastToken();
219         getChildDiff(diffList, parser, children[BODY], (MetadataElement)getClassDefinition(), CHANGED_CLASS_DEFINITION, parser.getToken(startToken).getEndOffset(), null);
220     }
221     
222     void setData(String JavaDoc name, List JavaDoc parameters, PrimaryExpression enclosingClass, ClassDefinition classDefinition, MultipartId className) {
223         setData(name, parameters);
224         changeChild(null, enclosingClass);
225         this.enclosingClass = enclosingClass;
226         changeChild(null, classDefinition);
227         this.classDefinition = classDefinition;
228         changeChild(null, className);
229         this.className = className;
230     }
231
232     protected void _delete() {
233         // --- delete components -------------------------------------------
234
if (childrenInited) {
235             deleteChild(enclosingClass);
236             deleteChild(classDefinition);
237             deleteChild(className);
238         }
239         // --- delete links -----------------------------------------------
240
// no links to delete
241
// --- call super ---------------------------------------
242
super._delete();
243     }
244     
245     public void replaceChild(Element oldElement,Element newElement) {
246         if (childrenInited) {
247             if (oldElement.equals(classDefinition)) {
248                 setClassDefinition((ClassDefinition)newElement);
249             } else if (oldElement.equals(enclosingClass)) {
250                 setEnclosingClass((PrimaryExpression)newElement);
251             } else if (oldElement.equals(className)) {
252                 setClassName((MultipartId)newElement);
253             } else
254                 super.replaceChild(oldElement,newElement);
255         }
256     }
257
258     public NamedElement getElement() {
259         NamedElement el=(NamedElement) getParser().getSemanticInfo(getASTree(), this);
260         
261         if (el instanceof Constructor) {
262             return el;
263         }
264         return null;
265     }
266     
267     public Element duplicate(JavaModelPackage targetExtent) {
268         return targetExtent.getNewClassExpression().createNewClassExpression(
269                 getName(),
270                 duplicateList(getParameters(), targetExtent),
271                 (PrimaryExpression) duplicateElement(getEnclosingClass(), targetExtent),
272                 (MultipartId) duplicateElement(getClassName(), targetExtent),
273                 (ClassDefinition) duplicateElement(getClassDefinition(), targetExtent)
274                );
275     }
276 }
277
Popular Tags