lm::JavaVoidMethodCaller Class Reference

Simple wrapper around calls to the void methods via JNI. More...

#include <JavaVoidMethodCaller.h>


Public Member Functions

 JavaVoidMethodCaller (JNIEnv *env, jclass java_class, jobject java_object, bool swallow_java_exceptions=true)
 Create method caller.
 ~JavaVoidMethodCaller (void)
 Destructor.
bool Call (const char *method_name, const char *method_signature)
 Call method without parameters.
template<typename T1>
bool Call (const char *method_name, const char *method_signature, T1 param_1)
 Call method with one parameter of primitive type.
template<typename T1, typename T2>
bool Call (const char *method_name, const char *method_signature, T1 param_1, T2 param_2)
 Call method with two parameters of primitive types.
template<typename T1>
bool Call (const char *method_name, const char *method_signature, const std::vector< T1 > &param_1)
 Call method with one parameter of array of primitive type.
template<typename T1, typename T2>
bool Call (const char *method_name, const char *method_signature, const std::vector< T1 > &param_1, const std::vector< T2 > &param_2)
 Call method with two parameters of arrays of primitive types.
bool GetSwallowExceptions (void)
 Get flag for exception swallowing.
bool SetSwallowException (bool se)
 Set flag for exception swallowing.
template<>
bool Call (const char *method_name, const char *method_signature, String param_1)
 Call method with one string parameter.
template<>
bool Call (const char *method_name, const char *method_signature, const std::vector< String > &param_1)
 Call method with one parameter that is array of strings.

Private Member Functions

typedef CREATE_TYPE_LIST_09 (void, jboolean, jbyte, jchar, jshort, jint, jlong, jfloat, jdouble) JavaTypes
 Typelist which contains all basic Java types.
typedef CREATE_TYPE_LIST_09 (void, jbooleanArray, jbyteArray, jcharArray, jshortArray, jintArray, jlongArray, jfloatArray, jdoubleArray) JavaArrayTypes
 Typelist which contains all primitive array Java types.
typedef CREATE_TYPE_LIST_09 (void, ArrayMethodAdapter_Boolean, ArrayMethodAdapter_Byte, ArrayMethodAdapter_Char, ArrayMethodAdapter_Short, ArrayMethodAdapter_Int, ArrayMethodAdapter_Long, ArrayMethodAdapter_Float, ArrayMethodAdapter_Double) ArrayMethodAdapters
 Adapter class for boolean array methods. Adapter class for byte array methods. Adapter class for char array methods. Adapter class for short array methods. Adapter class for int array methods. Adapter class for long array methods. Adapter class for float array methods. Adapter class for double array methods. Typelist which contains all adapters for different types of arrays.

Private Attributes

JNIEnv * jni_env
 Java environment.
jclass target_class
 Type of the object on which methods will be called.
jobject object
 Instance of the Java object on which methods will be called.
bool swallow_exceptions
 Flag which specifies if exception state is cleared after call that throws.

Data Structures

class  ArrayMethodAdapter_String
 Adapter class for string arrays in Java. More...


Detailed Description

Simple wrapper around calls to the void methods via JNI.

This class provides methods to call Java methods on some object with various types of parameters. Parameters of primitive types, strings and arrays of primitive types and strings are supported. Note that not all of the combinations are supported, but they can be easily added. Currently, methods with following signatures are supported:

where <type1> or <type2> are primitive types in Java (boolean, int, short, long, float, double, byte, char) or strings.
For more details about JNI see JNI specification which can be found here.


Constructor & Destructor Documentation

lm::JavaVoidMethodCaller::JavaVoidMethodCaller ( JNIEnv *  env,
jclass  java_class,
jobject  java_object,
bool  swallow_java_exceptions = true 
) [inline]

Create method caller.

Parameters:
env Java environment from which library has been initialized.
java_class Java class from which methods will be called.
java_object Instance of the java_class on which methods will be called.
swallow_java_exceptions If set to true, exception state will be cleared after each method call. If set to false, exception state will not be cleared.

lm::JavaVoidMethodCaller::~JavaVoidMethodCaller ( void   )  [inline]

Destructor.


Member Function Documentation

