KickJava   Java API By Example, From Geeks To Geeks.

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


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.Assignment;
23 import org.netbeans.jmi.javamodel.Element;
24 import org.netbeans.jmi.javamodel.Operator;
25 import org.netbeans.jmi.javamodel.OperatorEnum;
26 import org.netbeans.jmi.javamodel.PrefixExpression;
27 import org.netbeans.jmi.javamodel.PrimaryExpression;
28 import org.netbeans.jmi.javamodel.VariableAccess;
29 import org.netbeans.lib.java.parser.ASTree;
30 import org.netbeans.lib.java.parser.ASTreeTypes;
31 import org.netbeans.lib.java.parser.ParserTokens;
32 import org.netbeans.mdr.storagemodel.StorableObject;
33 import org.netbeans.modules.javacore.parser.ASTProvider;
34 import java.util.List JavaDoc;
35 import org.netbeans.jmi.javamodel.JavaModelPackage;
36 import org.netbeans.jmi.javamodel.Type;
37 import org.netbeans.jmi.javamodel.Variable;
38
39
40 /**
41  *
42  * @author Martin Matula
43  */

44 public abstract class VariableAccessImpl extends ElementReferenceImpl implements VariableAccess {
45     private boolean hasSuper = false;
46     private PrimaryExpression parentClass;
47     
48     /** Creates a new instance of VariableAccessImpl */
49     public VariableAccessImpl(StorableObject o) {
50         super(o);
51     }
52     
53     public void setParentClass(PrimaryExpression expression) {
54         objectChanged(CHANGED_PARENT_CLASS);
55         changeChild(getParentClass(), expression);
56         this.parentClass = expression;
57     }
58     
59     public PrimaryExpression getParentClass() {
60         if (!childrenInited) {
61             initChildren();
62         }
63         return parentClass;
64     }
65     
66     public boolean getHasSuper() {
67         if (isChanged(CHANGED_HAS_SUPER)) {
68             return hasSuper;
69         } else {
70             ASTree tree = getASTree();
71             return tree.getType() == ASTreeTypes.FIELD_ACCESS && tree.getSubTrees()[1] != null;
72         }
73     }
74     
75     public void setHasSuper(boolean hasSuper) {
76         objectChanged(CHANGED_HAS_SUPER);
77         this.hasSuper = hasSuper;
78     }
79     
80     public List JavaDoc getChildren() {
81         List JavaDoc list = new ArrayList JavaDoc(1);
82         addIfNotNull(list, getParentClass());
83         return list;
84     }
85     
86     protected void initChildren() {
87         childrenInited = false;
88         ASTree tree = getASTree();
89         if (tree != null) {
90             if (tree.getType() == ParserTokens.IDENTIFIER) {
91                 parentClass = null;
92             } else {
93                 parentClass = (PrimaryExpression) initOrCreate(parentClass, tree.getSubTrees()[0]);
94             }
95         }
96         childrenInited = true;
97     }
98     
99     protected ASTree getNameAST() {
100         ASTree tree = getASTree();
101         if (tree.getType() == ParserTokens.IDENTIFIER) {
102             return tree;
103         } else if (tree.getType() == ASTreeTypes.MULTI_PART_ID) {
104             return tree.getSubTrees()[1];
105         } else {
106             return tree.getSubTrees()[2];
107         }
108     }
109     
110     public Type getType() {
111         Object JavaDoc semInfo = getParser().getSemanticInfo(getASTree(), this);
112         
113         if (semInfo instanceof Variable)
114             return ((Variable)semInfo).getType();
115         return (Type)semInfo;
116     }
117     
118     public String JavaDoc getSourceText() {
119         String JavaDoc origElem;
120         if ((origElem = checkChange()) != null)
121             return origElem;
122         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
123         boolean hasSuper = getHasSuper ();
124         StatementImpl parentClass = (StatementImpl) getParentClass();
125         if (parentClass != null) {
126             buf.append(parentClass.getSourceText());
127             buf.append("."); // NOI18N
128
}
129         if (hasSuper) {
130             buf.append("super."); // NOI18N
131
}
132         buf.append(getName());
133         return buf.toString();
134     }
135
136     public void getDiff(List JavaDoc diff) {
137         ASTProvider parser = getParser();
138         ASTree tree = getASTree();
139
140         MetadataElement parentClass = (MetadataElement) getParentClass();
141         if (isChanged(CHANGED_HAS_SUPER | CHANGED_PARENT_CLASS) || (parentClass != null && parentClass.isChanged())) {
142             replaceNode(diff, parser, tree, getSourceText(), 0, null);
143         } else {
144             if (isChanged(CHANGED_NAME)) {
145                 replaceNode(diff, parser, getNameAST(), getName(), 0, null);
146             }
147         }
148     }
149     
150     void setData(String JavaDoc name, PrimaryExpression parentClass, boolean hasSuper) {
151         setData(name);
152         changeChild(null, parentClass);
153         this.parentClass = parentClass;
154         this.hasSuper = hasSuper;
155     }
156
157     protected void _delete() {
158         // --- delete components -------------------------------------------
159
if (childrenInited) {
160             deleteChild(parentClass);
161         }
162         // --- delete links -----------------------------------------------
163
// no links to delete
164
// --- call super ---------------------------------------
165
super._delete();
166     }
167         
168     public void replaceChild(Element oldElement,Element newElement) {
169         if (childrenInited) {
170             if (oldElement.equals(parentClass)) {
171                 setParentClass((PrimaryExpression)newElement);
172             }
173         }
174     }
175     
176     public Element duplicate(JavaModelPackage targetExtent) {
177         return targetExtent.getVariableAccess().createVariableAccess(
178                 getName(), (PrimaryExpression) duplicateElement(getParentClass(), targetExtent), getHasSuper());
179     }
180
181     public boolean isRead() {
182         Element parent = (Element) refImmediateComposite();
183         if (parent instanceof Assignment) {
184             Assignment assign=(Assignment)parent;
185             
186             if (assign.getLeftSide().equals(this) && assign.getOperator().equals(OperatorEnum.ASSIGN))
187                 return false;
188         }
189         return true;
190     }
191
192     public boolean isWrite() {
193         Element parent = (Element) refImmediateComposite();
194         if (parent instanceof Assignment) {
195             Assignment assign=(Assignment)parent;
196             
197             if (assign.getLeftSide().equals(this))
198                 return true;
199         } else if (parent instanceof PrefixExpression) { // prefix or postfix expression
200
PrefixExpression exp=(PrefixExpression)parent;
201             Operator op=exp.getOperator();
202             
203             if (op.equals(OperatorEnum.INC) || op.equals(OperatorEnum.DEC))
204                 return true;
205         }
206         return false;
207     }
208 }
209
Popular Tags