libcommonc++  0.7
JavaContext Class Reference

When the Invocation Interface is being used (that is, when a C++ program calls into Java code), the getContext() method of the JavaVirtualMachine class is used to obtain an appropriate JavaContext object. More...

#include <JavaContext.h++>

Public Member Functions

 JavaContext (JNIEnv *env)
 Construct a new JavaContext for the given JNI environment pointer. More...
 
 ~JavaContext ()
 Destructor. More...
 
jclass findClass (const String &name)
 Look up a class by name. More...
 
jclass defineClass (const String &name, const byte_t *buf, size_t length)
 Define a Java class from a buffer of raw bytecode. More...
 
jmethodID findMethod (jclass clazz, const String &signature)
 Look up a method or constructor by signature. More...
 
jobject createObject (jclass clazz, jmethodID constructor,...)
 Instantiate a new Java object. More...
 
jobject createObjectVA (jclass clazz, jmethodID constructor, va_list args)
 Instantiate a new Java object. More...
 
void callVoidMethod (jobject object, jmethodID method,...)
 Call a Java method that returns void. More...
 
jobject callObjectMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a Java Object. More...
 
String callStringMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a java.lang.String. More...
 
bool callBooleanMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a boolean. More...
 
byte_t callByteMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a byte. More...
 
char16_t callCharMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a char. More...
 
int16_t callShortMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a short. More...
 
int32_t callIntMethod (jobject object, jmethodID method,...)
 Call a Java method that returns an int. More...
 
int64_t callLongMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a long. More...
 
float callFloatMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a float. More...
 
double callDoubleMethod (jobject object, jmethodID method,...)
 Call a Java method that returns a double. More...
 
void callNonvirtualVoidMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns void. More...
 
jobject callNonvirtualObjectMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a Java object. More...
 
bool callNonvirtualBooleanMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a boolean. More...
 
byte_t callNonvirtualByteMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a byte. More...
 
char16_t callNonvirtualCharMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a char. More...
 
int16_t callNonvirtualShortMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a short. More...
 
int32_t callNonvirtualIntMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns an int. More...
 
int64_t callNonvirtualLongMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a long. More...
 
float callNonvirtualFloatMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a float. More...
 
double callNonvirtualDoubleMethod (jobject object, jclass clazz, jmethodID method,...)
 Call a non-virtual Java method that returns a double. More...
 
void callStaticVoidMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns void. More...
 
jobject callStaticObjectMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a Java Object. More...
 
String callStaticStringMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a java.lang.String. More...
 
bool callStaticBooleanMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a boolean. More...
 
byte_t callStaticByteMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a byte. More...
 
char16_t callStaticCharMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a char. More...
 
int16_t callStaticShortMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a short. More...
 
int32_t callStaticIntMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns an int. More...
 
int64_t callStaticLongMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a long. More...
 
float callStaticFloatMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a float. More...
 
double callStaticDoubleMethod (jclass clazz, jmethodID method,...)
 Call a static Java method that returns a double. More...
 
bool checkException (bool verbose=false)
 Determine if a Java exception has been thrown in the current thread. More...
 
void clearException ()
 Clear any pending Java exception that is currently being thrown in the current thread. More...
 
jthrowable getException ()
 Get the pending Java exception, if one is currently being thrown in the current thread. More...
 
bool throwException (jclass exception, const String &message)
 Throw a Java exception. More...
 
jfieldID findField (jclass clazz, const String &signature)
 Look up a field by signature. More...
 
jobject getObjectField (jobject object, jfieldID field)
 Get the value of an Object field in an Object. More...
 
bool getBooleanField (jobject object, jfieldID field)
 Get the value of an boolean field in an Object. More...
 
byte_t getByteField (jobject object, jfieldID field)
 Get the value of a byte field in an Object. More...
 
char16_t getCharField (jobject object, jfieldID field)
 Get the value of a char field in an Object. More...
 
int16_t getShortField (jobject object, jfieldID field)
 Get the value of a short field in an Object. More...
 
int32_t getIntField (jobject object, jfieldID field)
 Get the value of an int field in an Object. More...
 
int64_t getLongField (jobject object, jfieldID field)
 Get the value of a long field in an Object. More...
 
float getFloatField (jobject object, jfieldID field)
 Get the value of a float field in an Object. More...
 
double getDoubleField (jobject object, jfieldID field)
 Get the value of a double field in an Object. More...
 
