Package-level declarations
High-level typed access to Java methods, fields and arrays - Kotlin lambdas, ReadWriteProperty delegates and scoped DSLs. Everything here stays context(JniEnv, MemScope) / NativePlacement-friendly.
Methods
Fetch a method ID once, then call it as an ordinary function with reified types - no jArgs, no call<Type>Method:
// requires JniEnv and a NativePlacement / AutofreeScope
val add = clazz.methodId("add", "(II)I").asMethod<Int, Int, Int>()
val result = obj.add(2, 3) // 5asMethod - instance methods (0 to 10 typed parameters, any return type).
asStaticMethod - static methods; the receiver is the class.
asConstructor - constructors; the receiver is the class, returns
JObject?.asNonvirtualMethod - non-virtual dispatch; the first parameter is the
JClassto dispatch on.
Primitives in the type arguments must be non-null; reference types must be nullable.
Fields
Get or set a field through a Kotlin ReadWriteProperty - bind it once and use it like a property:
// requires JniEnv and an AutofreeScope
val jName: JString? by obj.stringField("name")
var name: String? by obj.stringField("name").asKotlin()Instance: objectField, stringField, booleanField…doubleField, and generic field. Static: staticObjectField, staticStringField, staticIntField… and staticField.
Each has two forms:
Kotlin
Stringname - encodes with String.modifiedUtf8 and throws if the field is missing.CValuesRef<ByteVar>name - returnsnullwhen the field is not found; passname.utf8orname.modifiedUtf8.
Convert an object delegate to a specific type with asType, and to a Kotlin String (reading/writing JString?) with asKotlin. Fields can also be bound by a ready JFieldID.
Arrays and strings, in place
Instead of getElements → mutate → releaseElements(mode) pairs, mutate the body in a scope and choose how changes are applied:
val javaArray: JIntArray = …
javaArray.modify({ /* onError */ }) { carray, isCopy ->
// this: ModifyingArrayScope
carray[0] = 42
commit() // copy back, keep the buffer
// finalize() - copy back and free
// abort() - discard changes
}modify on every
J<Primitive>Array(withcommit()/finalize()/abort()on ModifyingArrayScope), plus modifyCritical for pinning.modifyChars / modifyUTFChars / modifyCritical on a
JString.
Functions
Alias for releasing an array with ApplyChangesMode.Commit.
Returns lambda which calls newObject and returns the result.
fun ReadWriteProperty<Any?, JString?>.asKotlin(): ReadWriteProperty<Any?, String?>
Simplifies usage of stringField and staticStringField allowing to pass Kotlin strings directly.
Unifies usage of call<type>Method methods, returning lambda which internally calls method with appropriate type and returns the result.
Unifies usage of call<type>Method methods, returning lambda which internally calls method with appropriate type setting arguments with jArgs and returns the result.
Unifies usage of callNonvirtual<type>Method methods, returning lambda which internally calls method with appropriate type and returns the result.
Unifies usage of callNonvirtual<type>Method methods, returning lambda which internally calls method with appropriate type setting arguments with jArgs and returns the result.
Unifies usage of callStatic<type>Method methods, returning lambda which internally calls method with appropriate type and returns the result.
Unifies usage of callStatic<type>Method methods, returning lambda which internally calls method with appropriate type setting arguments with jArgs and returns the result.
inline fun JObject.booleanField(name: String): ReadWriteProperty<Any?, Boolean>
inline fun JObject.booleanField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Boolean>?
Returns a ReadWriteProperty for boolean field of the object with the specified name.
Returns a ReadWriteProperty for boolean field of the object with the specified fieldId.
inline fun JObject.byteField(name: String): ReadWriteProperty<Any?, Byte>
inline fun JObject.byteField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Byte>?
Returns a ReadWriteProperty for byte field of the object with the specified name.
Returns a ReadWriteProperty for byte field of the object with the specified fieldId.
inline fun JObject.charField(name: String): ReadWriteProperty<Any?, Char>
inline fun JObject.charField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Char>?
Returns a ReadWriteProperty for char field of the object with the specified name.
Returns a ReadWriteProperty for char field of the object with the specified fieldId.
Alias for releasing an array with ApplyChangesMode.Commit.
inline fun JObject.doubleField(name: String): ReadWriteProperty<Any?, Double>
inline fun JObject.doubleField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Double>?
Returns a ReadWriteProperty for double field of the object with the specified name.
Returns a ReadWriteProperty for double field of the object with the specified fieldId.
Alias for releasing an array with ApplyChangesMode.Commit.
inline fun JObject.floatField(name: String): ReadWriteProperty<Any?, Float>
inline fun JObject.floatField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Float>?
Returns a ReadWriteProperty for float field of the object with the specified name.
Returns a ReadWriteProperty for float field of the object with the specified fieldId.
inline fun JObject.intField(name: String): ReadWriteProperty<Any?, Int>
inline fun JObject.intField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Int>?
Returns a ReadWriteProperty for int field of the object with the specified name.
Returns a ReadWriteProperty for int field of the object with the specified fieldId.
inline fun JObject.longField(name: String): ReadWriteProperty<Any?, Long>
inline fun JObject.longField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Long>?
Returns a ReadWriteProperty for long field of the object with the specified name.
Returns a ReadWriteProperty for long field of the object with the specified fieldId.
inline fun JBooleanArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<UByteVar>, isCopy: Boolean) -> Unit)
Alias for working with boolean array's elements via getElements and releasing it with releaseElements.
inline fun JByteArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<ByteVar>, isCopy: Boolean) -> Unit)
Alias for working with byte array's elements via JByteArray.getElements and releasing it with JByteArray.releaseElements.
inline fun JCharArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<UShortVar>, isCopy: Boolean) -> Unit)
Alias for working with char array's elements via JCharArray.getElements and releasing it with JCharArray.releaseElements.
inline fun JDoubleArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<DoubleVar>, isCopy: Boolean) -> Unit)
Alias for working with double array's elements via JDoubleArray.getElements and releasing it with JDoubleArray.releaseElements.
inline fun JFloatArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<FloatVar>, isCopy: Boolean) -> Unit)
Alias for working with float array's elements via JFloatArray.getElements and releasing it with JFloatArray.releaseElements.
inline fun JIntArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<IntVar>, isCopy: Boolean) -> Unit)
Alias for working with int array's elements via JIntArray.getElements and releasing it with JIntArray.releaseElements.
inline fun JLongArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<LongVar>, isCopy: Boolean) -> Unit)
Alias for working with long array's elements via JLongArray.getElements and releasing it with JLongArray.releaseElements.
inline fun JShortArray.modify(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<ShortVar>, isCopy: Boolean) -> Unit)
Alias for working with short array's elements via JShortArray.getElements and releasing it with JShortArray.releaseElements.
inline fun JString.modifyChars(onError: () -> Unit, block: (CArrayPointer<UShortVar>, isCopy: Boolean) -> Unit)
Alias for safely retrieving a string's chars via getChars and releasing it with releaseChars.
inline fun <T : CPrimitiveVar> JPrimitiveArray<T>.modifyCritical(onError: () -> Unit, block: ModifyingArrayScope.(CArrayPointer<*>, isCopy: Boolean) -> Unit)
Alias for working with primitive array's elements via JPrimitiveArray.getElementsCritical. and releasing it with JPrimitiveArray.releaseElementsCritical.
inline fun JString.modifyCritical(onError: () -> Unit, block: (CArrayPointer<UShortVar>, isCopy: Boolean) -> Unit)
Alias for safely retrieving a string's chars via getCharsCritical and releaseCharsCritical.
inline fun JString.modifyUTFChars(onError: () -> Unit, block: (CArrayPointer<ByteVar>, isCopy: Boolean) -> Unit)
Alias for safely retrieving a string's bytes via getUTFChars and releasing it with releaseUTFChars.
Returns a ReadWriteProperty for Object field of the object with the specified fieldId.
inline fun JObject.objectField(name: String, sig: String): ReadWriteProperty<Any?, JObject?>
inline fun JObject.objectField(name: CValuesRef<ByteVar>, sig: CValuesRef<ByteVar>): ReadWriteProperty<Any?, JObject?>?
Returns a ReadWriteProperty for Object field of the object with the specified name and sig.
inline fun JObject.shortField(name: String): ReadWriteProperty<Any?, Short>
inline fun JObject.shortField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Short>?
Returns a ReadWriteProperty for short field of the object with the specified name.
Returns a ReadWriteProperty for short field of the object with the specified fieldId.
inline fun JClass.staticBooleanField(name: String): ReadWriteProperty<Any?, Boolean>
inline fun JClass.staticBooleanField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Boolean>?
Returns a ReadWriteProperty for static boolean field of the class with the specified name.
fun JClass.staticBooleanField(fieldId: JFieldID): ReadWriteProperty<Any?, Boolean>
Returns a ReadWriteProperty for static boolean field of the class with the specified fieldId.
inline fun JClass.staticByteField(name: String): ReadWriteProperty<Any?, Byte>
inline fun JClass.staticByteField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Byte>?
Returns a ReadWriteProperty for static byte field of the class with the specified name.
Returns a ReadWriteProperty for static byte field of the class with the specified fieldId.
inline fun JClass.staticCharField(name: String): ReadWriteProperty<Any?, Char>
inline fun JClass.staticCharField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Char>?
Returns a ReadWriteProperty for static char field of the class with the specified name.
Returns a ReadWriteProperty for static char field of the class with the specified fieldId.
inline fun JClass.staticDoubleField(name: String): ReadWriteProperty<Any?, Double>
inline fun JClass.staticDoubleField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Double>?
Returns a ReadWriteProperty for static double field of the class with the specified name.
fun JClass.staticDoubleField(fieldId: JFieldID): ReadWriteProperty<Any?, Double>
Returns a ReadWriteProperty for static double field of the class with the specified fieldId.
inline fun <T> JClass.staticField(fieldId: JFieldID): ReadWriteProperty<Any?, T>
Unifies usage of static*Field() functions.
inline fun JClass.staticFloatField(name: String): ReadWriteProperty<Any?, Float>
inline fun JClass.staticFloatField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Float>?
Returns a ReadWriteProperty for static float field of the class with the specified name.
Returns a ReadWriteProperty for static float field of the class with the specified fieldId.
inline fun JClass.staticIntField(name: String): ReadWriteProperty<Any?, Int>
inline fun JClass.staticIntField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Int>?
Returns a ReadWriteProperty for static int field of the class with the specified name.
Returns a ReadWriteProperty for static int field of the class with the specified fieldId.
inline fun JClass.staticLongField(name: String): ReadWriteProperty<Any?, Long>
inline fun JClass.staticLongField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Long>?
Returns a ReadWriteProperty for static long field of the class with the specified name.
Returns a ReadWriteProperty for static long field of the class with the specified fieldId.
fun JClass.staticObjectField(fieldId: JFieldID): ReadWriteProperty<Any?, JObject?>
Returns a ReadWriteProperty for static Object field of the class with the specified fieldId.
inline fun JClass.staticObjectField(name: String, sig: String): ReadWriteProperty<Any?, JObject?>
inline fun JClass.staticObjectField(name: CValuesRef<ByteVar>, sig: CValuesRef<ByteVar>): ReadWriteProperty<Any?, JObject?>?
Returns a ReadWriteProperty for static Object field of the class with the specified name and sig.
inline fun JClass.staticShortField(name: String): ReadWriteProperty<Any?, Short>
inline fun JClass.staticShortField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, Short>?
Returns a ReadWriteProperty for static short field of the class with the specified name.
Returns a ReadWriteProperty for static short field of the class with the specified fieldId.
inline fun JClass.staticStringField(name: String): ReadWriteProperty<Any?, JString?>
inline fun JClass.staticStringField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, JString?>?
Alias for staticObjectField with sig = "Ljava/lang/String;".
inline fun JObject.stringField(name: String): ReadWriteProperty<Any?, JString?>
inline fun JObject.stringField(name: CValuesRef<ByteVar>): ReadWriteProperty<Any?, JString?>?
Alias for objectField with sig = "Ljava/lang/String;".