📄 engineutils.java
字号:
isStaticOnly); } catch (NoSuchMethodException e) { // ok, so that didn't work - now try converting any primitive // wrapper types to their primitive counterparts try { // if args is null the NullPointerException will get caught // below and the right thing'll happen .. ugly but works for (int i = 0; i < args.length; i++) { if (args[i] instanceof Number) { if (args[i] instanceof Byte) argTypes[i] = byte.class; else if (args[i] instanceof Integer) argTypes[i] = int.class; else if (args[i] instanceof Long) argTypes[i] = long.class; else if (args[i] instanceof Float) argTypes[i] = float.class; else if (args[i] instanceof Double ) argTypes[i] = double.class; else if (args[i] instanceof Short ) argTypes[i] = short.class; } else if (args[i] instanceof Boolean) argTypes[i] = boolean.class; else if (args[i] instanceof Character) argTypes[i] = char.class; } m = MethodUtils.getMethod (beanClass, methodName, argTypes, isStaticOnly); } catch (Exception e2) { // throw the original throw e; } } // call it, and return the result try { return m.invoke (bean, args); } catch (Exception e) // 2003-02-23, --rgf, maybe an IllegalAccessException? { if (e instanceof IllegalAccessException && bMethodHasSetAccessible && Modifier.isPublic(m.getModifiers()) ) // if a public method allow access to it { m.setAccessible(true); // allow unconditional access to method return m.invoke (bean, args); } // re-throw the exception throw e; } } catch (Exception e) { // something went wrong while invoking method Throwable t = (e instanceof InvocationTargetException) ? ((InvocationTargetException)e).getTargetException () : null; throw new BSFException (BSFException.REASON_OTHER_ERROR, "method invocation failed: " + e + ((t==null) ? "" : (" target exception: " + t)), t); } } /** * Creates a new bean. The signature of the constructor that's invoked * is first taken as the types of the args, but if that fails, this tries * to convert any primitive wrapper type args to their primitive * counterparts to see whether a method exists that way. If it does, done. * * @param className fully qualified name of class to instantiate * @param args array of constructor args (or null if none) * * @return the created bean * * @exception BSFException if something goes wrong (@see * org.apache.cs.util.MethodUtils for the real * exceptions that can occur). */ public static Object createBean (String className, Object args[]) throws BSFException { Bean obj; Class[] argTypes = null; if (args != null) { argTypes = new Class[args.length]; for (int i = 0; i < args.length; i++) { argTypes[i] = (args[i] != null) ? args[i].getClass () : null; } } try { try { obj = ReflectionUtils.createBean (null, className, argTypes, args); return obj.value; } catch (NoSuchMethodException me) { // ok, so that didn't work - now try converting any primitive // wrapper types to their primitive counterparts try { // if args is null the NullPointerException will get caught // below and the right thing'll happen .. ugly but works for (int i = 0; i < args.length; i++) { if (args[i] instanceof Number) argTypes[i] = byte.class; else if (args[i] instanceof Boolean) argTypes[i] = boolean.class; else if (args[i] instanceof Character) argTypes[i] = char.class; } obj = ReflectionUtils.createBean (null, className, argTypes, args); return obj.value; } catch (Exception e) { // throw the previous exception throw me; } } } catch (Exception e) { throw new BSFException (BSFException.REASON_OTHER_ERROR, e.getMessage (), e); } } /** * Given a class return the type signature string fragment for it. * That is, return "I" for int, "J" for long, ... etc.. * * @param cl class object for whom the signature fragment is needed. * * @return the string representing the type signature */ public static String getTypeSignatureString (Class cl) { if (cl.isPrimitive ()) { if (cl == boolean.class) return "Z"; else if (cl == byte.class) return "B"; else if (cl == char.class) return "C"; else if (cl == short.class) return "S"; else if (cl == int.class) return "I"; else if (cl == long.class) return "J"; else if (cl == float.class) return "F"; else if (cl == double.class) return "D"; else return "V"; } else { StringBuffer sb = new StringBuffer ("L"); sb.append (cl.getName ()); sb.append (";"); return sb.toString().replace ('.', '/'); } } /** * Load a class using the class loader of given manager. If that fails * try using a class loader that loads from the tempdir of the manager. * * @param mgr BSFManager who's classLoader and tempDir props are * consulted * @param name name of the class to load * * @return the loaded class * * @exception BSFException if something goes wrong. */ public static Class loadClass (BSFManager mgr, String name) throws BSFException { ClassLoader classLoader = mgr.getClassLoader (); try { return (classLoader == null) ? // Class.forName (name) Thread.currentThread().getContextClassLoader().loadClass (name) : classLoader.loadClass (name); } catch (ClassNotFoundException e) { // try to load it from the temp dir using my own class loader try { if (bsfCL == null) bsfCL = new BSFClassLoader (); bsfCL.setTempDir (mgr.getTempDir ()); return bsfCL.loadClass (name); } catch (ClassNotFoundException e2) { throw new BSFException (BSFException.REASON_OTHER_ERROR, "unable to load class '" + name + "':" + e, e); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -