KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.modfact.jmi.reflect;
2
3 import javax.jmi.reflect.*;
4
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.*;
7
8 public class ReflectHelper {
9     
10     public static String JavaDoc getType(RefObject o) {
11         return (String JavaDoc) o.refMetaObject().refGetValue("name");
12     }
13
14     public static RefObject findObjectByName(Collection col, String JavaDoc name) {
15         Iterator it = col.iterator();
16         while(it.hasNext()) {
17             RefObject content = (RefObject) it.next();
18             if(content.refGetValue("name").equals(name)) {
19                 return content;
20             }
21         }
22         throw new RuntimeException JavaDoc("ElementNotFound name='" +name);
23     }
24
25     public static RefObject findObjectByName(RefClass c, String JavaDoc name) {
26         return findObjectByName(c.refAllOfClass(), name);
27     }
28     
29     
30     public static List filterContentsByType(RefObject container
31                 , String JavaDoc type
32                 , boolean considerSupertypes) {
33       Collection c = (considerSupertypes)?
34                               getContentsInAllSupertypes(container)
35                             : (Collection) container.refGetValue("contents");
36       Iterator it = (c).iterator();
37       List r = new Vector();
38       while(it.hasNext()) {
39         RefObject content = (RefObject) it.next();
40         if(content.refMetaObject().refGetValue("name").equals(type)) {
41             r.add(content);
42         }
43       }
44       return r;
45     }
46      
47     public static List getContentsInAllSupertypes(RefObject container) {
48        List r = new DistinctList();
49        Iterator it = ((List) container.refGetValue("supertypes")).iterator();
50        while(it.hasNext()) {
51          RefObject sup = (RefObject) it.next();
52          r.addAll((List)getContentsInAllSupertypes(sup));
53        }
54        r.addAll((List)container.refGetValue("contents"));
55        return r;
56     }
57     
58     
59     // find the othermost Package objects
60
public static Collection getOuterMostMetaPackages(RefPackage metamodel) {
61       Iterator it = metamodel.refClass("Package").refAllOfClass().iterator();
62       Set outermosts = new HashSet();
63       // filter outermost packages
64
while(it.hasNext()) {
65         RefObject p = (RefObject) it.next();
66         outermosts.add(p.refOutermostComposite());
67       }
68       return outermosts;
69     }
70         
71     // find all elements associated to this one and to any of its supertypes
72
static Collection queryInAllSupertypes(String JavaDoc assoName, String JavaDoc endName, RefObject end ) {
73       return queryInAllSupertypes(
74             end.refImmediatePackage().refAssociation(assoName)
75             , endName ,end );
76     }
77     
78     // find all elements associated to this one and to any of its supertypes
79
static Collection queryInAllSupertypes(RefAssociation a, String JavaDoc endName, RefObject end ) {
80        Collection r = new DistinctList();
81        Iterator it = ((Collection) end.refGetValue("supertypes")).iterator();
82        while(it.hasNext()) {
83          RefObject sup = (RefObject) it.next();
84          r.addAll( queryInAllSupertypes(a, endName, sup) );
85        }
86        r.addAll(a.refQuery(endName,end));
87        return r;
88     }
89     
90     
91     public static RefObject getOppositeAssociationEnd(RefObject end) {
92       RefObject asso = (RefObject) end.refGetValue("container");
93       Iterator it = ((Collection)asso.refGetValue("contents")).iterator();
94       while(it.hasNext()) {
95         RefObject content = (RefObject) it.next();
96         if( !content.equals(end)
97           && content.refMetaObject().refGetValue("name").equals("AssociationEnd"))
98             return content;
99       }
100       return null;
101     }
102   
103     
104     public static int getMultiplicity(RefObject meta) {
105       return ((Integer JavaDoc)((RefStruct)meta.refGetValue("multiplicity")).refGetValue("upper")).intValue();
106     }
107     
108     public static List arrayToList(Object JavaDoc[] array) {
109       List l = new Vector();
110       for(int i=0; i<array.length; i++) {
111         l.add(array[i]);
112       }
113       return l;
114     }
115     
116     
117     public static RefBaseObject resolveQualifiedNameInExtents(Map extentMap, List qName) {
118         String JavaDoc extentName = (String JavaDoc) qName.get(0);
119         RefPackage extent = (RefPackage) extentMap.get(extentName);
120         if(extent==null) {
121             return null;
122         }
123         return resolveQualifiedNameInExtent((RefPackageImpl)extent ,qName ,1);
124     }
125     
126     
127     //lookup for contained RefClass, RefPackage, RefAssociation
128
public static RefBaseObject resolveQualifiedNameInExtent(RefBaseObject extent, List qName) {
129       if(qName.isEmpty()) throw new RuntimeException JavaDoc("invalide argument: empty qName");
130       RefPackageImpl p = (RefPackageImpl) extent.refOutermostPackage();
131       if(!p.name.equals(qName.get(0))) {
132         throw new RuntimeException JavaDoc("invalide root package '"+ qName.get(0) +"'");
133       }
134       return resolveQualifiedNameInExtent(p ,qName ,1);
135     }
136     
137     private static RefBaseObject resolveQualifiedNameInExtent(RefPackageImpl p, List qName, int index) {
138       if(index >= qName.size()) return p;
139       String JavaDoc currentNameItem = (String JavaDoc)qName.get(index);
140       if(p.classNames.contains(currentNameItem)) {
141         return p.refClass(currentNameItem);
142       }
143       if(p.associationNames.contains(currentNameItem)) {
144         return p.refAssociation(currentNameItem);
145       }
146       return resolveQualifiedNameInExtent(
147           (RefPackageImpl)p.refPackage(currentNameItem) ,qName ,index+1);
148     }
149     
150     ////////////////////////////////////////////////////////////
151
// implementation of some M3 operations
152
/////////////////////////////////////////////////////////////
153

154     // ModelElement Namespace.resolveQualifiedName(List qualifiedName)
155
public static RefObject resolveQualifiedName(RefObject context, List qualifiedName) {
156        RefObject lookupElement = lookupElement(context, (String JavaDoc)qualifiedName.get(0));
157        if(qualifiedName.size()==1) {
158          return lookupElement;
159        }
160        return ReflectHelper.resolveQualifiedName(
161             lookupElement, qualifiedName.subList(1,qualifiedName.size())
162        );
163     }
164     
165     // ModelElement Namespace.lookupElement(String name)
166
public static RefObject lookupElement(RefObject context, String JavaDoc name) {
167         Iterator it = ((Collection) context.refGetValue("contents")).iterator();
168         while(it.hasNext()) {
169           RefObject content = (RefObject)it.next();
170           if(content.refGetValue("name").equals(name)) {
171              return content;
172           }
173         }
174         throw new RuntimeException JavaDoc(context +": Name not found: " +name);
175     }
176     
177     // ModelElement GeneralizableElement.lookupElementExtended(String name)
178
public static RefObject lookupElementExtended(RefObject context, String JavaDoc name) {
179         Iterator it = getContentsInAllSupertypes(context).iterator();
180         while(it.hasNext()) {
181           RefObject content = (RefObject)it.next();
182           if(content.refGetValue("name").equals(name)) {
183              return content;
184           }
185         }
186         // for debugging
187
/*
188         System.out.println("debug info...");
189         it = getContentsInAllSupertypes(this).iterator();
190         while(it.hasNext()) {
191           RefObject content = (RefObject)it.next();
192           System.out.println(content.refGetValue("name"));
193         }
194         */

195         throw new RuntimeException JavaDoc("Name not found '" +name +"' in namespace " +getQualifiedName(context) );
196     }
197
198     // List GeneralizableElement.allSupertypes()
199
public static List allSupertypes(RefObject context) {
200        List r = new DistinctList();
201        Iterator it = ((List)context.refGetValue("supertypes")).iterator();
202        while(it.hasNext()) {
203          RefObjectImpl sup = (RefObjectImpl) it.next();
204          r.addAll( ReflectHelper.allSupertypes(sup) );
205          r.add(sup);
206        }
207        return r;
208     }
209     
210     // derived attribute in ModelElement
211
public static List getQualifiedName(RefObject context) {
212       // *** NOT to be confised with RefPackage RefBaseObject_impl.container
213
RefObjectImpl containerObject = (RefObjectImpl) context.refGetValue("container");
214       List r = null;
215       if(containerObject!=null) {
216         r = ReflectHelper.getQualifiedName(containerObject);
217       } else {
218         r = new Vector();
219       }
220       r.add(context.refGetValue("name"));
221       return r;
222     }
223
224     public static Method JavaDoc getMethod(Class JavaDoc clazz, String JavaDoc name) {
225         Method JavaDoc[] ms = clazz.getMethods();
226         for(int i=0; i<ms.length; i++) {
227             if(ms[i].getName().equals(name)) {
228                 return ms[i];
229             }
230         }
231         return null;
232     }
233     
234     public static Collection findTagsFor(RefObject modelElement) {
235       RefAssociation asso = (RefAssociation) modelElement.refOutermostPackage().refAssociation("AttachesTo");
236       return asso.refQuery("modelElement",modelElement);
237     }
238     
239     public static String JavaDoc findTagValue(RefObject modelElement, String JavaDoc tagId) {
240      try {
241        Iterator it = findTagsFor(modelElement).iterator();
242        while(it.hasNext()) {
243          RefObject t = (RefObject) it.next();
244          if(tagId.equalsIgnoreCase((String JavaDoc)t.refGetValue("tagId"))) {
245            // System.out.println(t.refGetValue("values"));
246
return (String JavaDoc) ((Collection)t.refGetValue("values")).toArray()[0];
247          }
248        }
249      } catch(Exception JavaDoc e) {
250        e.printStackTrace();
251      }
252      return null;
253     }
254
255     public static String JavaDoc dotQName(List l) {
256         Iterator it = l.iterator();
257         String JavaDoc r = (String JavaDoc) it.next();
258         while(it.hasNext()) {
259             r = r +"." +it.next();
260         }
261         return r;
262     }
263
264     public static String JavaDoc unqualifiedName(String JavaDoc dotList) {
265         dotList = dotList.substring(
266             dotList.lastIndexOf(":")+1 ,dotList.length()
267         );
268         return dotList.substring(
269             dotList.lastIndexOf(".")+1 ,dotList.length()
270         );
271     }
272     
273     public static String JavaDoc getName(RefObject o) {
274         return (String JavaDoc) o.refGetValue("name");
275     }
276         
277     public Map getExtentMaps(Collection extents) {
278         Iterator it = extents.iterator();
279         Map result = new Hashtable();
280         while(it.hasNext()) {
281             RefPackage extent = (RefPackage)it.next();
282             String JavaDoc name = getName(extent.refMetaObject());
283             result.put(name, extent);
284         }
285         return result;
286     }
287
288 }
289
Popular Tags