1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 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 ; 35 import org.netbeans.jmi.javamodel.JavaModelPackage; 36 import org.netbeans.jmi.javamodel.Type; 37 import org.netbeans.jmi.javamodel.Variable; 38 39 40 44 public abstract class VariableAccessImpl extends ElementReferenceImpl implements VariableAccess { 45 private boolean hasSuper = false; 46 private PrimaryExpression parentClass; 47 48 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 getChildren() { 81 List list = new ArrayList (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 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 getSourceText() { 119 String origElem; 120 if ((origElem = checkChange()) != null) 121 return origElem; 122 StringBuffer buf = new StringBuffer (); 123 boolean hasSuper = getHasSuper (); 124 StatementImpl parentClass = (StatementImpl) getParentClass(); 125 if (parentClass != null) { 126 buf.append(parentClass.getSourceText()); 127 buf.append("."); } 129 if (hasSuper) { 130 buf.append("super."); } 132 buf.append(getName()); 133 return buf.toString(); 134 } 135 136 public void getDiff(List 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 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 if (childrenInited) { 160 deleteChild(parentClass); 161 } 162 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) { 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 |