KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > qvt > engine > Reflection


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 JavaDoc propName, Object JavaDoc val) throws Exception JavaDoc {
10     if(val==null) return;
11     try {
12       Object JavaDoc 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 JavaDoc e) {
25       String JavaDoc msg = "assign prop: "
26         +getTypeName(owner) +"." +propName +" = " + printValue(val);
27       throw new ReflectException(msg ,e);
28     }
29   }
30
31   static Object JavaDoc getPropertyValue(RefObject owner, String JavaDoc propName) throws Exception JavaDoc {
32     try {
33       return owner.refGetValue(propName);
34     } catch(Exception JavaDoc e) {
35       String JavaDoc msg = "call prop: "
36         +getTypeName(owner) +"." +propName;
37       throw new ReflectException(msg, e);
38     }
39   }
40
41   static Object JavaDoc invokeOperation(
42                 String JavaDoc opName
43               , Object JavaDoc context
44               , List args) throws Exception JavaDoc {
45     try {
46         java.lang.reflect.Method JavaDoc[] ops = context.getClass().getMethods();
47         java.lang.reflect.Method JavaDoc 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 JavaDoc r = m.invoke(context, args.toArray());
55         return r;
56     } catch(Exception JavaDoc e) {
57       String JavaDoc msg = "invoke " +opName +" with "
58         +getTypeName(context)
59         +" " +printValue(args);
60       throw new ReflectException(msg ,e);
61     }
62   }
63
64   static Object JavaDoc createObject(RefPackage pkg, String JavaDoc conceptName) throws Exception JavaDoc {
65     try {
66       return pkg.refClass(conceptName).refCreateInstance(null);
67     } catch(Exception JavaDoc e) {
68       String JavaDoc msg = "create " +conceptName +" in " + pkg;
69       throw new ReflectException(msg ,e);
70     }
71   }
72
73   static Collection allOfType(RefPackage pkg, String JavaDoc conceptName)
74       throws Exception JavaDoc {
75     try {
76       return pkg.refClass(conceptName).refAllOfType();
77     } catch(Exception JavaDoc e) {
78       String JavaDoc msg = "allOfType " +conceptName +" in " +pkg;
79       throw new ReflectException(msg ,e);
80     }
81   }
82
83   static Object JavaDoc getEnumValue(RefPackage pkg, String JavaDoc type, String JavaDoc val)
84     throws Exception JavaDoc {
85     try {
86       return pkg.refGetEnum(type ,val);
87     } catch(Exception JavaDoc e) {
88       String JavaDoc msg = "create enum: " +type +" = " +val +" in " +getTypeName(pkg);
89       throw new ReflectException(msg ,e);
90     }
91   }
92
93   static boolean isOfType(RefObject o, String JavaDoc type) throws Exception JavaDoc {
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 JavaDoc e) {
100       String JavaDoc msg = getTypeName(o) +" isOfType " +type;
101       throw new ReflectException(msg ,e);
102     }
103   }
104
105   static String JavaDoc getTypeName(Object JavaDoc o) {
106     if(!(o instanceof RefBaseObject)) return o.getClass().getName();
107     return (String JavaDoc) ((RefBaseObject)o).refMetaObject().refGetValue("name");
108   }
109
110   static String JavaDoc printValue(Object JavaDoc val) {
111     if(val==null) return "null";
112     if(val instanceof Collection) {
113       Object JavaDoc[] array = ((Collection)val).toArray();
114       String JavaDoc 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