1 19 20 package org.netbeans.lib.jmi.xmi; 21 22 import java.util.*; 23 24 import org.netbeans.lib.jmi.util.*; 25 26 import javax.jmi.reflect.*; 27 import javax.jmi.model.*; 28 29 public class ElementsCache extends Object { 30 31 33 private HashMap structureFields_cache = new HashMap (); 35 36 private HashMap instanceAttributes_cache = new HashMap (); 38 private HashMap classAttributes_cache = new HashMap (); 39 private HashMap references_cache = new HashMap (); 40 private HashMap subtypes_cache = null; 41 42 private RefPackage extent; 43 44 private HashMap outermostPackages = null; 46 private HashMap trackedPackages = null; 48 private HashMap proxies_cache = new HashMap (); 50 51 53 public ElementsCache (RefPackage extent) { 54 this.extent = extent; 55 } 56 57 59 63 private void findOutermostPackages (RefPackage pkg) { 64 if (trackedPackages.get (pkg) != null) 65 return; 66 String name; 67 MofPackage metaObj = (MofPackage) pkg.refMetaObject (); 68 if (metaObj.getContainer() == null) { 69 Iterator iter = metaObj.getQualifiedName ().iterator (); 70 String fqName = (String ) iter.next (); 71 while (iter.hasNext ()) 72 fqName = fqName.concat (".").concat ((String ) iter.next ()); 73 outermostPackages.put (fqName, pkg); 74 } 75 trackedPackages.put (pkg, pkg); 76 Iterator iter = pkg.refAllPackages ().iterator (); 77 while (iter.hasNext ()) { 78 findOutermostPackages ((RefPackage) iter.next ()); 79 } 80 } 81 82 85 private void cacheContainedElements (MofClass mofClass) { 86 List temp = new LinkedList (); 87 List superClasses = mofClass.allSupertypes (); 88 Namespace namespace = null; 89 Iterator it = superClasses.iterator (); 90 while (it.hasNext ()) { 91 namespace = (Namespace) it.next (); 92 temp.addAll (namespace.getContents ()); 93 } 94 temp.addAll (mofClass.getContents ()); 95 List instanceAttributes = new LinkedList (); 96 List classAttributes = new LinkedList (); 97 List references = new LinkedList (); 98 99 it = temp.iterator (); 100 while (it.hasNext ()) { 101 RefObject refObject = (RefObject) it.next (); 102 if (refObject instanceof Feature) { 103 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL); 104 if ((refObject instanceof Attribute) && (!((Attribute) refObject).isDerived ())) { 105 if (instanceLevel) { 106 instanceAttributes.add (refObject); 107 } else { 108 classAttributes.add (refObject); 109 } 110 } else if (refObject instanceof Reference) { 111 Association assoc = (Association) ((Reference) refObject). 112 getReferencedEnd ().getContainer (); 113 if (!assoc.isDerived ()) 114 references.add (refObject); 115 } } } instanceAttributes_cache.put (mofClass, instanceAttributes); 119 classAttributes_cache.put (mofClass, classAttributes); 120 references_cache.put (mofClass, references); 121 } 122 123 130 public List instanceAttributes (MofClass mofClass) { 131 List list = (List) instanceAttributes_cache.get (mofClass); 132 if (list == null) { 133 cacheContainedElements (mofClass); 134 list = (List) instanceAttributes_cache.get (mofClass); 135 } 136 return list; 137 } 138 139 142 public List localInstanceAttributes (MofClass mofClass) { 143 Iterator it = mofClass.getContents ().iterator (); 144 List result = new LinkedList (); 145 while (it.hasNext ()) { 146 Object refObject = it.next (); 147 if (refObject instanceof Feature) { 148 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL); 149 if (instanceLevel && (refObject instanceof Attribute) && (!((Attribute) refObject).isDerived ())) { 150 result.add (refObject); 151 } } } return result; 155 } 156 157 163 public List classAttributes (MofClass mofClass) { 164 List list = (List) classAttributes_cache.get (mofClass); 165 if (list == null) { 166 cacheContainedElements (mofClass); 167 list = (List) classAttributes_cache.get (mofClass); 168 } 169 return list; 170 } 171 172 178 public List references (MofClass mofClass) { 179 List list = (List) references_cache.get (mofClass); 180 if (list == null) { 181 cacheContainedElements (mofClass); 182 list = (List) references_cache.get (mofClass); 183 } 184 return list; 185 } 186 187 190 public List localReferences (MofClass mofClass) { 191 Iterator it = mofClass.getContents ().iterator (); 192 List result = new LinkedList (); 193 while (it.hasNext ()) { 194 Object refObject = it.next (); 195 if (refObject instanceof Feature) { 196 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL); 197 if (instanceLevel && (refObject instanceof Reference)) { 198 Association assoc = (Association) ((Reference) refObject). 199 getReferencedEnd ().getContainer (); 200 if (!assoc.isDerived ()) 201 result.add (refObject); 202 } } } return result; 206 } 207 208 211 public List structureFields (StructureType type) { 212 List fields = (List) structureFields_cache.get (type); 213 if (fields != null) 214 return fields; 215 fields = new LinkedList (); 217 Iterator content = type.getContents ().iterator (); 218 while (content.hasNext ()) { 219 Object element = content.next (); 220 if (element instanceof StructureField) 221 fields.add (element); 222 } structureFields_cache.put (type, fields); 224 return fields; 225 } 226 227 233 public RefBaseObject findProxy (ModelElement element) { 234 if (outermostPackages == null) { 235 trackedPackages = new HashMap (); 236 outermostPackages = new HashMap (); 237 findOutermostPackages (extent); 238 } 239 RefBaseObject proxy = (RefBaseObject) proxies_cache.get (element); 240 if (proxy != null) 241 return proxy; 242 LinkedList path = new LinkedList (); 243 ModelElement container = element.getContainer (); 244 while (container != null) { 245 path.add (container); 246 container = container.getContainer (); 247 } 248 MofPackage mofPackage = (MofPackage) path.removeLast (); 249 RefPackage refPackage = (RefPackage) outermostPackages.get (mofPackage.getName ()); 250 if (refPackage == null) { 251 Iterator iter = outermostPackages.entrySet ().iterator (); 252 while (iter.hasNext ()) { 253 RefPackage ref = (RefPackage) ((Map.Entry) iter.next ()).getValue (); 254 MofPackage meta = (MofPackage) ref.refMetaObject (); 255 if (meta.allSupertypes().contains(mofPackage)) { 256 refPackage = ref; 257 break; 258 } 259 } } 262 if (refPackage == null) 263 return null; 264 if (path.size () == 0) 265 proxy = refPackage; 266 while (path.size () > 0) { 267 ModelElement elem = (ModelElement) path.removeLast (); 268 if (elem instanceof MofPackage) { 269 refPackage = refPackage.refPackage (elem); 270 if (path.size () == 0) 271 proxy = refPackage; 272 } else { 273 if ((elem instanceof MofClass) && (path.size () == 0)) { 274 RefClass refClass = refPackage.refClass (elem); 275 proxy = refClass; 276 } else 277 break; 278 } } if (proxy != null) 281 proxies_cache.put (element, proxy); 282 return proxy; 283 } 284 285 288 public Set getAllSubtypes (MofClass clazz) { 289 if (subtypes_cache == null) { 290 subtypes_cache = new HashMap (); 291 cacheSubtypes (extent); 292 } 293 return (Set) subtypes_cache.get (clazz); 294 } 295 296 private void cacheSubtypes (RefPackage pkg) { 297 Iterator iter = pkg.refAllClasses ().iterator (); 298 while (iter.hasNext ()) { 299 MofClass mofClass = (MofClass) ((RefClass) iter.next ()).refMetaObject (); 300 Iterator supertypes = mofClass.allSupertypes ().iterator (); 301 while (supertypes.hasNext ()) { 302 MofClass superClass = (MofClass) supertypes.next (); 303 Set list = (Set) subtypes_cache.get (superClass); 304 if (list == null) { 305 list = new HashSet (); 306 subtypes_cache.put (superClass, list); 307 } list.add (mofClass); 309 } Set list = (Set) subtypes_cache.get (mofClass); 312 if (list == null) { 313 list = new HashSet (); 314 subtypes_cache.put (mofClass, list); 315 } list.add (mofClass); 317 } iter = pkg.refAllPackages ().iterator (); 319 while (iter.hasNext ()) 320 cacheSubtypes ((RefPackage) iter.next ()); 321 } 322 323 } 324 325 | Popular Tags |