Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gciatto/dummy-kt-jpype
https://github.com/gciatto/dummy-kt-jpype
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/gciatto/dummy-kt-jpype
- Owner: gciatto
- Created: 2021-09-05T12:29:44.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-12-05T08:48:06.000Z (about 1 year ago)
- Last Synced: 2024-10-23T07:50:51.451Z (3 months ago)
- Language: Kotlin
- Size: 62.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dummy project for assessing Kotlin-Python interoperability via JPype
## Problem (Hypothesis)
Kotlin-generated nested anonymous objects do not work as expected.
Consider the following Java code creating a lazy int range generator
```java
package dummy.kt.jpype;import java.util.Iterator;
public class IterablesJava {
public static Iterable intRange(int min, int max) {
return new Iterable() {
@Override
public Iterator iterator() {
return new Iterator() {
private int x = min;@Override
public boolean hasNext() {
return x < max;
}@Override
public Integer next() {
return x++;
}
};
}
};
}
}
```
and its Kotlin equivalent:
```kotlin
// file named Iterables.kt => Java class is named IterablesKt
package dummy.kt.jpypefun intRange(min: Int, max: Int): Iterable =
object : Iterable {
override fun iterator(): Iterator =
object : Iterator {
private var x = minoverride fun hasNext(): Boolean = x < max
override fun next(): Int = x++
}
}
```
Then, the following Python script using both `intRange` methods from Python:
```python
import jpype
import jpype.importsimport jpype
import jpype.imports# starting jvm
from dummy.kt.jpype import IterablesJava, IterablesKt
iterable = IterablesJava.intRange(1, 10) # works (calling the Java version)
iterator = iterable.iterator() # works
print(iterator.next()) # 1iterable = IterablesKt.intRange(1, 10) # works (calling the Python version)
iterator = iterable.iterator() # AttributeError: 'java.lang.Object$Anonymous' object has no attribute 'iterator'
print(iterator.next()) # not executed
```
fails when executing the Kotlin version of `intRange`.By inspecting the code, I'd say that JPype is struggling in correctly managing nested anonymous objects when the corresponding code is generated by Kotlin.
By inspecting the bytecode, I also note that the object returned by `iterable.iterator()` should not be of type `java.lang.Object$Anonymous` -- as reported by the exception message --, but rather `IterablesKt$intRange$1$iterator$1` (I guess).
### How to reproduce
1. Re-compile the JVM code by `./gradlew shadowJar`
- This should create a fat jar containing both the Java- and Kotlin-based implementations of `intRange` in `build/libs`
+ First run make take a while since Gradle and dependencies should be downloaded2. Restore Python dependencies via `pip install -r requirements.txt`
3. Run the `main.py` script via `python main.py`