KickJava   Java API By Example, From Geeks To Geeks.

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


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.jmi.javamodel.*;
22 import org.netbeans.lib.java.parser.ASTree;
23 import org.netbeans.mdr.storagemodel.StorableObject;
24 import org.netbeans.modules.javacore.parser.ASTProvider;
25 import org.netbeans.modules.javacore.parser.ASTUtil;
26 import org.netbeans.modules.javacore.parser.MDRParser;
27 import javax.jmi.reflect.ConstraintViolationException;
28 import java.util.*;
29
30 /**
31  *
32  * @author Martin Matula
33  */

34 public abstract class LocalVariableImpl extends TransientElement implements LocalVariable {
35     private String JavaDoc name;
36     private boolean isFinal;
37     private InitialValue initialValue;
38     private String JavaDoc initialValueText;
39     protected Type type;
40     private int dimCount;
41
42     /** Creates a new instance of LocalVariableImpl */
43     public LocalVariableImpl(StorableObject o) {
44         super(o);
45     }
46     
47     public String JavaDoc getName() {
48         if (isChanged(CHANGED_NAME)) {
49             return name;
50         } else {
51             return ASTUtil.getIdentifier(getASTree().getSubTrees()[0]);
52         }
53     }
54     
55     public void setName(String JavaDoc name) {
56         objectChanged(CHANGED_NAME);
57         this.name = name;
58     }
59
60     public int getDimCount() {
61         if (isChanged(CHANGED_DIM_COUNT)) {
62             return dimCount;
63         } else {
64             ASTree dims = getASTree().getSubTrees()[1];
65             if (dims == null) {
66                 return 0;
67             } else {
68                 return (dims.getLastToken() - dims.getFirstToken() + 1) / 2;
69             }
70         }
71     }
72
73     public void setDimCount(int dimCount) {
74         objectChanged(CHANGED_DIM_COUNT);
75         this.dimCount = dimCount;
76     }
77
78     public boolean isFinal() {
79         LocalVarDeclarationImpl parent = (LocalVarDeclarationImpl) refImmediateComposite();
80         if (parent == null) {
81             if (isChanged(CHANGED_IS_FINAL)) {
82                 return isFinal;
83             } else {
84                 return getASTree().getSubTrees()[0] != null;
85             }
86         } else {
87             return parent.isFinal();
88         }
89     }
90     
91     public void setFinal(boolean isFinal) {
92         LocalVarDeclarationImpl parent = (LocalVarDeclarationImpl) refImmediateComposite();
93         if (parent == null) {
94             objectChanged(CHANGED_IS_FINAL);
95             this.isFinal = isFinal;
96         } else {
97             throw new ConstraintViolationException(null, null);
98         }
99     }
100     
101     public List getAnnotations() {
102         Object JavaDoc parent = refImmediateComposite();
103         if (parent instanceof LocalVarDeclarationImpl) {
104             return ((LocalVarDeclarationImpl) parent).getAnnotations();
105         } else {
106             return Collections.EMPTY_LIST;
107         }
108     }
109     
110     public InitialValue getInitialValue() {
111         if (!childrenInited) {
112             initChildren();
113         }
114         return initialValue;
115     }
116     
117     public void setInitialValue(InitialValue initialValue) {
118         objectChanged(CHANGED_INITIAL_VALUE);
119         changeChild(getInitialValue(), initialValue);
120         this.initialValue = initialValue;
121     }
122
123     public TypeReference getTypeName() {
124         LocalVarDeclaration composite = (LocalVarDeclaration) refImmediateComposite();
125         if (composite != null) {
126             return composite.getTypeName();
127         }
128         return null;
129     }
130
131     public void setTypeName(TypeReference typeName) {
132         throw new ConstraintViolationException(null, null, "Cannot set typename on LocalVariable."); // NOI18N
133
}
134
135     public String JavaDoc getInitialValueText() {
136         if (isChanged(CHANGED_INITIAL_VALUE)) {
137             return initialValueText;
138         } else {
139             return extractInitialValueText();
140         }
141     }
142     
143     public void setInitialValueText(String JavaDoc initialValueText) {
144         objectChanged(CHANGED_INITIAL_VALUE);
145         this.initialValueText = initialValueText;
146     }
147     
148     private ASTree extractInitialValue() {
149         return getASTree().getSubTrees()[2];
150     }
151     
152     private String JavaDoc extractInitialValueText() {
153         MDRParser parser = getParser();
154         ASTree initValue = extractInitialValue();
155         if (initValue == null)
156             return null;
157         int firstToken = initValue.getFirstToken();
158         int lastToken = initValue.getLastToken();
159         return parser.getText(parser.getToken(firstToken), parser.getToken(lastToken));
160     }
161     
162     public List getChildren() {
163         List list = new ArrayList(1);
164         addIfNotNull(list, getInitialValue());
165         return list;
166     }
167     
168     protected void initChildren() {
169         childrenInited = false;
170         ASTree tree = getASTree();
171         if (tree != null) {
172             initialValue = (InitialValue) initOrCreate(initialValue, extractInitialValue());
173         }
174         childrenInited = true;
175     }
176
177     /**
178      */

179     protected ASTree getPartTree(ElementPartKind part) {
180         // name
181
if (ElementPartKindEnum.NAME.equals(part)) {
182             return getASTree().getSubTrees()[0];
183         }
184         throw new IllegalArgumentException JavaDoc("Invalid part for this element: " + part); // NOI18N
185
}
186     
187     public String JavaDoc getSourceText() {
188         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
189         String JavaDoc name = getName();
190         boolean nju = isNew();
191         if (!nju) IndentUtil.reformatHeadGarbage(this, buf);
192         buf.append(name);
193         appendDims(buf, getDimCount());
194         String JavaDoc initialValueText = getInitialValueText();
195         if (initialValueText != null && initialValueText.trim().length() != 0) {
196             formatElementPart(FIELD_EQUALS, buf);
197             buf.append(initialValueText);
198         } else {
199             TransientElement initialValue = (TransientElement) getInitialValue();
200             if (initialValue != null) {
201                 formatElementPart(FIELD_EQUALS, buf);
202                 buf.append(initialValue.getSourceText());
203             }
204         }
205         return buf.toString();
206     }
207     
208     public void getDiff(List diff) {
209         ASTProvider parser = getParser();
210         ASTree tree = getASTree();
211         ASTree[] children = tree.getSubTrees();
212         
213         if (isChanged(CHANGED_NAME)) {
214             replaceNode(diff, parser, children[0], getName(), 0, null);
215         }
216         if (isChanged(CHANGED_DIM_COUNT)) {
217             replaceNode(diff, parser, children[1], appendDims(new StringBuffer JavaDoc(), getDimCount()).toString(), getEndOffset(getParser(), children[0]), "");
218         }
219         if (isChanged(CHANGED_TYPE)) {
220             Type type = getType();
221             Type parentType = ((LocalVarDeclarationImpl) refImmediateComposite()).getType();
222             StringBuffer JavaDoc dims = new StringBuffer JavaDoc();
223             while (type != parentType) {
224                 type = ((Array) type).getType();
225                 formatElementPart(ARRAY_OPEN_BRACKET, dims);
226                 formatElementPart(ARRAY_CLOSE_BRACKET, dims);
227             }
228             replaceNode(diff, parser, children[1], dims.toString(), parser.getToken(children[0].getLastToken()).getEndOffset(), "");
229         }
230         if (isChanged(CHANGED_INITIAL_VALUE)) {
231             int index = children[1] != null ? children[1].getLastToken() : children[0].getLastToken();
232             int pos = parser.getToken(index).getEndOffset();
233             if (initialValueText == null) {
234                 getChildDiff(diff, parser, children[2], (MetadataElement) initialValue, CHANGED_INITIAL_VALUE, pos, formatElementPart(FIELD_EQUALS));
235             } else {
236                 replaceNode(diff, parser, children[2], initialValueText, pos, formatElementPart(FIELD_EQUALS));
237             }
238         } else if (isChanged(CHANGED_CHILDREN)) {
239             if (initialValue != null) {
240                 ((MetadataElement) initialValue).getDiff(diff);
241             }
242         }
243     }
244     
245     void setData(String JavaDoc name, List annotations, boolean isFinal, int dimCount, InitialValue initialValue, String JavaDoc initialValueText) {
246         this.name = name;
247         this.isFinal = isFinal;
248         changeChild(null, initialValue);
249         this.initialValue = initialValue;
250         this.initialValueText = initialValueText;
251         this.dimCount = dimCount;
252     }
253     
254     protected void _delete() {
255         // --- delete components -------------------------------------------
256
if (childrenInited) {
257             deleteChild(initialValue);
258         }
259         // --- delete links -----------------------------------------------
260
// no links to delete
261
// --- call super ---------------------------------------
262
super._delete();
263     }
264     
265     public void replaceChild(Element oldElement,Element newElement) {
266         if (childrenInited) {
267             if (oldElement.equals(initialValue)) {
268                 setInitialValue((InitialValue)newElement);
269             }
270         }
271     }
272
273     // Interfaces inherited from Variable, StructuralElement
274

275     /**
276      * Returns the value of reference type.
277      * @return Value of reference type.
278      */

279     public Type getType() {
280         if (isChanged(CHANGED_TYPE))
281             return type;
282         else {
283             Type type = ((LocalVarDeclarationImpl) refImmediateComposite()).getType();
284             ASTree dims = getASTree().getSubTrees()[1];
285             if (dims != null) {
286                 int d = (dims.getLastToken() - (dims.getFirstToken())) /2 + 1;
287                 ArrayClass arrClass = ((JavaModelPackage)type.refImmediatePackage()).getArray();
288                 for (int i = 0; i < d; i++){
289                     type = arrClass.resolveArray(type);
290                 }
291             }
292             return type;
293         }
294     }
295     
296     public Collection getReferences() {
297         Resource[] res = new Resource[]{getResource()};
298         UsageFinder finder = new UsageFinder(this);
299         return finder.getUsers(res);
300     }
301     public void setType(Type newValue) {
302         //System.out.println("LocalVariable._setType: "+((newValue!=null)?newValue.getName():"null"));
303
// objectChanged(CHANGED_TYPE);
304
// type = newValue;
305
throw new ConstraintViolationException(null, null, "Call setType on LocalVarDeclaration to change type of this variable."); // NOI18N
306
}
307     
308     public Element duplicate(JavaModelPackage targetExtent) {
309         return targetExtent.getLocalVariable().createLocalVariable(
310                 getName(), duplicateList(getAnnotations(), targetExtent), isFinal(),
311                 (TypeReference) duplicateElement(getTypeName(), targetExtent),
312                 getDimCount(),
313                 (InitialValue) duplicateElement(getInitialValue(), targetExtent),
314                 null
315                );
316     }
317 }
318
Popular Tags