public boolean isVoid() { if (constructor != null) { return false; } else if (method != null) { return method.getReturnType().equals(void.class); } else { throw new Py4JException("Null method or constructor"); } }
/** * <p> * Wrapper around Field.set * </p> * * @param obj * @param field * @param value */ public void setFieldValue(Object obj, Field field, Object value) { try { field.set(obj, value); } catch (Exception e) { logger.log(Level.SEVERE, "Error while setting field value of " + field, e); throw new Py4JException(e); } }
/** * <p> * Wrapper around Field.get * </p> * * @param obj * @param field * @return */ public Object getFieldValue(Object obj, Field field) { Object fieldValue = null; try { fieldValue = field.get(obj); } catch (Exception e) { logger.log(Level.SEVERE, "Error while fetching field value of " + field, e); throw new Py4JException(e); } return fieldValue; }
public Object createArray(String fqn, int[] dimensions) { Class<?> clazz = null; Object returnObject = null; try { clazz = TypeUtil.forName(fqn); returnObject = Array.newInstance(clazz, dimensions); } catch (Exception e) { logger.log(Level.WARNING, "Class FQN does not exist: " + fqn, e); throw new Py4JException(e); } return returnObject; }
private Object convertArgument(Class<?> arrayClass, Object objectToSet) { Object newObject = null; List<TypeConverter> converters = new ArrayList<TypeConverter>(); Class<?>[] parameterClasses = { arrayClass }; Class<?>[] argumentClasses = { objectToSet.getClass() }; int cost = MethodInvoker.buildConverters(converters, parameterClasses, argumentClasses); if (cost >= 0) { newObject = converters.get(0).convert(objectToSet); } else { throw new Py4JException("Cannot convert " + argumentClasses[0].getName() + " to " + arrayClass.getName()); } return newObject; }
/** * <p> * Assumes that commandPart is <b>not</b> empty. * </p> * * @param commandPart * @return The object referenced in this command part. */ public final static Object getReference(String commandPart, Gateway gateway) { String reference = commandPart.substring(1, commandPart.length()); if (reference.trim().length() == 0) { throw new Py4JException("Reference is empty."); } return gateway.getObject(reference); }
protected Py4JClientConnection getConnectionLock() { Py4JClientConnection cc = null; try { logger.log(Level.INFO, "Getting CB Connection"); lock.lock(); if (!isShutdown) { cc = getConnection(); logger.log(Level.INFO, "Acquired CB Connection"); } else { logger.log(Level.INFO, "Shutting down, no connection can be created."); } } catch (Exception e) { logger.log(Level.SEVERE, "Critical error while sending a command", e); throw new Py4JException("Error while obtaining a new communication channel", e); } finally { lock.unlock(); } return cc; }
public final static Object getReturnValue(String returnMessage, Gateway gateway) throws Throwable { final Object result = getObject(returnMessage.substring(1), gateway); if (isError(returnMessage)) { if (result instanceof Throwable) { throw (Throwable) result; } else { throw new Py4JException("An exception was raised by the Python Proxy. Return Message: " + result); } } else { return result; } }
public Field getField(String classFQN, String name) { Class<?> clazz = null; try { clazz = ReflectionUtil.classForName(classFQN); } catch (Exception e) { logger.log(Level.WARNING, "Class FQN does not exist: " + classFQN, e); throw new Py4JException(e); } return getField(clazz, name); }
private Object convertOutput(Method method, Object output) { Class<?> returnType = method.getReturnType(); // If output is None/null or expected return type is // Void then return output with no conversion if (output == null || returnType.equals(Void.TYPE)) { // Do not convert void return output; } Class<?> outputType = output.getClass(); Class<?>[] parameters = { returnType }; Class<?>[] arguments = { outputType }; List<TypeConverter> converters = new ArrayList<TypeConverter>(); int cost = MethodInvoker.buildConverters(converters, parameters, arguments); if (cost == -1) { // This will be wrapped into Py4JJavaException if the Java code is being called by Python. throw new Py4JException( "Incompatible output type. Expected: " + returnType.getName() + " Actual: " + outputType.getName()); } return converters.get(0).convert(output); }
/** * <p> * Assumes that commandPart is <b>not</b> empty. * </p> * * @param commandPart * @return A Python proxy specified in this command part. */ public static Object getPythonProxy(String commandPart, Gateway gateway) { String proxyString = commandPart.substring(1, commandPart.length()); String[] parts = proxyString.split(";"); int length = parts.length; Class<?>[] interfaces = new Class<?>[length - 1]; if (length < 2) { throw new Py4JException("Invalid Python Proxy."); } for (int i = 1; i < length; i++) { try { interfaces[i - 1] = ReflectionUtil.classForName(parts[i]); if (!interfaces[i - 1].isInterface()) { throw new Py4JException( "This class " + parts[i] + " is not an interface and cannot be used as a Python Proxy."); } } catch (ClassNotFoundException e) { throw new Py4JException("Invalid interface name: " + parts[i]); } } return gateway.createProxy(ReflectionUtil.getClassLoader(), interfaces, parts[0]); }
} catch (Exception e) { logger.log(Level.WARNING, "Could not invoke method or received an exception while invoking.", e); throw new Py4JException(e);
public MethodInvoker getConstructor(String classFQN, Object[] parameters) { Class<?> clazz = null; try { clazz = ReflectionUtil.classForName(classFQN); } catch (Exception e) { logger.log(Level.WARNING, "Class FQN does not exist: " + classFQN, e); throw new Py4JException(e); } return getConstructor(clazz, getClassParameters(parameters)); }
public MethodInvoker getMethod(String classFQN, String name, Object[] parameters) { Class<?> clazz = null; try { clazz = ReflectionUtil.classForName(classFQN); } catch (Exception e) { logger.log(Level.WARNING, "Class FQN does not exist: " + classFQN, e); throw new Py4JException(e); } return getMethod(clazz, name, getClassParameters(parameters)); }
/** * <p> * Checks if an object is an instance of a given class. * </p> * * @param classFQN * The fully qualified name of a class to check * @param object * The object * @return True if object is an instance of the class. */ public static boolean isInstanceOf(String classFQN, Object object) { Class<?> clazz = null; try { clazz = ReflectionUtil.classForName(classFQN); } catch (Exception e) { throw new Py4JException(e); } return isInstanceOf(clazz, object); }
/** * <p> * Replace the callback client with the new one which connects to the given address * and port. This method is useful if for some reason your CallbackServer changes its * address or you come to know of the address after Gateway has already instantiated. * </p> * <p> * This method <strong>is not thread-safe</strong>! Make sure that only * one thread calls this method. * </p> * * @param pythonAddress * The address used by a PythonProxyHandler to connect to a * Python gateway. * @param pythonPort * The port used by a PythonProxyHandler to connect to a Python * gateway. Essentially the port used for Python callbacks. */ public void resetCallbackClient(InetAddress pythonAddress, int pythonPort) { if (cbClient == null) { throw new Py4JException("Callback Client is already null and cannot be reset"); } cbClient.shutdown(); this.cbClient = cbClient.copyWith(pythonAddress, pythonPort); }
throw new Py4JException("Cannot obtain a new communication channel"); } else { logger.log(Level.SEVERE, "Error while sending a command.", pe); throw new Py4JException("Error while sending a command.", pe); throw new Py4JException("Error while sending a command."); } catch (Exception e) { logger.log(Level.SEVERE, "Critical error while giving back connection.", e); throw new Py4JException("Error while giving back connection.");
public MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters) { MethodDescriptor mDescriptor = new MethodDescriptor(clazz.getName(), clazz, parameters); MethodInvoker mInvoker = null; List<Constructor<?>> acceptableConstructors = null; LRUCache<MethodDescriptor, MethodInvoker> cache = cacheHolder.get(); mInvoker = cache.get(mDescriptor); if (mInvoker == null) { acceptableConstructors = getConstructorsByLength(clazz, parameters.length); if (acceptableConstructors.size() == 1) { mInvoker = MethodInvoker.buildInvoker(acceptableConstructors.get(0), parameters); } else { mInvoker = getBestConstructor(acceptableConstructors, parameters); } if (mInvoker != null && mInvoker.getCost() != -1) { cache.put(mDescriptor, mInvoker); } else { String errorMessage = "Constructor " + clazz.getName() + "(" + Arrays.toString(parameters) + ") does not exist"; logger.log(Level.WARNING, errorMessage); throw new Py4JException(errorMessage); } } return mInvoker; }
} else { reset = true; throw new Py4JException("Unknown command received: " + commandLine);
} else { reset = true; throw new Py4JException("Unknown command received: " + commandLine);