String getStringField (jobject object, jfieldID field)
 Get the value of a java.lang.String field in an Object. More...
 
jobject getStaticObjectField (jclass clazz, jfieldID field)
 Get the value of a static Object field in a class. More...
 
bool getStaticBooleanField (jclass clazz, jfieldID field)
 Get the value of a static boolean field in a class. More...
 
byte_t getStaticByteField (jclass clazz, jfieldID field)
 Get the value of a static byte field in a class. More...
 
char16_t getStaticCharField (jclass clazz, jfieldID field)
 Get the value of a static char field in a class. More...
 
int16_t getStaticShortField (jclass clazz, jfieldID field)
 Get the value of a static short field in a class. More...
 
int32_t getStaticIntField (jclass clazz, jfieldID field)
 Get the value of a static int field in a class. More...
 
int64_t getStaticLongField (jclass clazz, jfieldID field)
 Get the value of a static long field in a class. More...
 
float getStaticFloatField (jclass clazz, jfieldID field)
 Get the value of a static float field in a class. More...
 
double getStaticDoubleField (jclass clazz, jfieldID field)
 Get the value of a static double field in a class. More...
 
String getStaticStringField (jclass clazz, jfieldID field)
 Get the value of a static String field in a class. More...
 
void setObjectField (jobject object, jfieldID field, jobject value)
 Set the value of an Object field in an object. More...
 
void setBooleanField (jobject object, jfieldID field, bool value)
 Set the value of a boolean field in an object. More...
 
void setByteField (jobject object, jfieldID field, byte_t value)
 Set the value of a byte field in an object. More...
 
void setCharField (jobject object, jfieldID field, char16_t value)
 Set the value of a char field in an object. More...
 
void setShortField (jobject object, jfieldID field, int16_t value)
 Set the value of a short field in an object. More...
 
void setIntField (jobject object, jfieldID field, int32_t value)
 Set the value of an int field in an object. More...
 
void setLongField (jobject object, jfieldID field, int64_t value)
 Set the value of a long field in an object. More...
 
void setFloatField (jobject object, jfieldID field, float value)
 Set the value of a float field in an object. More...
 
void setDoubleField (jobject object, jfieldID field, double value)
 Set the value of a double field in an object. More...
 
void setStringField (jobject object, jfieldID field, const String &value)
 Set the value of a java.lang.String field in an object. More...
 
void setStaticObjectField (jclass clazz, jfieldID field, jobject value)
 Set the value of a static Object field in a class. More...
 
void setStaticBooleanField (jclass clazz, jfieldID field, bool value)
 Set the value of a static boolean field in a class. More...
 
void setStaticByteField (jclass clazz, jfieldID field, byte_t value)
 Set the value of a static byte field in a class. More...
 
void setStaticCharField (jclass clazz, jfieldID field, char16_t value)
 Set the value of a static char field in a class. More...
 
void setStaticShortField (jclass clazz, jfieldID field, int16_t value)
 Set the value of a static short field in a class. More...
 
void setStaticIntField (jclass clazz, jfieldID field, int32_t value)
 Set the value of a static int field in a class. More...
 
void setStaticLongField (jclass clazz, jfieldID field, int64_t value)
 Set the value of a static long field in a class. More...
 
void setStaticFloatField (jclass clazz, jfieldID field, float value)
 Set the value of a static float field in a class. More...
 
void setStaticDoubleField (jclass clazz, jfieldID field, double value)
 Set the value of a static double field in a class. More...
 
jbyteArray createByteArray (uint_t length)
 Create a Java byte array. More...
 
void setObjectArrayElement (jobjectArray array, uint_t index, jobject value)
 Set an element of a Java object array. More...
 
jobject getObjectArrayElement (jobjectArray array, uint_t index)
 Get an element of a Java object array. More...
 
uint_t getArrayLength (jarray array)
 Get the length of a Java array. More...
 
jobject createDirectByteBuffer (byte_t *buf, size_t size)
 Create a new Java DirectByteBuffer. More...
 
void * getDirectBufferRegion (jobject buf, uint64_t &size)
 Get a pointer to and the size of the memory region occupied by a Java DirectBuffer. More...
 
jobject createLocalRef (jobject object)
 Create a local reference to a Java object. More...
 
void deleteLocalRef (jobject ref)
 Delete a local reference. More...
 
void ensureLocalCapacity (uint_t capacity)
 Reserve space for local references. More...
 
