Qore jni Module  1.2.0
functions.

Functions

Jni::org::qore::jni::Class Jni::org::qore::jni::define_class (string name, binary bytecode)
 Creates a class object from bytecode data. More...
 
string Jni::org::qore::jni::get_version ()
 Returns the version of the JNI API. More...
 
Jni::java::lang::Object Jni::org::qore::jni::implement_interface (Jni::org::qore::jni::QoreInvocationHandler invocationHandler, Jni::java::lang::Class cls)
 Creates a Java object that implements given interface using an invocation handler. More...
 
Jni::java::lang::Object Jni::org::qore::jni::implement_interface (Jni::java::lang::ClassLoader classLoader, Jni::org::qore::jni::QoreInvocationHandler invocationHandler, Jni::java::lang::Class cls)
 Creates a Java object that implements given interface using an invocation handler using an explicit class loader. More...
 
Jni::java::lang::Object Jni::org::qore::jni::invoke (Jni::java::lang::reflect::Method method, *Jni::java::lang::Object object,...)
 Invokes a method with the given arguments in a virtual way; meaning that the method in the most derived class is executed; not necessarily the method passed as an argument. More...
 
Jni::java::lang::Object Jni::org::qore::jni::invoke_nonvirtual (Jni::java::lang::reflect::Method method, *Jni::java::lang::Object object,...)
 Invokes a method with the given arguments in a non-virtual way; meaning that even if the object provided is a child class, the method given in the first argument is executed. More...
 
Jni::java::lang::Class Jni::org::qore::jni::load_class (string name)
 Loads a Java class with given name. More...
 
Jni::org::qore::jni::JavaArray Jni::org::qore::jni::new_array (Jni::java::lang::Class cls, int size)
 Allocates a new Java array. More...
 
 Jni::org::qore::jni::set_save_object_callback (*code save_object_callback)
 Sets the "save object" callback for Qore objects created from Java code. More...
 

Detailed Description

Function Documentation

◆ define_class()

Jni::org::qore::jni::Class Jni::org::qore::jni::define_class ( string  name,
binary  bytecode 
)

Creates a class object from bytecode data.

Example:
java::lang::Class cls = define_class(name, bytecode);
Parameters
namethe name of the class (ex: "org/qore/my_package/ClassName")
bytecodethe byte code of the class
Note
the class will be created using the classloader for the containing Program object
Since
jni 1.1

◆ get_version()

string Jni::org::qore::jni::get_version ( )

Returns the version of the JNI API.

Returns
the version of the JNI API

◆ implement_interface() [1/2]

Jni::java::lang::Object Jni::org::qore::jni::implement_interface ( Jni::java::lang::ClassLoader  classLoader,
Jni::org::qore::jni::QoreInvocationHandler  invocationHandler,
Jni::java::lang::Class  cls 
)

Creates a Java object that implements given interface using an invocation handler using an explicit class loader.

Parameters
classLoaderthe class loader that loaded the interface
invocationHandlerthe invocation handler
clsthe interface to implement
Returns
a Java object that implements the interface by calling the invocation handler
Example:
ClassLoader classLoader = ...;
Class runnableClass = load_class("java/lang/Runnable");
QoreInvocationHandler h(any sub(Method m, *list args) { doRun(); });
Object runnableInstance = Jni::implement_interface(classLoader, h, runnableClass);
# runnableInstance now has a Java method void run() that calls Qore function doRun()

◆ implement_interface() [2/2]

Jni::java::lang::Object Jni::org::qore::jni::implement_interface ( Jni::org::qore::jni::QoreInvocationHandler  invocationHandler,
Jni::java::lang::Class  cls 
)

Creates a Java object that implements given interface using an invocation handler.

Parameters
invocationHandlerthe invocation handler
clsthe interface to implement
Returns
a Java object that implements the interface by calling the invocation handler
Example:
Class runnableClass = Jni::load_class("java/lang/Runnable");
QoreInvocationHandler h(any sub(Method m, *list args) { doRun(); });
Object runnableInstance = Jni::implement_interface(h, runnableClass);
# runnableInstance now has a Java method void run() that calls Qore function doRun()

◆ invoke()

Jni::java::lang::Object Jni::org::qore::jni::invoke ( Jni::java::lang::reflect::Method  method,
*Jni::java::lang::Object  object,
  ... 
)

Invokes a method with the given arguments in a virtual way; meaning that the method in the most derived class is executed; not necessarily the method passed as an argument.

Parameters
methodthe method to invoke
objectthe object to use to invoke the method; for static methods, this argument can be nothing
Note
  • reflection is not used, so exceptions are thrown directly (i.e. not wrapped as java.lang.reflect.InvocationTargetException objects)
  • for static method invocations, this function is identical to invoke_nonvirtual()

◆ invoke_nonvirtual()

Jni::java::lang::Object Jni::org::qore::jni::invoke_nonvirtual ( Jni::java::lang::reflect::Method  method,
*Jni::java::lang::Object  object,
  ... 
)

Invokes a method with the given arguments in a non-virtual way; meaning that even if the object provided is a child class, the method given in the first argument is executed.

Parameters
methodthe method to invoke
objectthe object to use to invoke the method; for static methods, this argument can be nothing
Note
  • reflection is not used, so exceptions are thrown directly (i.e. not wrapped as java.lang.reflect.InvocationTargetException objects)
  • for static method invocations, this function is identical to invoke()

◆ load_class()

Jni::java::lang::Class Jni::org::qore::jni::load_class ( string  name)

Loads a Java class with given name.

Parameters
namethe name of the class to load in internal ("java/lang/String") format
Returns
the loaded class
Exceptions
JNI-ERRORif the class cannot be loaded
Example:
Class c = Jni::load_class("java/lang/String");

◆ new_array()

Jni::org::qore::jni::JavaArray Jni::org::qore::jni::new_array ( Jni::java::lang::Class  cls,
int  size 
)

Allocates a new Java array.

Parameters
clsthe Class of the component type of the Array
sizethe size of the array to allocate
Returns
the allocated array

◆ set_save_object_callback()

Jni::org::qore::jni::set_save_object_callback ( *code  save_object_callback)

Sets the "save object" callback for Qore objects created from Java code.

Restrictions:
Qore::PO_NO_PROCESS_CONTROL
Example:
hash<string, object> object_cache;
code callback = sub (object obj) {
# save object in object cache, so it doesn't go out of scope
object_cache{obj.uniqueHash()} = obj;
}
Parameters
save_object_callbackthe callback to save any Qore objects created in Java code, must take an argument of type object

Due to the differences in garbage collection approaches between Qore and Java, Qore objects must be managed with a deterministic life cycle; Java objects have only weak references to Qore objects due to the lack of destructors in Java and the lack of determinism in the JVM for object lifecycle management.

The callback set here will be called any time a Qore object is created from Java code; if no callback is set, then the standard thread-local implementation is used where Qore objects are saved in a thread-local hash.

See also
Managing the Lifecycle of Qore objects from Java for more information
Jni::org::qore::jni::set_save_object_callback
set_save_object_callback(*code save_object_callback)
Sets the "save object" callback for Qore objects created from Java code.
Jni::org::qore::jni::implement_interface
Jni::java::lang::Object implement_interface(Jni::org::qore::jni::QoreInvocationHandler invocationHandler, Jni::java::lang::Class cls)
Creates a Java object that implements given interface using an invocation handler.
Jni::org::qore::jni::define_class
Jni::org::qore::jni::Class define_class(string name, binary bytecode)
Creates a class object from bytecode data.
Jni::org::qore::jni::load_class
Jni::java::lang::Class load_class(string name)
Loads a Java class with given name.