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 RefObjectImpl extends RefBaseObjectImpl 8 implements javax.jmi.reflect.RefObject, java.io.Serializable { 9 10 RefClassImpl classProxy; 11 12 Object [] attValues = null; 14 15 public RefObjectImpl() { 16 } 17 18 void setArgs(List args) { 19 if(args!=null) { 20 attValues = args.toArray(); 21 } else { 22 attValues = new Object [classProxy.attNames.size()]; 23 for(int i=0; i<attValues.length; i++ ) { 24 if(classProxy.attMultiplicities[i]!=1) 25 attValues[i] = new Vector(); 26 } 27 } 28 } 29 30 31 public javax.jmi.reflect.RefClass refClass() { 32 return classProxy; 33 } 34 35 public void refDelete() { 36 throw new RuntimeException ("No implementation: RefObject.refDelete"); 37 } 38 39 public boolean refIsInstanceOf(javax.jmi.reflect.RefObject meta, boolean considerSubtype) { 40 RefObject instanceType = refMetaObject(); 41 if(considerSubtype == false) { 42 return instanceType.equals(meta); 43 } 44 return isSubType(instanceType, meta); 45 } 46 47 public String toString() { 48 String type = super.toString(); 49 50 int i = classProxy.attNames.indexOf("name"); 51 if(i>=0) { 52 return type + (String ) attValues[i]; 53 } 54 return type; 55 } 56 57 boolean isSubType(RefObject sub, RefObject sup) { 58 if(sub.equals(sup)) return true; 59 Iterator it = ((Collection)sub.refGetValue("supertypes")).iterator(); 60 while(it.hasNext()) { 61 RefObject superType = (RefObject)it.next(); 62 if( isSubType(superType,sup)) return true; 63 } 64 return false; 65 } 66 67 68 public javax.jmi.reflect.RefFeatured refImmediateComposite() { 69 RefObject meta = refMetaObject(); 70 71 Iterator it = queryInAllSupertypes("IsOfType", "type", meta).iterator(); 77 while(it.hasNext()) { 78 RefObject typedElement = (RefObject)it.next(); 79 if(typedElement.refMetaObject().refGetValue("name").equals("AssociationEnd")) { 80 RefObject thisEnd = typedElement; 81 RefObject oppositeEnd = getOppositeAssociationEnd(thisEnd); 82 if(oppositeEnd.refGetValue("aggregation").toString().equals("composite")) { 83 RefObjectImpl assoMeta = (RefObjectImpl) thisEnd.refGetValue("container"); 84 Object asso = ReflectHelper.resolveQualifiedNameInExtent( 85 this, ReflectHelper.getQualifiedName(assoMeta) 86 ); 87 Collection c = (Collection) ((RefAssociation)asso).refQuery(thisEnd, this); 89 if(!c.isEmpty()) 90 return (RefFeatured)c.toArray()[0]; 91 } 92 } 93 } 94 return null; 95 } 96 97 98 public javax.jmi.reflect.RefFeatured refOutermostComposite() { 99 RefFeatured r = this; 100 RefFeatured p = this.refImmediateComposite(); 101 while((p!=null)&&(p instanceof RefObject)) { 102 r = p; 103 p = ((RefObject) p).refImmediateComposite(); 104 } 105 return (p==null)? r:p; 106 } 107 108 public javax.jmi.reflect.RefObject refMetaObject() { 109 if(metaObject!=null) return metaObject; 110 return findObjectByName( container.refClass("Class"), name); 111 } 112 113 115 public void refSetValue(String str, Object obj) { 116 if(!container.canModify) { 117 throw new RuntimeException ("Not allowed to modify"); 118 } 119 int i = classProxy.attNames.indexOf(str); 120 if(i>=0) { 121 if(classProxy.attMultiplicities[i]!=1) { 122 throw new RuntimeException ("Cannot do refSetValue with multivalued " +"'"+ str +"' in " +this ); 123 } 124 attValues[i] = obj; 126 return; 127 } 128 i = classProxy.refNames.indexOf(str); 129 if(i>=0) { 130 if(classProxy.refMultiplicities[i]!=1) { 131 throw new RuntimeException ("Cannot do refSetValue with multiple-value " +"'"+ str +"' in " +this ); 132 } 133 RefAssociationImpl a = (RefAssociationImpl) classProxy.associations()[i]; 134 135 if(obj!=null) { 136 if(a.firstEndName.equals(str)) { 137 a.refAddLink( (RefObject)obj, this ); 138 } else { 139 a.refAddLink(this, (RefObject)obj ); 140 } 141 } else { 142 RefObject old = (RefObject) refGetValue(str); 144 if(old==null) return; 145 if(a.firstEndName.equals(str)) { 146 a.refRemoveLink( old, this ); 147 } else { 148 a.refRemoveLink(this, old ); 149 } 150 } 151 return; 152 } 153 System.err.println("'"+ str +"' not found in " +this ); 154 throw new InvalidNameException("'"+ str +"' not found in " +this ); 155 } 156 157 public Object refGetValue(String str) { 158 159 int i = classProxy.attNames.indexOf(str); 160 if(i>=0) { 161 if( (!container.canModify) && (attValues[i] instanceof Collection) ) 162 return new Vector((Collection)attValues[i]); 163 else 164 return attValues[i]; 165 } 166 i = classProxy.refNames.indexOf(str); 167 if(i>=0) { 168 RefAssociationImpl a = classProxy.associations()[i]; 169 Collection c = a.refQuery( a.getOtherEndName(classProxy.assoEndNames[i]), (RefObject)this); 170 if(classProxy.refMultiplicities[i]==1) { 171 return c.isEmpty()? null : c.toArray()[0]; 172 } 173 return c; 174 } 175 176 if(str.equals("qualifiedName")) return ReflectHelper.getQualifiedName(this); 178 throw new RuntimeException ("'"+ str +"' not found in " +this ); 180 } 181 182 183 public Object refGetValue(javax.jmi.reflect.RefObject refObject) { 184 String name = (String ) refObject.refGetValue("name"); 185 return refGetValue(name); 186 } 187 188 public void refSetValue(javax.jmi.reflect.RefObject refObject, Object obj) { 189 String name = (String ) refObject.refGetValue("name"); 190 refSetValue(name, obj); 191 } 192 193 public Object refInvokeOperation(javax.jmi.reflect.RefObject refObject, java.util.List list) 194 throws javax.jmi.reflect.RefException { 195 String name = (String ) refObject.refGetValue("name"); 196 return refInvokeOperation(name, list); 197 } 198 199 200 public Object refInvokeOperation(String str, java.util.List list) throws javax.jmi.reflect.RefException { 202 Object r; 203 Method m = ReflectHelper.getMethod(this.getClass(), str); 204 try { 205 if(m==null) throw new RuntimeException ("operation not found:" +str); 206 r = m.invoke(this, list.toArray()); 207 return r; 208 } catch(Exception e) { 209 try { 211 m = ReflectHelper.getMethod(ReflectHelper.class,name); 212 if(m==null){ 213 if(e instanceof RuntimeException ) throw (RuntimeException ) e; 214 throw new RuntimeException (e); 215 } 216 List l2 = new Vector(); 217 l2.add(this); 218 l2.addAll(list); 219 r = m.invoke(this, l2.toArray()); 220 return r; 221 } catch(Exception e2) { 222 if(e2 instanceof RuntimeException ) throw (RuntimeException ) e2; 223 throw new RuntimeException (e2); 224 } 225 } 226 } 227 228 229 void setAttValue(String name, Object value) { 231 if(name.equals("qualifiedName")) return; 233 int i = classProxy.attNames.indexOf(name); 234 attValues[i] = value; 235 } 236 237 238 262 263 264 } 265 | Popular Tags |