1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.lang.reflect.Modifier ; 22 import org.netbeans.jmi.javamodel.*; 23 import org.netbeans.lib.java.parser.ASTree; 24 import org.netbeans.mdr.handlers.AttrListWrapper; 25 import org.netbeans.mdr.persistence.StorageException; 26 import org.netbeans.mdr.storagemodel.StorableObject; 27 import org.netbeans.modules.javacore.parser.*; 28 import org.openide.util.Utilities; 29 import org.openide.ErrorManager; 30 import java.util.*; 31 32 33 38 public abstract class FieldGroupImpl extends FeatureImpl implements FieldGroup { 39 public static final String FIELDS_ATTR = "fields"; private static final ElementInfo DEFAULT_INFO = new FieldGroupInfo(null, FieldGroupInfo.FIELDGROUP_TYPE, 0, null, null, null); 41 42 private TypeReference typeName; 43 private LightAttrList fields; 44 45 private boolean elementsInited = false; 46 47 48 protected FieldGroupImpl(StorableObject s) { 49 super(s); 50 } 51 52 public void setType(Type newValue) { 53 TypeRef tr = typeToTypeRef(newValue); 54 _setTypeName((TypeReference) typeRefToTypeReference(tr, 0), tr); 55 } 56 57 public Type getType() { 58 checkUpToDate(); 59 return resolveType(getTypeRef()); 60 } 61 62 public TypeReference getTypeName() { 63 checkUpToDate(); 64 if (!elementsInited) { 65 initASTElements(); 66 } 67 return typeName; 68 } 69 70 private void _setTypeName(TypeReference typeName, TypeRef typeRef) { 71 if (!disableChanges) { 72 objectChanged(CHANGED_TYPE); 73 changeChild(getTypeName(), typeName); 74 this.typeName = typeName; 75 } 76 setTypeRef(typeRef); 77 } 78 79 public void setTypeName(TypeReference typeName) { 80 _setTypeName(typeName, typeReferenceToTypeRef(typeName, 0)); 81 } 82 83 protected void resetChildren() { 84 super.resetChildren(); 85 if (childrenInited) { 86 resetASTElements(); 87 initChildren(); 88 } 89 } 90 91 public int getModifiers() { 92 ClassDefinition cd = getDeclaringClass(); 93 int mods = super.getModifiers(); 94 if (cd instanceof JavaClass && ((JavaClass)cd).isInterface()) { 95 return mods | Modifier.STATIC | Modifier.FINAL; 96 } else { 97 return mods; 98 } 99 } 100 101 protected void initASTElements() { 102 elementsInited = false; 103 if (!childrenInited) { 104 initChildren(); 105 } 106 ElementInfo info = getElementInfo(); 107 ASTree tree = info.getTypeAST(this); 108 typeName = (TypeReference) initOrCreate(typeName, tree); 109 elementsInited = true; 110 } 111 112 protected void matchPersistent(ElementInfo info) { 113 super.matchPersistent(info); 114 FieldGroupInfo fgInfo = (FieldGroupInfo)info; 115 116 if (!isPersisted()) { 117 setPersisted(true); 118 persist(); 119 setTypeRef(fgInfo.type); 120 persistChildren(getPersistentList("annotations", super_getAnnotations()), ((FeatureInfo) info).annotations); 121 persistChildren(getPersistentList(FIELDS_ATTR, super_getFields()), fgInfo.fields); 122 } else { 123 if (!Utilities.compareObjects(fgInfo.type, getTypeRef())) { 124 setTypeRef(fgInfo.type); 125 } 126 processMembers(getAnnotations(), fgInfo.annotations); 127 processMembers(getFields(), fgInfo.fields); 128 } 129 } 130 131 protected void matchElementInfo(ElementInfo newInfo) { 132 super.matchElementInfo(newInfo); 133 resetASTElements(); 134 } 135 136 public void replaceChild(Element oldElement, Element newElement) { 137 if (isPersisted()) { 138 if (replaceObject(getFields(), oldElement, newElement)) return; 139 } 140 if (elementsInited && oldElement.equals(typeName)) { 141 setTypeName((MultipartId) newElement); 142 return; 143 } 144 super.replaceChild(oldElement, newElement); 145 } 146 147 protected void resetASTElements() { 148 if (elementsInited) { 149 if (typeName != null) { 150 TypeReference temp = typeName; 151 typeName = null; 152 changeChild(temp, null); 153 temp.refDelete(); 154 } 155 elementsInited = false; 156 } 157 } 158 159 protected List getInitedChildren() { 160 List list = super.getInitedChildren(); 161 if (childrenInited) { 162 list.addAll(fields); 163 } 164 if (elementsInited) { 165 addIfNotNull(list, typeName); 166 } 167 return list; 168 } 169 170 public List getFields() { 171 checkUpToDate(); 172 if (fields == null) { 173 fields = createChildrenList(FIELDS_ATTR, (AttrListWrapper) super_getFields(), null, CHANGED_FEATURES); 174 } 175 return fields; 176 } 177 178 List getPersistentFields() { 179 AttrListWrapper result = (AttrListWrapper) super_getFields(); 180 result.setAttrName("fields"); return result; 182 } 183 184 void reinitFields() { 185 if (fields != null) { 186 fields.setInnerList(super_getFields()); 187 } 188 } 189 190 public List getChildren() { 191 List result = super.getChildren(); 192 addIfNotNull(result, getTypeName()); 193 result.addAll(getFields()); 194 return result; 195 } 196 197 201 String getRawText() { 202 StringBuffer buf = new StringBuffer (); 203 generateNewJavaDoc(buf); 204 generateNewModifiers(buf); 205 if (getTypeName() != null) { 206 buf.append(((MetadataElement) getTypeName()).getSourceText()); 207 } else { 208 buf.append(getType().getName()); 209 } 210 buf.append(' '); 212 for (Iterator fieldIt = getFields().iterator(); fieldIt.hasNext(); ) { 213 FieldImpl f = (FieldImpl) fieldIt.next(); 214 buf.append(f.getSourceText()); 215 if (fieldIt.hasNext()) 216 formatElementPart(COMMA, buf); 217 else 218 break; 219 } 220 buf.append(';'); return buf.toString(); 222 } 223 224 public void getDiff(List diffList) { 225 ASTProvider parser = getParser(); 226 ASTree tree = getASTree(); 227 ASTree[] children = tree.getSubTrees(); 228 229 replaceJavaDoc(diffList); 231 if (isChanged(CHANGED_MODIFIERS) || isChanged(CHANGED_ANNOTATION)) { 233 diffModifiers(diffList, children[TYPE], parser); 234 } else if (children[0] != null) { 235 FieldGroupInfo astInfo=(FieldGroupInfo)getElementInfo(); 236 237 getCollectionDiff(diffList, parser, CHANGED_ANNOTATION, astInfo.annotations, getAnnotations(), parser.getToken(children[0].getLastToken()).getEndOffset(), " "); } 239 getChildDiff(diffList, parser, children[TYPE], (MetadataElement) getTypeName(), CHANGED_TYPE); 241 243 getCollectionDiff(diffList, parser, CHANGED_FEATURES, ((FieldGroupInfo) getElementInfo()).fields, 244 getFields(), parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(COMMA)); 245 } 246 247 protected String getIndentation() { 248 return ((MetadataElement) refImmediateComposite()).getIndentation().concat(INDENTATION); 249 } 250 251 private static final int TYPE = 1; 253 254 protected ElementInfo getDefaultInfo() { 255 return DEFAULT_INFO; 256 } 257 258 private List getNakedFeatures() { 259 try { 260 return (List) ((StorableObject) _getDelegate()).getAttribute(FIELDS_ATTR); } catch (StorageException e) { 262 throw (GeneralException) ErrorManager.getDefault().annotate(new GeneralException(e.getMessage()), e); 263 } 264 } 265 266 protected abstract List super_getFields(); 267 268 public List getPersistentFeatures() { 269 AttrListWrapper list = (AttrListWrapper) super_getFields(); 270 list.setAttrName(FIELDS_ATTR); return list; 272 } 273 274 protected void initChildren() { 275 boolean fail = true; 277 _lock(true); 278 try { 279 childrenInited = false; 280 fields = createChildrenList(fields, FIELDS_ATTR, (AttrListWrapper) super_getFields(), ((FieldGroupInfo) getElementInfo()).fields, CHANGED_FEATURES); 281 childrenInited = true; 282 283 if (elementsInited) { 284 initASTElements(); 285 } 286 fail = false; 287 } finally { 288 _unlock(fail); 292 } 293 } 294 295 void setData(TypeReference typeName, List fields) { 296 this.fields = createChildrenList(FIELDS_ATTR, (AttrListWrapper) super_getFields(), fields, CHANGED_FEATURES); 297 changeChild(null, typeName); 298 this.typeName = typeName; 299 elementsInited = true; 300 childrenInited = true; 301 } 302 303 protected void _delete() { 304 deleteChildren(FIELDS_ATTR, (AttrListWrapper) super_getFields()); 305 if (elementsInited) { 306 deleteChild(typeName); 307 } 308 super._delete(); 309 } 310 311 public Element duplicate(JavaModelPackage targetExtent) { 312 return targetExtent.getFieldGroup().createFieldGroup( 313 getName(), 314 duplicateList(getAnnotations(), targetExtent), 315 getModifiers(), 316 null, 317 (JavaDoc) duplicateElement(getJavadoc(), targetExtent), 318 (TypeReference) duplicateElement(getTypeName(), targetExtent), 319 duplicateList(getFields(), targetExtent) 320 ); 321 322 } 323 } 324 | Popular Tags |