@@ -1256,10 +1256,10 @@ quality can improve to exact or derived depending on the cast type.
12561256Not every conversion is possible between Java types. Types that cannot be
12571257converted are considerer to be conversion type " none" .
12581258
1259+ Details on how method overloads are resolved are given in `Method Resolution` _.
12591260Details on the standard conversions provided by JPype are given in the section
12601261`Type Matching` _.
12611262
1262-
12631263.. _cast:
12641264
12651265Java casting
@@ -1338,65 +1338,6 @@ whenever a Java method is called, whenever a Java field is set, and whenever
13381338Python returns a value back to Java.
13391339
13401340
1341- .. _methods:
1342-
1343- Method resolution
1344- ================ =
1345-
1346- Because Java supports method overloading and Python does not , JPype wraps Java
1347- methods as a " method dispatch" . The dispatch is a collection of all of the
1348- methods from class and all of its parents which share the same name. The job
1349- of the dispatch is chose the method to call.
1350-
1351- Enforcement of the strong typing of Java must be performed at runtime within
1352- Python. Each time a method is invoked, JPype must match against the list of
1353- all possible methods that the class implements and choose the best
1354- possible overload. For this reason the methods that appear in a JPype class
1355- will not be the actual Java methods, but rather a " dispatch" whose job is
1356- deciding which method should be called based on the type of the provided
1357- arguments.
1358-
1359- If no method is found that matches the provided arguments, the method dispatch
1360- will produce a `` TypeError `` . This is the exact same outcome that Python uses
1361- when enforcing type safety within a function. If a type doesn' t match a
1362- `` TypeError `` will be produced.
1363-
1364- .. _jpype_types_method_resolution_dispatch_example:
1365-
1366- Dispatch example
1367- ----------------
1368-
1369- When JPype is unable to decide which overload of a method to call, the user
1370- must resolve the ambiguity. This is where casting comes in .
1371-
1372- Take for example the `` java.io.PrintStream`` class . This class has a variant of
1373- the print and println methods!
1374-
1375- So for the following code:
1376-
1377- .. code- block:: python
1378-
1379- java.lang.System.out.println(1 )
1380-
1381- JPype will automatically choose the `` println(long )`` method, because the Python
1382- int matches exactly with the Java long , while all the other numerical types
1383- are only " implicit" matches. However, if that is not the version you
1384- wanted to call you must cast it. In this case we will use a primitive
1385- type to construct the correct type .
1386-
1387- Changing the line thus:
1388-
1389- .. code- block:: python
1390-
1391- java.lang.System.out.println(JByte(1 )) # <--- wrap the 1 in a JByte
1392-
1393- This tells JPype to choose the byte version. When dealing with Java types, JPype follows
1394- the standard Java matching rules. Types can implicitly grow to larger types
1395- but will not shrink without an explicit cast.
1396-
1397-
1398- .. _jpype_types_primitive_types:
1399-
14001341Primitive Types
14011342============== =
14021343
@@ -2207,8 +2148,10 @@ The following Python words will trigger name mangling of a Java name:
22072148========== = ========== = ============ = ========== = ==========
22082149
22092150
2151+ .. _methods:
22102152.. _jpype_types_method_resolution:
22112153
2154+
22122155Method Resolution
22132156================ =
22142157
@@ -2269,15 +2212,36 @@ For example, consider the following code:
22692212 jlist = java.util.ArrayList()
22702213 [jlist.add(fruit) for fruit in fruits] # Cached resolution for each iteration
22712214
2272- In this case, JPype caches the resolution of the `` add(String)`` method after
2273- the first call, and subsequent calls reuse the cached result. This optimization
2274- is particularly beneficial in loops and list comprehensions.
2215+ In this case, JPype caches the resolution for `` add(str )`` to `` add(String)``
2216+ method after the first call, and subsequent calls reuse the cached result. This
2217+ optimization is particularly beneficial in loops and list comprehensions. A
2218+ call to `` add(int )`` would trigger a new resolution. The next call to `` add(str )``
2219+ will once again trigger a resolution request.
22752220
22762221** Note** : For an in - depth discussion on how this caching mechanism improves
22772222loop performance, particularly in list comprehensions, see the
22782223:ref:`Performance < miscellaneous_topics_performance> ` section.
22792224
2225+ Interactions of Custom Converters and Caching
2226+ -------------------------------------------- -
22802227
2228+ It is unwise to define very broad conversions as it can interact poorly with
2229+ caching. Suppose that one defined a convertion from all Python strings to the
2230+ Java class for date under some condition. Or perhaps an even broader convserion
2231+ was defined such as all Python classes that inherit from object .
2232+
2233+ If such overly broad conversions are applied to a function
2234+ for which both date and string were acceptable it were prefer the date
2235+ conversion when method resolution starts. As the type for the cache was string
2236+ it would attempt the out of order resolution of date first. If the
2237+ condition yield a fail it will fall back to normal method resolution, but
2238+ an overly broad conversion specialization may end up being dispatched to the
2239+ previously defined conversion.
2240+
2241+ Under normal operation of JPype the type conversions are narrowly defined such
2242+ that the cache will always yield the proper resolution. But user defined
2243+ conversions may cause unexpected results. In such a case, a cast operation to
2244+ the Java type would be required to resolve the ambiguity.
22812245
22822246
22832247
@@ -5607,6 +5571,7 @@ Result without ``--ignore E402``
56075571
56085572 jpype.startJVM()
56095573
5574+
56105575.. _miscellaneous_topics_performance:
56115576
56125577Performance
@@ -5627,8 +5592,8 @@ only those parts that need it in C.** Except this time, it's write the parts
56275592that need it in Java.
56285593
56295594Everytime an object is passed back and forth, it will incure a conversion
5630- cost.. In cases where a given object (be it a string, an object , an array, etc ... ) is passed often
5631- into Java, the object should be converted once and cached.
5595+ cost.. In cases where a given object (be it a string, an object , an array, etc
5596+ ... ) is passed often into Java, the object should be converted once and cached.
56325597For most situations, this will address speed issues.
56335598
56345599To improve speed issues, JPype has converted all of the base classes into
@@ -5681,6 +5646,7 @@ the letter "s".
56815646
56825647JPype has not been tested with other autocompletion engines such as Kite.
56835648
5649+
56845650.. _miscellaneous_topics_garbage_collection:
56855651
56865652Garbage collection
@@ -5874,6 +5840,7 @@ Tracing is useful for identifying failures that originate in one JNI call but
58745840manifest later. However, this mode produces verbose logs and is recommended
58755841only for advanced debugging.
58765842
5843+
58775844.. _instrumentation:
58785845
58795846Instrumentation
@@ -5890,6 +5857,7 @@ the name of a function or a predefined fault point. When the fault point is
58905857encountered, a `` SystemError `` is raised. Instrumentation is primarily useful
58915858for verifying the robustness of JPype' s exception handling mechanisms.
58925859
5860+
58935861.. _using- debugger:
58945862
58955863Using a Debugger
@@ -5913,6 +5881,7 @@ This configuration allows the debugger to bypass JVM-related faults while
59135881capturing legitimate errors. Additionally, disable Python' s fault handler to
59145882avoid interference with segmentation fault reporting.
59155883
5884+
59165885.. _caller sensitive:
59175886
59185887Caller- Sensitive Methods
@@ -5939,6 +5908,7 @@ JPype Known limitations
59395908This section lists those limitations that are unlikely to change, as they come
59405909from external sources.
59415910
5911+
59425912.. _miscellaneous_topics_jpype_known_limitations_annotations:
59435913
59445914Annotations
0 commit comments