/******************************************************************************/ /* File: utils.beh */ /* */ /* Describes a "utilities" class that provides methods for dynamically */ /* instantiating a class with the specified name, and for invoking methods */ /* on an object. It uses metadata about objects and classes provided by the */ /* java run-time library to achieve this. */ /* */ /******************************************************************************/ Utils { {{ public static Object instantiate(String className) { Object returnValue = null; try { Class cl = Class.forName(className); returnValue = cl.newInstance(); } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } return returnValue; } public static boolean executeSetMethod(Object obj, String methodName, Object arg) { return executeSetMethod(obj, methodName, arg, arg.getClass().getName()); } public static boolean executeSetMethod(Object obj, String methodName, Object arg, String argClassName) { try { Class objClass = obj.getClass(); Class argClass = Class.forName(argClassName); Class argTypes[] = new Class[] {argClass}; java.lang.reflect.Method m = objClass.getMethod(methodName, argTypes); Object args[] = new Object[] {arg}; m.invoke(obj, args); return true; } catch (ClassNotFoundException e) { } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (java.lang.reflect.InvocationTargetException e) { } return false; } }} }