jobject createGlobalRef (jobject object)
 Create a global reference to a Java object. More...
 
void deleteGlobalRef (jobject ref)
 Delete a local reference. More...
 
String decodeString (jstring str)
 Decode a java.lang.String into a String. More...
 
jstring encodeString (const String &str)
 Encode a String into a java.lang.String. More...
 
uint_t getStringLength (jstring str)
 Get the length of a Java string. More...
 
void * getArrayData (jarray array, uint_t &length, bool *isCopy=NULL)
 Obtain a pointer to the body of a primitive Java array. More...
 
void releaseArrayData (jarray array, void *data)
 Release the buffer returned by getArrayData(). More...
 
jbyteArray createByteArray (const byte_t *data, size_t length)
 Create a Java byte array. More...
 
jarray createObjectArray (jclass type, uint_t length, jobject initialValue=NULL)
 Create a Java object array. More...
 
bool isSameObject (jobject object1, jobject object2)
 Test two Java object references for equality. More...
 
bool isInstanceOf (jobject object, jclass clazz)
 Test if a Java object is an instance of an interface or another class. More...
 
bool isAssignableFrom (jclass clazz1, jclass clazz2)
 Test if an object of a class or interface can be safely cast to a class or interface of another class. More...
 
jclass getClassForObject (jobject object)
 Get the class for a Java object. More...
 
jclass getSuperclass (jclass clazz)
 Get the superclass for a Java class. More...
 
void enterMonitor (jobject object)
 Enter the monitor associated with a Java object. More...
 
void exitMonitor (jobject object)
 Exit the monitor associated with a Java object. More...
 
void registerNativeMethods (jclass clazz, const JavaNativeMethod methods[])
 Register native methods for a Java class. More...
 
void unregisterNativeMethods (jclass clazz)
 Unregister all native methods for a Java class. More...
 
void pushLocalFrame (uint_t capacity=8)
 Create a new local reference frame. More...
 
void popLocalFrame ()
 Destroy the topmost local reference frame. More...
 
bool isVerbose () const
 Determine if verbose mode is enabled for this context. More...
 

Static Public Member Functions

static String encodeType (const String &type)
 Encode a datatype into a JNI datatype descriptor. More...
 
static bool parseSignature (const String &signature, String &method, String &descriptor, bool &isStatic)
 Parse a method signature into a method name, a JNI method descriptor, and a static flag. More...
 

Friends

class JavaVirtualMachine
 

Detailed Description

When the Invocation Interface is being used (that is, when a C++ program calls into Java code), the getContext() method of the JavaVirtualMachine class is used to obtain an appropriate JavaContext object.

In C++ code that implements a Java native method, on the other hand, a JavaContext instance must be explicitly constructed from the JNIEnv pointer that was passed into the native function.

This is a lightweight object that should typically be stack-allocated, and may be passed and copied by value.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ JavaContext()

JavaContext ( JNIEnv *  env)

Construct a new JavaContext for the given JNI environment pointer.

Parameters
envThe JNI environment pointer.

◆ ~JavaContext()

Destructor.

Member Function Documentation

◆ callBooleanMethod()

bool callBooleanMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a boolean.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callByteMethod()

byte_t callByteMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a byte.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callCharMethod()

char16_t callCharMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a char.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callDoubleMethod()

double callDoubleMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a double.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callFloatMethod()

float callFloatMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a float.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callIntMethod()

int32_t callIntMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns an int.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callLongMethod()

int64_t callLongMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a long.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualBooleanMethod()

bool callNonvirtualBooleanMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a boolean.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualByteMethod()

byte_t callNonvirtualByteMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a byte.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualCharMethod()

char16_t callNonvirtualCharMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a char.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualDoubleMethod()

double callNonvirtualDoubleMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a double.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualFloatMethod()

float callNonvirtualFloatMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a float.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualIntMethod()

int32_t callNonvirtualIntMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns an int.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualLongMethod()

int64_t callNonvirtualLongMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a long.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualObjectMethod()

jobject callNonvirtualObjectMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a Java object.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualShortMethod()

int16_t callNonvirtualShortMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns a short.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callNonvirtualVoidMethod()

void callNonvirtualVoidMethod ( jobject  object,
jclass  clazz,
jmethodID  method,
  ... 
)

Call a non-virtual Java method that returns void.

Parameters
objectThe object instance.
clazzThe method's defining class.
methodThe method to call.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callObjectMethod()

