Commit 2fd4f54a authored by Sylvain Berfini's avatar Sylvain Berfini 🎩
Browse files

More work on the java wrapper

parent 11325a90
......@@ -68,8 +68,10 @@ class JavaTranslator(object):
def __init__(self, packageName):
package_dirs = packageName.split('.')
self.jni_package = ''
self.jni_path = ''
for directory in package_dirs:
self.jni_package += directory + '_'
self.jni_path += directory + '/'
self.docTranslator = metadoc.SandcastleJavaTranslator()
......@@ -230,19 +232,38 @@ class JavaTranslator(object):
methodDict = {}
methodDict['classCName'] = 'Linphone' + className.to_camel_case()
methodDict['className'] = className.to_camel_case()
methodDict['classImplName'] = className.to_camel_case() + 'Impl'
methodDict['jniPath'] = self.jni_path
methodDict['return'] = self.translate_type(_method.returnType, jni=True, isReturn=True)
methodDict['hasReturn'] = not methodDict['return'] == 'void'
methodDict['hasListReturn'] = methodDict['return'] == 'jobjectArray'
methodDict['hasReturn'] = not methodDict['return'] == 'void' and not methodDict['hasListReturn']
methodDict['hasNormalReturn'] = not methodDict['hasListReturn']
methodDict['name'] = 'Java_' + self.jni_package + className.to_camel_case() + 'Impl_' + _method.name.to_camel_case(lower=True)
methodDict['notStatic'] = not static
methodDict['c_name'] = 'linphone_' + className.to_snake_case() + "_" + _method.name.to_snake_case()
methodDict['returnObject'] = methodDict['hasReturn'] and type(_method.returnType) is AbsApi.ClassType
methodDict['returnClassName'] = self.translate_type(_method.returnType)
methodDict['isRealObjectArray'] = False
methodDict['isStringObjectArray'] = False
if methodDict['hasListReturn']:
if _method.returnType.name == 'string_array':
methodDict['isStringObjectArray'] = True
elif type(_method.returnType.containedTypeDesc) is AbsApi.ClassType:
methodDict['isRealObjectArray'] = True
methodDict['objectClassCName'] = 'Linphone' + _method.returnType.containedTypeDesc.desc.name.to_camel_case()
methodDict['objectClassName'] = _method.returnType.containedTypeDesc.desc.name.to_camel_case()
methodDict['objectClassImplName'] = _method.returnType.containedTypeDesc.desc.name.to_camel_case() + 'Impl'
else:
print 'toto'
methodDict['params'] = 'JNIEnv *env, jobject thiz' if static else 'JNIEnv *env, jobject thiz, jlong ptr'
methodDict['params_impl'] = ''
methodDict['strings'] = []
methodDict['objects'] = []
methodDict['lists'] = []
methodDict['array'] = []
methodDict['returnedObjectGetter'] = ''
for arg in _method.args:
methodDict['params'] += ', '
......@@ -254,6 +275,12 @@ class JavaTranslator(object):
methodDict['objects'].append({'object': argname, 'objectClassCName': 'Linphone' + arg.type.desc.name.to_camel_case()})
methodDict['params_impl'] += 'c_' + argname
elif type(arg.type) is AbsApi.ListType:
isStringList = type(arg.type.containedTypeDesc) is AbsApi.BaseType and arg.type.containedTypeDesc.name == 'string'
isObjList = type(arg.type.containedTypeDesc) is AbsApi.ClassType
methodDict['lists'].append({'list': argname, 'isStringList': isStringList, 'isObjList': isObjList})
methodDict['params_impl'] += 'bctbx_list_' + argname
elif type(arg.type) is AbsApi.BaseType:
if arg.type.name == 'string':
methodDict['strings'].append({'string': argname})
......
......@@ -112,16 +112,48 @@ void Java_{{jniPrefix}}{{classImplName}}_unref(JNIEnv* env, jobject thiz, jlong
{{#notStatic}}{{classCName}} *cptr = ({{classCName}}*)ptr;{{/notStatic}}
{{#strings}}
const char* c_{{string}} = GetStringUTFChars(env, {{string}});
{{/strings}}
{{#objects}}
{{/strings}}{{#objects}}
{{objectClassCName}}* c_{{object}} = NULL;
if ({{object}}) c_{{object}} = ({{objectClassCName}}*)GetObjectNativePtr(env, {{object}});
{{/objects}}
{{/objects}}{{#lists}}
bctbx_list_t *bctbx_list_{{list}} = NULL;
int count = env->GetArrayLength({{list}});
for (int i=0; i < count; i++) {
{{#isStringList}}
jstring obj = (jstring) env->GetObjectArrayElement({{list}}, i);
const char *str = GetStringUTFChars(env, obj);
if (str) {
bctbx_list_{{list}} = bctbx_list_append(bctbx_list_{{list}}, ms_strdup(str));
ReleaseStringUTFChars(env, obj, str);
}
{{/isStringList}}
{{#isObjList}}
jobject obj = env->GetObjectArrayElement({{list}}, i);
bctbx_list_{{list}} = bctbx_list_append(bctbx_list_{{list}}, GetObjectNativePtr(env, obj));
{{/isObjList}}
}
{{/lists}}{{#hasListReturn}}
bctbx_list_t *list = {{c_name}}({{#notStatic}}cptr{{/notStatic}}{{params_impl}});
size_t count = bctbx_list_size(list);
{{#isRealObjectArray}}jobjectArray jni_list_result = env->NewObjectArray(count, env->FindClass("{{jniPath}}{{objectClassImplName}}"), NULL);{{/isRealObjectArray}}
{{#isStringObjectArray}}jobjectArray jni_list_result = env->NewObjectArray(count, env->FindClass("java/lang/String"), env->NewStringUTF(""));{{/isStringObjectArray}}
for (size_t i = 0; i < count; i++) {
{{#isRealObjectArray}}
{{objectClassCName}}* c_object = ({{objectClassCName}}*)list->data;
jobject object = get{{objectClassName}}(c_object);
{{/isRealObjectArray}}
{{#isStringObjectArray}}jstring object = list->data ? env->NewStringUTF(list->data) : 0;{{/isStringObjectArray}}
if (object != 0) {
env->SetObjectArrayElement(jni_list_result, i, object);
{{#isRealObjectArray}}env->DeleteLocalRef(object);{{/isRealObjectArray}}
}
list = bctbx_list_next(list);
}
{{/hasListReturn}}{{#hasNormalReturn}}
{{#hasReturn}}{{return}} jni_result = {{#returnObject}}get{{returnClassName}}({{/returnObject}}{{/hasReturn}}{{c_name}}({{#notStatic}}cptr{{/notStatic}}{{params_impl}}){{#returnObject}}){{/returnObject}};
{{#strings}}
{{/hasNormalReturn}}{{#strings}}
ReleaseStringUTFChars(env, {{string}}, c_{{string}});
{{/strings}}
{{#hasReturn}}return jni_result;{{/hasReturn}}
{{/strings}}{{#hasReturn}}return jni_result;{{/hasReturn}}{{#hasListReturn}}return jni_list_result;{{/hasListReturn}}
}
{{/methods}}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment