C12Adapter Opensource C++ Interface
|
Scoped Java environment handler. More...
Public Types | |
enum | { JniVersion = JNI_VERSION_1_4 } |
Public Member Functions | |
MJavaEnv () | |
Most useful constructor that fetches the environment and stores it internally for further use. More... | |
MJavaEnv (JNIEnv *env) | |
Constructor that takes an explicit environment. More... | |
~MJavaEnv () | |
Destructor that unregisters the java environment. More... | |
void | AddToLocalObjects (jobject obj) |
Add a given Java object to the list of local objects that have to be deleted at destruction. More... | |
jbyteArray | NewLocalByteArray (int size) |
Create a new uninitialized Java byte array and add it to the list of local references. More... | |
jbyteArray | NewLocalByteArray (const char *buff, int size) |
Create a new initialized Java byte array and add it to the list of local references. More... | |
void | ExceptionCppToJava (MException &ex) |
Object method that initializes the java exception in the current environment from a given C++ exception. More... | |
void | CheckForJavaException () |
Convert java exception, if it is present, into C++. More... | |
jclass | FindClass (const char *javaClassName) |
Find Java class, throw error if it is not found. More... | |
jmethodID | GetMethodID (jclass clazz, const char *name, const char *signature) |
Get Java method ID, throw error if it is not found. More... | |
jmethodID | GetStaticMethodID (jclass clazz, const char *name, const char *signature) |
Get Java static method ID, throw error if it is not found. More... | |
jfieldID | GetFieldID (jclass clazz, const char *name, const char *signature) |
Get Java field ID, throw error if it is not found. More... | |
jfieldID | GetStaticFieldID (jclass clazz, const char *name, const char *signature) |
Get Java static field ID, throw error if it is not found. More... | |
JNIEnv * | GetEnv () |
const JNIEnv * | GetEnv () const |
JNIEnv * | operator-> () |
const JNIEnv * | operator-> () const |
jstring | NewLocalStringUTF (const char *str) |
jstring | NewLocalStringUTF (const MStdString &str) |
Static Public Member Functions | |
static void | StaticExceptionCppToJava (JNIEnv *jenv, MException &ex) |
Class method that initializes java exception in the current environment from a given C++ exception. More... | |
static void | CheckForJniError (jint code, const char *errorMessage) |
Check for JNI error code. More... | |
Scoped Java environment handler.
Use this class as a wrapper of methods that call Java code from C++ as it provides java environment variable for the current thread context. Typical, most generic and reliable example of full use of the class:
As a slight optimization it is possible to reuse java environment class among several calls, however one has to be careful as java environment has to be created and released within a context of a single Java call, or a single detached C++ thread.
MJavaEnv::MJavaEnv | ( | ) |
Most useful constructor that fetches the environment and stores it internally for further use.
The class has to be created in C++ code that is by itself working within Java. If necessary, and the current C++ thread is not registered with Java, the registering is performed.
MJavaEnv::MJavaEnv | ( | JNIEnv * | env | ) |
Constructor that takes an explicit environment.
This has to be called within the registered Java thread when the environment is received already. It is assumed that the java environment is already registered with java thread.
env | Java environment to be used within the object. |
MJavaEnv::~MJavaEnv | ( | ) |
Destructor that unregisters the java environment.
If the environment was executed in a context of a C++ only thread it is unregistered.
void MJavaEnv::AddToLocalObjects | ( | jobject | obj | ) |
Add a given Java object to the list of local objects that have to be deleted at destruction.
This method is a handy way of dealing with JNI local references that have to be removed at the exit of method.
void MJavaEnv::CheckForJavaException | ( | ) |
Convert java exception, if it is present, into C++.
This call should be made after any method such as JNI Call*Method that can raise Java exceptions. If a Java exception takes place, it is converted into a C++ exception of the most appropriate MeteringSDK exception class and thrown as C++ exception into C++. Any non-MeteringSDK exceptions will be converted into MException.
In between Call*Method and CheckForJavaException only very few JNI methods can be called:
as specified at http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#exception_handling
|
static |
Check for JNI error code.
Throw a C++ exception if the given result of JNI function is erroneous, not equal to JNI_OK.
code | Result of a JNI function such as AttachCurrentThread or GetEnv. |
errorMessage | Whatever will be the error message if code is not JNI_OK. This string should not have C format parameters. |
|
inline |
Object method that initializes the java exception in the current environment from a given C++ exception.
The robustness of this method assumes the presence of MTools Java facade that provides the exception hierarchy that matches the one of MeteringSDK. After the call is made, the execution should immediately reach Java code for the exception to be raised from there.
ex | C++ exception that has to be converted into java exception of the current environment. |
jclass MJavaEnv::FindClass | ( | const char * | javaClassName | ) |
Find Java class, throw error if it is not found.
Different from JNI FindClass, still available as
this object method, callable with a period like this:
will throw an exception if there is no such class.
javaClassName | Java class name signature as specified by Java. |
|
inline |
|
inline |
jfieldID MJavaEnv::GetFieldID | ( | jclass | clazz, |
const char * | name, | ||
const char * | signature | ||
) |
Get Java field ID, throw error if it is not found.
Different from JNI GetFieldID, still available as
this object method, callable with a period like this:
will throw an exception if there is no such field.
clazz | Java class object. |
name | Field name. |
signature | Field signature, as defined by Java. |
jmethodID MJavaEnv::GetMethodID | ( | jclass | clazz, |
const char * | name, | ||
const char * | signature | ||
) |
Get Java method ID, throw error if it is not found.
Different from JNI GetMethodID, still available as
this object method, callable with a period like this:
will throw an exception if there is no such method.
clazz | Java class object. |
name | Method name. |
signature | Method signature, as defined by Java. |
jfieldID MJavaEnv::GetStaticFieldID | ( | jclass | clazz, |
const char * | name, | ||
const char * | signature | ||
) |
Get Java static field ID, throw error if it is not found.
Different from JNI GetStaticFieldID, still available as
this object method, callable with a period like this:
will throw an exception if there is no such static field.
clazz | Java class object. |
name | Field name. |
signature | Field signature, as defined by Java. |
jmethodID MJavaEnv::GetStaticMethodID | ( | jclass | clazz, |
const char * | name, | ||
const char * | signature | ||
) |
Get Java static method ID, throw error if it is not found.
Different from JNI GetStaticMethodID, still available as
this object method, callable with a period like this:
will throw an exception if there is no such method.
clazz | Java class object. |
name | Method name. |
signature | Method signature, as defined by Java. |
jbyteArray MJavaEnv::NewLocalByteArray | ( | int | size | ) |
Create a new uninitialized Java byte array and add it to the list of local references.
size | Size of the array. |
jbyteArray MJavaEnv::NewLocalByteArray | ( | const char * | buff, |
int | size | ||
) |
Create a new initialized Java byte array and add it to the list of local references.
buff | Buffer with byte values. |
size | Size of the buffer and the result byte array. |
jstring MJavaEnv::NewLocalStringUTF | ( | const char * | str | ) |
Create a new Java string from UTF string and add it to the list of local references.
str | String in UTF encoding from which a Java string has to be created. |
|
inline |
Create a new Java string from UTF string and add it to the list of local references.
str | String in UTF encoding from which a Java string has to be created. |
|
inline |
Call JNI methods using this environment class.
This operator saves a call of GetEnv() so that instead of
one has:
Notice, MJavaEnv provides a number of wrapper methods that in addition to natives verifies the result of the operation and throws an exception at failure. For example, notice '.' instead of '->'
|
inline |
Call JNI methods using this environment class.
This operator saves a call of GetEnv() so that instead of
one has:
Notice, MJavaEnv provides a number of wrapper methods that in addition to natives verifies the result of the operation and throws an exception at failure. For example, notice '.' instead of '->'
|
static |
Class method that initializes java exception in the current environment from a given C++ exception.
The robustness of this method assumes the presence of MTools Java facade that provides the exception hierarchy that matches the one of MeteringSDK. After the call is made the execution should immediately reach Java code for the exception to be raised from there.
jenv | Native Java environment. |
ex | C++ exception that has to be converted into java exception for the current environment. |