template<>
bool lm::JavaVoidMethodCaller::Call ( const char *  method_name,
const char *  method_signature,
const std::vector< String > &  param_1 
)

Call method with one parameter that is array of strings.

This is specialization of the Call function template for methods with one array parameter. This method works only with Unicode strings. Strings are automatically translated to the Java-compatible jstring objects.

Parameters:
method_name Name of the method to call. Name is case-sensitive.
method_signature Signature string of the method to call (as reported by the javap -s).
param_1 std::pair object which describes array of strings. First element of the pair contains elements of the array (that is, std::wstring *). Second element of the pair contains length of the array (number of strings in array).
Returns:
true on success, false otherwise (method call failed or exception has been thrown).
Note:
If swallow_exceptions is set to true, exception state will be cleared if exception has been thrown during method call.

template<>
bool lm::JavaVoidMethodCaller::Call ( const char *  method_name,
const char *  method_signature,
String  param_1 
)

Call method with one string parameter.

This is specialization of the Call function template for methods which take only one argument - Unicode string. String is automatically translated to the Java-compatible jstring object and that object is passed to the method as its only parameter.

Parameters:
method_name Name of the method to call. Name is case-sensitive.
method_signature Signature string of the method (as reported by the javap -s).
param_1 String that will be passed as the only argument.
Returns:
true on success, false otherwise (method call failed or exception has been thrown).
Note:
If swallow_exceptions is set to true, exception state will be cleared if exception has been thrown during method call.

template<typename T1, typename T2>
bool lm::JavaVoidMethodCaller::Call ( const char *  method_name,
const char *  method_signature,
const std::vector< T1 > &  param_1,
const std::vector< T2 > &  param_2 
) [inline]

Call method with two parameters of arrays of primitive types.

Parameters:
method_name Name of the method to call. Name is case-sensitive.
method_signature Signature string of the method (can be generated for example via javap -s).
param_1 std::vector which contains elements of the first array. Elements can be one of the following types: jboolean, jchar, jbyte, jint, jshort, jlong, jfloat, jdouble. If the type of array's elements is invalid, error is reported via COMPILE_TIME_CHECK macro.
param_2 std::vector which contains elements of the second array. Elements can be one of the following types: jboolean, jchar, jbyte, jint, jshort, jlong, jfloat, jdouble. If the type of array's elements is invalid, error is reported via COMPILE_TIME_CHECK macro.
Returns:
true on success, false otherwise (if exception has been thrown or method was not found).
Note:
If swallow_exceptions is set to true, exception state will be cleared if exception has been thrown during method call.

template<typename T1>
bool lm::JavaVoidMethodCaller::Call ( const char *  method_name,
const char *  method_signature,
const std::vector< T1 > &  param_1 
) [inline]

Call method with one parameter of array of primitive type.

Parameters:
method_name Name of the method to call. Name is case-sensitive.
method_signature Signature string of the method (can be generated for example via javap -s).
param_1 std::vector which contains elements of the array. Elements can be one of the following types: jboolean, jchar, jbyte, jint, jshort, jlong, jfloat, jdouble. Separate specialization exists for string arrays. If the type of array's elements is invalid, error is reported via COMPILE_TIME_CHECK macro.
Returns:
true on success, false otherwise (if exception has been thrown or method was not found).
Note:
If swallow_exceptions is set to true, JVM exception state will be cleared if exception has been thrown during method call.

template<typename T1, typename T2>
bool lm::JavaVoidMethodCaller::Call ( const char *  method_name,
const char *  method_signature,
T1  param_1,
T2  param_2 
) [inline]

Call method with two parameters of primitive types.

Parameters:
method_name Name of the method to call. Name is case-sensitive.
method_signature Signature string of the method (can be generated for example via javap -s).
param_1 First parameter that will be passed to the Java method. Parameter type can be one of jboolean, jchar, jbyte, jint, jshort, jlong, jfloat, jdouble. If type is incorrect, error is reported during compilation via COMPILE_TIME_CHECK macro.
param_2 Second parameter that will be passed to the Java method. Parameter type can be one of jboolean, jchar, jbyte, jint, jshort, jlong, jfloat, jdouble. If type is incorrect, error is reported during compilation via COMPILE_TIME_CHECK macro.
Returns:
true on success, false otherwise (if exception has been thrown or method was not found).
Note:
If swallow_exceptions is set to true, exception state will be cleared if exception has been thrown during method call.

