A Python module to access Java classes as Python classes using JNI.
—
PyJNIus provides a comprehensive type system for generating JNI method signatures and handling type conversions between Python and Java. This system enables precise type specification for manual class definitions and supports automatic type conversion.
Functions for creating JNI method signatures from Python type specifications.
def signature(returns: type, takes: list) -> str:
"""
Generate JNI method signature string from return and parameter types.
Args:
returns: Return type (primitive, JavaClass, or array type)
takes: List of parameter types
Returns:
JNI signature string (e.g., '(Ljava/lang/String;I)V')
"""
def with_signature(returns: type, takes: list) -> callable:
"""
Decorator that generates signature and applies java_method decorator.
Args:
returns: Return type specification
takes: List of parameter type specifications
Returns:
Decorator function that applies java_method with generated signature
"""Usage Examples:
from jnius import signature, JavaClass, jint, jvoid
from jnius import autoclass
String = autoclass('java.lang.String')
# Generate signatures for different method types
sig1 = signature(jvoid, []) # '()V' - no parameters, void return
sig2 = signature(jint, []) # '()I' - no parameters, int return
sig3 = signature(String, [jint]) # '(I)Ljava/lang/String;' - int parameter, String return
sig4 = signature(jvoid, [String, jint]) # '(Ljava/lang/String;I)V' - String and int parameters
# Use with manual class definition
from jnius import JavaClass, MetaJavaClass, JavaMethod
class MyClass(JavaClass, metaclass=MetaJavaClass):
__javaclass__ = 'com/example/MyClass'
# Using generated signatures
processData = JavaMethod(signature(String, [String, jint]))
getValue = JavaMethod(signature(jint, []))Type specifiers for Java primitive types used in signature generation.
jboolean: type # Java boolean type specifier
jbyte: type # Java byte type specifier
jchar: type # Java char type specifier
jdouble: type # Java double type specifier
jfloat: type # Java float type specifier
jint: type # Java int type specifier
jlong: type # Java long type specifier
jshort: type # Java short type specifier
jvoid: type # Java void type specifierJNI Signature Mapping:
jboolean → Zjbyte → Bjchar → Cjdouble → Djfloat → Fjint → Ijlong → Jjshort → Sjvoid → VFunction for creating array type specifiers from element types.
def JArray(of_type: type) -> type:
"""
Create array type specifier for signature generation.
Args:
of_type: Element type (primitive, JavaClass, or nested array)
Returns:
Array type specifier for use in signatures
"""Usage Examples:
from jnius import JArray, jint, jdouble, signature, autoclass
String = autoclass('java.lang.String')
# Array type specifiers
int_array = JArray(jint) # int[] → [I
double_array = JArray(jdouble) # double[] → [D
string_array = JArray(String) # String[] → [Ljava/lang/String;
# Multi-dimensional arrays
int_matrix = JArray(JArray(jint)) # int[][] → [[I
# Using in signatures
array_method_sig = signature(jvoid, [int_array]) # '([I)V'
matrix_method_sig = signature(int_matrix, []) # '()[[I'
# Manual class with array methods
class ArrayProcessor(JavaClass, metaclass=MetaJavaClass):
__javaclass__ = 'com/example/ArrayProcessor'
processIntArray = JavaMethod(signature(jvoid, [int_array]))
getStringArray = JavaMethod(signature(string_array, []))
createMatrix = JavaMethod(signature(int_matrix, [jint, jint]))Alternative decorator that uses type specifications instead of raw JNI signatures.
@with_signature(returns, takes)
def method_name(self, ...): ...Usage Examples:
from jnius import PythonJavaClass, MetaJavaClass, with_signature
from jnius import jint, jvoid, autoclass
String = autoclass('java.lang.String')
class Calculator(PythonJavaClass, metaclass=MetaJavaClass):
__javaclass__ = 'com/example/Calculator'
@with_signature(jint, [jint, jint])
def add(self, a, b):
pass # Automatically gets signature '(II)I'
@with_signature(jvoid, [String])
def setName(self, name):
pass # Automatically gets signature '(Ljava/lang/String;)V'
@with_signature(String, [])
def getName(self):
pass # Automatically gets signature '()Ljava/lang/String;'
# Usage
calc = Calculator()
result = calc.add(5, 3) # 8
calc.setName('MyCalculator')
name = calc.getName() # 'MyCalculator'Internal functions used by the signature system for type analysis.
def _jni_type_spec(jclass: type) -> str:
"""
Generate JNI type specification string for a given type.
Args:
jclass: Type to analyze (JavaClass, primitive, or array)
Returns:
JNI type specification string
Note:
Internal function used by signature generation system.
"""
def get_signature(cls_tp) -> str:
"""
Convert a Java Class type to its JNI signature string.
Args:
cls_tp: Java Class object or type
Returns:
JNI signature string for the class
Note:
Used by reflection system for type analysis.
"""This function handles the conversion between Python type objects and JNI type specification strings:
L<classname>; format[<element_type> formatPyJNIus automatically handles type conversion between Python and Java:
Python to Java:
bool → booleanint → int, long, byte, short (based on range)float → double, floatstr → Stringlist → Java arrays (when appropriate)dict → Map implementations (when appropriate)Java to Python:
Manual Control: For precise control over type conversion, use explicit type specifications in signatures and cast functions for runtime type conversion.
Install with Tessl CLI
npx tessl i tessl/pypi-pyjnius