jobject callObjectMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a Java Object.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callShortMethod()

int16_t callShortMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a short.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticBooleanMethod()

bool callStaticBooleanMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a boolean.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticByteMethod()

byte_t callStaticByteMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a byte.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticCharMethod()

char16_t callStaticCharMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a char.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticDoubleMethod()

double callStaticDoubleMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a double.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticFloatMethod()

float callStaticFloatMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a float.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticIntMethod()

int32_t callStaticIntMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns an int.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticLongMethod()

int64_t callStaticLongMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a long.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticObjectMethod()

jobject callStaticObjectMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a Java Object.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticShortMethod()

int16_t callStaticShortMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a short.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticStringMethod()

String callStaticStringMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns a java.lang.String.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value, as a String.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStaticVoidMethod()

void callStaticVoidMethod ( jclass  clazz,
jmethodID  method,
  ... 
)

Call a static Java method that returns void.

Parameters
clazzThe Java class.
methodThe method to call.
Returns
The method's return value.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callStringMethod()

String callStringMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns a java.lang.String.

Parameters
objectThe object instance.
methodThe method to call.
Returns
The method's return value, encoded as a String.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ callVoidMethod()

void callVoidMethod ( jobject  object,
jmethodID  method,
  ... 
)

Call a Java method that returns void.

Parameters
objectThe object instance.
methodThe method to call.
Exceptions
JavaExceptionIf the called method throws an exception, or if an invocation error occurs.

◆ checkException()

bool checkException ( bool  verbose = false)

Determine if a Java exception has been thrown in the current thread.

Parameters
verboseIf true, print the exception stack trace to standard error.
Returns
true if an exception has been thrown, false otherwise.

◆ clearException()

void clearException ( )

Clear any pending Java exception that is currently being thrown in the current thread.

◆ createByteArray() [1/2]

jbyteArray createByteArray ( uint_t  length)

Create a Java byte array.

Parameters
lengthThe length of the array.
Returns
The array.
Exceptions
JavaExceptionIf an error occurs.

◆ createByteArray() [2/2]

jbyteArray createByteArray ( const byte_t data,
size_t  length 
)

Create a Java byte array.

Parameters
dataThe data to copy into the array.
lengthThe length of the array.
Returns
The newly created array.
Exceptions
JavaExceptionIf an error occurs.

◆ createDirectByteBuffer()

jobject createDirectByteBuffer ( byte_t buf,
size_t  size 
)

Create a new Java DirectByteBuffer.

Parameters
bufA pointer to the natively-allocated memory block for the buffer.
sizeThe size of the memory block, in bytes.
Returns
The newly created DirectByteBuffer object.
Exceptions
JavaExceptionIf an error occurs.

◆ createGlobalRef()

jobject createGlobalRef ( jobject  object)

Create a global reference to a Java object.

Parameters
objectThe object.
Returns
The new global reference.
Exceptions
JavaExceptionIf an error occurs.

◆ createLocalRef()

jobject createLocalRef ( jobject  object)

Create a local reference to a Java object.

Parameters
objectThe object.
Returns
The new local reference.
Exceptions
JavaExceptionIf an error occurs.

◆ createObject()

jobject createObject ( jclass  clazz,
jmethodID  constructor,
  ... 
)

Instantiate a new Java object.

Parameters
clazzThe class.
constructorThe constructor to invoke.
Exceptions
JavaExceptionIf the constructor throws an exception, or if an invocation error occurs.

◆ createObjectArray()

jarray createObjectArray ( jclass  type,
uint_t  length,
jobject  initialValue = NULL 
)

Create a Java object array.

Parameters
typeThe array type.
lengthThe length of the array.
initialValueAn optional initial value to store into every element in the new array.
Returns
The newly created array.
Exceptions
JavaExceptionIf an error occurs.

◆ createObjectVA()

jobject createObjectVA ( jclass  clazz,
jmethodID  constructor,
va_list  args 
)

Instantiate a new Java object.

Parameters
clazzThe class.
constructorThe constructor to invoke.
argsThe arguments to pass to the constructor.
Exceptions
JavaExceptionIf the constructor throws an exception, or if an invocation error occurs.

◆ decodeString()

String decodeString ( jstring  str)

Decode a java.lang.String into a String.

Parameters
strThe Java string.
Returns
The decoded String.
Exceptions
JavaExceptionIf an error occurs.

