1 package org.objectweb.modfact.qvt.engine; 2 3 import javax.jmi.reflect.*; 4 5 import java.util.*; 6 7 public class Reflection { 8 9 static void assignProperty(RefObject owner, String propName, Object val) throws Exception { 10 if(val==null) return; 11 try { 12 Object currentValue = owner.refGetValue(propName); 13 if( currentValue instanceof Collection ) { 14 Collection prop = (Collection) currentValue; 15 prop.clear(); 16 if(val instanceof Collection) { 17 prop.addAll((Collection) val); 18 } else { 19 prop.add(val); 20 } 21 } else { 22 owner.refSetValue(propName, val); 23 } 24 } catch(Exception e) { 25 String msg = "assign prop: " 26 +getTypeName(owner) +"." +propName +" = " + printValue(val); 27 throw new ReflectException(msg ,e); 28 } 29 } 30 31 static Object getPropertyValue(RefObject owner, String propName) throws Exception { 32 try { 33 return owner.refGetValue(propName); 34 } catch(Exception e) { 35 String msg = "call prop: " 36 +getTypeName(owner) +"." +propName; 37 throw new ReflectException(msg, e); 38 } 39 } 40 41 static Object invokeOperation( 42 String opName 43 , Object context 44 , List args) throws Exception { 45 try { 46 java.lang.reflect.Method [] ops = context.getClass().getMethods(); 47 java.lang.reflect.Method m = null; 48 for(int i=0; i<ops.length; i++) { 49 if(ops[i].getName().equals(opName)) { 50 m = ops[i]; 51 break; 52 } 53 } 54 Object r = m.invoke(context, args.toArray()); 55 return r; 56 } catch(Exception e) { 57 String msg = "invoke " +opName +" with " 58 +getTypeName(context) 59 +" " +printValue(args); 60 throw new ReflectException(msg ,e); 61 } 62 } 63 64 static Object createObject(RefPackage pkg, String conceptName) throws Exception { 65 try { 66 return pkg.refClass(conceptName).refCreateInstance(null); 67 } catch(Exception e) { 68 String msg = "create " +conceptName +" in " + pkg; 69 throw new ReflectException(msg ,e); 70 } 71 } 72 73 static Collection allOfType(RefPackage pkg, String conceptName) 74 throws Exception { 75 try { 76 return pkg.refClass(conceptName).refAllOfType(); 77 } catch(Exception e) { 78 String msg = "allOfType " +conceptName +" in " +pkg; 79 throw new ReflectException(msg ,e); 80 } 81 } 82 83 static Object getEnumValue(RefPackage pkg, String type, String val) 84 throws Exception { 85 try { 86 return pkg.refGetEnum(type ,val); 87 } catch(Exception e) { 88 String msg = "create enum: " +type +" = " +val +" in " +getTypeName(pkg); 89 throw new ReflectException(msg ,e); 90 } 91 } 92 93 static boolean isOfType(RefObject o, String type) throws Exception { 94 if(!(o instanceof RefObject)) return false; 95 RefObject refo = ((RefObject) o); 96 try { 97 RefObject c = o.refImmediatePackage().refClass(type).refMetaObject(); 98 return o.refIsInstanceOf(c ,true); 99 } catch(Exception e) { 100 String msg = getTypeName(o) +" isOfType " +type; 101 throw new ReflectException(msg ,e); 102 } 103 } 104 105 static String getTypeName(Object o) { 106 if(!(o instanceof RefBaseObject)) return o.getClass().getName(); 107 return (String ) ((RefBaseObject)o).refMetaObject().refGetValue("name"); 108 } 109 110 static String printValue(Object val) { 111 if(val==null) return "null"; 112 if(val instanceof Collection) { 113 Object [] array = ((Collection)val).toArray(); 114 String r ="{"; 115 for(int i=0; i<array.length; i++) { 116 r = r + " " +printValue(array[i]); 117 } 118 return r + "}"; 119 } 120 if(val instanceof RefBaseObject) { 121 return getTypeName((RefBaseObject)val); 122 } 123 return val.toString(); 124 } 125 } 126 127 128 129 130 | Popular Tags |