Sunday, August 18, 2013

SWTUtils doesn't support invoking methods with parameter

SWTUtils.invokeMethod allows you to invoke only methods with no parameters.

Here's a customized version of the method that allows you to invokes methods on objects with parameters

    public static Object invokeMethod(final Object object, String methodName,
            final Object[] params) throws NoSuchMethodException,
            IllegalAccessException, InvocationTargetException {
        Class[] paramTypes = getParamTypes(params);
        final Method method = object.getClass().getMethod(methodName,
                paramTypes);
        Widget widget = null;
        final Object result;
        if (object instanceof Widget) {
            widget = (Widget) object;
            result = UIThreadRunnable.syncExec(widget.getDisplay(),
                    new Result<Object>() {
                        @Override
                        public Object run() {
                            try {
                                return method.invoke(object, params);
                            } catch (Exception niceTry) {
                            }
                            return null;
                        }
                    });
        } else {
            result = method.invoke(object, params);
        }

        return result;
    }

Still this method only supports Object[] parameter, it doesn't support any primitive parameter like int, boolean.



No comments:

Post a Comment