1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
#########
Reference
#########
**********
Python API
**********
This reference addresses the jpy Python module.
jpy Functions
=============
.. py:function:: create_jvm(options)
:module: jpy
Create the Java VM using the given *options* sequence of strings. Possible option strings are of the form:
+-------------------------------+------------------------------------------------------------------------------------------------------------------------+
| Option | Meaning |
+===============================+========================================================================================================================+
| ``-D<name>=<value>`` | Set a `Java system property <http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties%28%29>`_. |
| | The most important system property is ``java.class.path`` to include your Java libraries. You may also consider |
| | ``java.library.path`` if your Java code uses native code provided in shared libraries. |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------+
| ``-verbose[:class|gc|jni]`` | Enable verbose output. The options can be followed by a comma-separated list |
| | of names indicating what kind of messages will be printed by the JVM. |
| | For example, ``-verbose:gc,class`` instructs the JVM to print GC and class |
| | loading related messages. Standard names include: gc, class, and jni. |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------+
| ``-X<value>`` | Set a non-standard JVM option which usually begins with ``-X`` or an underscore. For example, |
| | the Oracle JDK/JRE supports ``-Xms`` and ``-Xmx`` options to allow programmers specify the initial |
| | and maximum heap size. Please refer to the documentation of the used Java Runtime Environment (JRE). |
+-------------------------------+------------------------------------------------------------------------------------------------------------------------+
The function throws a runtime error on failure. It has no return value.
Usage example::
jpy.create_jvm(['-Xmx512M', '-Djava.class.path=/usr/home/norman/jpy-test/classes'])
.. py:function:: destroy_jvm()
:module: jpy
Destroy the Java Virtual Machine. The function has no effect if the JVM is has not yet been created or has already
been destroyed. No return value.
.. py:function:: get_type(name, resolve=False)
:module: jpy
Return a type object for the given, fully qualified Java type *name* which is the name of a Java primitive type,
a Java class name, or a Java array type name.
Java class names must be fully qualified, e.g. ``'java.awt.Point'``. For inner classes a dollar sign is
used to separate it from its containing class, e.g. ``'java.awt.geom.Ellipse2D$Float'``.
Java array type names have a trailing opening bracket, followed by either a Java class name and a trailing semicolon
or followed by one of the primitive type indicators:
* ``'Z'``, the Java ``boolean`` type (an 8-bit Boolean value)
* ``'C'``, Java ``char`` type (a 16-bit unicode character)
* ``'B'``, Java ``byte`` type (an 8-bit signed integer number)
* ``'S'``, Java ``short`` type (a 16-bit signed integer number)
* ``'I'``, Java ``int`` type (a 32-bit signed integer number)
* ``'J'``, Java ``long`` type (a 64-bit signed integer number)
* ``'F'``, Java ``float`` type (a 32-bit floating point number)
* ``'D'``, Java ``double`` type (a 64-bit floating point number)
Examples: ``'[java.awt.Point;'`` (1d object array), ``'[[[F'`` (3d float array).
If the returned Java type has public constructors it can be used to create Java object instances in the same way
Python objects are created from their types, e.g.::
String = jpy.get_type('java.lang.String')
s = String(‘Hello jpy!’)
s = s.substring(0, 5)
The returned Java types are also used to access the type's static fields and methods::
Runtime = jpy.get_type('java.lang.Runtime')
rt = Runtime.getRuntime()
tm = rt.totalMemory()
The returned Java types have a `jclass` attribute which returns the actual Java object. This allows for using
the Java types where a Java method would expect a parameter of type `java.lang.Class`. They also have a `jclassname`
attribute, which returns the Java type associated with the 'obj' reference. The `jclassname` is the reference's
declared type (which may be a supertype), rather than the object's runtime type (as returned by
`obj.getClass().getName()`).
To instantiate Java array objects, the :py:func:`jpy.array()` function is used.
Implementation note: All types loaded so far from the Java VM are stored in the global :py:data:`jpy.types` variable.
If the requested type does not already exists in :py:data:`jpy.types`, the class is newly loaded from the Java VM.
The root class of all Java types retrieved that way is :py:class:`jpy.JType`.
Make sure that :py:func:`jpy.create_jvm()` has already been called. Otherwise the function fails with a runtime
exception.
.. py:function:: array(item_type, init)
:module: jpy
Create a Java array object for the given *item_type* and of the given initializer *init*.
*item_type* may be a *type* object as returned by the :py:func:`jpy.get_type()` function or a type *name* as it is
used for the :py:func:`jpy.get_type()` function. In addition, the name of a Java primitive type can be used:
* ``'boolean'`` (an 8-bit Boolean value)
* ``'char'`` (a 16-bit unicode character)
* ``'byte'`` (an 8-bit signed integer number)
* ``'short'`` (a 16-bit signed integer number)
* ``'int'`` (a 32-bit signed integer number)
* ``'long'`` (a 64-bit signed integer number)
* ``'float'`` (a 32-bit floating point number)
* ``'double'`` (a 64-bit floating point number)
The value for the *init* parameter may bei either an array length in the range ``0`` to ``2**31-1`` or a sequence
of objects which all must be convertible to the given *item_type*.
Make sure that :py:func:`jpy.create_jvm()` has already been called. Otherwise the function fails with a runtime
exception.
Examples:::
a = jpy.array('java.lang.String', ['A', 'B', 'C'])
a = jpy.array('int', [1, 2, 3])
a = jpy.array('float', 512)
.. py:function:: cast(jobj, type)
:module: jpy
Convert a Java object to a Java object with the given Java *type* (type object or type name, see
:py:func:`jpy.get_type()`). If *jobj* is already of *type*, *jobj* is returned. If *jobj* is an instance of
*type*, a new wrapper object will be created for this type, otherwise ``None`` is returned.
This function is useful if you need to convert the `java.util.Object` values returned e.g. by Java collections
(implementations of the `java.util.Set`, `java.util.Map`, `java.util.List` & Co.) to specific types. For example::
ArrayList = jpy.get_type('java.util.ArrayList')
File = jpy.get_type('java.io.File')
al = ArrayList()
al.add(File('/home/bibo/.jpy'))
item = al.get(0)
# item has type java.util.Object, but actually is a java.io.File
print(type(item))
item = jpy.cast(item, File)
# item has now type java.io.File
print(type(item))
Make sure that :py:func:`jpy.create_jvm()` has already been called. Otherwise the function fails with a runtime
exception.
Variables
=========
.. py:data:: types
:module: jpy
A dictionary that maps Java class names to the respective Python type objects (wrapped Java classes).
You should never modify the value of this variable nor directly modify the dictionary's contents.
.. py:data:: type_callbacks
:module: jpy
Contains callbacks which are called before jpy translates Java methods to Python methods while Java classes are
being loaded. These callbacks can be used to annotate Java methods so that jpy can better translate them to
Python. This is a powerful but advanced jpy feature that you usually don't have to use.
Consider a Java method::
double[] readData(long offset, int length, double[] data);
of some Java class ``Reader``. From the method's documentation we know that if we pass ``null`` for *data*,
it will create a new array of the given length, read data into it and the return that instance.
If we pass an existing array it will be reused instead. From plain Java class introspection, jpy can neither
detect if a primitive array parameter is modified by a method and/or whether it shall serve as the method's
return value.
To overcome the problem of such semantics inherent to a Java method implementation, jpy uses a dictionary
``type_callbacks`` in which you can register a Java class name with a callable of following signature: ::
callback(type, method)
This can be used to equip specific Java methods of a class with additional information while the Java class
is being loaded from the Java VM. *type* is the Java class and *method* is the current class method being
loaded. *method* is of type :py:class:`jpy.JMethod`. The callback should return either ``True`` or ``False``.
If it returns ``False``, jpy will not add the given method to the Python version of the Java class.
Here is an example: ::
def annotate_Reader_readData_methods(type, method):
if method.name == 'readData' and method.param_count == 3:
param_type_str = str(method.get_param_type(1))
if param_type_str == "<class '[I'>" || param_type_str == "<class '[D'>":
method.set_param_mutable(2, True)
method.set_param_return(2, True)
return True
class_name = 'com.acme.Reader'
jpy.type_callbacks[class_name] = annotate_Reader_readData_methods
# This will invoke the callback above
Reader = jpy.get_type(class_name)
Once a method parameter is annotated that way, jpy can transfer the semantics of a Java method to Python.
For example::
import numpy as np
r = Reader('test.tif')
a = np.array(1024, np.dtype=np.float64)
a = r.read(0, len(a), a)
r.close()
Here a call to the ``read`` method will modify the numpy array's content as desired and return the
same array instance as indicated by the Java method's specification.
.. py:data:: type_translations
:module: jpy
Contains callbacks which are called when instantiating a Python object from a Java object.
After the standard wrapping of the Java object as a Python object, the Java type name is looked up in this
dictionary. If the returned item is a callable, the callable is called with the JPy object as an argument,
and the callable's result is returned to the user.
.. py:data:: VerboseExceptions.enabled
:module: jpy
If set to true, then jpy will produce more verbose exception messages; which include the full Java stack trace.
If set to false, then jpy produces exceptions using only the underlying Java exception's toString method.
.. py:data:: diag
:module: jpy
An object used to control output of diagnostic information for debugging. This variable is only useful for jpy
modification and further development.
.. py:data:: diag.flags
:module: jpy
Integer bit-combination of diagnostic flags (see following F_* constants).
If this value is not zero, diagnostic messages are printed to the standard output stream for any subsequent
jpy library calls. Its default value is ``jpy.diag.F_OFF`` which is zero.
For example::
jpy.diag.flags = jpy.diag.F_EXEC + jpy.diag.F_JVM
The following flags are defined:
* ``F_OFF`` - Don't print any diagnostic messages
* ``F_ERR`` - Errors: print diagnostic information when erroneous states are detected
* ``F_TYPE`` - Type resolution: print diagnostic messages while generating Python classes from Java classes
* ``F_METH`` - Method resolution: print diagnostic messages while resolving Java overloaded methods
* ``F_EXEC`` - Execution: print diagnostic messages when Java code is executed
* ``F_MEM`` - Memory: print diagnostic messages when wrapped Java objects are allocated/deallocated
* ``F_JVM`` - JVM: print diagnostic information usage of the Java VM Invocation API
* ``F_ALL`` - Print all possible diagnostic messages
Types
=====
You will never have to use the following type directly. But it may be of use to know where they come from when they are
referred to, e.g. in error messages.
.. py:class:: JType
:module: jpy
This type is the base class for all type representing Java classes. It is actually a meta-type used to dynamically
create Python type instances from loaded Java classes. Such derived types are returned by
:py:func:`jpy.get_type` instead or can be directly looked up in :py:data:`jpy.types`.
.. py:class:: JOverloadedMethod
:module: jpy
This type represents an overloaded Java method. It is composed of one or more :py:class:`jpy.JMethod` objects.
.. py:class:: JMethod
:module: jpy
This type represents a Java method. It is part of a :py:class:`jpy.JOverloadedMethod`.
.. py:attribute:: name
The method's name. Read-only attribute.
.. py:attribute:: return_type
The method's return type. Read-only attribute.
.. py:attribute:: param_count
The method's parameter count. Read-only attribute.
.. py:method:: JMethod.get_param_type(i) -> type
Get the type of the *i*-th Java method parameter.
.. py:method:: JMethod.is_param_return(i) -> bool
Return ``True`` if arguments passed to the *i*-th Java method parameter will be the return value of the method, ``False`` otherwise.
.. py:method:: JMethod.set_param_return(i, value)
Set if arguments passed to the *i*-th Java method parameter will be the return value of the method, with *value* being a Boolean.
.. py:method:: JMethod.is_param_output(i) -> bool
Return ``True`` if the arguments passed to the *i*-th Java method parameter is a mere output (and not read from), ``False`` otherwise.
.. py:method:: JMethod.set_param_output(i, value)
Set if arguments passed to the *i*-th Java method parameter is a mere output (and not read from), with *value* being a Boolean.
Used to optimise Python buffer to Java array parameter passing.
.. py:method:: JMethod.is_param_mutable(i) -> bool
Return ``True`` if the arguments passed to the *i*-th Java method parameter is mutable, ``False`` otherwise.
.. py:method:: JMethod.set_param_mutable(i, value)
Set if arguments passed to the *i*-th Java method parameter is mutable, with *value* being a Boolean.
.. py:class:: JField
:module: jpy
This type represents is used to represent Java class fields.
Type Conversions
================
This section describes the type possible type conversions made by jpy when Python values are passed as arguments
to Java typed parameters. In the tables given below are the generated match values ranging from (types never match)
to 100 (full match) when comparing a given Java parameter type (rows) with a provided Python value (columns). These
match values are also used for finding the best matching Java method overload for a given Python argument tuple.
Java primitive types
--------------------
+--------------+--------------+----------+---------+------------+--------+
| | ``NoneType`` | ``bool`` | ``int`` | ``float`` | number |
+==============+==============+==========+=========+============+========+
| ``boolean`` | 1 | 100 | 10 | 0 | 0 |
+--------------+--------------+----------+---------+------------+--------+
| ``char`` | 0 | 10 | 100 | 0 | 0 |
+--------------+--------------+----------+---------+------------+--------+
| ``byte`` | 0 | 10 | 100 | 0 | 0 |
+--------------+--------------+----------+---------+------------+--------+
| ``short`` | 0 | 10 | 100 | 0 | 0 |
+--------------+--------------+----------+---------+------------+--------+
| ``int`` | 0 | 10 | 100 | 0 | 0 |
+--------------+--------------+----------+---------+------------+--------+
| ``long`` | 0 | 10 | 100 | 0 | 0 |
+--------------+--------------+----------+---------+------------+--------+
| ``float`` | 0 | 1 | 10 | 90 | 50 |
+--------------+--------------+----------+---------+------------+--------+
| ``double`` | 0 | 1 | 10 | 100 | 50 |
+--------------+--------------+----------+---------+------------+--------+
Java object types
-----------------
+-------------------------+--------------+----------+---------+------------+---------+
| | ``NoneType`` | ``bool`` | ``int`` | ``float`` | ``str`` |
+=========================+==============+==========+=========+============+=========+
| ``java.lang.Boolean`` | 1 | 100 | 10 | 0 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Character`` | 1 | 10 | 100 | 0 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Byte`` | 1 | 10 | 100 | 0 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Short`` | 1 | 10 | 100 | 0 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Integer`` | 1 | 10 | 100 | 0 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Long`` | 1 | 10 | 100 | 0 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Float`` | 1 | 1 | 10 | 90 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Double`` | 1 | 1 | 10 | 100 | 0 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.String`` | 1 | 0 | 0 | 0 | 100 |
+-------------------------+--------------+----------+---------+------------+---------+
| ``java.lang.Object`` | 1 | 10 | 10 | 10 | 10 |
+-------------------------+--------------+----------+---------+------------+---------+jpy
Java primitive array types
--------------------------
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| | ``NoneType`` | seq | buf('b') | buf('B') | buf('u') | buf('h') | buf('H') | buf('i') | buf('I') | buf('l') | buf('L') | buf('q') | buf('Q') | buf('f') | buf('d') |
+================+==============+=====+==========+==========+==========+==========+==========+==========+==========+==========+==========+==========+==========+==========+==========+
| ``boolean[]`` | 1 | 10 | 100 | 100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| ``char[]`` | 1 | 10 | 0 | 0 | 100 | 80 | 90 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| ``byte[]`` | 1 | 10 | 100 | 90 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| ``short[]`` | 1 | 10 | 0 | 0 | 0 | 100 | 90 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| ``int[]`` | 1 | 10 | 0 | 0 | 0 | 0 | 0 | 100 | 90 | 100 | 90 | 0 | 0 | 0 | 0 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| ``long[]`` | 1 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 100 | 90 | 0 | 0 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| ``float[]`` | 1 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 100 | 0 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| ``double[]`` | 1 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 100 |
+----------------+--------------+-----+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
If a python buffer is passed as argument to a primitive array parameter, but it doesn't match the buffer types
given above, the a match value of 10 applies, as long as the item size of a buffer matches the Java array item size.
Java object array types
-----------------------
For String arrays, if a sequence is matched with a value of 80 if all the elements in the sequence are Python strings.
todo
********
Java API
********
jpy's Java API documentation has been generated from Java source code using the javadoc tool.
It can be found `here <_static/java-apidocs/index.html>`_.
|