Qore jni Module  1.0.1
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 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::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...
 

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 Prorgam 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::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()

◆ implement_interface() [2/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()

◆ 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 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_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