in src/main/java/org/apache/bsf/util/EngineUtils.java [134:212]
public static Object callBeanMethod(final Object bean, final String methodName, final Object[] args) throws BSFException {
Class[] argTypes = null;
// determine arg types. note that a null argtype
// matches any object type
if (args != null) {
argTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argTypes[i] = (args[i] == null) ? null : args[i].getClass();
}
}
// we want to allow a static call to occur on an object, similar
// to what Java allows. So isStaticOnly is set to false.
final boolean isStaticOnly = false;
final Class beanClass = (bean instanceof Class) ? (Class) bean : bean.getClass();
// now try to call method with the right signature
try {
Method m;
try {
m = MethodUtils.getMethod(beanClass, methodName, argTypes, isStaticOnly);
} catch (final 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 (final Exception e2) {
// throw the original
throw e;
}
}
// call it, and return the result
try {
return m.invoke(bean, args);
} catch (final 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 (final Exception e) {
// something went wrong while invoking method
final Throwable t = (e instanceof InvocationTargetException) ? ((InvocationTargetException) e).getTargetException() : null;
throw new BSFException(BSFException.REASON_OTHER_ERROR,
"[EngineUtils.callBeanMethod()] method invocation failed: " + e + ((t == null) ? "" : (" target exception: " + t)), t);
}
}