@Test public void testInvoke() { Cat cat = new Cat(); try { Method m = Cat.class.getMethod("meow10", float.class); MethodInvoker invoker = new MethodInvoker(m, new TypeConverter[] { new TypeConverter(TypeConverter.DOUBLE_TO_FLOAT) }, 0); invoker.invoke(cat, new Object[] { new Double(2.0) }); m = Cat.class.getMethod("meow11", new Class[0]); invoker = new MethodInvoker(m, null, 0); invoker.invoke(cat, new Object[0]); m = Cat.class.getMethod("meow10", float.class); invoker = new MethodInvoker(m, null, 0); invoker.invoke(cat, new Object[] { new Float(1.1f) }); } catch (Exception e) { e.printStackTrace(); fail(); } }
public Object invoke(Object obj, Object[] arguments) { Object returnObject = null; try { Object[] newArguments = arguments; if (converters != null) { int size = arguments.length; newArguments = new Object[size]; for (int i = 0; i < size; i++) { newArguments[i] = converters.get(i).convert(arguments[i]); } } if (method != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { method.setAccessible(true); return null; } }); returnObject = method.invoke(obj, newArguments); } else if (constructor != null) { constructor.setAccessible(true); returnObject = constructor.newInstance(newArguments); } } catch (InvocationTargetException ie) { logger.log(Level.WARNING, "Exception occurred in client code.", ie); throw new Py4JJavaException(ie.getCause()); } catch (Exception e) { logger.log(Level.WARNING, "Could not invoke method or received an exception while invoking.", e); throw new Py4JException(e); } return returnObject; }
MethodInvoker { public Object invoke(Object obj, Object[] arguments) { Object returnObject = null; try { Object[] newArguments = arguments; if (converters != null) { int size = arguments.length; newArguments = new Object[size]; for (int i = 0; i < size; i++) { newArguments[i] = converters.get(i).convert(arguments[i]); } } if (method != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { method.setAccessible(true); return null; } }); returnObject = method.invoke(obj, newArguments); } else if (constructor != null) { constructor.setAccessible(true); returnObject = constructor.newInstance(newArguments); } } catch (InvocationTargetException ie) { logger.log(Level.WARNING, "Exception occurred in client code.", ie); throw new Py4JJavaException(ie.getCause()); } catch (Exception e) { logger.log(Level.WARNING, "Could not invoke method or received an exception while invoking.", e); throw new Py4JException(e); } return returnObject; } }
MethodInvoker { public Object invoke(Object obj, Object[] arguments) { Object returnObject = null; try { Object[] newArguments = arguments; if (converters != null) { int size = arguments.length; newArguments = new Object[size]; for (int i = 0; i < size; i++) { newArguments[i] = converters.get(i).convert(arguments[i]); } } if (method != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { method.setAccessible(true); return null; } }); returnObject = method.invoke(obj, newArguments); } else if (constructor != null) { constructor.setAccessible(true); returnObject = constructor.newInstance(newArguments); } } catch (InvocationTargetException ie) { logger.log(Level.WARNING, "Exception occurred in client code.", ie); throw new Py4JJavaException(ie.getCause()); } catch (Exception e) { logger.log(Level.WARNING, "Could not invoke method or received an exception while invoking.", e); throw new Py4JException(e); } return returnObject; } MethodInvoker(Constructor<?> constructor, TypeConverter[] converters, int cost);  MethodInvoker(Method method, TypeConverter[] converters, int cost); }
MethodInvoker { public Object invoke(Object obj, Object[] arguments) { Object returnObject = null; try { Object[] newArguments = arguments; if (converters != null) { int size = arguments.length; newArguments = new Object[size]; for (int i = 0; i < size; i++) { newArguments[i] = converters.get(i).convert(arguments[i]); } } if (method != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { method.setAccessible(true); return null; } }); returnObject = method.invoke(obj, newArguments); } else if (constructor != null) { constructor.setAccessible(true); returnObject = constructor.newInstance(newArguments); } } catch (InvocationTargetException ie) { logger.log(Level.WARNING, "Exception occurred in client code.", ie); throw new Py4JJavaException(ie.getCause()); } catch (Exception e) { logger.log(Level.WARNING, "Could not invoke method or received an exception while invoking.", e); throw new Py4JException(e); } return returnObject; } MethodInvoker(Constructor<?> constructor, TypeConverter[] converters, int cost);  MethodInvoker(Method method, TypeConverter[] converters, int cost); static int buildConverters(List<TypeConverter> converters, Class<?>[] parameters, Class<?>[] arguments); static MethodInvoker buildInvoker(Constructor<?> constructor, Class<?>[] arguments); static MethodInvoker buildInvoker(Method method, Class<?>[] arguments); Constructor<?> getConstructor(); List<TypeConverter> getConverters(); int getCost(); Method getMethod(); Object invoke(Object obj, Object[] arguments); boolean isVoid(); }
MethodInvoker { public Object invoke(Object obj, Object[] arguments) { Object returnObject = null; try { Object[] newArguments = arguments; if (converters != null) { int size = arguments.length; newArguments = new Object[size]; for (int i = 0; i < size; i++) { newArguments[i] = converters.get(i).convert(arguments[i]); } } if (method != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { method.setAccessible(true); return null; } }); returnObject = method.invoke(obj, newArguments); } else if (constructor != null) { constructor.setAccessible(true); returnObject = constructor.newInstance(newArguments); } } catch (InvocationTargetException ie) { logger.log(Level.WARNING, "Exception occurred in client code.", ie); throw new Py4JJavaException(ie.getCause()); } catch (Exception e) { logger.log(Level.WARNING, "Could not invoke method or received an exception while invoking.", e); throw new Py4JException(e); } return returnObject; } MethodInvoker(Constructor<?> constructor, TypeConverter[] converters, int cost);  MethodInvoker(Method method, TypeConverter[] converters, int cost); static int buildConverters(List<TypeConverter> converters, Class<?>[] parameters, Class<?>[] arguments); static MethodInvoker buildInvoker(Constructor<?> constructor, Class<?>[] arguments); static MethodInvoker buildInvoker(Method method, Class<?>[] arguments); Constructor<?> getConstructor(); List<TypeConverter> getConverters(); int getCost(); Method getMethod(); Object invoke(Object obj, Object[] arguments); boolean isVoid(); final static int INVALID_INVOKER_COST; final static int MAX_DISTANCE; static final MethodInvoker INVALID_INVOKER; }