template<typename T1>
bool lm::JavaVoidMethodCaller::Call ( const char *  method_name,
const char *  method_signature,
T1  param_1 
) [inline]

Call method with one parameter of primitive type.

Parameters:
method_name Name of the method to call. Name is case-sensitive.
method_signature Signature string of the method (can be generated for example via javap -s).
param_1 Parameter that will be passed to the Java method. Type of this parameter can be one of jchar, jbyte, jint, jshort, jlong, jfloat, jdouble, jboolean. If type is invalid, error is reported during compilation via COMPILE_TIME_CHECK macro.
Returns:
true on success, false otherwise (if exception has been thrown or method was not found).
Note:
If swallow_exceptions is set to true, exception state will be cleared if exception has been thrown during method call.

bool lm::JavaVoidMethodCaller::Call ( const char *  method_name,
const char *  method_signature 
) [inline]

Call method without parameters.

Parameters:
method_name Name of the method to call. Name is case-sensitive.
method_signature Signature string of the method (can be generated for example via javap -s).
Returns:
true on success, false otherwise (if exception has been thrown or method was not found).
Note:
If swallow_exceptions is set to true, exception state will be cleared if exception has been thrown during method call.

typedef lm::JavaVoidMethodCaller::CREATE_TYPE_LIST_09 ( void  ,
ArrayMethodAdapter_Boolean  ,
ArrayMethodAdapter_Byte  ,
ArrayMethodAdapter_Char  ,
ArrayMethodAdapter_Short  ,
ArrayMethodAdapter_Int  ,
ArrayMethodAdapter_Long  ,
ArrayMethodAdapter_Float  ,
ArrayMethodAdapter_Double   
) [private]

Adapter class for boolean array methods. Adapter class for byte array methods. Adapter class for char array methods. Adapter class for short array methods. Adapter class for int array methods. Adapter class for long array methods. Adapter class for float array methods. Adapter class for double array methods. Typelist which contains all adapters for different types of arrays.

This typelist is used when new template instantiation of the array Call method is being created. During instantiation of the Call method correct adapter is selected based on the types of the parameters.

typedef lm::JavaVoidMethodCaller::CREATE_TYPE_LIST_09 ( void  ,
jbooleanArray  ,
jbyteArray  ,
jcharArray  ,
jshortArray  ,
jintArray  ,
jlongArray  ,
jfloatArray  ,
jdoubleArray   
) [private]

Typelist which contains all primitive array Java types.

This list is used when selecting correct return type in ArrayMethodAdapter_### classes.

typedef lm::JavaVoidMethodCaller::CREATE_TYPE_LIST_09 ( void  ,
jboolean  ,
jbyte  ,
jchar  ,
jshort  ,
jint  ,
jlong  ,
jfloat  ,
jdouble   
) [private]

Typelist which contains all basic Java types.

This list is used in all Call methods to verify types of the arguments in compilation-time.

bool lm::JavaVoidMethodCaller::GetSwallowExceptions ( void   )  [inline]

Get flag for exception swallowing.

Returns:
true if exceptions are swallowed during method calls, false otherwise. If this is set to true, exception state of the JVM is cleared if exception has been thrown during method call.

bool lm::JavaVoidMethodCaller::SetSwallowException ( bool  se  )  [inline]

Set flag for exception swallowing.

Parameters:
se New value of the flag. If this is set to true, exception state of the JVM is cleared if exception has been thrown during method call.
Returns:
Old state of the flag.


Field Documentation

JNIEnv* lm::JavaVoidMethodCaller::jni_env [private]

Java environment.

jobject lm::JavaVoidMethodCaller::object [private]

Instance of the Java object on which methods will be called.

bool lm::JavaVoidMethodCaller::swallow_exceptions [private]

Flag which specifies if exception state is cleared after call that throws.

jclass lm::JavaVoidMethodCaller::target_class [private]

Type of the object on which methods will be called.


The documentation for this class was generated from the following file:
Generated on Tue Dec 19 17:43:49 2006 for Load Monitor for Linux by  doxygen 1.4.7