KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > reflect > RefObjectImpl


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 JavaDoc {
9     
10     RefClassImpl classProxy;
11     
12     // Attritbute
13
Object JavaDoc[] 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 JavaDoc[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 JavaDoc("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 JavaDoc toString() {
48       String JavaDoc type = super.toString();
49
50       int i = classProxy.attNames.indexOf("name");
51       if(i>=0) {
52         return type + (String JavaDoc) 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        // First, find aggregated association that associate this element with its composite
72
// To do this :
73
// Navivation in meta level: metaobject(type)-(typed)AssociationEnd(contained)-(container)Association(container)-(contained)AssociationEnd[aggregation=composite]
74
// then, we get the association from association meta-object
75
// then we use this association to navigate to the composite object in instance level
76
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 JavaDoc asso = ReflectHelper.resolveQualifiedNameInExtent(
85                 this, ReflectHelper.getQualifiedName(assoMeta)
86              );
87              // System.err.println(asso.getClass());
88
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     // implements RefFeature
114

115     public void refSetValue(String JavaDoc str, Object JavaDoc obj) {
116         if(!container.canModify) {
117             throw new RuntimeException JavaDoc("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 JavaDoc("Cannot do refSetValue with multivalued " +"'"+ str +"' in " +this );
123           }
124           // System.out.println(this +".refSetvalue " +str +"=" +obj );
125
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 JavaDoc("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             //remove old associated object
143
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 JavaDoc refGetValue(String JavaDoc 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")) // this attribute has "isDerived"=true
177
return ReflectHelper.getQualifiedName(this);
178         //System.err.println("'"+ str +"' not found in " +this );
179
throw new RuntimeException JavaDoc("'"+ str +"' not found in " +this );
180     }
181
182        
183     public Object JavaDoc refGetValue(javax.jmi.reflect.RefObject refObject) {
184         String JavaDoc name = (String JavaDoc) refObject.refGetValue("name");
185         return refGetValue(name);
186     }
187     
188     public void refSetValue(javax.jmi.reflect.RefObject refObject, Object JavaDoc obj) {
189         String JavaDoc name = (String JavaDoc) refObject.refGetValue("name");
190         refSetValue(name, obj);
191     }
192     
193     public Object JavaDoc refInvokeOperation(javax.jmi.reflect.RefObject refObject, java.util.List JavaDoc list)
194                                 throws javax.jmi.reflect.RefException {
195         String JavaDoc name = (String JavaDoc) refObject.refGetValue("name");
196         return refInvokeOperation(name, list);
197     }
198     
199    
200     // the operation names must be distinct
201
public Object JavaDoc refInvokeOperation(String JavaDoc str, java.util.List JavaDoc list) throws javax.jmi.reflect.RefException {
202         Object JavaDoc r;
203         Method m = ReflectHelper.getMethod(this.getClass(), str);
204         try {
205             if(m==null) throw new RuntimeException JavaDoc("operation not found:" +str);
206             r = m.invoke(this, list.toArray());
207             return r;
208         } catch(Exception JavaDoc e) {
209             // try M2 operation implementations in reflective repository
210
try {
211               m = ReflectHelper.getMethod(ReflectHelper.class,name);
212               if(m==null){
213                 if(e instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) e;
214                 throw new RuntimeException JavaDoc(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 JavaDoc e2) {
222                 if(e2 instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) e2;
223                 throw new RuntimeException JavaDoc(e2);
224             }
225         }
226     }
227     
228     
229     // use for initialize M3
230
void setAttValue(String JavaDoc name, Object JavaDoc value) {
231        if(name.equals("qualifiedName")) // this attribute has "isDerived"=true
232
return;
233        int i = classProxy.attNames.indexOf(name);
234        attValues[i] = value;
235     }
236    
237     
238 // ////////////////////////////////////////////////////////////
239
// // implementation of some M3 operations
240
// //
241
// // ModelElement Namespace.resolveQualifiedName(List qualifiedName)
242
// public RefObject _resolveQualifiedName(List qualifiedName) {
243
// return ReflectHelper.resolveQualifiedName(this, qualifiedName);
244
// }
245
// // ModelElement Namespace.lookupElement(String name)
246
// public RefObject _lookupElement(String name) {
247
// return ReflectHelper.lookupElement(this, name);
248
// }
249
// // ModelElement GeneralizableElement.lookupElementExtended(String name)
250
// public RefObject _lookupElementExtended(String name) {
251
// return ReflectHelper.lookupElementExtended(this, name);
252
// }
253
// // List GeneralizableElement.allSupertypes()
254
// public List _allSupertypes() {
255
// return ReflectHelper.allSupertypes(this);
256
// }
257
// // derived attribute in ModelElement
258
// public List _getQualifiedName() {
259
// return ReflectHelper.getQualifiedName(this);
260
// }
261

262     
263     
264 }
265
Popular Tags