◆ defineClass()

jclass defineClass ( const String name,
const byte_t buf,
size_t  length 
)

Define a Java class from a buffer of raw bytecode.

Parameters
nameThe name of the class.
bufThe byte buffer containing the bytecode.
lengthThe length of the buffer.
Returns
The new class object.
Exceptions
JavaExceptionIf the class could not be loaded.

◆ deleteGlobalRef()

void deleteGlobalRef ( jobject  ref)

Delete a local reference.

Parameters
refThe reference.

◆ deleteLocalRef()

void deleteLocalRef ( jobject  ref)

Delete a local reference.

Parameters
refThe reference.

◆ encodeString()

jstring encodeString ( const String str)

Encode a String into a java.lang.String.

Parameters
strThe String.
Returns
The encoded Java string.
Exceptions
JavaExceptionIf an error occurs.

◆ encodeType()

String encodeType ( const String type)
static

Encode a datatype into a JNI datatype descriptor.

For example, "String[]" is encoded as "[Ljava/lang/String;".

Parameters
typeThe datatype.
Returns
The encoding of the datatype.

◆ ensureLocalCapacity()

void ensureLocalCapacity ( uint_t  capacity)

Reserve space for local references.

Parameters
capacityThe number of local references to reserve capacity for.
Exceptions
JavaExceptionIf an error occurs.

◆ enterMonitor()

void enterMonitor ( jobject  object)

Enter the monitor associated with a Java object.

The method blocks if another thread is currently in the monitor.

Parameters
objectThe object.
Exceptions
JavaExceptionIf an error occurs.

◆ exitMonitor()

void exitMonitor ( jobject  object)

Exit the monitor associated with a Java object.

Parameters
objectThe object.
Exceptions
JavaExceptionIf an error occurs.

◆ findClass()

jclass findClass ( const String name)

Look up a class by name.

The class name must be fully qualified (for example, "java.util.Vector"), with the following classes as the only exceptions: String, Object, Class.

Parameters
nameThe name of the class.
Returns
A reference to the Java class.
Exceptions
JavaExceptionIf the class was not found.

◆ findField()

jfieldID findField ( jclass  clazz,
const String signature 
)

Look up a field by signature.

The signature must be specified in Java source code form, for example "String name" or "static int foo".

Parameters
clazzThe Java class.
signatureThe signature of the field.
Returns
A reference to the field.
Exceptions
JavaExceptionIf the field was not found.

◆ findMethod()

jmethodID findMethod ( jclass  clazz,
const String signature 
)

Look up a method or constructor by signature.

The signature must be specified in Java source code form, without parameter names, for example "int foo(int, byte[])" or "static boolean bar(java.util.Vector, int, String)", or "(int, String, String)" (a constructor).

Parameters
clazzThe Java class.
signatureThe signature of the method.
Returns
A reference to the method.
Exceptions
JavaExceptionIf the method was not found.

◆ getArrayData()

void * getArrayData ( jarray  array,
uint_t length,
bool *  isCopy = NULL 
)

Obtain a pointer to the body of a primitive Java array.

This data may be used and modified safely (without any intervening JNI calls) until a matching call to releaseArrayData().

Parameters
arrayThe primitive array.
lengthA parameter in which to store the length of the buffer containing the array data.
isCopyIf not NULL, a pointer in which to store a flag indicating whether the returned buffer is a copy of the array data.
Returns
A pointer to a buffer containing the array data.
Exceptions
JavaExceptionIf an error occurs.

◆ getArrayLength()

uint_t getArrayLength ( jarray  array)

Get the length of a Java array.

Parameters
arrayThe array.
Returns
The number of elements in the array.
Exceptions
JavaExceptionIf an error occurs.

◆ getBooleanField()

bool getBooleanField ( jobject  object,
jfieldID  field 
)

Get the value of an boolean field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getByteField()

byte_t getByteField ( jobject  object,
jfieldID  field 
)

Get the value of a byte field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getCharField()

char16_t getCharField ( jobject  object,
jfieldID  field 
)

Get the value of a char field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getClassForObject()

jclass getClassForObject ( jobject  object)

Get the class for a Java object.

Parameters
objectThe object.
Returns
The class.

◆ getDirectBufferRegion()

void * getDirectBufferRegion ( jobject  buf,
uint64_t &  size 
)

Get a pointer to and the size of the memory region occupied by a Java DirectBuffer.

