KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayReference;
22 import org.netbeans.jmi.javamodel.MultipartId;
23 import org.netbeans.jmi.javamodel.NamedElement;
24 import org.netbeans.lib.java.parser.ASTree;
25 import org.netbeans.mdr.storagemodel.StorableObject;
26
27 import java.util.List JavaDoc;
28 import org.netbeans.jmi.javamodel.Element;
29 import org.netbeans.jmi.javamodel.JavaModelPackage;
30
31 /**
32  *
33  * @author Martin Matula
34  */

35 public abstract class ArrayReferenceImpl extends TypeReferenceImpl implements ArrayReference {
36     private int dimCount;
37
38     /** Creates a new instance of MultipartIdImpl */
39     public ArrayReferenceImpl(StorableObject o) {
40         super(o);
41     }
42     
43     protected ASTree getNameAST() {
44         return getASTree();
45     }
46
47     public String JavaDoc getName() {
48         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
49         buf.append(getParent().getName());
50         appendDims(buf);
51         return buf.toString();
52     }
53     
54     void setData(String JavaDoc name, MultipartId parent, int dimCount) {
55         super.setData(name, parent);
56         this.dimCount = dimCount;
57     }
58
59     public String JavaDoc getSourceText() {
60         String JavaDoc origElem;
61         if ((origElem = checkChange()) != null)
62             return origElem;
63         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
64         buf.append(((MetadataElement) getParent()).getSourceText());
65         appendDims(buf);
66         return buf.toString();
67     }
68
69     public void setDimCount(int dimCount) {
70         objectChanged(CHANGED_DIM_COUNT);
71         this.dimCount = dimCount;
72     }
73
74     public int getDimCount() {
75         if (isChanged(CHANGED_DIM_COUNT)) {
76             return dimCount;
77         } else {
78             ASTree dimsTree = getASTree().getSubTrees()[1];
79             return (dimsTree.getLastToken() - dimsTree.getFirstToken() + 1) / 2;
80         }
81     }
82
83     private StringBuffer JavaDoc appendDims(StringBuffer JavaDoc buf) {
84         for (int i = 0; i < getDimCount(); i++) {
85             formatElementPart(ARRAY_OPEN_BRACKET, buf);
86             formatElementPart(ARRAY_CLOSE_BRACKET, buf);
87         }
88         return buf;
89     }
90
91     public void getDiff(List JavaDoc diff) {
92         ASTree tree = getASTree();
93         getChildDiff(diff, getParser(), tree.getSubTrees()[0], (MetadataElement) getParent(), CHANGED_PARENT);
94         if (isChanged(CHANGED_DIM_COUNT)) {
95             replaceNode(diff, getParser(), tree.getSubTrees()[1], appendDims(new StringBuffer JavaDoc()).toString(), 0, null);
96         }
97     }
98     
99     public NamedElement getElement() {
100         return (NamedElement) getParser().getSemanticInfo(getASTree(), this);
101     }
102     
103     /**
104      * Creates copy of the ArrayReference instance
105      */

106     public Element duplicate(JavaModelPackage targetExtent) {
107         return targetExtent.getArrayReference().createArrayReference(
108             getName(),
109             (MultipartId) duplicateElement(getParent(), targetExtent),
110             getDimCount()
111         );
112     }
113 }
114
Popular Tags