KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.lib.java.parser.ASTree;
22 import org.netbeans.lib.java.parser.ASTreeTypes;
23 import org.netbeans.lib.java.parser.ParserTokens;
24 import org.netbeans.mdr.storagemodel.StorableObject;
25 import org.netbeans.modules.javacore.parser.ASTProvider;
26 import org.netbeans.modules.javacore.parser.AnnotationInfo;
27 import org.netbeans.modules.javacore.parser.MDRParser;
28 import java.util.*;
29 import org.netbeans.jmi.javamodel.*;
30 import org.netbeans.mdr.handlers.AttrListWrapper;
31 import org.netbeans.mdr.handlers.BaseObjectHandler;
32 import org.netbeans.mdr.persistence.StorageException;
33 import org.netbeans.mdr.storagemodel.StorableFeatured;
34 import org.openide.ErrorManager;
35
36 /**
37  *
38  * @author Martin Matula
39  */

40 public abstract class LocalVarDeclarationImpl extends StatementImpl implements LocalVarDeclaration {
41     private LightAttrList variables;
42     private boolean isFinal;
43     private TypeReference typeName;
44     private LightAttrList annotations = null;
45     
46     /** Creates a new instance of LocalVarDeclarationImpl */
47     public LocalVarDeclarationImpl(StorableObject o) {
48         super(o);
49     }
50     
51     public boolean isFinal() {
52         if (isChanged(CHANGED_IS_FINAL)) {
53             return isFinal;
54         } else {
55             Object JavaDoc[] modInfo=(Object JavaDoc[])getModInfo();
56             return ((Boolean JavaDoc)modInfo[0]).booleanValue();
57         }
58     }
59
60     public void setFinal(boolean isFinal) {
61         objectChanged(CHANGED_IS_FINAL);
62         this.isFinal = isFinal;
63     }
64     
65     public TypeReference getTypeName() {
66         if (!childrenInited) {
67             initChildren();
68         }
69         return typeName;
70     }
71
72     public void setTypeName(TypeReference typeName) {
73         objectChanged(CHANGED_TYPE);
74         changeChild(getTypeName(), typeName);
75         this.typeName = typeName;
76     }
77
78     public Type getType() {
79         return (Type) getTypeName().getElement();
80     }
81     
82     public void setType(Type newValue) {
83         MultipartIdClass proxy = ((JavaModelPackage) refImmediatePackage()).getMultipartId();
84         setTypeName(proxy.createMultipartId(newValue.getName(), null, null));
85     }
86
87     public List getVariables() {
88         if (!childrenInited) {
89             initChildren();
90         }
91         return variables;
92     }
93     
94     public List/*<org.netbeans.jmi.javamodel.Annotation>*/ getAnnotations() {
95         if (!childrenInited) {
96             initChildren();
97         }
98         return annotations;
99     }
100     
101     public List getChildren() {
102         List result = new ArrayList();
103         result.addAll(getAnnotations());
104         addIfNotNull(result, getTypeName());
105         result.addAll(getVariables());
106         return result;
107     }
108     
109     protected void initChildren() {
110         childrenInited = false;
111         ASTree tree = getASTree();
112         if (tree != null) {
113             AnnotationInfo[] annotInfos = getAnnotInfos();
114             typeName = (TypeReference) initOrCreate(typeName, tree.getSubTrees()[1]);
115             variables = createChildrenList(variables, "variables", tree.getSubTrees()[2], ASTreeTypes.VARIABLE_DECLARATORS, CHANGED_VARIABLES, false); // NOI18N
116
createChildrenList(annotInfos);
117         }
118         childrenInited = true;
119     }
120
121     public String JavaDoc getRawText() {
122         boolean useSemicolon = !(refImmediateComposite() instanceof ForStatement);
123         if (!isNew()) {
124             if (!isChanged()) {
125                 MDRParser parser = getParser();
126                 ASTree tree = getASTree();
127                 String JavaDoc origSrc = parser.getSourceText();
128                 int startIndex, endIndex;
129                 String JavaDoc semicolon;
130                 String JavaDoc indentation;
131                 boolean isSemicolon = parser.getToken(tree.getLastToken()).getType() == ParserTokens.SEMICOLON;
132                 if (useSemicolon == isSemicolon) {
133                     semicolon = indentation = "";
134                     startIndex = IndentUtil.getElementStart(this);
135                     endIndex = IndentUtil.getElementEnd(this);
136                 } else {
137                     startIndex = getStartOffset(parser, tree, false);
138                     if (useSemicolon) {
139                         semicolon = ";"; // NOI18N
140
indentation = getIndentation();
141                         endIndex = IndentUtil.getElementEnd(this);
142                     } else {
143                         indentation = semicolon = "";
144                         endIndex = getEndOffset(parser, parser.getToken(tree.getLastToken() - 1));
145                     }
146                 }
147                 return indentation + origSrc.substring(startIndex, endIndex) + semicolon;
148             } else {
149                 if (!childrenInited) {
150                     initChildren();
151                 }
152             }
153         }
154         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(37);
155         for (Iterator annIt = getAnnotations().iterator(); annIt.hasNext(); ) {
156             AnnotationImpl ann = (AnnotationImpl) annIt.next();
157             buf.append(ann.getSourceText()).append('\n').append(getIndentation());
158         }
159         if (isFinal()) {
160             buf.append("final "); // NOI18N
161
}
162         TypeReference typeName = getTypeName();
163         if (typeName != null) {
164             buf.append(((MetadataElement) typeName).getSourceText());
165         } else {
166             buf.append(getType().getName());
167         }
168         if (((TransientElement) getVariables().get(0)).isNew()) {
169             buf.append(' ');
170         }
171         Iterator iter = getVariables().iterator();
172         while (iter.hasNext()) {
173             TransientElement var = (TransientElement) iter.next();
174             buf.append(var.getSourceText());
175             if (iter.hasNext()) {
176                 formatElementPart(MetadataElement.COMMA, buf);
177             }
178         }
179         if (useSemicolon) {
180             buf.append(";");
181         }
182         return buf.toString();
183     }
184
185     public void getDiff(List diff) {
186         ASTree tree = getASTree();
187         MDRParser parser = (MDRParser) tree.getASTContext();
188         ASTree[] children = tree.getSubTrees();
189         AnnotationInfo infos[] = getAnnotInfos();
190
191         if (isChanged(CHANGED_IS_FINAL) || isChanged(CHANGED_ANNOTATION)) {
192             diffModifiers(diff, children[1], parser);
193         } else if (children[0] != null) {
194             getCollectionDiff(diff, parser, CHANGED_ANNOTATION, infos, getAnnotations(), parser.getToken(children[0].getLastToken()).getEndOffset(), " "); // NOI18N
195
}
196         getChildDiff(diff, parser, children[1], (MetadataElement) getTypeName(), CHANGED_TYPE);
197         getCollectionDiff(diff, parser, CHANGED_VARIABLES, children[2],
198             ASTreeTypes.VARIABLE_DECLARATORS, getVariables(),
199             parser.getToken(children[2].getLastToken()).getEndOffset(), formatElementPart(MetadataElement.COMMA));
200     }
201     
202     void setData(List variables, boolean isFinal, TypeReference typeName) {
203         this.variables = createChildrenList("variables", variables, CHANGED_VARIABLES); // NOI18N
204
createChildrenList(null);
205         this.isFinal = isFinal;
206         changeChild(null, typeName);
207         this.typeName = typeName;
208     }
209
210     protected void _delete() {
211         // --- delete components -------------------------------------------
212
if (childrenInited) {
213             deleteChildren(variables);
214             deleteChild(typeName);
215         }
216         // --- delete links -----------------------------------------------
217
// no links to delete
218
// --- call super ---------------------------------------
219
super._delete();
220     }
221     
222     public void replaceChild(Element oldElement,Element newElement) {
223         if (childrenInited) {
224             if (replaceObject(annotations, oldElement, newElement)) return;
225             if (replaceObject(variables, oldElement, newElement)) return;
226             if (oldElement.equals(typeName)) {
227                 setTypeName((TypeReference)newElement);
228             }
229         }
230     }
231
232     public Element duplicate(JavaModelPackage targetExtent) {
233         LocalVarDeclarationImpl var = (LocalVarDeclarationImpl) targetExtent.getLocalVarDeclaration().createLocalVarDeclaration(
234                 isFinal(),
235                 (TypeReference) duplicateElement(getTypeName(), targetExtent),
236                 duplicateList(variables, targetExtent)
237                );
238         var.getAnnotations().addAll(duplicateList(getAnnotations(), targetExtent));
239         return var;
240     }
241     
242     // ------------ PRIVATE METHODS --------------------------------------------
243

244     protected void diffModifiers(List diffList, ASTree nextNode, ASTProvider parser) {
245         String JavaDoc text = "";
246         for (Iterator annIt = getAnnotations().iterator(); annIt.hasNext(); ) {
247             AnnotationImpl ann = (AnnotationImpl) annIt.next();
248             text += ann.getSourceText();
249             text += '\n';
250             text += getIndentation();
251         }
252         if (isFinal()) {
253             text += "final"; // NOI18N
254
}
255         int nextToken = nextNode.getFirstToken();
256         int startOffset, endOffset;
257         int startToken;
258         endOffset = parser.getToken(nextToken).getStartOffset();
259         ASTree modifiers = getASTree().getSubTrees()[0];
260         if (modifiers != null) {
261             startToken = modifiers.getFirstToken();
262             startOffset = parser.getToken(startToken).getStartOffset();
263             if (text.length() > 0) {
264                 int endToken = modifiers.getLastToken();
265                 endOffset = parser.getToken(endToken).getEndOffset();
266             }
267         } else {
268             startOffset = endOffset;
269             if (isFinal()) {
270                 text += ' ';
271             }
272         }
273         diffList.add(new DiffElement(startOffset, endOffset, text));
274     }
275
276     private void createChildrenList(AnnotationInfo[] infos) {
277         DeferredAttrList deferredList;
278         try {
279             if (annotations != null && infos != null) {
280                 deferredList = (DeferredAttrList) ((AttrListWrapper) annotations.getInnerList()).getInnerList();
281                 int i = 0;
282                 for (ListIterator it = deferredList.listIterator(); it.hasNext(); i++) {
283                     AnnotationImpl im = (AnnotationImpl) it.next();
284                     if (isNew()) {
285                         im.setNew();
286                     } else {
287                         if (i >= infos.length) {
288                             it.remove();
289                         } else {
290                             im.setElementInfo(infos[i]);
291                         }
292                     }
293                 }
294                 for (; i < infos.length; i++) {
295                     deferredList.add(createElement(infos[i]));
296                 }
297             } else {
298                 JavaModelPackage pkg = (JavaModelPackage) refImmediatePackage();
299                 StorableFeatured storable = (StorableFeatured) ((BaseObjectHandler) pkg.getLocalVariable())._getDelegate();
300                 deferredList = new DeferredAttrList((StorableFeatured)_getDelegate(), storable.getClassProxy().getAttrDesc("annotations"), new ArrayList()); // NOI18N
301
annotations = createWrapper("annotations", deferredList, CHANGED_ANNOTATION); // NOI18N
302
if (deferredList != null && infos != null) {
303                     for (int i = 0; i < infos.length; i++) {
304                         MetadataElement s = createElement(infos[i]);
305                         if (s != null) {
306                             deferredList.add(s);
307                         }
308                     }
309                 }
310             }
311         } catch (StorageException e) {
312             throw (GeneralException) ErrorManager.getDefault().annotate(new GeneralException(e.getMessage()), e);
313         }
314     }
315     
316     private AnnotationInfo[] getAnnotInfos() {
317         Object JavaDoc[] modInfo=(Object JavaDoc[])getModInfo();
318         AnnotationInfo[] ann=(AnnotationInfo[])modInfo[1];
319         return ann == null ? AnnotationInfo.EMPTY_ANNOTATIONS : ann;
320     }
321
322     private static final Object JavaDoc[] NO_MODIFIERS=new Object JavaDoc[]{Boolean.FALSE,null};
323     
324     private Object JavaDoc getModInfo() {
325         ASTree tree = getASTree();
326         MDRParser parser = (MDRParser) tree.getASTContext();
327         ASTree modifiers = tree.getSubTrees()[0];
328         return modifiers == null ? NO_MODIFIERS : parser.getSemanticInfo(modifiers, null);
329     }
330 }
331
Popular Tags