1 19 20 package org.netbeans.mdr.test; 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 = new HashMap (); 41 private HashMap associationEnds_cache = new HashMap (); 42 private HashMap classInstances_cache = new HashMap (); 43 private HashMap associationEndInstances_cache = new HashMap (); 44 private RefPackage extent; 45 46 private HashMap outermostPackages = new HashMap (); 48 private HashMap trackedPackages; 50 private HashMap proxies_cache = new HashMap (); 52 53 55 public ElementsCache (RefPackage extent) { 56 this.extent = extent; 57 trackedPackages = new HashMap (); findOutermostPackages (extent); 59 } 60 61 63 67 private void findOutermostPackages (RefPackage pkg) { 68 if (trackedPackages.get (pkg) != null) 69 return; 70 String name; 71 MofPackage metaObj = (MofPackage) pkg.refMetaObject (); 72 if (metaObj.getContainer() == null) { 73 Iterator iter = metaObj.getQualifiedName ().iterator (); 74 String fqName = (String ) iter.next (); 75 while (iter.hasNext ()) 76 fqName = fqName.concat (".").concat ((String ) iter.next ()); 77 outermostPackages.put (fqName, pkg); 78 } 79 trackedPackages.put (pkg, pkg); 80 Iterator iter = pkg.refAllPackages ().iterator (); 81 while (iter.hasNext ()) { 82 findOutermostPackages ((RefPackage) iter.next ()); 83 } 84 } 85 86 89 private void cacheContainedElements (Classifier classifier) { 90 List temp = new LinkedList (); 91 List superClasses = classifier.allSupertypes (); 92 Namespace namespace = null; 93 Iterator it = superClasses.iterator (); 94 while (it.hasNext ()) { 95 namespace = (Namespace) it.next (); 96 temp.addAll (namespace.getContents ()); 97 } 98 temp.addAll (classifier.getContents ()); 99 List instanceAttributes = new LinkedList (); 100 List classAttributes = new LinkedList (); 101 List references = new LinkedList (); 102 List assocEnds = new LinkedList (); 103 List assocEndInstances = new LinkedList(); 104 105 it = temp.iterator (); 106 while (it.hasNext ()) { 107 RefObject refObject = (RefObject) it.next (); 108 if (refObject instanceof Feature) { 109 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL); 110 if ((refObject instanceof Attribute) && (!((Attribute) refObject).isDerived ())) { 111 if (instanceLevel) { 112 instanceAttributes.add (refObject); 113 } else { 114 classAttributes.add (refObject); 115 } 116 } else if (refObject instanceof Reference) { 117 Association assoc = (Association) ((Reference) refObject). 118 getReferencedEnd ().getContainer (); 119 if (!assoc.isDerived ()) 120 references.add (refObject); 121 } } else if (refObject instanceof AssociationEnd) { 124 assocEnds.add(refObject); 125 Classifier type = ((AssociationEnd)refObject).getType (); 126 while (type instanceof AliasType) 127 type = ((AliasType) type).getType (); 128 assocEndInstances.add(classInstances((MofClass)type)); 129 } 130 } instanceAttributes_cache.put (classifier, instanceAttributes); 132 classAttributes_cache.put (classifier, classAttributes); 133 references_cache.put (classifier, references); 134 associationEnds_cache.put (classifier, assocEnds); 135 associationEndInstances_cache.put (classifier, assocEndInstances); 136 } 137 138 145 public List instanceAttributes (MofClass mofClass) { 146 List list = (List) instanceAttributes_cache.get (mofClass); 147 if (list == null) { 148 cacheContainedElements (mofClass); 149 list = (List) instanceAttributes_cache.get (mofClass); 150 } 151 return list; 152 } 153 154 160 public List classAttributes (MofClass mofClass) { 161 List list = (List) classAttributes_cache.get (mofClass); 162 if (list == null) { 163 cacheContainedElements (mofClass); 164 list = (List) classAttributes_cache.get (mofClass); 165 } 166 return list; 167 } 168 169 175 private List references (MofClass mofClass) { 176 List list = (List) references_cache.get (mofClass); 177 if (list == null) { 178 cacheContainedElements (mofClass); 179 list = (List) references_cache.get (mofClass); 180 } 181 return list; 182 } 183 184 187 public List structureFields (StructureType type) { 188 List fields = (List) structureFields_cache.get (type); 189 if (fields != null) 190 return fields; 191 fields = new LinkedList (); 193 Iterator content = type.getContents ().iterator (); 194 while (content.hasNext ()) { 195 Object element = content.next (); 196 if (element instanceof StructureField) 197 fields.add (element); 198 } structureFields_cache.put (type, fields); 200 return fields; 201 } 202 203 209 public RefBaseObject findProxy (ModelElement element) { 210 RefBaseObject proxy = (RefBaseObject) proxies_cache.get (element); 211 if (proxy != null) 212 return proxy; 213 LinkedList path = new LinkedList (); 214 ModelElement container = element.getContainer (); 215 while (container != null) { 216 path.add (container); 217 container = container.getContainer (); 218 } 219 MofPackage mofPackage = (MofPackage) path.removeLast (); 220 RefPackage refPackage = (RefPackage) outermostPackages.get (mofPackage.getName ()); 221 if (refPackage == null) { 222 Iterator iter = outermostPackages.entrySet ().iterator (); 223 while (iter.hasNext ()) { 224 RefPackage ref = (RefPackage) ((Map.Entry) iter.next ()).getValue (); 225 MofPackage meta = (MofPackage) ref.refMetaObject (); 226 if (meta instanceof MofPackage) { 227 refPackage = ref; 228 break; 229 } 230 } } 233 if (refPackage == null) 234 return null; 235 if (path.size () == 0) 236 proxy = refPackage; 237 while (path.size () > 0) { 238 ModelElement elem = (ModelElement) path.removeLast (); 239 if (elem instanceof MofPackage) { 240 refPackage = refPackage.refPackage (elem); 241 if (path.size () == 0) 242 proxy = refPackage; 243 } else { 244 if ((elem instanceof MofClass) && (path.size () == 0)) { 245 RefClass refClass = refPackage.refClass (elem); 246 proxy = refClass; 247 } else 248 break; 249 } } if (proxy != null) 252 proxies_cache.put (element, proxy); 253 return proxy; 254 } 255 256 private void cacheSubtypes (RefPackage pkg) { 257 Iterator iter = pkg.refAllClasses ().iterator (); 258 while (iter.hasNext ()) { 259 MofClass mofClass = (MofClass) ((RefClass) iter.next ()).refMetaObject (); 260 if (mofClass.isAbstract ()) 261 continue; 262 Iterator supertypes = mofClass.allSupertypes ().iterator (); 263 while (supertypes.hasNext ()) { 264 MofClass superClass = (MofClass) supertypes.next (); 265 List list = (List) subtypes_cache.get (superClass); 266 if (list == null) { 267 list = new LinkedList (); 268 subtypes_cache.put (superClass, list); 269 } list.add (mofClass); 271 } } iter = pkg.refAllPackages ().iterator (); 274 while (iter.hasNext ()) 275 cacheSubtypes ((RefPackage) iter.next ()); 276 } 277 278 public List nonAbstractSubtypes (MofClass mofClass) { 279 List subtypes = (List) subtypes_cache.get (mofClass); 280 if (subtypes != null) 281 return subtypes; 282 cacheSubtypes (extent); 283 return (List) subtypes_cache.get (mofClass); 284 } 285 286 292 public List associationEnds (Association mofAssoc) { 293 List list = (List) associationEnds_cache.get (mofAssoc); 294 if (list == null) { 295 cacheContainedElements (mofAssoc); 296 list = (List) associationEnds_cache.get (mofAssoc); 297 } 298 return list; 299 } 300 301 307 public List associationEndInstances (Association mofAssoc) { 308 List list = (List) associationEndInstances_cache.get (mofAssoc); 309 if (list == null) { 310 cacheContainedElements (mofAssoc); 311 list = (List) associationEndInstances_cache.get (mofAssoc); 312 } 313 return list; 314 } 315 316 public RefObject[] classInstances(MofClass type) { 317 RefObject[] ret = (RefObject[])classInstances_cache.get(type); 318 if (ret == null) { 319 RefPackage refPackage = (RefPackage) findProxy (type); 320 RefClass proxy = refPackage.refClass (type); 321 Collection col = proxy.refAllOfType (); 322 ret = (RefObject[])col.toArray(new RefObject[col.size()]); 323 classInstances_cache.put(type, ret); 324 } 325 return ret; 326 } 327 } 328 | Popular Tags |