1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import org.netbeans.lib.java.parser.ASTree; 22 import org.netbeans.lib.java.parser.ASTreeTypes; 23 import org.netbeans.lib.java.parser.ParserTokens; 24 import org.netbeans.mdr.storagemodel.StorableObject; 25 import org.netbeans.modules.javacore.parser.ASTProvider; 26 import org.netbeans.modules.javacore.parser.AnnotationInfo; 27 import org.netbeans.modules.javacore.parser.MDRParser; 28 import java.util.*; 29 import org.netbeans.jmi.javamodel.*; 30 import org.netbeans.mdr.handlers.AttrListWrapper; 31 import org.netbeans.mdr.handlers.BaseObjectHandler; 32 import org.netbeans.mdr.persistence.StorageException; 33 import org.netbeans.mdr.storagemodel.StorableFeatured; 34 import org.openide.ErrorManager; 35 36 40 public abstract class LocalVarDeclarationImpl extends StatementImpl implements LocalVarDeclaration { 41 private LightAttrList variables; 42 private boolean isFinal; 43 private TypeReference typeName; 44 private LightAttrList annotations = null; 45 46 47 public LocalVarDeclarationImpl(StorableObject o) { 48 super(o); 49 } 50 51 public boolean isFinal() { 52 if (isChanged(CHANGED_IS_FINAL)) { 53 return isFinal; 54 } else { 55 Object [] modInfo=(Object [])getModInfo(); 56 return ((Boolean )modInfo[0]).booleanValue(); 57 } 58 } 59 60 public void setFinal(boolean isFinal) { 61 objectChanged(CHANGED_IS_FINAL); 62 this.isFinal = isFinal; 63 } 64 65 public TypeReference getTypeName() { 66 if (!childrenInited) { 67 initChildren(); 68 } 69 return typeName; 70 } 71 72 public void setTypeName(TypeReference typeName) { 73 objectChanged(CHANGED_TYPE); 74 changeChild(getTypeName(), typeName); 75 this.typeName = typeName; 76 } 77 78 public Type getType() { 79 return (Type) getTypeName().getElement(); 80 } 81 82 public void setType(Type newValue) { 83 MultipartIdClass proxy = ((JavaModelPackage) refImmediatePackage()).getMultipartId(); 84 setTypeName(proxy.createMultipartId(newValue.getName(), null, null)); 85 } 86 87 public List getVariables() { 88 if (!childrenInited) { 89 initChildren(); 90 } 91 return variables; 92 } 93 94 public List getAnnotations() { 95 if (!childrenInited) { 96 initChildren(); 97 } 98 return annotations; 99 } 100 101 public List getChildren() { 102 List result = new ArrayList(); 103 result.addAll(getAnnotations()); 104 addIfNotNull(result, getTypeName()); 105 result.addAll(getVariables()); 106 return result; 107 } 108 109 protected void initChildren() { 110 childrenInited = false; 111 ASTree tree = getASTree(); 112 if (tree != null) { 113 AnnotationInfo[] annotInfos = getAnnotInfos(); 114 typeName = (TypeReference) initOrCreate(typeName, tree.getSubTrees()[1]); 115 variables = createChildrenList(variables, "variables", tree.getSubTrees()[2], ASTreeTypes.VARIABLE_DECLARATORS, CHANGED_VARIABLES, false); createChildrenList(annotInfos); 117 } 118 childrenInited = true; 119 } 120 121 public String getRawText() { 122 boolean useSemicolon = !(refImmediateComposite() instanceof ForStatement); 123 if (!isNew()) { 124 if (!isChanged()) { 125 MDRParser parser = getParser(); 126 ASTree tree = getASTree(); 127 String origSrc = parser.getSourceText(); 128 int startIndex, endIndex; 129 String semicolon; 130 String indentation; 131 boolean isSemicolon = parser.getToken(tree.getLastToken()).getType() == ParserTokens.SEMICOLON; 132 if (useSemicolon == isSemicolon) { 133 semicolon = indentation = ""; 134 startIndex = IndentUtil.getElementStart(this); 135 endIndex = IndentUtil.getElementEnd(this); 136 } else { 137 startIndex = getStartOffset(parser, tree, false); 138 if (useSemicolon) { 139 semicolon = ";"; indentation = getIndentation(); 141 endIndex = IndentUtil.getElementEnd(this); 142 } else { 143 indentation = semicolon = ""; 144 endIndex = getEndOffset(parser, parser.getToken(tree.getLastToken() - 1)); 145 } 146 } 147 return indentation + origSrc.substring(startIndex, endIndex) + semicolon; 148 } else { 149 if (!childrenInited) { 150 initChildren(); 151 } 152 } 153 } 154 StringBuffer buf = new StringBuffer (37); 155 for (Iterator annIt = getAnnotations().iterator(); annIt.hasNext(); ) { 156 AnnotationImpl ann = (AnnotationImpl) annIt.next(); 157 buf.append(ann.getSourceText()).append('\n').append(getIndentation()); 158 } 159 if (isFinal()) { 160 buf.append("final "); } 162 TypeReference typeName = getTypeName(); 163 if (typeName != null) { 164 buf.append(((MetadataElement) typeName).getSourceText()); 165 } else { 166 buf.append(getType().getName()); 167 } 168 if (((TransientElement) getVariables().get(0)).isNew()) { 169 buf.append(' '); 170 } 171 Iterator iter = getVariables().iterator(); 172 while (iter.hasNext()) { 173 TransientElement var = (TransientElement) iter.next(); 174 buf.append(var.getSourceText()); 175 if (iter.hasNext()) { 176 formatElementPart(MetadataElement.COMMA, buf); 177 } 178 } 179 if (useSemicolon) { 180 buf.append(";"); 181 } 182 return buf.toString(); 183 } 184 185 public void getDiff(List diff) { 186 ASTree tree = getASTree(); 187 MDRParser parser = (MDRParser) tree.getASTContext(); 188 ASTree[] children = tree.getSubTrees(); 189 AnnotationInfo infos[] = getAnnotInfos(); 190 191 if (isChanged(CHANGED_IS_FINAL) || isChanged(CHANGED_ANNOTATION)) { 192 diffModifiers(diff, children[1], parser); 193 } else if (children[0] != null) { 194 getCollectionDiff(diff, parser, CHANGED_ANNOTATION, infos, getAnnotations(), parser.getToken(children[0].getLastToken()).getEndOffset(), " "); } 196 getChildDiff(diff, parser, children[1], (MetadataElement) getTypeName(), CHANGED_TYPE); 197 getCollectionDiff(diff, parser, CHANGED_VARIABLES, children[2], 198 ASTreeTypes.VARIABLE_DECLARATORS, getVariables(), 199 parser.getToken(children[2].getLastToken()).getEndOffset(), formatElementPart(MetadataElement.COMMA)); 200 } 201 202 void setData(List variables, boolean isFinal, TypeReference typeName) { 203 this.variables = createChildrenList("variables", variables, CHANGED_VARIABLES); createChildrenList(null); 205 this.isFinal = isFinal; 206 changeChild(null, typeName); 207 this.typeName = typeName; 208 } 209 210 protected void _delete() { 211 if (childrenInited) { 213 deleteChildren(variables); 214 deleteChild(typeName); 215 } 216 super._delete(); 220 } 221 222 public void replaceChild(Element oldElement,Element newElement) { 223 if (childrenInited) { 224 if (replaceObject(annotations, oldElement, newElement)) return; 225 if (replaceObject(variables, oldElement, newElement)) return; 226 if (oldElement.equals(typeName)) { 227 setTypeName((TypeReference)newElement); 228 } 229 } 230 } 231 232 public Element duplicate(JavaModelPackage targetExtent) { 233 LocalVarDeclarationImpl var = (LocalVarDeclarationImpl) targetExtent.getLocalVarDeclaration().createLocalVarDeclaration( 234 isFinal(), 235 (TypeReference) duplicateElement(getTypeName(), targetExtent), 236 duplicateList(variables, targetExtent) 237 ); 238 var.getAnnotations().addAll(duplicateList(getAnnotations(), targetExtent)); 239 return var; 240 } 241 242 244 protected void diffModifiers(List diffList, ASTree nextNode, ASTProvider parser) { 245 String text = ""; 246 for (Iterator annIt = getAnnotations().iterator(); annIt.hasNext(); ) { 247 AnnotationImpl ann = (AnnotationImpl) annIt.next(); 248 text += ann.getSourceText(); 249 text += '\n'; 250 text += getIndentation(); 251 } 252 if (isFinal()) { 253 text += "final"; } 255 int nextToken = nextNode.getFirstToken(); 256 int startOffset, endOffset; 257 int startToken; 258 endOffset = parser.getToken(nextToken).getStartOffset(); 259 ASTree modifiers = getASTree().getSubTrees()[0]; 260 if (modifiers != null) { 261 startToken = modifiers.getFirstToken(); 262 startOffset = parser.getToken(startToken).getStartOffset(); 263 if (text.length() > 0) { 264 int endToken = modifiers.getLastToken(); 265 endOffset = parser.getToken(endToken).getEndOffset(); 266 } 267 } else { 268 startOffset = endOffset; 269 if (isFinal()) { 270 text += ' '; 271 } 272 } 273 diffList.add(new DiffElement(startOffset, endOffset, text)); 274 } 275 276 private void createChildrenList(AnnotationInfo[] infos) { 277 DeferredAttrList deferredList; 278 try { 279 if (annotations != null && infos != null) { 280 deferredList = (DeferredAttrList) ((AttrListWrapper) annotations.getInnerList()).getInnerList(); 281 int i = 0; 282 for (ListIterator it = deferredList.listIterator(); it.hasNext(); i++) { 283 AnnotationImpl im = (AnnotationImpl) it.next(); 284 if (isNew()) { 285 im.setNew(); 286 } else { 287 if (i >= infos.length) { 288 it.remove(); 289 } else { 290 im.setElementInfo(infos[i]); 291 } 292 } 293 } 294 for (; i < infos.length; i++) { 295 deferredList.add(createElement(infos[i])); 296 } 297 } else { 298 JavaModelPackage pkg = (JavaModelPackage) refImmediatePackage(); 299 StorableFeatured storable = (StorableFeatured) ((BaseObjectHandler) pkg.getLocalVariable())._getDelegate(); 300 deferredList = new DeferredAttrList((StorableFeatured)_getDelegate(), storable.getClassProxy().getAttrDesc("annotations"), new ArrayList()); annotations = createWrapper("annotations", deferredList, CHANGED_ANNOTATION); if (deferredList != null && infos != null) { 303 for (int i = 0; i < infos.length; i++) { 304 MetadataElement s = createElement(infos[i]); 305 if (s != null) { 306 deferredList.add(s); 307 } 308 } 309 } 310 } 311 } catch (StorageException e) { 312 throw (GeneralException) ErrorManager.getDefault().annotate(new GeneralException(e.getMessage()), e); 313 } 314 } 315 316 private AnnotationInfo[] getAnnotInfos() { 317 Object [] modInfo=(Object [])getModInfo(); 318 AnnotationInfo[] ann=(AnnotationInfo[])modInfo[1]; 319 return ann == null ? AnnotationInfo.EMPTY_ANNOTATIONS : ann; 320 } 321 322 private static final Object [] NO_MODIFIERS=new Object []{Boolean.FALSE,null}; 323 324 private Object getModInfo() { 325 ASTree tree = getASTree(); 326 MDRParser parser = (MDRParser) tree.getASTContext(); 327 ASTree modifiers = tree.getSubTrees()[0]; 328 return modifiers == null ? NO_MODIFIERS : parser.getSemanticInfo(modifiers, null); 329 } 330 } 331 | Popular Tags |