1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.jmi.model.Association; 27 import javax.jmi.reflect.RefAssociation; 28 import javax.jmi.reflect.RefObject; 29 import org.netbeans.api.mdr.events.AssociationEvent; 30 import org.netbeans.api.mdr.events.AttributeEvent; 31 import org.netbeans.jmi.javamodel.AnnotationType; 32 import org.netbeans.jmi.javamodel.Extends; 33 import org.netbeans.jmi.javamodel.IsOfAnnotationType; 34 import org.netbeans.jmi.javamodel.Type; 35 import org.netbeans.mdr.handlers.AttrListWrapper; 36 import org.netbeans.modules.javacore.internalapi.JavaModelUtil; 37 import org.netbeans.modules.javacore.parser.ASTProvider; 38 import org.netbeans.modules.javacore.parser.AnnotationInfo; 39 import org.netbeans.modules.javacore.parser.ElementInfo; 40 import org.netbeans.jmi.javamodel.Annotation; 41 import org.netbeans.jmi.javamodel.Element; 42 import org.netbeans.jmi.javamodel.JavaModelPackage; 43 import org.netbeans.jmi.javamodel.MultipartId; 44 import org.netbeans.jmi.javamodel.TypeReference; 45 import org.netbeans.lib.java.parser.ASTree; 46 import org.netbeans.lib.java.parser.ASTreeTypes; 47 import org.netbeans.mdr.storagemodel.StorableObject; 48 import org.netbeans.modules.javacore.parser.TypeRef; 49 import org.openide.util.Utilities; 50 51 52 58 public abstract class AnnotationImpl extends SemiPersistentElement implements Annotation { 59 private static final ElementInfo DEFAULT_INFO = new AnnotationInfo(null, AnnotationInfo.ANNOTATION_TYPE, null, null); 60 61 private LightAttrList attributeValues; 62 private MultipartId typeName = null; 63 private String typeRef = null; 64 protected boolean elementsInited = false; 65 66 67 public AnnotationImpl(StorableObject s) { 68 super(s); 69 } 70 71 protected void matchPersistent(ElementInfo newInfo) { 72 super.matchPersistent(newInfo); 73 74 AnnotationInfo info = (AnnotationInfo) newInfo; 75 76 if (!isPersisted()) { 77 setPersisted(true); 78 persist(); 79 setTypeRef(info.type); 80 persistChildren(getPersistentList("attributeValues", super_getAttributeValues()), info.values); 81 } else { 82 if (!Utilities.compareObjects(info.type, getTypeRef())) { 83 Type type = resolveType(info.type); 84 Type old = resolveType(getTypeRef()); 85 _setType(type); 86 fireAssocChange("type",old ,type); } 88 processMembers(getAttributeValues(), info.values); 89 } 90 } 91 92 protected abstract List super_getAttributeValues(); 93 94 protected void matchElementInfo(ElementInfo newInfo) { 95 super.matchElementInfo(newInfo); 96 resetASTElements(); 97 } 98 99 protected ElementInfo getDefaultInfo() { 100 return DEFAULT_INFO; 101 } 102 103 protected void resetASTElements() { 104 deleteChild(typeName); 105 typeName = null; 106 elementsInited = false; 107 } 108 109 protected void resetChildren() { 110 super.resetChildren(); 111 if (attributeValues != null) attributeValues.setInnerList(getPersistentList("attributeValues", super_getAttributeValues())); 112 if (childrenInited) { 113 resetASTElements(); 114 initChildren(); 115 } 116 } 117 118 122 public AnnotationType getType() { 123 Type type; 124 125 checkUpToDate(); 126 type = resolveType(getTypeRef()); 127 if (type instanceof AnnotationType) 128 return (AnnotationType)type; 129 return null; 130 } 131 132 137 public void setType(AnnotationType newValue) { 138 _setType(newValue); 139 } 140 141 private void _setType(Type newValue) { 142 TypeRef tr = typeToTypeRef(newValue); 143 MultipartId typeReference = null; 144 if (!disableChanges) { 145 typeReference = (MultipartId) typeRefToTypeReference(tr, 0); 146 } 147 fireTypeNameChange(typeReference); 148 _setTypeName(typeReference, tr); 149 } 150 151 protected final void fireAssocChange(String endName, Object oldValue, Object newValue) { 152 fireAssocChange(endName, oldValue, newValue, AssociationEvent.POSITION_NONE); 153 } 154 155 protected final void fireAssocChange(String endName, Object oldValue, Object newValue, int position) { 156 IsOfAnnotationType source = ((JavaModelPackage) refImmediatePackage()).getIsOfAnnotationType(); 157 if (_getMdrStorage().eventsEnabled()) { 158 AssociationEvent event = new AssociationEvent( 159 source, 160 AssociationEvent.EVENT_ASSOCIATION_SET, 161 this, 162 endName, 163 (RefObject) oldValue, 164 (RefObject) newValue, 165 position 166 ); 167 _getMdrStorage().getEventNotifier().ASSOCIATION.firePlannedChange(source, event); 168 } 169 } 170 171 172 public MultipartId getTypeName() { 173 checkUpToDate(); 174 if (!elementsInited) { 175 initASTElements(); 176 } 177 return typeName; 178 } 179 180 public void setTypeName(MultipartId typeName) { 181 _setTypeName(typeName, typeReferenceToTypeRef(typeName, 0)); 182 } 183 184 private void _setTypeName(MultipartId typeName, TypeRef typeRef) { 185 if (!disableChanges) { 186 objectChanged(CHANGED_TYPE); 187 changeChild(getTypeName(), typeName); 188 this.typeName = typeName; 189 } 190 setTypeRef(typeRef); 191 } 192 193 private void fireTypeNameChange(TypeReference typeReference) { 194 Object oldValue = null; 195 Object newValue = null; 196 if (elementsInited && !disableChanges) { 197 oldValue = getTypeName(); 198 newValue = typeReference; 199 } 200 fireAttrChange("typeName", oldValue, newValue); } 202 203 public void replaceChild(Element oldElement, Element newElement) { 204 if (childrenInited) { 205 if (elementsInited) { 206 if (oldElement.equals(typeName)) { 207 setTypeName((MultipartId) newElement); 208 return; 209 } 210 } 211 if (replaceObject(getAttributeValues(), oldElement, newElement)) return; 212 } 213 super.replaceChild(oldElement, newElement); 214 } 215 216 public List getChildren() { 217 List list = new ArrayList (getAttributeValues().size() + 1); 218 addIfNotNull(list, getTypeName()); 219 list.addAll(getAttributeValues()); 220 return list; 221 } 222 223 public void fixImports(Element scope, Element original) { 224 Annotation ann=(Annotation)original; 225 AnnotationType type=ann.getType(); 226 if (type!=null) { 227 setTypeName(JavaModelUtil.resolveImportsForClass(scope,type)); 228 } 229 fixImports(scope,getAttributeValues(),ann.getAttributeValues()); 230 } 231 232 public List getInitedChildren() { 233 if (childrenInited) { 234 List list = new ArrayList (10); 235 if (elementsInited) { 236 addIfNotNull(list, typeName); 237 } 238 list.addAll(getAttributeValues()); 239 return list; 240 } 241 return Collections.EMPTY_LIST; 242 } 243 244 public List getAttributeValues() { 245 checkUpToDate(); 246 if (attributeValues == null) { 247 attributeValues = createChildrenList("attributeValues", (AttrListWrapper) super_getAttributeValues(), null, CHANGED_ANNOTATION); 248 } 249 return attributeValues; 250 } 251 252 String getRawText() { 253 StringBuffer buf = new StringBuffer (); 254 buf.append('@').append(((MultipartIdImpl) getTypeName()).getSourceText()); 255 List attributes = getAttributeValues(); 256 if (!attributes.isEmpty()) { 257 buf.append('('); 258 Iterator aIt = attributes.iterator(); 259 buf.append(((AttributeValueImpl) aIt.next()).getSourceText()); 260 while (aIt.hasNext()) { 261 formatElementPart(COMMA, buf); 262 buf.append(((AttributeValueImpl) aIt.next()).getSourceText()); 263 } 264 buf.append(')'); 265 }; 266 return buf.toString(); 267 } 268 269 public void getDiff(List diffList) { 270 ASTProvider parser = getParser(); 271 ASTree[] children = getASTree().getSubTrees(); 272 AnnotationInfo astInfo = (AnnotationInfo) getElementInfo(); 273 getChildDiff(diffList, parser, children[0], (MetadataElement) getTypeName(), CHANGED_TYPE); 274 if (astInfo.values.length == 0) { 275 if (isChanged(CHANGED_ANNOTATION)) { 276 StringBuffer buf = new StringBuffer (); 277 Collection values = getAttributeValues(); 278 if (!values.isEmpty()) { 279 buf.append('('); 280 Iterator it = values.iterator(); 281 while (it.hasNext()) { 282 buf.append(((AttributeValueImpl) it.next()).getSourceText()); 283 if (it.hasNext()) { 284 formatElementPart(COMMA, buf); 285 } 286 } 287 buf.append(')'); 288 } 289 int endOffset = getEndOffset(parser, children[0]); 290 diffList.add(new DiffElement(endOffset, endOffset, buf.toString())); 291 } 292 } else if (getAttributeValues().isEmpty()) { 293 if (isChanged(CHANGED_ANNOTATION)) { 294 replaceNode(diffList, parser, children[1], "", 0, null); 295 } 296 } else { 297 getCollectionDiff(diffList, parser, CHANGED_ANNOTATION, astInfo.values, getAttributeValues(), parser.getToken(children[1].getLastToken()).getStartOffset(), ", "); } 299 } 300 protected void initChildren() { 302 childrenInited = false; 303 AnnotationInfo info = (AnnotationInfo) getElementInfo(); 304 attributeValues = createChildrenList(attributeValues, "attributeValues", (AttrListWrapper) super_getAttributeValues(), info.values, CHANGED_ANNOTATION); 305 childrenInited = true; 306 307 if (elementsInited) { 308 initASTElements(); 309 } 310 } 311 312 protected void initASTElements() { 313 elementsInited = false; 314 if (!childrenInited) { 315 initChildren(); 316 } 317 AnnotationInfo info = (AnnotationInfo) getElementInfo(); 318 ASTree tree = info.getTypeAST(this); 319 typeName = (MultipartId) initOrCreate(typeName, tree); 320 elementsInited = true; 321 } 322 323 protected void setData(MultipartId typeName, List attributeValues) { 324 changeChild(null, typeName); 325 this.typeName = typeName; 326 setTypeRef(typeReferenceToTypeRef(typeName, 0)); 327 this.attributeValues = createChildrenList("attributeValues", (AttrListWrapper) super_getAttributeValues(), attributeValues, CHANGED_ANNOTATION); childrenInited = true; 329 elementsInited = true; 330 } 331 332 protected void setTypeRef(TypeRef type) { 333 _getDelegate().setSlot1(type); 334 } 335 336 public TypeRef getTypeRef() { 337 return (TypeRef) _getDelegate().getSlot1(); 338 } 339 340 public boolean isPersisted() { 341 return _getDelegate().getSlot2() != null; 342 } 343 344 public void setPersisted(boolean persisted) { 345 _getDelegate().setSlot2(persisted ? "" : null); 346 } 347 348 void childChanged(MetadataElement mpi) { 349 super.childChanged(mpi); 350 if (elementsInited) { 351 if (mpi == typeName) { 352 setTypeName((MultipartId) mpi); 353 } 354 } 355 } 356 357 protected void _delete() { 358 if (elementsInited) { 359 deleteChild(typeName); 360 } 361 deleteChildren("attributeValues", (AttrListWrapper) super_getAttributeValues()); 362 super._delete(); 363 } 364 365 public Element duplicate(JavaModelPackage targetExtent) { 366 return targetExtent.getAnnotation().createAnnotation( 367 (MultipartId) ((MetadataElement) getTypeName()).duplicate(targetExtent), 368 duplicateList(getAttributeValues(), targetExtent) 369 ); 370 } 371 372 public String getName() { 373 throw new UnsupportedOperationException (); 374 } 375 376 protected void matchName(ElementInfo info) { 377 } 378 } 379 | Popular Tags |