diff options
author | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-01-28 15:32:03 +0000 |
---|---|---|
committer | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-01-28 15:32:03 +0000 |
commit | 8f527b1eb68a15b09d528e417812aa1040dbabb7 (patch) | |
tree | 68e72a771e3cf2e3cf58fc6e088d84a53cafb6a5 /bindings/java | |
parent | 88554aacc9eced7cfaece40461f9c86eddee922c (diff) | |
download | lasso-8f527b1eb68a15b09d528e417812aa1040dbabb7.tar.gz lasso-8f527b1eb68a15b09d528e417812aa1040dbabb7.tar.xz lasso-8f527b1eb68a15b09d528e417812aa1040dbabb7.zip |
Bindings java: use utils.py methods, make set_hash_of_objects more robust
Diffstat (limited to 'bindings/java')
-rw-r--r-- | bindings/java/lang.py | 422 | ||||
-rw-r--r-- | bindings/java/wrapper_top.c | 109 |
2 files changed, 267 insertions, 264 deletions
diff --git a/bindings/java/lang.py b/bindings/java/lang.py index fc0fdd5b..9d221192 100644 --- a/bindings/java/lang.py +++ b/bindings/java/lang.py @@ -38,24 +38,22 @@ def with_return_owner(d): def generate_arg_list(self,args): def arg_to_decl(arg): - type, name, option = arg - return self.JNI_arg_type(type) + ' ' + format_as_camelcase(name) + return self.java_arg_type(arg) + ' ' + format_as_camelcase(arg_name(arg)) return ', '.join([ arg_to_decl(x) for x in args if not is_out(x)]) def generate_arg_list2(args): def arg_to_decl(arg): - type, name, option = arg if is_out(arg): return 'output' - return format_as_camelcase(name) + return format_as_camelcase(arg_name(arg)) return ', '.join([ arg_to_decl(x) for x in args ]) -def generate_arg_list3(self,args): +def generate_arg_list3(self, args): def arg_to_decl(arg): - type, name, option = arg if is_out(arg): return 'Object[] output' - return self.JNI_arg_type(type) + ' ' + format_as_camelcase(name) + r = self.java_arg_type(arg) + ' ' + format_as_camelcase(arg_name(arg)) + return r return ', '.join([ arg_to_decl(x) for x in args]) def convert_class_name(lasso_name): @@ -68,21 +66,29 @@ def mangle_name(name): s = s.replace('[', '_3') return s -def jni_elem_type(type): - if type in ('char*', 'guchar*', 'const guchar*', 'gchar*', 'const char*', 'const gchar*'): +def jni_glist_elem_type(type): + if is_cstring(type): return 'jstring' - elif type == 'xmlNode*': + elif is_xml_node(type): return 'jstring' + elif is_object(type): + return 'jobject' else: + return Exception('No jni_glist_elem_type for %s' % (type,)) + +def jni_hashtable_elem_type(type): + if is_object(type): return 'jobject' + else: + return 'jstring' def JNI_elem_type(type): - if type in ('char*', 'gchar*', 'const char*', 'const gchar*'): + if is_cstring(type): return 'String' - elif type == 'xmlNode*': + elif is_xml_node(type): return 'String' - elif type != None and type.startswith('Lasso'): - return type[5:] + elif is_object(type): + return convert_class_name(type) else: return 'Object' @@ -99,16 +105,12 @@ def error_to_exception(error_name): name = format_as_camelcase(name.lower()) return (super+name+'Exception',super+'Exception') -def wrapper_decl(name, jnitype, fd): +def wrapper_decl(name, jnitype): jniname = wrapper_name(name) - print >> fd, 'JNIEXPORT %s JNICALL %s(JNIEnv *env, jclass clss' % \ - (jnitype,jniname), + return 'JNIEXPORT %s JNICALL %s(JNIEnv *env, jclass clss' % (jnitype,jniname) def is_collection(type): - return type in ('const GList*','GList*','GHashTable*') - -def is_string_type(type): - return type in ['string', 'char*', 'const char*', 'gchar*', 'const gchar*'] + return is_glist(type) or is_hashtable(type) class Binding: def __init__(self, binding_data): @@ -223,23 +225,21 @@ protected static native void destroy(long cptr); print >>fd, 'boolean ', print >>fd, '%s_get();' % c[1] - def JNI_arg_type(self, vtype): - if vtype == 'gboolean': + def java_arg_type(self, vtype): + if is_boolean(vtype): return 'boolean' - elif vtype in ['int','gint'] + self.binding_data.enums: + elif is_int(vtype, self.binding_data): return 'int' - elif vtype in ('char*', 'gchar*', 'const char*', 'const gchar*'): + elif is_cstring(vtype): return 'String' - elif vtype in ('const GList*','GList*','GHashTable*'): + elif is_collection(vtype): return 'Object[]' - elif vtype == 'xmlNode*': + elif is_xml_node(vtype): return 'String' - elif isinstance(vtype,basestring) and vtype.startswith('Lasso'): - if vtype.endswith('*'): - vtype = vtype[:-1] - return convert_class_name(vtype) + elif is_object(vtype): + return convert_class_name(unpointerize(unconstify(vtype))) else: - return 'GObject' + raise Exception('java_arg_type failed for %s' % vtype) def JNI_return_type(self, vtype): if vtype: @@ -263,11 +263,12 @@ protected static native void destroy(long cptr); return 'void' def JNI_member_type(self,member): - type, name, options = member - if type in ('const GList*','GList*','GHashTable*'): - return self.JNI_arg_type(options.get('element-type')) + if is_glist(member): + return self.java_arg_type(element_type(member)) + elif is_hashtable(member): + return self.java_arg_type(element_type(member) or 'char*') else: - return self.JNI_arg_type(type) + return self.java_arg_type(member) def JNI_function_name(self, m): if m.rename: @@ -310,12 +311,6 @@ protected static native void destroy(long cptr); print >> fd, ' public static native %s[] %s(GObject obj);' % (jtype,name) name = '%s_set' % prefix print >> fd, ' public static native void %s(GObject obj, %s[] value);' % (name,jtype) - name = '%s_add' % prefix - print >> fd, ' public static native void %s(GObject obj, String key, %s value);' % (name,jtype) -# name = '%s_remove' % prefix -# print >> fd, ' public static native void %s(GObject obj, String key);' % (name) -# name = '%s_get_by_name' % prefix -# print >> fd, ' public static native %s[] %s(GObject obj, String key);' % (jtype,name) else: name = '%s_get' % prefix print >> fd, ' public static native %s %s(GObject obj);' % (jtype,name) @@ -365,17 +360,17 @@ protected static native void destroy(long cptr); for c in self.binding_data.constants: s = c[1]+'_get' if c[0] == 'i': - wrapper_decl(s,'jint',fd) + print >>fd, wrapper_decl(s,'jint') print >>fd, ') {' print >>fd, ' return %s;' % c[1] print >>fd, '}' elif c[0] == 's': - wrapper_decl(s,'jstring',fd) + print >>fd, wrapper_decl(s,'jstring') print >>fd, ') {' print >>fd, ' return (*env)->NewStringUTF(env, %s);' % c[1] print >>fd, '}' elif c[0] == 'b': - wrapper_decl(s,'jboolean',fd) + print >>fd, wrapper_decl(s,'jboolean') print >>fd, ') {' print >>fd, '#ifdef %s' % c[1] print >>fd, ' return 1;' @@ -386,73 +381,87 @@ protected static native void destroy(long cptr); print >> fd, '/* End of declaration of constants */' def jni_return_type(self, type): - if type == 'gboolean': + if type is None: + return 'void' + elif is_boolean(type): return 'jboolean' - elif type in ['int','gint'] + self.binding_data.enums: + elif is_int(type, self.binding_data): return 'jint' - elif type in ('char*', 'gchar*', 'const char*', 'const gchar*'): + elif is_cstring(type): return 'jstring' - elif type in ('const GList*','GList*','GHashTable*'): + elif is_glist(type) or is_hashtable(type): return 'jobjectArray' - elif type == 'xmlNode*': + elif is_xml_node(type): return 'jstring' - elif not type: - return 'void' - else: + elif is_object(type): return 'jobject' + else: + raise Exception('No jni_return_type for %s' % type) - def c_to_java_value(self, left, right, type, options): - if type == 'gboolean': + def c_to_java_value(self, left, right, type): + if is_boolean(type): return '%s = (jboolean)%s' % (left,right) - elif type in ['int', 'gint'] + self.binding_data.enums: + elif is_int(type, self.binding_data): return '%s = (jint)%s' % (left, right) - elif is_string_type(type): + elif is_cstring(type): return 'string_to_jstring(env, %s, &%s)' % (right, left) - elif type in ('const GList*','GList*',): - element_type = options.get('element-type') - if element_type == 'char*': + elif is_glist(type): + el_type = element_type(type) + if is_cstring(el_type): return 'get_list_of_strings(env, %s, &%s)' % (right, left) - elif element_type == 'xmlNode*': + elif is_xml_node(el_type): return 'get_list_of_xml_nodes(env, %s, &%s)' % (right, left) - else: + elif is_object(el_type): return 'get_list_of_objects(env, %s, &%s)' % (right, left) + else: + raise Exception('c_to_java_value failed, %s' % ((left, right, type),)) elif is_hashtable(type): - element_type = options.get('element-type') - if is_object(element_type): + el_type = element_type(type) + if is_object(el_type): return 'get_hash_of_objects(env, %s, &%s)' % (right, left) else: return 'get_hash_of_strings(env, %s, &%s)' % (right, left) - elif type == 'xmlNode*': + elif is_xml_node(type): return 'xml_node_to_jstring(env, %s, &%s)' % (right, left) - else: - if options.get('return_owner'): + elif is_object(type): + if is_transfer_full(type): return 'gobject_to_jobject(env, (GObject*)%s, &%s);' % (right, left) else: return 'gobject_to_jobject_and_ref(env, (GObject*)%s, &%s);' % (right, left) + else: + raise Exception('c_to_java_value failed, %s' % ((left, right, type),)) - def java_to_c_value(self, left, right, type, options): - if type in ['gboolean','int', 'gint'] + self.binding_data.enums: - return '%s = (%s)%s;' % (left,type,right) - elif is_string_type(type): + def java_to_c_value(self, left, right, type, full = False): + if is_boolean(type) or is_int(type, self.binding_data): + return '%s = (%s)%s;' % (left,arg_type(type),right) + elif is_cstring(type): return 'jstring_to_string(env, %s, (char**)&%s);' % (right,left) - elif type in ('const GList*','GList*',): - element_type = options.get('element-type') - if element_type == 'char*': + elif is_glist(type): + el_type = element_type(type) + if is_cstring(el_type): return 'set_list_of_strings(env, &%s,%s);' % (left,right) - elif element_type == 'xmlNode*': + elif is_xml_node(el_type): return 'set_list_of_xml_nodes(env, &%s, %s);' % (left, right) - else: + elif is_object(el_type): return 'set_list_of_objects(env, &%s, %s);' % (left, right) - elif type in ('GHashTable*',): - element_type = options.get('element-type') - if element_type == 'char*': - return 'set_hash_of_strings(env, %s, %s);' % (left,right) else: + raise Exception('java_to_c_value failed: %s' % ((left, right, type),)) + elif is_hashtable(type): + el_type = element_type(type) + if is_object(el_type): return 'set_hash_of_objects(env, %s, %s);' % (left,right) - elif type == 'xmlNode*': + else: + return 'set_hash_of_strings(env, %s, %s);' % (left,right) + elif is_xml_node(type): return 'jstring_to_xml_node(env, %s, &%s);' % (right, left) + elif is_object(type): + if is_transfer_full(type) or full: + return 'jobject_to_gobject(env, %s, (GObject**)&%s);' % (right, left) + else: + return 'jobject_to_gobject_noref(env, %s, (GObject**)&%s);' % (right, left) else: - return 'jobject_to_gobject(env, %s, (GObject**)&%s);' % (right, left) + raise Exception('java_to_c_value failed: %s' % ((left, right, type),)) + def generate_wrapper_function(self, m, fd): print >> fd, '/* Wrapper function for ', @@ -476,7 +485,7 @@ protected static native void destroy(long cptr); jtype = 'jlong' else: jtype = self.jni_return_type(m.return_type) - wrapper_decl(name, jtype, fd) + print >>fd, wrapper_decl(name, jtype) parse_tuple_format = [] parse_tuple_args = [] idx = 0 @@ -494,7 +503,10 @@ protected static native void destroy(long cptr); for arg in m.args: idx = idx + 1 arg_type, arg_name, arg_options = arg - print >> fd, ' %s %s;' % (arg_type.replace('const ',''),arg_name) + if is_pointer(arg): + print >> fd, ' %s %s = NULL;' % (arg_type.replace('const ',''),arg_name) + else: + print >> fd, ' %s %s;' % (arg_type.replace('const ',''),arg_name) # Declare return vars if m.return_type: print >> fd, ' %s return_value;' % m.return_type @@ -503,7 +515,7 @@ protected static native void destroy(long cptr); for arg in m.args: idx = idx + 1 arg_type, arg_name, arg_options = arg - print >> fd, ' %s' % self.java_to_c_value(arg_name, 'jarg%s' % idx, arg_type, arg_options) + print >> fd, ' %s' % self.java_to_c_value(arg_name, 'jarg%s' % idx, arg) if debug: print >> fd, ' printf("%s' % name, arglist = '' @@ -512,7 +524,7 @@ protected static native void destroy(long cptr); arglist = arglist + ', %s' % arg_name if self.is_int_type(arg_type): print >> fd, '%i', - elif is_string_type(arg_type): + elif is_cstring(arg_type): print >> fd, '%s', else: print >> fd, '%p', @@ -534,11 +546,11 @@ protected static native void destroy(long cptr); for arg in m.args: idx=idx+1 arg_type, arg_name, arg_options = arg - if is_string_type(arg_type): + if is_cstring(arg_type): print >> fd, ' if (%s)' % arg_name print >> fd, ' g_free(%s);' % arg_name elif arg_type == 'GList*' or arg_type == 'const GList*': - if is_string_type(arg_options.get('element-type')): + if is_cstring(element_type(arg)): print >> fd, ' free_glist(&%s, (GFunc)free);' % arg_name elif is_object(element_type(arg)): print >> fd, ' free_glist(&%s, (GFunc)g_object_unref);' % arg_name @@ -553,133 +565,132 @@ protected static native void destroy(long cptr); options = {} if m.return_owner: options = with_return_owner({}) - print >> fd, ' %s;' % self.c_to_java_value('ret','return_value', m.return_type, options) + print >> fd, ' %s;' % self.c_to_java_value('ret','return_value', m.return_arg) if m.return_owner: if m.return_type == 'GList*' or m.return_type == 'const GList*': print >> fd, ' free_glist(&return_value, NULL);' - elif is_string_type(m.return_type) and not is_const(m.return_arg): + elif is_cstring(m.return_type) and not is_const(m.return_arg): print >> fd, ' if (return_value)' print >> fd, ' g_free(return_value);' print >> fd, ' return ret;' print >> fd, ' }' + def generate_wrapper_getter(self, c, m, fd): + type = arg_type(m) + name = arg_name(m) + klass = c.name + prefix = self.JNI_member_function_prefix(c,m) + return_type = self.jni_return_type(m) + signature = wrapper_decl("%s_get" % prefix, return_type) + field = 'gobj->%s' % name + d = locals() + print >>fd, ''' +/* Getter for %(type)s %(klass)s.%(name)s */ +%(signature)s, jobject jobj) { + %(klass)s *gobj = NULL; + jobject_to_gobject_noref(env, jobj, (GObject**)&gobj);''' % d + if debug: + print >> fd, ' printf("%(prefix)s_get %%p %%p\\n", gobj, %(field)s);' % d + print >> fd, ' %(return_type)s ret = 0;' % d + print >> fd, ' if (gobj) {' + print >> fd, ' %s;' % self.c_to_java_value ('ret', d['field'], m) + print >> fd, ''' } else { + throw_by_name(env, "java/lang/NullPointerException", "no gobject correspond to the given object"); + } + return ret; + } +''' + + def generate_wrapper_setter(self, c, m, fd): + type = arg_type(m) + name = arg_name(m) + klass = c.name + prefix = self.JNI_member_function_prefix(c,m) + return_type = self.jni_return_type(m) + signature = wrapper_decl("%s_set" % prefix, 'void') + field = 'gobj->%s' % name + d = locals() + + print >> fd,'/* Setter for %(type)s %(klass)s.%(name)s */' % d + print >> fd, '%(signature)s, jobject jobj, %(return_type)s value)\n {' % d + print >> fd, ' %(klass)s *gobj = NULL;' % d + if debug: + print >> fd, ' printf("%(prefix)s_set %%p %%p\\n", gobj, value);' % d + print >> fd, ' jobject_to_gobject_noref(env, jobj, (GObject**)&gobj);' + print >> fd, ' if (!gobj) {' + print >> fd, ' throw_by_name(env, "java/lang/NullPointerException", "no gobject correspond to the given object");' + print >> fd, ' }' + print >> fd, ' %s' % self.java_to_c_value(d['field'], 'value', m, full = True) + print >> fd, '}' + + def generate_wrapper_adder(self, c, m, fd): + type = arg_type(m) + name = arg_name(m) + el_type = element_type(m) + jni_el_type = jni_glist_elem_type(el_type) + klass = c.name + prefix = self.JNI_member_function_prefix(c,m) + return_type = self.jni_return_type(m) + signature = wrapper_decl("%s_add" % prefix, 'void') + field = 'gobj->%s' % name + d = locals() + + print >> fd,'/* Adder for %(type)s<%(el_type)s> %(klass)s.%(name)s */' % d + print >> fd, '%(signature)s, jobject jobj, %(jni_el_type)s value)\n {' % d + print >> fd, ' %(klass)s *gobj = NULL;' % d + print >> fd, ' jobject_to_gobject_noref(env, jobj, (GObject**)&gobj);' + if is_cstring(el_type): + print >> fd, ' add_to_list_of_strings(env, &%(field)s, value);' % d + elif is_xml_node(el_type): + print >> fd, ' add_to_list_of_xml_nodes(env, &%(field)s, value);' % d + elif is_object(el_type): + print >> fd, ' add_to_list_of_objects(env, &%(field)s, value);' % d + else: + raise Exception('generate_wrapper_adder failed for %s.%s' % (c,m)) + print >> fd, '}' + + def generate_wrapper_remover(self, c, m, fd): + type = arg_type(m) + name = arg_name(m) + klass = c.name + el_type = element_type(m) + jni_el_type = jni_glist_elem_type(el_type) + prefix = self.JNI_member_function_prefix(c,m) + return_type = self.jni_return_type(m) + signature = wrapper_decl("%s_remove" % prefix, 'void') + field = 'gobj->%s' % name + d = locals() + + if is_xml_node(el_type): + print >>sys.stderr, 'W: remove for list of xml node not supported: %s' % (m,) + return + print >> fd,'/* Remover for %(type)s<%(el_type)s> %(klass)s.%(name)s */' % d + print >> fd, '%(signature)s, jobject jobj, %(jni_el_type)s value)\n {' % d + print >> fd, ' %(klass)s *gobj = NULL;' % d + print >> fd, ' jobject_to_gobject_noref(env, jobj, (GObject**)&gobj);' + if is_cstring(el_type): + print >> fd, ' remove_from_list_of_strings(env, &%(field)s,value);' % d + elif is_object(el_type): + print >> fd, ' remove_from_list_of_objects(env, &%(field)s,value);' % d + else: + raise Exception('remove_from_list unsupported for %s.%s' % (c,m,)) + print >> fd, '}' + print >> fd, '' + def generate_wrapper_getter_setter(self, c, fd): klassname = c.name for m in c.members: + # getter + self.generate_wrapper_getter(c, m, fd) + self.generate_wrapper_setter(c, m, fd) mtype = m[0] prefix = self.JNI_member_function_prefix(c,m) - # getter jtype = self.jni_return_type(mtype) - print >> fd,'/* Getter for %s %s.%s */' % (mtype,klassname,m[1]) - wrapper_decl("%s_get" % prefix, jtype, fd) - print >> fd, ', jobject jobj)\n {' - print >> fd, ' %s *gobj;' % klassname - print >> fd, ' jobject_to_gobject(env, jobj, (GObject**)&gobj);' - if debug: - print >> fd, ' printf("%s_get %%p %%p\\n", gobj, gobj->%s);' % (prefix, m[1]) - print >> fd, ' %s ret = 0;' % jtype - print >> fd, ' if (gobj) {' - print >> fd, ' %s;' % self.c_to_java_value ('ret','gobj->%s' % m[1], mtype, m[2]) - print >> fd, ' } else {' - print >> fd, ' throw_by_name(env, "java/lang/NullPointerException", "no gobject correspond to the given object");' - print >> fd, ' }' - print >> fd, ' return ret;' - print >> fd, '}' - print >> fd, '' - # setter - print >> fd,'/* Setter for %s %s.%s */' % (mtype,klassname,m[1]) - wrapper_decl("%s_set" % prefix, 'void', fd) - print >> fd, ', jobject jobj, %s value)\n {' % self.jni_return_type(mtype) - print >> fd, ' %s *gobj;' % klassname - if debug: - print >> fd, ' printf("%s_set %%p %%p\\n", gobj, value);' % prefix - print >> fd, ' jobject_to_gobject(env, jobj, (GObject**)&gobj);' - print >> fd, ' if (!gobj) {' - print >> fd, ' throw_by_name(env, "java/lang/NullPointerException", "no gobject correspond to the given object");' - print >> fd, ' }' - if not self.is_int_type(mtype) and not is_collection(mtype): - print >> fd, ' if (gobj->%s) {' % m[1] - if is_string_type(mtype): - print >> fd, ' g_free(gobj->%s);' % m[1] - else: - print >> fd, ' g_object_unref(gobj->%s);' % m[1] - print >> fd, ' }' - print >> fd, ' %s' % self.java_to_c_value('gobj->%s' % m[1], 'value', mtype, m[2]) - if self.is_gobject_type(mtype): - print >> fd, ' if (gobj->%s) {' % m[1] - print >> fd, ' g_object_ref(gobj->%s);' % m[1] - print >> fd, ' }' - print >> fd, '}' # add/remove - if mtype in ('const GList*','GList*', ): - # add - print >> fd,'/* Adder for %s %s.%s */' % (mtype,klassname,m[1]) - element_type = m[2].get('element-type') - wrapper_decl("%s_add" % prefix, 'void', fd) - print >> fd, ', jobject jobj, %s value)\n {' % jni_elem_type(element_type) - print >> fd, ' %s *gobj;' % klassname - print >> fd, ' jobject_to_gobject(env, jobj, (GObject**)&gobj);' - if is_string_type(element_type): - print >> fd, ' add_to_list_of_strings(env, &gobj->%s,value);' % m[1] - elif element_type in ('xmlNode*',): - print >> fd, ' add_to_list_of_xml_nodes(env, &gobj->%s,value);' % m[1] - else: - print >> fd, ' add_to_list_of_objects(env, &gobj->%s,value);' % m[1] - print >> fd, '}' - # remove - if is_xml_node(element_type): - print >>sys.stderr, 'W: remove for list of xml node not supported: %s' % (m,) - else: - print >> fd,'/* Remover for %s %s.%s */' % (mtype,klassname,m[1]) - wrapper_decl("%s_remove" % prefix, 'void', fd) - print >> fd, ', jobject jobj, %s value)\n {' % jni_elem_type(element_type) - print >> fd, ' %s *gobj;' % klassname - print >> fd, ' jobject_to_gobject(env, jobj, (GObject**)&gobj);' - if is_cstring(element_type): - print >> fd, ' remove_from_list_of_strings(env, &gobj->%s,value);' % m[1] - elif is_object(element_type): - print >> fd, ' remove_from_list_of_objects(env, &gobj->%s,value);' % m[1] - else: - raise Exception('remove_from_list unsupported for %s' % (m,)) - print >> fd, '}' - # add/remove/get_by_name - if mtype in ('GHashTable*',): - # add - print >> fd,'/* Adder for %s %s.%s */' % (mtype,klassname,m[1]) - element_type = m[2].get('element-type') - wrapper_decl("%s_add" % prefix, 'void', fd) - print >> fd, ', jobject jobj, jstring key, %s value)\n {' % jni_elem_type(element_type) - print >> fd, ' %s *gobj;' % klassname - print >> fd, ' jobject_to_gobject(env, jobj, (GObject**)&gobj);' - if element_type in ('char*','gchar*'): - print >> fd, ' add_to_hash_of_strings(env, gobj->%s,value,key);' % m[1] - else: - print >> fd, ' add_to_hash_of_objects(env, gobj->%s,value,key);' % m[1] - print >> fd, '}' -# # remove -# print >> fd,'/* Remover for %s %s.%s */' % (mtype,klassname,m[1]) -# wrapper_decl("%s_remove" % prefix, 'void', fd) -# print >> fd, ', jobject jobj, jstring key)\n {' -# print >> fd, ' %s *gobj;' % klassname -# print >> fd, ' jobject_to_gobject(env, jobj, (GObject**)&gobj);' -# if element_type in ('char*','gchar*'): -# print >> fd, ' remove_from_hash_of_strings(env, gobj->%s,key);' % m[1] -# else: -# print >> fd, ' remove_from_hash_of_objects(env, gobj->%s,key);' % m[1] -# print >> fd, '}' -# # get by name -# print >> fd,'/* Get by name for %s %s.%s */' % (mtype,klassname,m[1]) -# wrapper_decl("%s_get_by_name" % prefix, jni_elem_type(element_type) , fd) -# print >> fd, ', jobject jobj, jstring key)\n {' -# print >> fd, ' %s *gobj;' % klassname -# print >> fd, ' jobject_to_gobject(env, jobj, (GObject**)&gobj);' -# if element_type in ('char*','gchar*'): -# print >> fd, ' return get_hash_of_strings_by_name(env, gobj->%s,key);' % m[1] -# else: -# print >> fd, ' return get_hash_of_objects_by_name(env, gobj->%s,key);' % m[1] -# print >> fd, '}' + if is_glist(mtype): + self.generate_wrapper_adder(c, m, fd) + self.generate_wrapper_remover(c, m, fd) -# def generate_exception_switch_case(self, fd, name, orig): print >> fd, ' if (errorCode == LassoConstants.%s) {' % orig[6:] print >> fd, ' throw new %s(errorCode);' % name @@ -830,17 +841,6 @@ protected static native void destroy(long cptr); print >> fd, ' public Map get%s() {' % jname print >> fd, ' return arrayToMap(LassoJNI.%s_get(this));' % prefix print >> fd, ' }' - print >> fd, ' public void addTo%s(String key, %s value) {' % (jname,jtype) - print >> fd, ' LassoJNI.%s_add(this, key, value);' % prefix - print >> fd, ' }' -# print >> fd, ' public void removeFrom%s(String key) {' % (jname) -# print >> fd, ' LassoJNI.%s_remove(this, key);' % prefix -# print >> fd, ' }' - #print ' void set%s(%s[] value)' % (jname,jtype) - #print ' %s[] get%s()' % (jtype,jname) - #print ' void addTo%s(String key, %s value)' % (jname,jtype) - #print ' void removeFrom%s(String key)' % (jname,jtype) - #print ' %s get%sByName(String key)' % (jtype,jname) else: print >> fd, ' public void set%s(%s value) {' % (jname,jtype) print >> fd, ' LassoJNI.%s_set(this, value);' % prefix @@ -848,8 +848,6 @@ protected static native void destroy(long cptr); print >> fd, ' public %s get%s() {' % (jtype,jname) print >> fd, ' return LassoJNI.%s_get(this);' % prefix print >> fd, ' }' - #print ' void set%s(%s value)' % (jname,jtype) - #print ' %s get%s()' % (jtype,jname) print >> fd, ' /* Methods */' for m in c.methods: return_type = self.JNI_return_type(m.return_type) diff --git a/bindings/java/wrapper_top.c b/bindings/java/wrapper_top.c index a4b8a08b..b8fa0c99 100644 --- a/bindings/java/wrapper_top.c +++ b/bindings/java/wrapper_top.c @@ -44,7 +44,7 @@ G_GNUC_UNUSED static int gobject_to_jobject(JNIEnv *env, GObject *obj, jobject * G_GNUC_UNUSED static int gobject_to_jobject_and_ref(JNIEnv *env, GObject *obj, jobject *jobj); G_GNUC_UNUSED static int jobject_to_gobject(JNIEnv *env, jobject obj, GObject **gobj); -G_GNUC_UNUSED static int jobject_to_gobject_for_list(JNIEnv *env, jobject obj, GObject **gobj); +G_GNUC_UNUSED static int jobject_to_gobject_noref(JNIEnv *env, jobject obj, GObject **gobj); G_GNUC_UNUSED static void free_glist(GList **list, GFunc free_function); G_GNUC_UNUSED static int get_list(JNIEnv *env, const char *clsName, const GList *list, Converter convert, jobjectArray *jarr); G_GNUC_UNUSED static int set_list(JNIEnv *env, GList **list, jobjectArray jarr, GFunc free_function, OutConverter convert); @@ -61,21 +61,21 @@ G_GNUC_UNUSED static int get_hash_by_name(JNIEnv *env, GHashTable *hashtable, js #define get_list_of_objects(env,list,jarr) get_list(env,"java/lang/Object",list,(Converter)gobject_to_jobject_and_ref,jarr) #define set_list_of_strings(env,list,jarr) set_list(env,list,jarr,(GFunc)g_free,(OutConverter)jstring_to_string) #define set_list_of_xml_nodes(env,list,jarr) set_list(env,list,jarr,(GFunc)xmlFreeNode,(OutConverter)jstring_to_xml_node) -#define set_list_of_objects(env,list,jarr) set_list(env,list,jarr,(GFunc)g_object_unref,(OutConverter)jobject_to_gobject_for_list) +#define set_list_of_objects(env,list,jarr) set_list(env,list,jarr,(GFunc)g_object_unref,(OutConverter)jobject_to_gobject) // remove_from_list_of_strings is now implemented directly //#define remove_from_list_of_strings(env,list,obj) remove_from_list(env,list,obj,(GFunc)g_free,(GCompareFunc)strcmp,(OutConverter)jstring_to_local_string) //#define remove_from_list_of_xml_nodes(env,list,obj) remove_from_list(env,list,obj,(GFunc)xmlFreeNode,(GCompareFunc)strcmp,(OutConverter)jstring_to_xml_node) -#define remove_from_list_of_objects(env,list,obj) remove_from_list(env,list,obj,(GFunc)g_object_unref,(GCompareFunc)gpointer_equal,(OutConverter)jobject_to_gobject) +#define remove_from_list_of_objects(env,list,obj) remove_from_list(env,list,obj,(GFunc)g_object_unref,(GCompareFunc)gpointer_equal,(OutConverter)jobject_to_gobject_noref) #define add_to_list_of_strings(env,list,obj) add_to_list(env,list,obj,(OutConverter)jstring_to_string) #define add_to_list_of_xml_nodes(env,list,obj) add_to_list(env,list,obj,(OutConverter)jstring_to_xml_node) // Use jobject_to_gobject_for_list because ref count must be augmented by one when inserted inside a list -#define add_to_list_of_objects(env,list,obj) add_to_list(env,list,obj,(OutConverter)jobject_to_gobject_for_list) +#define add_to_list_of_objects(env,list,obj) add_to_list(env,list,obj,(OutConverter)jobject_to_gobject) #define get_hash_of_strings(env,hash,jarr) get_hash(env,"java/lang/String",hash,(Converter)string_to_jstring, jarr) #define get_hash_of_objects(env,hash,jarr) get_hash(env,"java/lang/Object",hash,(Converter)gobject_to_jobject_and_ref, jarr) //#define remove_from_hash_of_strings(env,hash,key) remove_from_hash(env,hash,key) //#define remove_from_hash_of_objects(env,hash,key) remove_from_hash(env,hash,key) #define add_to_hash_of_strings(env,hash,key,obj) add_to_hash(env,hash,key,obj,(OutConverter)jstring_to_string,(GFunc)g_free) -#define add_to_hash_of_objects(env,hash,key,obj) add_to_hash(env,hash,key,obj,(OutConverter)jobject_to_gobject_for_list,(GFunc)g_object_unref) +#define add_to_hash_of_objects(env,hash,key,obj) add_to_hash(env,hash,key,obj,(OutConverter)jobject_to_gobject,(GFunc)g_object_unref) //#define get_hash_of_strings_by_name(end,hash,key) get_hash_by_name(end,hash,key,(Converter)string_to_jstring) //#define get_hash_of_objects_by_name(end,hash,key) get_hash_by_name(end,hash,key,(Converter)gobject_to_jobject_and_ref) G_GNUC_UNUSED static void throw_by_name(JNIEnv *env, const char *name, const char *msg); @@ -261,11 +261,11 @@ string_to_jstring_and_free(JNIEnv *env, char* str, jstring *jstr) { /** Convert a jstring to a C string and copy it. Returned string is owner by the caller.*/ static int jstring_to_string(JNIEnv *env, jstring jstr, char **str) { - const char *local_str; + const char *local_str = NULL; g_return_val_if_fail(jstring_to_local_string(env, jstr, &local_str), 0); if (local_str) { - *str = g_strdup(local_str); + lasso_assign_string(*str, local_str); release_local_string(env, jstr, local_str); if (!str) { /* Maybe launch a OutOfMemoryException. */ @@ -282,7 +282,7 @@ jstring_to_string(JNIEnv *env, jstring jstr, char **str) { /* xmlNode handling */ static int xml_node_to_jstring(JNIEnv *env, xmlNode *xmlnode, jstring *jstr) { - xmlOutputBufferPtr buf; + xmlOutputBufferPtr buf = NULL; g_error_if_fail(env); if (! xmlnode) { @@ -295,7 +295,7 @@ xml_node_to_jstring(JNIEnv *env, xmlNode *xmlnode, jstring *jstr) { int ret = 1; xmlNodeDumpOutput(buf, NULL, xmlnode, 0, 1, NULL); xmlOutputBufferFlush(buf); - xmlChar *str; + xmlChar *str = NULL; if (buf->conv == NULL) { str = buf->buffer->content; } else { @@ -317,7 +317,7 @@ static int jstring_to_xml_node(JNIEnv *env, jstring jstr, xmlNode **xmlnode) { xmlDoc *doc = NULL; xmlNode *node = NULL; - const char *local_str; + const char *local_str = NULL; int ret = 1; g_error_if_fail(env && xmlnode); @@ -331,12 +331,9 @@ jstring_to_xml_node(JNIEnv *env, jstring jstr, xmlNode **xmlnode) { goto out; } node = xmlDocGetRootElement(doc); - if (node != NULL) { - node = xmlCopyNode(node, 1); - } } out: - *xmlnode = node; + lasso_assign_xml_node(*xmlnode, node) if (doc) lasso_release_doc(doc); if (jstr && local_str) @@ -347,7 +344,7 @@ out: /* lasso objects handling impl */ static void create_class_name(char *dest, const char *typename) { - char *ret; + char *ret = NULL; ret = strstr(typename, "Lasso"); if (ret) { @@ -381,7 +378,7 @@ gobject_to_jobject_aux(JNIEnv *env, GObject *obj, gboolean doRef, jobject *jobj) } else { /* Create the shadow object */ char clsName[sizeof(LASSO_ROOT)+50] = LASSO_ROOT; - const char *typename; + const char *typename = NULL; typename = G_OBJECT_TYPE_NAME(obj); create_class_name(clsName, typename); @@ -416,7 +413,7 @@ gobject_to_jobject_and_ref(JNIEnv *env, GObject *obj, jobject *jobj) { static int jobject_to_gobject(JNIEnv *env, jobject obj, GObject **gobj) { jlong value = 0; - GObject *gobject; + GObject *gobject = NULL; g_error_if_fail(env); @@ -434,7 +431,7 @@ jobject_to_gobject(JNIEnv *env, jobject obj, GObject **gobj) { #undef s return 0; } else { - *gobj = gobject; + lasso_assign_gobject(*gobj, gobject); return 1; } } @@ -442,10 +439,10 @@ jobject_to_gobject(JNIEnv *env, jobject obj, GObject **gobj) { /** Get the gobject encapsulated by the java object obj and increase its ref count. The only * use for this function is composed with set_list_of_objects or set_hash_of_object. */ static int -jobject_to_gobject_for_list(JNIEnv *env, jobject obj, GObject **gobj) { +jobject_to_gobject_noref(JNIEnv *env, jobject obj, GObject **gobj) { g_return_val_if_fail(jobject_to_gobject(env, obj, gobj), 0); if (*gobj) { - g_object_ref(*gobj); + g_object_unref(*gobj); } return 1; } @@ -498,9 +495,9 @@ out: * Use convert to convert the java objects to C values. */ static int set_list(JNIEnv *env, GList **list, jobjectArray jarr, GFunc free_function, OutConverter convert) { - jobject element; - jsize size; - jsize i; + jobject element = NULL; + jsize size = 0; + jsize i = 0; GList *new = NULL; g_error_if_fail (list && free_function && convert && env); @@ -508,7 +505,7 @@ set_list(JNIEnv *env, GList **list, jobjectArray jarr, GFunc free_function, OutC if (! get_array_size(env, jarr, &size)) goto error; for (i=0; i < size; i++) { - gpointer result; + gpointer result = NULL; if (! get_array_element(env, jarr, i, &element) || ! convert(env, element, &result)) { @@ -533,8 +530,8 @@ error: **/ static int remove_from_list(JNIEnv *env, GList **list, jobject obj, GFunc free_function, GCompareFunc compare, OutConverter convert) { - gpointer data; - GList *found; + gpointer data = NULL; + GList *found = NULL; g_error_if_fail(env && list && compare && convert && free_function); g_return_val_if_fail(obj, 1); @@ -548,8 +545,8 @@ remove_from_list(JNIEnv *env, GList **list, jobject obj, GFunc free_function, GC } static int remove_from_list_of_strings(JNIEnv *env, GList **list, jstring jstr) { - const char *local_string; - GList *found; + const char *local_string = NULL; + GList *found = NULL; g_error_if_fail(env && list); g_return_val_if_fail(jstr, 1); @@ -568,7 +565,7 @@ remove_from_list_of_strings(JNIEnv *env, GList **list, jstring jstr) { */ static int add_to_list(JNIEnv* env, GList** list, jobject obj, OutConverter convert) { - gpointer data; + gpointer data = NULL; g_error_if_fail(env && list && convert); g_return_val_if_fail(convert(env, obj, &data),0); @@ -582,7 +579,7 @@ add_to_list(JNIEnv* env, GList** list, jobject obj, OutConverter convert) { static int get_hash(JNIEnv *env, char *clsName, GHashTable *hashtable, Converter convert, jobjectArray *jarr) { - jsize l,i; + jsize l = 0, i = 0; GList *keys = NULL, *values = NULL; int ret = 1; @@ -598,8 +595,8 @@ get_hash(JNIEnv *env, char *clsName, GHashTable *hashtable, Converter convert, j goto out; } for (i=0; i < 2*l && keys && values; i+=2) { - jstring key; - jobject value; + jstring key = NULL; + jobject value = NULL; if (! (string_to_jstring(env, (char*)keys->data, &key) && convert(env, (gpointer)values->data, &value) @@ -627,7 +624,7 @@ out: static int set_hash_of_objects(JNIEnv *env, GHashTable *hashtable, jobjectArray jarr) { - jsize l, i; + jsize l = 0, i = 0; g_error_if_fail (env && hashtable); if (jarr) { @@ -638,12 +635,21 @@ set_hash_of_objects(JNIEnv *env, GHashTable *hashtable, jobjectArray jarr) return 0; } for (i = 1; i < l; i += 2) { - jobject jobj; - GObject *gobj; + jobject jobj = NULL; + GObject *gobj = NULL; g_return_val_if_fail(get_array_element(env, jarr, i, &jobj), 0); - g_return_val_if_fail(jobject_to_gobject(env, jobj, &gobj), 0); - g_object_ref(gobj); + g_return_val_if_fail(jobject_to_gobject_noref(env, jobj, &gobj), 0); + (*env)->DeleteLocalRef(env, jobj); + } + /* increment ref count of objects */ + for (i = 1; i < l; i += 2) { + jobject jobj = NULL; + GObject *gobj = NULL; + + get_array_element(env, jarr, i, &jobj); + jobject_to_gobject(env, jobj, &gobj); + (*env)->DeleteLocalRef(env, jobj); } } /** Remove old values, if hashtable is well initialized @@ -652,21 +658,20 @@ set_hash_of_objects(JNIEnv *env, GHashTable *hashtable, jobjectArray jarr) /** Insert new values */ if (jarr) { for (i = 0; i < l; i += 2) { - jstring jkey; - char *key; - jobject jvalue; - GObject *value; + jstring jkey = NULL; + char *key = NULL; + jobject jvalue = NULL; + GObject *value = NULL; g_return_val_if_fail(get_array_element(env, jarr, i, &jkey), 0); g_return_val_if_fail(get_array_element(env, jarr, i+1, &jvalue), 0); g_return_val_if_fail(jstring_to_string(env, jkey, &key), 0); - if (! jobject_to_gobject(env, jvalue, &value)) { + if (! jobject_to_gobject_noref(env, jvalue, &value)) { if (key) g_free(key); g_hash_table_remove_all(hashtable); return 0; } - /* Can use insert because hash table is empty */ g_hash_table_insert (hashtable, key, value); (*env)->DeleteLocalRef(env, jkey); (*env)->DeleteLocalRef(env, jvalue); @@ -688,7 +693,7 @@ set_hash_of_objects(JNIEnv *env, GHashTable *hashtable, jobjectArray jarr) */ static int set_hash_of_strings(JNIEnv *env, GHashTable *hashtable, jobjectArray jarr) { - jsize l,i; + jsize l = 0, i = 0; g_error_if_fail (env && hashtable); @@ -700,10 +705,10 @@ set_hash_of_strings(JNIEnv *env, GHashTable *hashtable, jobjectArray jarr) { return 0; } for (i = 0; i < l; i += 2) { - jstring jkey; - char *key; - jstring jvalue; - char *value; + jstring jkey = NULL; + char *key = NULL; + jstring jvalue = NULL; + char *value = NULL; g_return_val_if_fail(get_array_element(env, jarr, i, &jkey) && get_array_element(env, jarr, i+1, &jvalue) @@ -730,7 +735,7 @@ set_hash_of_strings(JNIEnv *env, GHashTable *hashtable, jobjectArray jarr) { /** Remove the value for the given key from hashtable. */ static int remove_from_hash(JNIEnv *env, GHashTable *hashtable, jstring jkey) { - const char *key; + const char *key = NULL; g_error_if_fail (env && hashtable); @@ -764,8 +769,8 @@ error: static int get_hash_by_name(JNIEnv *env, GHashTable *hashtable, jstring jkey, Converter convert, jobject *jvalue) { - const char *key; - gpointer value; + const char *key = NULL; + gpointer value = NULL; g_error_if_fail (env && hashtable && convert); @@ -797,7 +802,7 @@ JNIEXPORT void JNICALL Java_com_entrouvert_lasso_LassoJNI_destroy(JNIEnv *env, j g_object_unref(obj); } JNIEXPORT void JNICALL Java_com_entrouvert_lasso_LassoJNI_set_1shadow_1object(JNIEnv *env, jclass cls, jlong cptr, jobject shadow_object) { - GObject *gobj; + GObject *gobj = NULL; gobj = convert_jlong_to_gobject(cptr); set_shadow_object(env, gobj, shadow_object); |