1 package org.objectweb.modfact.jmi.reflect; 2 3 import javax.jmi.reflect.*; 4 5 import java.lang.reflect.Method ; 6 import java.util.*; 7 8 public class ReflectHelper { 9 10 public static String getType(RefObject o) { 11 return (String ) o.refMetaObject().refGetValue("name"); 12 } 13 14 public static RefObject findObjectByName(Collection col, String 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 ("ElementNotFound name='" +name); 23 } 24 25 public static RefObject findObjectByName(RefClass c, String name) { 26 return findObjectByName(c.refAllOfClass(), name); 27 } 28 29 30 public static List filterContentsByType(RefObject container 31 , String 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 public static Collection getOuterMostMetaPackages(RefPackage metamodel) { 61 Iterator it = metamodel.refClass("Package").refAllOfClass().iterator(); 62 Set outermosts = new HashSet(); 63 while(it.hasNext()) { 65 RefObject p = (RefObject) it.next(); 66 outermosts.add(p.refOutermostComposite()); 67 } 68 return outermosts; 69 } 70 71 static Collection queryInAllSupertypes(String assoName, String endName, RefObject end ) { 73 return queryInAllSupertypes( 74 end.refImmediatePackage().refAssociation(assoName) 75 , endName ,end ); 76 } 77 78 static Collection queryInAllSupertypes(RefAssociation a, String 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 )((RefStruct)meta.refGetValue("multiplicity")).refGetValue("upper")).intValue(); 106 } 107 108 public static List arrayToList(Object [] 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 extentName = (String ) 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 public static RefBaseObject resolveQualifiedNameInExtent(RefBaseObject extent, List qName) { 129 if(qName.isEmpty()) throw new RuntimeException ("invalide argument: empty qName"); 130 RefPackageImpl p = (RefPackageImpl) extent.refOutermostPackage(); 131 if(!p.name.equals(qName.get(0))) { 132 throw new RuntimeException ("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 currentNameItem = (String )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 154 public static RefObject resolveQualifiedName(RefObject context, List qualifiedName) { 156 RefObject lookupElement = lookupElement(context, (String )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 public static RefObject lookupElement(RefObject context, String 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 (context +": Name not found: " +name); 175 } 176 177 public static RefObject lookupElementExtended(RefObject context, String 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 195 throw new RuntimeException ("Name not found '" +name +"' in namespace " +getQualifiedName(context) ); 196 } 197 198 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 public static List getQualifiedName(RefObject context) { 212 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 getMethod(Class clazz, String name) { 225 Method [] 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 findTagValue(RefObject modelElement, String tagId) { 240 try { 241 Iterator it = findTagsFor(modelElement).iterator(); 242 while(it.hasNext()) { 243 RefObject t = (RefObject) it.next(); 244 if(tagId.equalsIgnoreCase((String )t.refGetValue("tagId"))) { 245 return (String ) ((Collection)t.refGetValue("values")).toArray()[0]; 247 } 248 } 249 } catch(Exception e) { 250 e.printStackTrace(); 251 } 252 return null; 253 } 254 255 public static String dotQName(List l) { 256 Iterator it = l.iterator(); 257 String r = (String ) it.next(); 258 while(it.hasNext()) { 259 r = r +"." +it.next(); 260 } 261 return r; 262 } 263 264 public static String unqualifiedName(String 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 getName(RefObject o) { 274 return (String ) 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 name = getName(extent.refMetaObject()); 283 result.put(name, extent); 284 } 285 return result; 286 } 287 288 } 289 | Popular Tags |