Class Protocol
- java.lang.Object
-
- py4j.Protocol
-
public class Protocol extends java.lang.ObjectThis class defines the protocol used to communicate between two virtual machines (e.g., Python and Java).
Currently, the protocol requires type information (e.g., is this string an integer, an object reference or a boolean?) to be embedded with each command part. The rational is that the source virtual machine is usually better at determining the type of objects it sends.
An input command is usually composed of:
- A command name (e.g., c for call)
- Optionally, a sub command name (e.g., 'a' for concatenate in the list command)
- A list of command parts (e.g., the name of a method, the value of a parameter, etc.)
- The End of Command marker (e)
The various parts of a command are separated by \n characters. These characters are automatically escaped and unescaped in Strings on both sides (Java and Python).
An output command is usually composed of:
- A success or error code (y for yes, x for exception)
- A return value (e.g., n for null, v for void, or any other value like a String)
This class should be used only if the user creates new commands.
-
-
Field Summary
Fields Modifier and Type Field Description static charARRAY_TYPEstatic java.lang.StringAUTH_COMMAND_NAMEstatic charBOOLEAN_TYPEstatic charBYTES_TYPEstatic charCLASS_TYPEstatic charDECIMAL_TYPEstatic java.lang.StringDEFAULT_JVM_OBJECT_IDstatic charDOUBLE_TYPEstatic charENDstatic charEND_OUTPUTstatic java.lang.StringENTRY_POINT_OBJECT_IDstatic charERRORstatic java.lang.StringERROR_COMMANDstatic charFATAL_ERRORstatic java.lang.StringGATEWAY_SERVER_IDstatic charINTEGER_TYPEstatic charITERATOR_TYPEstatic charLIST_TYPEstatic charLONG_TYPEstatic charMAP_TYPEstatic charMETHOD_TYPEstatic charNO_MEMBERstatic java.lang.StringNO_SUCH_FIELDstatic charNULL_TYPEstatic charPACKAGE_TYPEstatic java.lang.StringPYTHON_INFINITYstatic java.lang.StringPYTHON_NANstatic java.lang.StringPYTHON_NEGATIVE_INFINITYstatic charPYTHON_PROXY_TYPEstatic charREFERENCE_TYPEstatic charRETURN_MESSAGEstatic charSET_TYPEstatic java.lang.StringSTATIC_PREFIXstatic charSTRING_TYPEstatic charSUCCESSstatic charVOIDstatic java.lang.StringVOID_COMMAND
-
Constructor Summary
Constructors Constructor Description Protocol()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.StringencodeBytes(byte[] bytes)Transform the byte array into Base64 characters.static java.lang.StringgetAuthCommand(java.lang.String authToken)static booleangetBoolean(java.lang.String commandPart)Assumes that commandPart is not empty.static byte[]getBytes(java.lang.String commandPart)Assumes that commandPart is not empty.static java.math.BigDecimalgetDecimal(java.lang.String commandPart)Assumes that commandPart is not empty.static doublegetDouble(java.lang.String commandPart)Assumes that commandPart is not empty.static intgetInteger(java.lang.String commandPart)Assumes that commandPart is not empty.static longgetLong(java.lang.String commandPart)Assumes that commandPart is not empty.static java.lang.StringgetMemberOutputCommand(char memberType)static java.lang.StringgetMemberOutputCommand(char memberType, java.lang.String fqn)static java.lang.StringgetNoSuchFieldOutputCommand()static java.lang.ObjectgetNull(java.lang.String commandPart)Method provided for consistency.static java.lang.ObjectgetObject(java.lang.String commandPart, Gateway gateway)static java.lang.StringgetOutputCommand(ReturnObject rObject)static java.lang.StringgetOutputErrorCommand()static java.lang.StringgetOutputErrorCommand(java.lang.String errorMessage)static java.lang.StringgetOutputErrorCommand(java.lang.Throwable throwable)static java.lang.StringgetOutputFatalErrorCommand(java.lang.Throwable throwable)static java.lang.StringgetOutputVoidCommand()static chargetPrimitiveType(java.lang.Object primitiveObject)static java.lang.ObjectgetPythonProxy(java.lang.String commandPart, Gateway gateway)Assumes that commandPart is not empty.static java.lang.ObjectgetPythonProxyHandler(java.lang.ClassLoader classLoader, java.lang.Class[] interfacesToImplement, java.lang.String objectId, Gateway gateway)Deprecated.static java.lang.ObjectgetReference(java.lang.String commandPart, Gateway gateway)Assumes that commandPart is not empty.static java.lang.ObjectgetReturnValue(java.lang.String returnMessage, Gateway gateway)static java.lang.ThrowablegetRootThrowable(java.lang.Throwable throwable, boolean skipInvocation)static java.lang.StringgetString(java.lang.String commandPart)Assumes that commandPart is not empty.static java.lang.StringgetThrowableAsString(java.lang.Throwable throwable)static booleanisBoolean(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisBytes(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisDecimal(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisDouble(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisEmpty(java.lang.String commandPart)static booleanisEnd(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisError(java.lang.String returnMessage)Assumes that commandPart is not null.static booleanisInteger(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisLong(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisNull(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisPythonProxy(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisReference(java.lang.String commandPart)Assumes that commandPart is not empty.static booleanisReturnMessage(java.lang.String commandPart)static booleanisString(java.lang.String commandPart)Assumes that commandPart is not empty.
-
-
-
Field Detail
-
BYTES_TYPE
public static final char BYTES_TYPE
- See Also:
- Constant Field Values
-
INTEGER_TYPE
public static final char INTEGER_TYPE
- See Also:
- Constant Field Values
-
LONG_TYPE
public static final char LONG_TYPE
- See Also:
- Constant Field Values
-
BOOLEAN_TYPE
public static final char BOOLEAN_TYPE
- See Also:
- Constant Field Values
-
DOUBLE_TYPE
public static final char DOUBLE_TYPE
- See Also:
- Constant Field Values
-
DECIMAL_TYPE
public static final char DECIMAL_TYPE
- See Also:
- Constant Field Values
-
STRING_TYPE
public static final char STRING_TYPE
- See Also:
- Constant Field Values
-
REFERENCE_TYPE
public static final char REFERENCE_TYPE
- See Also:
- Constant Field Values
-
LIST_TYPE
public static final char LIST_TYPE
- See Also:
- Constant Field Values
-
SET_TYPE
public static final char SET_TYPE
- See Also:
- Constant Field Values
-
ARRAY_TYPE
public static final char ARRAY_TYPE
- See Also:
- Constant Field Values
-
MAP_TYPE
public static final char MAP_TYPE
- See Also:
- Constant Field Values
-
ITERATOR_TYPE
public static final char ITERATOR_TYPE
- See Also:
- Constant Field Values
-
NULL_TYPE
public static final char NULL_TYPE
- See Also:
- Constant Field Values
-
PYTHON_PROXY_TYPE
public static final char PYTHON_PROXY_TYPE
- See Also:
- Constant Field Values
-
PACKAGE_TYPE
public static final char PACKAGE_TYPE
- See Also:
- Constant Field Values
-
CLASS_TYPE
public static final char CLASS_TYPE
- See Also:
- Constant Field Values
-
METHOD_TYPE
public static final char METHOD_TYPE
- See Also:
- Constant Field Values
-
NO_MEMBER
public static final char NO_MEMBER
- See Also:
- Constant Field Values
-
VOID
public static final char VOID
- See Also:
- Constant Field Values
-
RETURN_MESSAGE
public static final char RETURN_MESSAGE
- See Also:
- Constant Field Values
-
END
public static final char END
- See Also:
- Constant Field Values
-
END_OUTPUT
public static final char END_OUTPUT
- See Also:
- Constant Field Values
-
ERROR
public static final char ERROR
- See Also:
- Constant Field Values
-
FATAL_ERROR
public static final char FATAL_ERROR
- See Also:
- Constant Field Values
-
SUCCESS
public static final char SUCCESS
- See Also:
- Constant Field Values
-
AUTH_COMMAND_NAME
public static final java.lang.String AUTH_COMMAND_NAME
- See Also:
- Constant Field Values
-
ERROR_COMMAND
public static final java.lang.String ERROR_COMMAND
- See Also:
- Constant Field Values
-
VOID_COMMAND
public static final java.lang.String VOID_COMMAND
- See Also:
- Constant Field Values
-
NO_SUCH_FIELD
public static final java.lang.String NO_SUCH_FIELD
- See Also:
- Constant Field Values
-
ENTRY_POINT_OBJECT_ID
public static final java.lang.String ENTRY_POINT_OBJECT_ID
- See Also:
- Constant Field Values
-
DEFAULT_JVM_OBJECT_ID
public static final java.lang.String DEFAULT_JVM_OBJECT_ID
- See Also:
- Constant Field Values
-
GATEWAY_SERVER_ID
public static final java.lang.String GATEWAY_SERVER_ID
- See Also:
- Constant Field Values
-
STATIC_PREFIX
public static final java.lang.String STATIC_PREFIX
- See Also:
- Constant Field Values
-
PYTHON_NAN
public static final java.lang.String PYTHON_NAN
- See Also:
- Constant Field Values
-
PYTHON_INFINITY
public static final java.lang.String PYTHON_INFINITY
- See Also:
- Constant Field Values
-
PYTHON_NEGATIVE_INFINITY
public static final java.lang.String PYTHON_NEGATIVE_INFINITY
- See Also:
- Constant Field Values
-
-
Method Detail
-
encodeBytes
public static java.lang.String encodeBytes(byte[] bytes)
Transform the byte array into Base64 characters.
- Parameters:
bytes-- Returns:
-
getBoolean
public static final boolean getBoolean(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The boolean value corresponding to this command part.
-
getBytes
public static final byte[] getBytes(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The byte array corresponding to this command part.
-
getDecimal
public static final java.math.BigDecimal getDecimal(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The decimal value corresponding to this command part.
-
getDouble
public static final double getDouble(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The double value corresponding to this command part.
-
getInteger
public static final int getInteger(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The integer value corresponding to this command part.
-
getLong
public static final long getLong(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The long value corresponding to this command part.
-
getMemberOutputCommand
public static final java.lang.String getMemberOutputCommand(char memberType)
-
getMemberOutputCommand
public static final java.lang.String getMemberOutputCommand(char memberType, java.lang.String fqn)
-
getNoSuchFieldOutputCommand
public static java.lang.String getNoSuchFieldOutputCommand()
-
getNull
public static final java.lang.Object getNull(java.lang.String commandPart)
Method provided for consistency. Just returns null.
- Parameters:
commandPart-- Returns:
- null.
-
getObject
public static final java.lang.Object getObject(java.lang.String commandPart, Gateway gateway)
-
getOutputCommand
public static final java.lang.String getOutputCommand(ReturnObject rObject)
-
getOutputErrorCommand
public static final java.lang.String getOutputErrorCommand()
-
getOutputErrorCommand
public static final java.lang.String getOutputErrorCommand(java.lang.String errorMessage)
-
getOutputErrorCommand
public static final java.lang.String getOutputErrorCommand(java.lang.Throwable throwable)
-
getOutputFatalErrorCommand
public static final java.lang.String getOutputFatalErrorCommand(java.lang.Throwable throwable)
-
getOutputVoidCommand
public static final java.lang.String getOutputVoidCommand()
-
getAuthCommand
public static final java.lang.String getAuthCommand(java.lang.String authToken)
-
getPrimitiveType
public static char getPrimitiveType(java.lang.Object primitiveObject)
-
getPythonProxy
public static java.lang.Object getPythonProxy(java.lang.String commandPart, Gateway gateway)Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- A Python proxy specified in this command part.
-
getPythonProxyHandler
public static java.lang.Object getPythonProxyHandler(java.lang.ClassLoader classLoader, java.lang.Class[] interfacesToImplement, java.lang.String objectId, Gateway gateway)Deprecated.Legacy method. Please use Gateway.createProxy.
- Parameters:
classLoader-interfacesToImplement-objectId-gateway-- Returns:
-
getReference
public static final java.lang.Object getReference(java.lang.String commandPart, Gateway gateway)Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The object referenced in this command part.
-
getReturnValue
public static final java.lang.Object getReturnValue(java.lang.String returnMessage, Gateway gateway) throws java.lang.Throwable- Throws:
java.lang.Throwable
-
getRootThrowable
public static final java.lang.Throwable getRootThrowable(java.lang.Throwable throwable, boolean skipInvocation)
-
getString
public static final java.lang.String getString(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- The reference contained in this command part.
-
getThrowableAsString
public static final java.lang.String getThrowableAsString(java.lang.Throwable throwable)
-
isBoolean
public static final boolean isBoolean(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a boolean
-
isReturnMessage
public static final boolean isReturnMessage(java.lang.String commandPart)
- Parameters:
commandPart-- Returns:
- True if the command part is a return message
-
isBytes
public static final boolean isBytes(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a byte array
-
isDecimal
public static final boolean isDecimal(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a decimal
-
isDouble
public static final boolean isDouble(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a double
-
isEmpty
public static final boolean isEmpty(java.lang.String commandPart)
-
isEnd
public static final boolean isEnd(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is the end token
-
isError
public static final boolean isError(java.lang.String returnMessage)
Assumes that commandPart is not null.
- Parameters:
returnMessage-- Returns:
- True if the return message is an error
-
isInteger
public static final boolean isInteger(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is an integer
-
isLong
public static final boolean isLong(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a long
-
isNull
public static final boolean isNull(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is null
-
isPythonProxy
public static final boolean isPythonProxy(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a python proxy
-
isReference
public static final boolean isReference(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a reference
-
isString
public static final boolean isString(java.lang.String commandPart)
Assumes that commandPart is not empty.
- Parameters:
commandPart-- Returns:
- True if the command part is a reference
-
-