1 55 56 package org.apache.bsf.util; 57 58 import java.lang.reflect.*; 59 import java.util.*; 60 import java.io.*; 61 import java.beans.*; 62 63 import org.apache.bsf.*; 64 import org.apache.bsf.util.*; 65 66 73 public class EngineUtils { 74 static BSFClassLoader bsfCL; 77 78 80 99 public static void addEventListener (Object bean, String eventSetName, 100 String filter, 101 BSFEngine engine, BSFManager manager, 102 String source, int lineNo, 103 int columnNo, Object script) 104 throws BSFException { 105 BSFEventProcessor ep = new BSFEventProcessor (engine, manager, filter, 106 source, lineNo, columnNo, 107 script); 108 try { 109 ReflectionUtils.addEventListener (bean, eventSetName, ep); 110 } catch (Exception e) { 111 e.printStackTrace (); 112 throw new BSFException (BSFException.REASON_OTHER_ERROR, 113 "ouch while adding event listener: " + e, e); 114 } 115 } 116 118 133 public static Object callBeanMethod (Object bean, String methodName, 134 Object [] args) throws BSFException { 135 Class [] argTypes = null; 137 if (args != null) { 138 argTypes = new Class [args.length]; 139 for (int i = 0; i < args.length; i++) { 140 argTypes[i] = (args[i] == null) ? null : args[i].getClass (); 141 } 142 } 143 144 boolean isStaticOnly = false; 147 Class beanClass = (bean instanceof Class ) ? (Class )bean : bean.getClass (); 148 149 try { 151 Method m; 152 try { 153 m = MethodUtils.getMethod (beanClass, methodName, argTypes, 154 isStaticOnly); 155 } catch (NoSuchMethodException e) { 156 try { 159 for (int i = 0; i < args.length; i++) { 162 if (args[i] instanceof Number ) { 163 argTypes[i] = byte.class; 167 if(args[i] instanceof Float ) argTypes[i]= float.class; 168 else if(args[i] instanceof Double ) argTypes[i]= double.class; 169 } else if (args[i] instanceof Boolean ) { 170 argTypes[i] = boolean.class; 171 } 172 } 173 m = MethodUtils.getMethod (beanClass, methodName, argTypes, 174 isStaticOnly); 175 } catch (Exception e2) { 176 throw e; 178 } 179 } 180 181 return m.invoke (bean, args); 183 } catch (Exception e) { 184 Throwable t = (e instanceof InvocationTargetException) ? 186 ((InvocationTargetException)e).getTargetException () : 187 null; 188 throw new BSFException (BSFException.REASON_OTHER_ERROR, 189 "method invocation failed: " + e + 190 ((t==null)?"":(" target exception: "+t)), t); 191 } 192 } 193 195 210 public static Object createBean (String className, 211 Object args[]) throws BSFException { 212 Bean obj; 213 214 Class [] argTypes = null; 215 if (args != null) { 216 argTypes = new Class [args.length]; 217 for (int i = 0; i < args.length; i++) { 218 argTypes[i] = (args[i] != null) ? args[i].getClass () : null; 219 } 220 } 221 222 try { 223 try { 224 obj = ReflectionUtils.createBean (null, className, argTypes, args); 225 return obj.value; 226 } catch (NoSuchMethodException me) { 227 try { 230 for (int i = 0; i < args.length; i++) { 233 if (args[i] instanceof Number ) { 234 argTypes[i] = byte.class; 238 } else if (args[i] instanceof Boolean ) { 239 argTypes[i] = boolean.class; 240 } 241 } 242 obj = ReflectionUtils.createBean (null, className, argTypes, args); 243 return obj.value; 244 } catch (Exception e) { 245 throw me; 247 } 248 } 249 } catch (Exception e) { 250 throw new BSFException (BSFException.REASON_OTHER_ERROR, 251 e.getMessage (), e); 252 } 253 } 254 256 264 public static String getTypeSignatureString (Class cl) { 265 if (cl.isPrimitive ()) { 266 if (cl == boolean.class) { 267 return "Z"; 268 } else if (cl == byte.class) { 269 return "B"; 270 } else if (cl == char.class) { 271 return "C"; 272 } else if (cl == short.class) { 273 return "S"; 274 } else if (cl == int.class) { 275 return "I"; 276 } else if (cl == long.class) { 277 return "J"; 278 } else if (cl == float.class) { 279 return "F"; 280 } else if (cl == double.class) { 281 return "D"; 282 } else { return "V"; 284 } 285 } else { 286 StringBuffer sb = new StringBuffer ("L"); 287 sb.append (cl.getName ()); 288 sb.append (";"); 289 return sb.toString().replace ('.', '/'); 290 } 291 } 292 294 306 public static Class loadClass (BSFManager mgr, String name) 307 throws BSFException { 308 ClassLoader classLoader = mgr.getClassLoader (); 309 try { 310 return (classLoader == null) ? Class.forName (name) 311 : classLoader.loadClass (name); 312 } catch (ClassNotFoundException e) { 313 try { 315 if (bsfCL == null) { 316 bsfCL = new BSFClassLoader (); 317 } 318 bsfCL.setTempDir (mgr.getTempDir ()); 319 return bsfCL.loadClass (name); 320 } catch (ClassNotFoundException e2) { 321 throw new BSFException (BSFException.REASON_OTHER_ERROR, 322 "unable to load class '" + name + "':" + e, e); 323 } 324 } 325 } 326 } 327 | Popular Tags |