public class PyObject
extends Object
Represents a Python object (of Python/C API type
PyObject*
) in the Python interpreter.
Since:
createProxy
(
Class
<T> type)
Create a Java proxy instance of this Python object which contains compatible methods to the ones provided in the
interface given by the
type
parameter.
Object
createProxy
(
PyLib.CallableKind
callableKind,
Class
<?>... types)
Create a Java proxy instance of this Python object (or module) which contains compatible methods
(or functions) to the ones provided in the interfaces given by all the
type
parameters.
boolean
equals
(
Object
o)
Indicates whether some other object is "equal to" this one.
static
PyObject
executeCode
(
String
code,
PyInputMode
mode)
Executes Python source code.
static
PyObject
executeCode
(
String
code,
PyInputMode
mode,
Map
<
String
,
Object
> globals,
Map
<
String
,
Object
> locals)
Executes Python source code in the context specified by the
globals
and
locals
maps.
protected void
finalize
()
Decrements the reference count of the Python object which this class represents.
PyObject
getAttribute
(
String
name)
Gets the Python value of a Python attribute.
getAttribute
(
String
name,
Class
<? extends T> valueType)
Gets the value of a Python attribute as Java object of a given type.
double
getDoubleValue
()
getIntValue
()
<T> T[]
getObjectArrayValue
(
Class
<? extends T> itemType)
Gets this Python object as Java
Object[]
value of the given item type.
Object
getObjectValue
()
Gets this Python object as Java
Object
value.
getPointer
()
String
getStringValue
()
hashCode
()
Computes a hash code from this object's pointer value.
<T> void
setAttribute
(
String
name,
T value)
Sets the value of a Python attribute from the given Java object.
<T> void
setAttribute
(
String
name,
T value,
Class
<? extends T> valueType)
Sets the value of a Python attribute from the given Java object and Java type (if given).
String
toString
()
Gets a string representation of the object using the format "PyObject(pointer=<value>)".
public static PyObject executeCode(String code,
PyInputMode mode)
Executes Python source code.
Parameters:
code
- The Python source code.
mode
- The execution mode.
Returns:
The result of executing the code as a Python object.
public static PyObject executeCode(String code,
PyInputMode mode,
Map<String,Object> globals,
Map<String,Object> locals)
Executes Python source code in the context specified by the
globals
and
locals
maps.
If a Java value in the
globals
and
locals
maps cannot be directly converted into a Python object, a Java wrapper will be created instead.
If a Java value is a wrapped Python object of type
PyObject
, it will be unwrapped.
Parameters:
code
- The Python source code.
mode
- The execution mode.
globals
- The global variables to be set.
locals
- The locals variables to be set.
Returns:
The result of executing the code as a Python object.
protected void finalize()
throws Throwable
Decrements the reference count of the Python object which this class represents.
Overrides:
finalize
in class
Object
Throws:
Throwable
- If any error occurs.
getObjectValue
public Object getObjectValue()
Gets this Python object as Java
Object
value.
If this Python object cannot be converted into a Java object, a Java wrapper of type
PyObject
will be returned.
If this Python object is a wrapped Java object, it will be unwrapped.
Returns:
This Python object as a Java
Object
value.
getObjectArrayValue
public <T> T[] getObjectArrayValue(Class<? extends T> itemType)
Gets this Python object as Java
Object[]
value of the given item type.
Appropriate type conversions from Python to Java will be performed as needed.
If a Python item value cannot be converted into a Java item object, a Java wrapper of type
PyObject
will be returned.
If a Python item value is a wrapped Java object, it will be unwrapped.
If this Python object value is a wrapped Java array object of given type, it will be unwrapped.
Type Parameters:
T
- The expected item type name.
Parameters:
itemType
- The expected item type class.
Returns:
This Python object as a Java
Object[]
value.
getAttribute
public PyObject getAttribute(String name)
Gets the Python value of a Python attribute.
If the Python value cannot be converted into a Java object, a Java wrapper of type
PyObject
will be returned.
If the Python value is a wrapped Java object, it will be unwrapped.
Parameters:
name
- A name of the Python attribute.
Returns:
A wrapper for the returned Python attribute value.
public <T> T getAttribute(String name,
Class<? extends T> valueType)
Gets the value of a Python attribute as Java object of a given type.
Appropriate type conversions from Python to Java will be performed using the given value type.
If the Python value cannot be converted into a Java object, a Java wrapper of type
PyObject
will be returned.
If the Python value is a wrapped Java object, it will be unwrapped.
Type Parameters:
T
- The expected value type name.
Parameters:
name
- A name of the Python attribute.
valueType
- The type of the value or
null
, if unknown
Returns:
The Python attribute value as Java object.
public <T> void setAttribute(String name,
T value)
Sets the value of a Python attribute from the given Java object.
If the Java
value
cannot be directly converted into a Python object, a Java wrapper will be created instead.
If the Java
value
is a wrapped Python object of type
PyObject
, it will be unwrapped.
Type Parameters:
T
- The value type name.
Parameters:
name
- A name of the Python attribute.
value
- The new attribute value as Java object.
T value,
Class
<? extends T> valueType)
Sets the value of a Python attribute from the given Java object and Java type (if given).
Appropriate type conversions from Java to Python will be performed using the given value type.
If the Java
value
cannot be directly converted into a Python object, a Java wrapper will be created instead.
If the Java
value
is a wrapped Python object of type
PyObject
, it will be unwrapped.
Type Parameters:
T
- The value type name.
Parameters:
name
- A name of the Python attribute.
value
- The new attribute value as Java object.
valueType
- The value type used for the conversion. May be
null
.
public PyObject callMethod(String name,
Object... args)
Call the callable Python method with the given name and arguments.
If a Java value in
args
cannot be directly converted into a Python object, a Java wrapper will be created instead.
If the Java value in
args
is a wrapped Python object of type
PyObject
, it will be unwrapped.
Parameters:
name
- A name of a Python attribute that evaluates to a callable object.
args
- The arguments for the method call.
Returns:
A wrapper for the returned Python object.
public PyObject call(String name,
Object... args)
Call the callable Python object with the given name and arguments.
If a Java value in
args
cannot be directly converted into a Python object, a Java wrapper will be created instead.
If the Java value in
args
is a wrapped Python object of type
PyObject
, it will be unwrapped.
Parameters:
name
- A name of a Python attribute that evaluates to a callable object,
args
- The arguments for the call.
Returns:
A wrapper for the returned Python object.
createProxy
public <T> T createProxy(Class<T> type)
Create a Java proxy instance of this Python object which contains compatible methods to the ones provided in the
interface given by the
type
parameter.
Type Parameters:
T
- The interface name.
Parameters:
type
- The interface class.
Returns:
A (proxy) instance implementing the given interface.
createProxy
public Object createProxy(PyLib.CallableKind callableKind,
Class<?>... types)
Create a Java proxy instance of this Python object (or module) which contains compatible methods
(or functions) to the ones provided in the interfaces given by all the
type
parameters.
Parameters:
callableKind
- The kind of calls to be made.
types
- The interface types.
Returns:
A instance implementing the all the given interfaces which serves as a proxy for the given Python object (or module).
toString
public final String toString()
Gets a string representation of the object using the format "PyObject(pointer=<value>)".
Overrides:
toString
in class
Object
Returns:
A string representation of the object.
See Also:
getPointer()
equals
public final boolean equals(Object o)
Indicates whether some other object is "equal to" this one.
Overrides:
equals
in class
Object
Parameters:
o
- The other object.
Returns:
true
if the other object is an instance of
PyObject
and if their pointers are equal,
false
otherwise.
See Also:
getPointer()
hashCode
public final int hashCode()
Computes a hash code from this object's pointer value.
Overrides:
hashCode
in class
Object
Returns:
A hash code value for this object.
See Also:
getPointer()