1 package org.objectweb.modfact.jmi.reflect; 2 3 import java.lang.reflect.*; 4 import javax.jmi.reflect.*; 5 import java.util.*; 6 7 public class RefClassImpl extends DataTypeContainer 8 implements javax.jmi.reflect.RefClass, java.io.Serializable { 9 10 Collection allOfClass = new Vector(); 11 List directSubtypes = null; List directSupertypes = null; 14 Object [] classAttValues = null; 17 int[] classAttMultiplicities = null; List classAttNames = new Vector(); 19 20 int[] attMultiplicities = null; List attNames = new Vector(); 24 25 private transient RefAssociationImpl[] assos = null; 27 List[] assoNames = null; 28 String [] assoEndNames = null; 29 int[] refMultiplicities = null; List refNames = new Vector(); 31 32 public RefClassImpl() { 33 } 34 35 36 public java.util.Collection refAllOfClass() { 37 return new Vector(allOfClass); 38 } 39 40 public java.util.Collection refAllOfType() { 41 Collection allOfType = new DistinctList(allOfClass); 42 Iterator it = directSubtypes().iterator(); 44 while(it.hasNext()) { 45 RefClass subtype = (RefClass)it.next(); 46 allOfType.addAll(subtype.refAllOfType()); 47 } 48 return allOfType; 49 } 50 51 List directSubtypes() { 53 if(directSubtypes!=null) return directSubtypes(); 54 RefObject meta = refMetaObject(); 55 Iterator it = meta.refOutermostPackage() 56 .refAssociation("Generalizes") 57 .refQuery("supertype",meta) 58 .iterator(); 59 List r = new Vector(); 60 while(it.hasNext()) { 61 RefObjectImpl typeMeta = (RefObjectImpl) it.next(); 62 r.add( ReflectHelper.resolveQualifiedNameInExtent( 63 this, ReflectHelper.getQualifiedName(typeMeta) 64 ) 65 ); 66 } 67 return r; 68 } 69 70 List directSupertypes() { 72 if(directSupertypes!=null) return directSupertypes(); 73 RefObject meta = refMetaObject(); 74 Iterator it = meta.refOutermostPackage() 75 .refAssociation("Generalizes") 76 .refQuery("subtype",meta) 77 .iterator(); 78 List r = new Vector(); 79 while(it.hasNext()) { 80 RefObjectImpl typeMeta = (RefObjectImpl) it.next(); 81 r.add( ReflectHelper.resolveQualifiedNameInExtent( 82 this, ReflectHelper.getQualifiedName(typeMeta) 83 ) 84 ); 85 } 86 return r; 87 } 88 89 RefAssociationImpl[] associations() { 90 if(assos!=null) return assos; 91 assos = new RefAssociationImpl[assoNames.length]; 92 for(int i=0; i<assoNames.length; i++) { 93 assos[i] = (RefAssociationImpl) ReflectHelper.resolveQualifiedNameInExtent(this, assoNames[i]); 94 } 95 return assos; 96 } 97 98 99 public javax.jmi.reflect.RefObject refCreateInstance(java.util.List args) { 100 if(!container.canModify) { 101 throw new RuntimeException ("Not allowed to modify"); 102 } 103 if(args!=null) { 104 if( args.size()!=attNames.size()) 105 throw new RuntimeException (this +": Wrong size argument " 106 +args.size() +" expect " +attNames.size() +"." 107 ); 108 for(int i=0; i<attMultiplicities.length; i++ ) { 111 if((attMultiplicities[i]!=1) && (args.get(i) instanceof Collection)) 112 throw new RuntimeException (this +": Invalide args at " +i 113 +" " +args.get(i) 114 +" should be Collection type" 115 ); 116 } 117 } 118 return _refCreateInstance(args); 119 } 120 121 javax.jmi.reflect.RefObject _refCreateInstance(java.util.List list) { 122 RefObjectImpl instance = newObject(); 123 124 instance.metaObject = metaObject; 125 instance.container = container; 126 instance.name = name; 127 instance.classProxy = this; 128 129 instance.setArgs(list); 130 allOfClass.add(instance); 131 return instance; 132 } 133 134 public javax.jmi.reflect.RefObject refMetaObject() { 135 if(metaObject!=null) return metaObject; 136 return findObjectByName( container.refClass("Class"), name); 137 } 138 139 140 141 void readContentsFromMetaObject() { 142 readContent_without_Inheritance(); 143 readContent_with_Inheritance(); 144 readDataTypeContents(); 145 } 146 147 void readContent_without_Inheritance() { 148 Iterator it; 149 it = ((Collection)metaObject.refGetValue("contents")).iterator(); 150 while(it.hasNext()) { 151 RefObject content = (RefObject)it.next(); 152 String contentName = (String ) content.refGetValue("name"); 153 String contentType = (String ) content.refMetaObject().refGetValue("name"); 154 if(contentType.equals("Attribute")) { 155 if( content.refImmediatePackage() 156 .refGetEnum("ScopeKind" ,"classifier_level") 157 .equals( 158 content.refGetValue("scope") 159 ) 160 && Boolean.FALSE.equals(content.refGetValue("isDerived")) 161 ) { 162 classAttNames.add(contentName); 163 } 164 } 165 } 166 classAttMultiplicities = new int[classAttNames.size()]; 168 169 it = ((Collection)metaObject.refGetValue("contents")).iterator(); 170 while(it.hasNext()) { 171 RefObject content = (RefObject)it.next(); 172 String contentName = (String ) content.refGetValue("name"); 173 String contentType = (String ) content.refMetaObject().refGetValue("name"); 174 175 if(contentType.equals("Attribute")) { 176 if( content.refImmediatePackage().refGetEnum("ScopeKind" ,"classifier_level").equals( 177 content.refGetValue("scope") 178 ) && Boolean.FALSE.equals(content.refGetValue("isDerived")) 179 ) { 180 int i = classAttNames.indexOf(contentName); 181 classAttMultiplicities[i] = getMultiplicity(content); 182 } 183 } 184 } 185 } 186 187 void readContent_with_Inheritance() { 188 Iterator it; 189 it = getContentsInAllSupertypes(metaObject).iterator(); 190 while(it.hasNext()) { 191 RefObject content = (RefObject)it.next(); 192 String contentName = (String ) content.refGetValue("name"); 193 String contentType = (String ) content.refMetaObject().refGetValue("name"); 194 195 if(contentType.equals("Attribute")) { 196 197 199 if( content.refImmediatePackage().refGetEnum("ScopeKind" ,"instance_level").equals( 200 content.refGetValue("scope") 201 ) && Boolean.FALSE.equals(content.refGetValue("isDerived")) 202 ) { 203 attNames.add(contentName); 204 } 205 } else if(contentType.equals("Reference")) { 206 refNames.add(contentName); 207 } 208 } 209 attMultiplicities = new int[attNames.size()]; 211 212 assoNames = new List[refNames.size()]; 214 assoEndNames = new String [refNames.size()]; 215 refMultiplicities = new int[refNames.size()]; 216 217 it = getContentsInAllSupertypes(metaObject).iterator(); 218 while(it.hasNext()) { 219 RefObject content = (RefObject)it.next(); 220 String contentName = (String ) content.refGetValue("name"); 221 String contentType = (String ) content.refMetaObject().refGetValue("name"); 222 223 if(contentType.equals("Attribute")) { 224 if( content.refImmediatePackage().refGetEnum("ScopeKind" ,"instance_level").equals( 225 content.refGetValue("scope") 226 ) && Boolean.FALSE.equals(content.refGetValue("isDerived")) 227 ) { 228 int i = attNames.indexOf(contentName); 229 attMultiplicities[i] = getMultiplicity(content); 230 } 231 232 } else if(contentType.equals("Reference")) { 233 int i = refNames.indexOf(contentName); 234 refMultiplicities[i] = getMultiplicity(content); 235 RefObject assoEnd = (RefObject) content.refGetValue("referencedEnd"); 236 assoEndNames[i] = (String ) assoEnd.refGetValue("name"); 237 RefObjectImpl asso = (RefObjectImpl) metaObject.refImmediatePackage().refAssociation("Contains").refQuery("containedElement", assoEnd).toArray()[0]; 238 assoNames[i] = ReflectHelper.getQualifiedName(asso); 239 } 240 } 241 } 242 243 244 245 247 public void refSetValue(String str, Object obj) { 248 int i = classAttNames.indexOf(str); 249 if(i>=0) { 250 if(classAttMultiplicities[i]!=1) { 251 throw new RuntimeException ("Cannot do refSetValue with multiple-value " +"'"+ str +"' in " +this ); 252 } 253 classAttValues[i] = obj; 254 return; 255 } 256 Iterator it = directSupertypes().iterator(); 257 while(it.hasNext()) { 258 try { 259 RefClassImpl superClass = (RefClassImpl) it.next(); 260 superClass.refSetValue(str, obj); 261 return; 262 } catch(InvalidNameException e) { 263 } 264 } 265 throw new InvalidNameException("'"+ str +"' not found in " +this ); 266 } 267 268 public Object refGetValue(String str) { 269 int i = classAttNames.indexOf(str); 270 if(i>=0) { 271 return classAttValues[i]; 272 } 273 Iterator it = directSupertypes().iterator(); 274 while(it.hasNext()) { 275 try { 276 RefClassImpl superClass = (RefClassImpl) it.next(); 277 return superClass.refGetValue(str); 278 } catch(InvalidNameException e) { 279 } 281 } 282 throw new InvalidNameException("'"+ str +"' not found in " +this ); 283 } 284 285 public Object refGetValue(javax.jmi.reflect.RefObject refObject) { 286 String name = (String ) refObject.refGetValue("name"); 287 return refGetValue(name); 288 } 289 290 public void refSetValue(javax.jmi.reflect.RefObject refObject, Object obj) { 291 String name = (String ) refObject.refGetValue("name"); 292 refSetValue(name, obj); 293 } 294 295 public Object refInvokeOperation(javax.jmi.reflect.RefObject refObject, java.util.List list) 296 throws javax.jmi.reflect.RefException { 297 String name = (String ) refObject.refGetValue("name"); 298 return refInvokeOperation(name, list); 299 } 300 301 transient List opNames = null; 302 transient Method[] ops = null; 303 304 public Object refInvokeOperation(String str, java.util.List list) throws javax.jmi.reflect.RefException { 306 if(opNames==null) loadOperations(); 307 Object r; 308 Method m = ops[ opNames.indexOf(str) ]; 309 try { 310 r = m.invoke(this, list.toArray()); 311 return r; 312 } catch(Exception e) { 313 throw new RuntimeException (e); 314 } 315 } 316 317 void loadOperations() { 318 ops = getClass().getMethods(); 319 opNames = new Vector(); 320 for(int i=0; i<ops.length; i++) { 321 opNames.add(ops[i].getName()); 322 } 323 } 324 325 public RefObjectImpl newObject() { 327 return new RefObjectImpl(); 328 } 329 330 331 } 332 | Popular Tags |