1 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 34 public abstract class LocalVariableImpl extends TransientElement implements LocalVariable { 35 private String name; 36 private boolean isFinal; 37 private InitialValue initialValue; 38 private String initialValueText; 39 protected Type type; 40 private int dimCount; 41 42 43 public LocalVariableImpl(StorableObject o) { 44 super(o); 45 } 46 47 public String 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 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 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."); } 134 135 public String getInitialValueText() { 136 if (isChanged(CHANGED_INITIAL_VALUE)) { 137 return initialValueText; 138 } else { 139 return extractInitialValueText(); 140 } 141 } 142 143 public void setInitialValueText(String 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 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 179 protected ASTree getPartTree(ElementPartKind part) { 180 if (ElementPartKindEnum.NAME.equals(part)) { 182 return getASTree().getSubTrees()[0]; 183 } 184 throw new IllegalArgumentException ("Invalid part for this element: " + part); } 186 187 public String getSourceText() { 188 StringBuffer buf = new StringBuffer (); 189 String name = getName(); 190 boolean nju = isNew(); 191 if (!nju) IndentUtil.reformatHeadGarbage(this, buf); 192 buf.append(name); 193 appendDims(buf, getDimCount()); 194 String 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 (), getDimCount()).toString(), getEndOffset(getParser(), children[0]), ""); 218 } 219 if (isChanged(CHANGED_TYPE)) { 220 Type type = getType(); 221 Type parentType = ((LocalVarDeclarationImpl) refImmediateComposite()).getType(); 222 StringBuffer dims = new StringBuffer (); 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 name, List annotations, boolean isFinal, int dimCount, InitialValue initialValue, String 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 if (childrenInited) { 257 deleteChild(initialValue); 258 } 259 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 275 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 throw new ConstraintViolationException(null, null, "Call setType on LocalVarDeclaration to change type of this variable."); } 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 |