Parameters
bufThe DirectBuffer object.
sizeThe variable in which to store the size of the memory region.
Returns
A pointer to the beginning of the memory region, or NULL on failure.

◆ getDoubleField()

double getDoubleField ( jobject  object,
jfieldID  field 
)

Get the value of a double field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getException()

jthrowable getException ( )

Get the pending Java exception, if one is currently being thrown in the current thread.

Returns
The exception object, or NULL if none.

◆ getFloatField()

float getFloatField ( jobject  object,
jfieldID  field 
)

Get the value of a float field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getIntField()

int32_t getIntField ( jobject  object,
jfieldID  field 
)

Get the value of an int field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getLongField()

int64_t getLongField ( jobject  object,
jfieldID  field 
)

Get the value of a long field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getObjectArrayElement()

jobject getObjectArrayElement ( jobjectArray  array,
uint_t  index 
)

Get an element of a Java object array.

Parameters
arrayThe array.
indexThe index of the element.
Returns
The value of the element.
Exceptions
JavaExceptionIf an error occurs.

◆ getObjectField()

jobject getObjectField ( jobject  object,
jfieldID  field 
)

Get the value of an Object field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getShortField()

int16_t getShortField ( jobject  object,
jfieldID  field 
)

Get the value of a short field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticBooleanField()

bool getStaticBooleanField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static boolean field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticByteField()

byte_t getStaticByteField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static byte field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticCharField()

char16_t getStaticCharField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static char field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticDoubleField()

double getStaticDoubleField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static double field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticFloatField()

float getStaticFloatField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static float field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticIntField()

int32_t getStaticIntField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static int field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticLongField()

int64_t getStaticLongField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static long field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticObjectField()

jobject getStaticObjectField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static Object field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticShortField()

int16_t getStaticShortField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static short field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field.
Exceptions
JavaExceptionIf an error occurs.

◆ getStaticStringField()

String getStaticStringField ( jclass  clazz,
jfieldID  field 
)

Get the value of a static String field in a class.

Parameters
clazzThe class.
fieldThe field ID.
Returns
The value of the field as a String.
Exceptions
JavaExceptionIf an error occurs.

◆ getStringField()

String getStringField ( jobject  object,
jfieldID  field 
)

Get the value of a java.lang.String field in an Object.

Parameters
objectThe object.
fieldThe field ID.
Returns
The value of the field, encoded as a String.
Exceptions
JavaExceptionIf an error occurs.

◆ getStringLength()

uint_t getStringLength ( jstring  str)

Get the length of a Java string.

Parameters
strThe string.
Returns
The length of the string.

◆ getSuperclass()

jclass getSuperclass ( jclass  clazz)

Get the superclass for a Java class.

Parameters
clazzThe class.
Returns
The superclass, or NULL if the class is an interface or if it is java.lang.Object.

◆ isAssignableFrom()

bool isAssignableFrom ( jclass  clazz1,
jclass  clazz2 
)

Test if an object of a class or interface can be safely cast to a class or interface of another class.

Parameters
clazz1The class of the object to be cast.
clazz2The class of the cast.
Returns
true if the cast is valid, false otherwise.

◆ isInstanceOf()

bool isInstanceOf ( jobject  object,
jclass  clazz 
)

Test if a Java object is an instance of an interface or another class.

Parameters
objectThe object.
clazzThe class or interface.
Returns
true if the object is an instance of the other class or interface, false otherwise.

◆ isSameObject()

bool isSameObject ( jobject  object1,
jobject  object2 
)

Test two Java object references for equality.

Parameters
object1The first object reference.
object2The second object reference.
Returns
true if the references both refer to the same object, false otherwise.

◆ isVerbose()

bool isVerbose ( ) const
inline

Determine if verbose mode is enabled for this context.

◆ parseSignature()

bool parseSignature ( const String signature,
String method,
String descriptor,
bool &  isStatic 
)
static

Parse a method signature into a method name, a JNI method descriptor, and a static flag.

For example, "static int foo(int, char[])" results in a method name of "foo", a JNI method descriptor of "(I[C)I", and a static flag of true.

Parameters
signatureThe signature to parse.
methodThe string in which to place the method name.
descriptorThe string in which to place the JNI method descriptor.
isStaticThe flag in which to store the static flag (true if a "static" qualifier is present, false otherwise).
Returns
true if the signature was parsed successfully, false otherwise.

◆ popLocalFrame()

void popLocalFrame ( )

