1 18 19 package org.objectweb.kilim.model.mapping; 20 21 import java.io.FileWriter ; 22 import java.io.PrintWriter ; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Modifier ; 27 28 import org.objectweb.kilim.KilimException; 29 import org.objectweb.kilim.model.services.DefaultRuntimeClassLoader; 30 31 35 public class JavaRuntimeMapper implements Mapper { 36 PrintWriter pW=null; 37 FileWriter fW = null; 38 39 42 public void enterContext(MappingContext aContext) throws KilimException { } 43 44 47 public void leaveContext(MappingContext aContext) throws KilimException { } 48 49 54 public Object getGetterValue (Object aSupport, boolean isStatic, String fieldName, MappingContext aContext) throws KilimException { 55 56 Class clazz = null; 57 if (aSupport instanceof Class && isStatic) { 58 clazz = (Class ) aSupport; 59 } else { 60 clazz = aSupport.getClass(); 61 } 62 63 Field field = null; 64 try { 65 field = clazz.getField(fieldName); 66 } catch (Exception exc) { 67 throw new KilimException(exc); 68 } 69 70 if (field == null) { 71 throw new KilimException("attempt to get the value of an unknown field " + fieldName + " in setter of " + clazz.getName()); 72 } 73 74 boolean jStatic = (field.getModifiers() & Modifier.STATIC) != 0; 75 if (isStatic && !jStatic) { 76 throw new KilimException("attempt to acces as static a non static field " + fieldName + " in " + clazz.getName()); 77 } 78 79 if (!isStatic && jStatic) { 80 throw new KilimException("attempt to acces as non static a static field " + fieldName + " in " + clazz.getName()); 81 } 82 83 Object resultValue = null; 84 try { 85 resultValue = field.get(aSupport); 86 } catch (Exception ex) { 87 throw new KilimException(ex); 88 } 89 90 return resultValue; 91 } 92 93 98 public void executeSetter(Object aSupport, boolean isStatic, String fieldName, Object toBeSet, MappingContext aContext) throws KilimException { 99 Class clazz = null; 100 if (aSupport instanceof Class && isStatic) { 101 clazz = (Class ) aSupport; 102 } else { 103 clazz = aSupport.getClass(); 104 } 105 106 Field field = null; 107 try { 108 field = clazz.getField(fieldName); 109 } catch (Exception exc) { 110 throw new KilimException(exc); 111 } 112 113 if (field == null) { 114 throw new KilimException("attempt to access an unknown field " + fieldName + " in setter of " + clazz.getName()); 115 } 116 117 boolean jStatic = (field.getModifiers() & Modifier.STATIC) != 0; 118 if (isStatic && !jStatic) { 119 throw new KilimException("attempt to acces as static a non static field " + fieldName + " in " + clazz.getName()); 120 } 121 122 if (!isStatic && jStatic) { 123 throw new KilimException("attempt to acces as non static a static field " + fieldName + " in " + clazz.getName()); 124 } 125 126 if (toBeSet == null) { 127 throw new KilimException("attempt to set a non initialized value to field " + fieldName + " in " + clazz.getName()); 128 } 129 130 try { 131 field.set(aSupport, toBeSet); 132 } catch (Exception exc) { 133 throw new KilimException(exc); 134 } 135 } 136 137 142 public Object getMethodValue(Object aSupport, boolean isStatic, String aMethodName, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 143 144 if (aSupport == null) { 145 throw new KilimException("attempt to invoke method \"" + aMethodName + "()\" on a null target."); 146 } 147 148 Class clazz = isStatic ? (Class ) aSupport : aSupport.getClass(); 149 150 int paramNumber = paramObjects.length; 151 Class [] paramTypes = new Class [paramNumber]; 152 for (int i = 0; i < paramNumber; i++) { 153 paramTypes[i] = DefaultRuntimeClassLoader.instance.getClass(typeNames[i]); 154 } 155 156 Method method = null; 157 try { 158 method = clazz.getMethod(aMethodName, paramTypes); 159 } catch (Exception exc) { 160 throw new KilimException(exc); 161 } 162 163 if (method == null) { 164 throw new KilimException("attempt to acces an unknown or ill declared method " + aMethodName + " in class " + clazz.getName()); 165 } 166 167 boolean jStatic = (method.getModifiers() & Modifier.STATIC) != 0; 168 if (isStatic && !jStatic) { 169 throw new KilimException("attempt to acces a non static method as static " + aMethodName + " in class " + clazz.getName()); 170 } 171 172 if (!isStatic && jStatic) { 173 throw new KilimException("attempt to acces a static method as non static " + aMethodName + " in class " + clazz.getName()); 174 } 175 176 Object resultValue = null; 177 try { 178 resultValue = method.invoke(aSupport, paramObjects); 179 } catch (Exception e) { 180 e.printStackTrace(); 181 StringBuffer buff = new StringBuffer ("exception while calling :\n\"" + aSupport + "\"[" + aSupport.getClass() + "]." + aMethodName + "("); 182 for (int i = 0; i < paramObjects.length; i++) { 183 buff.append("\""); 184 if (paramObjects[i] != null) { 185 buff.append(paramObjects[i].toString()); 186 buff.append("\"["); 187 buff.append(paramObjects[i].getClass()); 188 buff.append("]"); 189 } else { 190 buff.append("\"null\" [unknown]"); 191 } 192 if (i != 0) { 193 buff.append(","); 194 } 195 } 196 buff.append(")."); 197 throw new KilimException(buff.toString()); 198 } 199 200 return resultValue; 201 } 202 203 206 public void executeMethod(Object aSupport, boolean isStatic, String aMethodName, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 207 getMethodValue(aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext); 208 } 209 210 213 public Object getConstructorValue(Class aClass, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 214 215 int paramNumber = paramObjects.length; 216 Class [] paramTypes = new Class [paramNumber]; 217 218 for (int i = 0; i < paramNumber; i++) { 219 paramTypes[i] = DefaultRuntimeClassLoader.instance.getClass(typeNames[i]); 220 } 221 222 Constructor method = null; 223 try { 224 method = aClass.getConstructor(paramTypes); 225 } catch (Exception exc) { 226 throw new KilimException(exc); 227 } 228 229 if (method == null) { 230 throw new KilimException("attempt to use an unknown constructor in class " + aClass); 231 } 232 233 Object resultValue = null; 234 try { 235 resultValue = method.newInstance(paramObjects); 236 } catch (Exception e) { 237 e.printStackTrace(); 238 StringBuffer buff = new StringBuffer ("exception while calling constructor " + aClass.getName() + "("); 239 for (int i = 0; i < paramObjects.length; i++) { 240 if (paramObjects[i] != null) { 241 buff.append(paramObjects[i].toString()); 242 buff.append("\"["); 243 buff.append(paramObjects[i].getClass()); 244 buff.append("]"); 245 } else { 246 buff.append("\"null\" [unknown]"); 247 } 248 buff.append(")."); 249 throw new KilimException(buff.toString()); 250 } 251 } 252 253 return resultValue; 254 } 255 256 259 public void executeConstructor(Class aClass, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 260 getConstructorValue(aClass, paramObjects, typeNames, aContext); 261 } 262 263 266 public Object getExternalValue(Object value, MappingContext aContext) { 267 return value; 268 } 269 270 273 public Object getPropertyValue(Object aValue, MappingContext aContext) { 274 return aValue; 275 } 276 277 280 public Object getClassValue(String aClassName, MappingContext aContext) throws KilimException { 281 return DefaultRuntimeClassLoader.instance.getClass(aClassName); 282 } 283 284 287 public Object getEventSourceValue(MappingContext aContext) throws KilimException { 288 throw new KilimException("attempt to get a value directly from an event source"); 289 } 290 291 294 public Object getNullElementValue(MappingContext aContext) { 295 return null; 296 } 297 298 301 public void executeNullElement(MappingContext aContext) { } 302 } | Popular Tags |