1 /* 2 * Copyright (C) 2008 - 2013 Vivien Malerba <malerba@gnome-db.org> 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the 16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 #include <glib.h> 20 #include <glib-object.h> 21 #include <stdlib.h> 22 #include <string.h> 23 #include <jni.h> 24 #include <glib/gstdio.h> 25 26 #ifndef __JNI_WRAPPER__ 27 #define __JNI_WRAPPER__ 28 29 /* 30 * Conversion method 31 */ 32 jlong jni_cpointer_to_jlong (gconstpointer c_pointer); 33 gconstpointer jni_jlong_to_cpointer (jlong value); 34 35 /* 36 * general functions 37 */ 38 typedef jint (*CreateJavaVMFunc) (JavaVM **, void **, void *); 39 40 JNIEnv *jni_wrapper_create_vm (JavaVM **out_jvm, CreateJavaVMFunc create_func, 41 const gchar *lib_path, 42 const gchar *class_path, GError **error); 43 void jni_wrapper_destroy_vm (JavaVM *jvm); 44 gboolean jni_wrapper_handle_exception (JNIEnv *jvm, gint *out_error_code, 45 gchar **out_sql_state, GError **error); 46 47 jclass jni_wrapper_class_get (JNIEnv *jvm, const gchar *class_name, GError **error); 48 GValue *jni_wrapper_instantiate_object (JNIEnv *jenv, jclass klass, 49 const gchar *signature, GError **error, ...); 50 51 /* 52 * methods 53 */ 54 typedef struct { 55 jclass klass; /* JNI global reference */ 56 gchar *ret_type; 57 gboolean is_static; 58 jmethodID mid; 59 } JniWrapperMethod; 60 JniWrapperMethod *jni_wrapper_method_create (JNIEnv *jenv, jclass jklass, 61 const gchar *method_name, const gchar *signature, 62 gboolean is_static, GError **error); 63 GValue *jni_wrapper_method_call (JNIEnv *jenv, JniWrapperMethod *method, GValue *object, 64 gint *out_error_code, gchar **out_sql_state, GError **error, ...); 65 void jni_wrapper_method_free (JNIEnv *jenv, JniWrapperMethod *method); 66 67 /* 68 * fields access 69 */ 70 typedef struct { 71 jclass klass; /* JNI global reference */ 72 gchar *type; 73 gboolean is_static; 74 jfieldID fid; 75 } JniWrapperField; 76 JniWrapperField *jni_wrapper_field_create (JNIEnv *jenv, jclass jklass, 77 const gchar *field_name, const gchar *signature, 78 gboolean is_static, GError **error); 79 GValue *jni_wrapper_field_get (JNIEnv *jenv, JniWrapperField *field, 80 GValue *object, GError **error); 81 gboolean jni_wrapper_field_set (JNIEnv *jenv, JniWrapperField *field, 82 GValue *object, const GValue *value, GError **error); 83 void jni_wrapper_field_free (JNIEnv *jenv, JniWrapperField *field); 84 85 86 /* 87 * jobject wrapper in a GValue 88 */ 89 #define GDA_TYPE_JNI_OBJECT (gda_jni_object_get_type()) 90 typedef struct { 91 JavaVM *jvm; 92 jobject jobj; 93 } GdaJniObject; 94 jobject gda_value_get_jni_object (const GValue *value); 95 GValue *gda_value_new_jni_object (JavaVM *jvm, JNIEnv *env, jobject jni_object); 96 void gda_value_set_jni_object (GValue *value, JavaVM *jvm, JNIEnv *env, jobject jni_object); 97 GType gda_jni_object_get_type (void) G_GNUC_CONST; 98 gpointer gda_jni_object_copy (gpointer boxed); 99 void gda_jni_object_free (gpointer boxed); 100 101 #endif 102