Destroy the topmost local reference frame.

All local references created in the frame will be freed.

◆ pushLocalFrame()

void pushLocalFrame ( uint_t  capacity = 8)

Create a new local reference frame.

Parameters
capacityThe minimal local reference capacity of the new frame.
Exceptions
JavaExceptionIf an error occurs.

◆ registerNativeMethods()

void registerNativeMethods ( jclass  clazz,
const JavaNativeMethod  methods[] 
)

Register native methods for a Java class.

Parameters
clazzThe class.
methodsAn array of native methods to register. The last element in the array must have the function field set to NULL.
Exceptions
JavaExceptionIf an error occurs.

◆ releaseArrayData()

void releaseArrayData ( jarray  array,
void *  data 
)

Release the buffer returned by getArrayData().

The data will be copied back into the original array, if necessary.

Parameters
arrayThe primitive array.
dataThe buffer containing the array data.

◆ setBooleanField()

void setBooleanField ( jobject  object,
jfieldID  field,
bool  value 
)

Set the value of a boolean field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setByteField()

void setByteField ( jobject  object,
jfieldID  field,
byte_t  value 
)

Set the value of a byte field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setCharField()

void setCharField ( jobject  object,
jfieldID  field,
char16_t  value 
)

Set the value of a char field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setDoubleField()

void setDoubleField ( jobject  object,
jfieldID  field,
double  value 
)

Set the value of a double field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setFloatField()

void setFloatField ( jobject  object,
jfieldID  field,
float  value 
)

Set the value of a float field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setIntField()

void setIntField ( jobject  object,
jfieldID  field,
int32_t  value 
)

Set the value of an int field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setLongField()

void setLongField ( jobject  object,
jfieldID  field,
int64_t  value 
)

Set the value of a long field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setObjectArrayElement()

void setObjectArrayElement ( jobjectArray  array,
uint_t  index,
jobject  value 
)

Set an element of a Java object array.

Parameters
arrayThe array.
indexThe index of the element.
valueThe new value for the element.
Exceptions
JavaExceptionIf an error occurs.

◆ setObjectField()

void setObjectField ( jobject  object,
jfieldID  field,
jobject  value 
)

Set the value of an Object field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setShortField()

void setShortField ( jobject  object,
jfieldID  field,
int16_t  value 
)

Set the value of a short field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticBooleanField()

void setStaticBooleanField ( jclass  clazz,
jfieldID  field,
bool  value 
)

Set the value of a static boolean field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticByteField()

void setStaticByteField ( jclass  clazz,
jfieldID  field,
byte_t  value 
)

Set the value of a static byte field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticCharField()

void setStaticCharField ( jclass  clazz,
jfieldID  field,
char16_t  value 
)

Set the value of a static char field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticDoubleField()

void setStaticDoubleField ( jclass  clazz,
jfieldID  field,
double  value 
)

Set the value of a static double field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticFloatField()

void setStaticFloatField ( jclass  clazz,
jfieldID  field,
float  value 
)

Set the value of a static float field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticIntField()

void setStaticIntField ( jclass  clazz,
jfieldID  field,
int32_t  value 
)

Set the value of a static int field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticLongField()

void setStaticLongField ( jclass  clazz,
jfieldID  field,
int64_t  value 
)

Set the value of a static long field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticObjectField()

void setStaticObjectField ( jclass  clazz,
jfieldID  field,
jobject  value 
)

Set the value of a static Object field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStaticShortField()

void setStaticShortField ( jclass  clazz,
jfieldID  field,
int16_t  value 
)

Set the value of a static short field in a class.

Parameters
clazzThe class.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ setStringField()

void setStringField ( jobject  object,
jfieldID  field,
const String value 
)

Set the value of a java.lang.String field in an object.

Parameters
objectThe object.
fieldThe field ID.
valueThe new value for the field.
Exceptions
JavaExceptionIf an error occurs.

◆ throwException()

bool throwException ( jclass  exception,
const String message 
)

Throw a Java exception.

Parameters
exceptionThe class of the exception.
messageThe exception message.
Returns
true on success, false otherwise.

◆ unregisterNativeMethods()

void unregisterNativeMethods ( jclass  clazz)

Unregister all native methods for a Java class.

Parameters
clazzThe class.

Friends And Related Function Documentation

◆ JavaVirtualMachine

friend class JavaVirtualMachine
friend

The documentation for this class was generated from the following files: