public static T callMethod()

in utils/src/main/java/org/jclouds/karaf/utils/ServiceHelper.java [192:212]


    public static <S, T> T callMethod(S service, String methodName, Class<T> returnType) {
        T result = null;
        Class c = service.getClass();
        try {
            //Ugly way to get the Context, but there doesn't seem to be a better one.
            Method m = c.getMethod(methodName);
            Object obj = m.invoke(service);
            if (returnType.isAssignableFrom(obj.getClass())) {
                result = returnType.cast(obj);
            } else {
                throw new IllegalArgumentException("Service doesn't have a context.");
            }
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException(e);
        } catch (InvocationTargetException e) {
            throw new IllegalArgumentException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }
        return result;
    }