def generate_fetch_v2(self): """Gets fetch method implementation code. Paris with <generate_fetch_native_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower( fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format( self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format( self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__plural_object_name) if not fetch_command.is_plural: # singular implementation if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') fetch_function += indent(4) + '@Nullable\n' fetch_function += indent(4) + 'public {0} '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + '{\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string( by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format( fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR else: # regular implementation fetch_function += indent(4) + 'public {0}[] '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + ' {\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string( by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format( fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def generate_fetch_declarations(self): declaration = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) bys = self.__convert_bys_to_string(by_list, self.fetch_has_sign) if fetch_command.alias != '': fetch_fun_name = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'Fetch{0}FromCache'.format(self.object_name) else: fetch_fun_name = 'Fetch{0}FromCache'.format(self.plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') declaration += 'JNIEXPORT jlong JNICALL Java_com_lesschat_core_{0}_{1}_native'. \ format(self.group_name, self.manager_name) declaration += fetch_fun_name + bys else: declaration += 'JNIEXPORT jlongArray JNICALL Java_com_lesschat_core_{0}_{1}_native'. \ format(self.group_name, self.manager_name) declaration += fetch_fun_name + bys return declaration
def generate_fetch_native(self): """Gets fetch method JNI part implementation code. Paris with <generate_fetch>. Returns: Implementation of JNI part of fetch methods. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__object_name) else: fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') fetch_function += indent(4) + 'private native long native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string(by_list, True, False) + ';' + _JAVA_BR else: fetch_function += indent(4) + 'private native long[] native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string(by_list, True, False) + ';' + _JAVA_BR return fetch_function
def generate_delete_declarations(self, pre_spaces): declaration = '' for delete_command in self.delete_commands: by_list = [] if delete_command.where != '': by_list = re.split(',', delete_command.where) if not delete_command.is_plural: # validation if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') # logic if delete_command.alias_or_none is None: declaration += '{0}void Delete{1}FromCache{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list)) else: declaration += '{0}void {1}{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list), delete_command.alias_or_none) else: if delete_command.alias_or_none is None: declaration += '{0}void Delete{1}FromCache{2} const;\n\n'\ .format(pre_spaces, self.plural_object_name, self.__convert_bys_to_string(by_list)) else: print(delete_command.alias_or_none) declaration += '{0}void {1}{2} const;\n\n'\ .format(pre_spaces, delete_command.alias_or_none, self.__convert_bys_to_string(by_list)) return declaration
def generate_fetch(self): fetch_function = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') fetch_function += function_space(1) + 'public {0} fetch{0}FromCache{1} {{\n' \ .format(self.__rename_object_name_if_list(), self.__convert_bys_to_string(by_list, False, False)) fetch_function += function_space(2) + 'long handler = nativeFetch{0}FromCache{1};\n\n' \ .format(self.object_name, self.__convert_bys_to_string(by_list, False, True)) fetch_function += function_space(2) + 'if (handler == JniHelper.sNullPointer) {\n' fetch_function += function_space(3) + 'return null;\n' fetch_function += function_space(2) + '}\n\n' fetch_function += function_space(2) + 'return new {0}(handler);\n'.format(self.__rename_object_name_if_list()) fetch_function += function_space(1) + '}' + _JAVA_BR else: fetch_function += function_space(1) + 'public List<{0}> fetch{1}FromCache{2} {{\n' \ .format(self.__rename_object_name_if_list(), self.plural_object_name, self.__convert_bys_to_string(by_list, False, False)) fetch_function += function_space(2) + 'long[] handlers = nativeFetch{0}FromCache{1};\n\n'\ .format(self.plural_object_name, self.__convert_bys_to_string(by_list, False, True)) fetch_function += function_space(2) + 'List<{0}> {1} = new ArrayList<>();\n'\ .format(self.__rename_object_name_if_list(), self.__to_object_name_java_style() + 's') fetch_function += function_space(2) + 'for (long handler: handlers) {\n' fetch_function += function_space(3) + '{0}.add(new {1}(handler));\n'\ .format(self.__to_object_name_java_style() + 's', self.__rename_object_name_if_list()) fetch_function += function_space(2) + '}\n\n' fetch_function += function_space(2) + 'return {0};\n'\ .format(self.__to_object_name_java_style() + 's') fetch_function += function_space(1) + '}' + _JAVA_BR return fetch_function
def generate_fetch_declarations(self, pre_spaces): declaration = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: # validation if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') # logic if fetch_command.alias_or_none is None: declaration += '{0}std::unique_ptr<{1}> Fetch{1}FromCache{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list)) else: declaration += '{0}std::unique_ptr<{1}> {3}{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list), fetch_command.alias_or_none) else: if fetch_command.alias_or_none is None: declaration += '{0}std::vector<std::unique_ptr<{1}>> Fetch{2}FromCache{3} const;\n\n'\ .format(pre_spaces, self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) else: declaration += '{0}std::vector<std::unique_ptr<{1}>> {2}{3} const;\n\n'\ .format(pre_spaces, self.object_name, fetch_command.alias_or_none, self.__convert_bys_to_string(by_list)) return declaration
def generate_delete_declarations(self, pre_spaces): declaration = '' for delete_command in self.delete_commands: by_list = [] if delete_command.where != '': by_list = re.split(',', delete_command.where) if not delete_command.is_plural: # validation if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') # logic if delete_command.alias_or_none is None: declaration += '{0}void Delete{1}FromCache{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list)) else: declaration += '{0}void {1}{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list), delete_command.alias_or_none) else: if delete_command.alias_or_none is None: declaration += '{0}void Delete{1}FromCache{2} const;\n\n'\ .format(pre_spaces, self.plural_object_name, self.__convert_bys_to_string(by_list)) else: print(delete_command.alias_or_none) declaration += '{0}void {1}{2} const;\n\n'\ .format(pre_spaces, delete_command.alias_or_none, self.__convert_bys_to_string(by_list)) return declaration
def generate_fetch_declarations(self, pre_spaces): declaration = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: # validation if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') # logic if fetch_command.alias_or_none is None: declaration += '{0}std::unique_ptr<{1}> Fetch{1}FromCache{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list)) else: declaration += '{0}std::unique_ptr<{1}> {3}{2} const;\n\n'\ .format(pre_spaces, self.object_name, self.__convert_bys_to_string(by_list), fetch_command.alias_or_none) else: if fetch_command.alias_or_none is None: declaration += '{0}std::vector<std::unique_ptr<{1}>> Fetch{2}FromCache{3} const;\n\n'\ .format(pre_spaces, self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) else: declaration += '{0}std::vector<std::unique_ptr<{1}>> {2}{3} const;\n\n'\ .format(pre_spaces, self.object_name, fetch_command.alias_or_none, self.__convert_bys_to_string(by_list)) return declaration
def generate_fetch_native(self): """Gets fetch method JNI part implementation code. Paris with <generate_fetch>. Returns: Implementation of JNI part of fetch methods. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__object_name) else: fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') fetch_function += indent( 4) + 'private native long native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string( by_list, True, False) + ';' + _JAVA_BR else: fetch_function += indent( 4) + 'private native long[] native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string( by_list, True, False) + ';' + _JAVA_BR return fetch_function
def generate_fetch_declarations(self): declaration = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) bys = self.__convert_bys_to_string(by_list, self.fetch_has_sign) if fetch_command.alias != '': fetch_fun_name = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'Fetch{0}FromCache'.format(self.object_name) else: fetch_fun_name = 'Fetch{0}FromCache'.format( self.plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') declaration += 'JNIEXPORT jlong JNICALL Java_com_lesschat_core_{0}_{1}_native'. \ format(self.group_name, self.manager_name) declaration += fetch_fun_name + bys else: declaration += 'JNIEXPORT jlongArray JNICALL Java_com_lesschat_core_{0}_{1}_native'. \ format(self.group_name, self.manager_name) declaration += fetch_fun_name + bys return declaration
def set_enum_class_name(self, enum_class_name): if enum_class_name is None: enum_class_name = '' self.enum_class_name = enum_class_name # check input if self.value == 4 and enum_class_name == '': skr_log_warning('Enum value should declare its enum class name via "enum"')
def set_enum_class_name(self, enum_class_name): if enum_class_name is None: enum_class_name = '' self.enum_class_name = enum_class_name # check input if self.value == 4 and enum_class_name == '': skr_log_warning( 'Enum value should declare its enum class name via "enum"')
def to_where_equal_sql(self): if self.var_type == VarType.cpp_string: return '{0} + "=\'" + {1} + "\'"'.format(self.to_sql_key(), self.name) elif self.var_type == VarType.cpp_string_array: skr_log_warning('SQLite where does not support array as filter') return '' elif self.var_type == VarType.cpp_enum: return '{0} + "=" + std::to_string(static_cast<int>({1}))'.format(self.to_sql_key(), self.name) else: return '{0} + "=" + std::to_string({1})'.format(self.to_sql_key(), self.name)
def generate_fetch(self): """Gets fetch method implementation code. Paris with <generate_fetch_native>. New development should use <generate_fetch_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower(fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format(self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format(self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') fetch_function += indent(4) + 'public {0} '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + '{\n' fetch_function += indent(8) + 'long handler = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string(by_list, False, True) + ';\n\n' fetch_function += indent(8) + 'if (handler == JniHelper.sNullPointer) {\n' fetch_function += indent(12) + 'return null;\n' fetch_function += indent(8) + '}\n\n' fetch_function += indent(8) + 'return new {0}(handler);\n'.format(self.__java_object_name()) fetch_function += indent(4) + '}' + _JAVA_BR else: fetch_function += indent(4) + 'public List<{0}> '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + ' {\n' fetch_function += indent(8) + 'long[] handlers = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string(by_list, False, True) + ';\n\n' fetch_function += indent(8) + 'List<{0}> {1} = new ArrayList<>();\n'\ .format(self.__java_object_name(), self.__to_object_name_java_style() + 's') fetch_function += indent(8) + 'for (long handler: handlers) {\n' fetch_function += indent(12) + '{0}.add(new {1}(handler));\n'\ .format(self.__to_object_name_java_style() + 's', self.__java_object_name()) fetch_function += indent(8) + '}\n\n' fetch_function += indent(8) + 'return {0};\n'\ .format(self.__to_object_name_java_style() + 's') fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def jni_delete_local_ref(self): """Gets JNI delete local ref method. Returns: A string which is JNI delete local ref method. """ if self.__var_type == VarType.cpp_string or self.__var_type == VarType.cpp_string_array: return 'env->DeleteLocalRef(j{0});'.format(string_utils.to_title_style_name(self.__name)) elif self.__var_type == VarType.cpp_object or self.__var_type == VarType.cpp_object_array: skr_logger.skr_log_warning('JniVariable.jni_delete_local_ref() : Not supported type') return 'env->DeleteLocalRef(j{0});'.format(string_utils.first_char_to_upper(self.__name)) else: return ''
def __cpp_fetch_method_name(self, fetch_command): by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') return 'Fetch{0}FromCache{1}'\ .format(self.object_name, self.__convert_bys_to_cpp_string(by_list)) else: return 'Fetch{0}FromCache{1}'\ .format(self.plural_object_name, self.__convert_bys_to_cpp_string(by_list))
def to_where_equal_sql(self): if self.var_type == VarType.cpp_string: return '{0} + "=\'" + {1} + "\'"'.format(self.to_sql_key(), self.name) elif self.var_type == VarType.cpp_string_array: skr_log_warning('SQLite where does not support array as filter') return '' elif self.var_type == VarType.cpp_enum: return '{0} + "=" + std::to_string(static_cast<int>({1}))'.format( self.to_sql_key(), self.name) else: return '{0} + "=" + std::to_string({1})'.format( self.to_sql_key(), self.name)
def generate_fetch_declarations(self): declaration = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') declaration += '- (nullable LCC{0} *)fetch{0}FromCache{1};\n\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list)) else: declaration += '- (NSArray<LCC{0} *> *)fetch{1}FromCache{2};\n\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) return declaration
def generate_fetch_native(self): fetch_function = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') fetch_function += function_space(1) + 'private native {0} nativeFetch{1}FromCache{2};'\ .format('long', self.object_name, self.__convert_bys_to_string(by_list, True, False)) + _JAVA_BR else: fetch_function += function_space(1) + 'private native long[] nativeFetch{0}FromCache{1};'\ .format(self.plural_object_name, self.__convert_bys_to_string(by_list, True, False)) + _JAVA_BR return fetch_function
def generate_fetch_declarations(self): declaration = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') declaration += '- (nullable LCC{0} *)fetch{0}FromCache{1};\n\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list)) else: declaration += '- (NSArray<LCC{0} *> *)fetch{1}FromCache{2};\n\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) return declaration
def generate_fetch_v2(self): """Gets fetch method implementation code. Paris with <generate_fetch_native_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower(fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format(self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format(self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__plural_object_name) if not fetch_command.is_plural: # singular implementation if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') fetch_function += indent(4) + '@Nullable\n' fetch_function += indent(4) + 'public {0} '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + '{\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string(by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format(fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR else: # regular implementation fetch_function += indent(4) + 'public {0}[] '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + ' {\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string(by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format(fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def generate_android_enum(self, pre_spaces): """Generates Android Style enums, uses int with @IntDef decoration. Args: pre_spaces: A string with only white spaces describes the spaces in front of each line of code. Returns: A string which is Java enum class implementation. For example: Retention(RetentionPolicy.SOURCE) @IntDef({VISIBILITY_PUBLIC, VISIBILITY_PRIVATE}) public @interface Visibility {} public static final int VISIBILITY_PUBLIC = 1; public static final int VISIBILITY_PRIVATE = 2; """ name_list = [] int_def_list = [] name_prefix = '{0}_'.format(self.__enum_class_name.upper()) for int_alias_tuple in self.__int_alias_tuple_list: name = '{0}{1}'.format(name_prefix, int_alias_tuple[1]) name_list.append(name) int_def_list.append('public static final int {0} = {1};'.format( name, int_alias_tuple[0])) java_enum = '{0}@Retention(RetentionPolicy.SOURCE)\n'.format( pre_spaces) java_enum += '{0}@IntDef({{'.format(pre_spaces) if len(name_list) > 0: for name in name_list: java_enum += '{0}, '.format(name) java_enum = java_enum[:-2] # remove comma and space else: skr_log_warning('Java enum should at least has one definition') java_enum += '})\n' java_enum += '{0}public @interface {1} {{}}\n'.format( pre_spaces, self.__enum_class_name) for int_def in int_def_list: java_enum += '{0}{1}\n'.format(pre_spaces, int_def) return java_enum
def to_jni_signature_v2(self): if self.value == 1: return 'Z' elif self.value == 2: return 'I' elif self.value == 3: return 'Ljava/lang/String;' elif self.value == 4: return 'I' elif self.value == 5: return '[Ljava/lang/String;' elif self.value == 6: return 'J' elif self.value == 7: # cannot generate now return '[Ljava/lang/Object;' elif self.value == 8: # cannot generate now return 'Ljava/lang/Object;' else: skr_log_warning('Unsupported value')
def to_jni_signature(self): if self.value == 1: return 'Z' elif self.value == 2: return 'I' elif self.value == 3: return 'Ljava/lang/String;' elif self.value == 4: return 'I' elif self.value == 5: return '[Ljava/lang/String;' elif self.value == 6: return 'J' elif self.value == 7: return '[J' elif self.value == 8: return 'J' else: skr_log_warning('Unsupported value')
def __jni_variable_from_cpp_class(self, config): instance_name = string_utils.cpp_class_name_to_cpp_file_name( self.__class_name) if self.__var_type == VarType.cpp_bool: return 'static_cast<jboolean>({0}.is_{1}())'.format( instance_name, self.__name) elif self.__var_type == VarType.cpp_enum: return 'static_cast<jint>({0}.{1}())'.format( instance_name, self.__name) elif self.__var_type == VarType.cpp_int: return 'static_cast<jint>({0}.{1}())'.format( instance_name, self.__name) elif self.__var_type == VarType.cpp_string: return 'env->NewStringUTF({0}.{1}().c_str())'.format( instance_name, self.__name) elif self.__var_type == VarType.cpp_string_array: return '{2}::JniHelper::JobjectArrayFromStringVector({0}.{1}())'.format( instance_name, self.__name, config.cpp_namespace) elif self.__var_type == VarType.cpp_time: return 'static_cast<jlong>({0}.{1}()) * 1000'.format( instance_name, self.__name) elif self.__var_type == VarType.cpp_object: skr_logger.skr_log_warning( 'JniVariable.__jni_variable_from_cpp_class() : C++ object is not supported' ) return '' elif self.__var_type == VarType.cpp_object_array: skr_logger.skr_log_warning( 'JniVariable.__jni_variable_from_cpp_class() : C++ object array is not supported' ) return '' else: skr_logger.skr_log_warning( 'JniVariable.__jni_variable_from_cpp_class() : Not supported type' )
def __jni_variable_from_cpp_class(self, config): instance_name = string_utils.cpp_class_name_to_cpp_file_name(self.__class_name) if self.__var_type == VarType.cpp_bool: return 'static_cast<jboolean>({0}.is_{1}())'.format(instance_name, self.__name) elif self.__var_type == VarType.cpp_enum: return 'static_cast<jint>({0}.{1}())'.format(instance_name, self.__name) elif self.__var_type == VarType.cpp_int: return 'static_cast<jint>({0}.{1}())'.format(instance_name, self.__name) elif self.__var_type == VarType.cpp_string: return 'env->NewStringUTF({0}.{1}().c_str())'.format(instance_name, self.__name) elif self.__var_type == VarType.cpp_string_array: return '{2}::JniHelper::JobjectArrayFromStringVector({0}.{1}())'.format(instance_name, self.__name, config.cpp_namespace) elif self.__var_type == VarType.cpp_time: return 'static_cast<jlong>({0}.{1}()) * 1000'.format(instance_name, self.__name) elif self.__var_type == VarType.cpp_object: skr_logger.skr_log_warning('JniVariable.__jni_variable_from_cpp_class() : C++ object is not supported') return '' elif self.__var_type == VarType.cpp_object_array: skr_logger.skr_log_warning( 'JniVariable.__jni_variable_from_cpp_class() : C++ object array is not supported') return '' else: skr_logger.skr_log_warning('JniVariable.__jni_variable_from_cpp_class() : Not supported type')
def generate_fetch_declarations(self, config): """Generates Objective-C++ fetch declarations. Args: config: A <Config> object represents user-defined info. Returns: A string which is Objective-C++ fetch declarations. """ declaration = '' for fetch_command in self.fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') declaration += '- (nullable {2}{0} *)fetch{0}FromCache{1};\n\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list), config.objc_prefix) else: declaration += '- (NSArray<LCC{0} *> *)fetch{1}FromCache{2};\n\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) return declaration
def generate_http_function_implementations_v2(self, config): """Generates HTTP - Cache methods. Args: config: A <Config> object represents user-defined config including namespace and package path. Returns: A string that is JNI http methods. """ namespace = config.cpp_namespace implementation = '' for api in self.apis: implementation += 'JNIEXPORT void JNICALL Java_{3}_{0}_{1}_native{2}\n' \ .format(self.group_name, self.manager_name, api.function_name, config.jni_package_path) implementation += ' (JNIEnv* env, jobject thiz, jlong handler' for input_var in api.input_var_list: implementation += ', {0} {1}'.format( input_var.var_type.to_jni_getter_string(), input_var.to_param_style_name()) implementation += ', jobject success, jobject failure) {\n' implementation += function_space(1) + 'const {1}::{0}* core_manager = ' \ 'reinterpret_cast<{1}::{0}*>(handler);\n\n' \ .format(self.manager_name, namespace) response_interface = '' if len(api.output_var_list) != 0: response_interface += function_space(1) response_interface += 'jclass global_on_{0}_jinterface = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass(on_{0}_jinterface.c_str())));'\ .format(api.function_name) implementation += response_interface implementation += "\n" implementation += function_space( 1 ) + 'jobject global_success_listener = env->NewGlobalRef(success);\n' implementation += function_space( 1 ) + 'jobject global_failure_listener = env->NewGlobalRef(failure);\n' for input_var in api.input_var_list: implementation += _JNI_SPACE + input_var.to_getter_string( ) + " cpp_" + input_var.to_param_style_name() implementation += ' = ' + input_var.cpp_variable_from_jni_variable( input_var.to_param_style_name(), config) implementation += ';\n' implementation += '\n' implementation += function_space(1) + 'core_manager->{0}('.format( api.function_name) space_length = len( function_space(1) + 'core_manager->{0}('.format(api.function_name)) space = '' for i in range(space_length): space += " " index = 0 for input_var in api.input_var_list: if index == 0: implementation += 'cpp_{0},\n'.format( input_var.to_param_style_name()) else: implementation += space + 'cpp_{0},\n'.format( input_var.to_param_style_name()) index += 1 para_to_inner = '[' if len(response_interface) != 0: para_to_inner += 'global_on_{0}_jinterface'.format( api.function_name) + ', ' para_to_inner += 'global_success_listener, global_failure_listener](' if len(api.input_var_list) > 0: implementation += space + para_to_inner else: implementation += para_to_inner space_length = len(para_to_inner) for i in range(space_length): space += " " implementation += 'bool success,\n' implementation += space + '{0} error'.format(config.cpp_error_type) for i in range(len(api.output_var_list)): implementation += ',\n{0}{1} {2}'.format( space, api.output_var_list[i].to_getter_string(namespace), api.output_var_list[i].name) implementation += '){\n' implementation += function_space( 2 ) + 'JNIEnv *thread_env = {0}::JniHelper::GetJniEnv();\n'.format( namespace) implementation += function_space( 2 ) + 'jmethodID successMethod = thread_env->GetMethodID(' + '\n' if len(api.output_var_list) == 0: implementation += function_space(4) + 'lesschat::JniReferenceCache::SharedCache()' \ '->web_success_jinterface(),' + '\n' implementation += function_space( 4) + '"onResponse", "()V"' + "\n" else: implementation += function_space( 4) + 'global_on_{0}_jinterface, \n'.format( api.function_name) implementation += function_space(4) + '"on{0}",\n'.format( api.function_name) java_callback_function_sign_str = '' for output_var in api.output_var_list: java_callback_function_sign_str += output_var.var_type.to_jni_signature_v2( ) implementation += function_space(4) + '"({0})V"\n'.format( java_callback_function_sign_str) implementation += function_space(2) + ");\n" implementation += function_space( 2 ) + 'jmethodID failureMethod = thread_env->GetMethodID(' + '\n' implementation += function_space(4) + 'lesschat::JniReferenceCache::SharedCache()' \ '->web_failure_jinterface(),\n' implementation += function_space(4) + '"onFailure", \n' implementation += function_space(4) + '"(Ljava/lang/String;)V"\n' implementation += function_space(2) + ');\n' if config.cpp_error_type == 'const std::string&': implementation += function_space( 2 ) + 'jstring jerror = thread_env->NewStringUTF(error.c_str());\n\n' elif config.cpp_error_type == 'worktile::WebApi::Error': implementation += function_space( 2 ) + 'jstring jerror = jni_env->NewStringUTF(error.description.c_str());\n\n' else: skr_log_warning( 'This is a tmp ugly implementation, you should support your own type here.' ) implementation += function_space(2) + 'if (success){\n' implementation += function_space( 3) + 'thread_env->CallVoidMethod(\n' implementation += function_space(4) + 'global_success_listener,\n' implementation += function_space(4) + 'successMethod' if len(api.output_var_list) != 0: for output_var in api.output_var_list: implementation += ',\n' + function_space(3) + \ output_var.jni_variable_from_cpp_variable_v2(output_var.name, namespace) implementation += '\n' implementation += function_space(2) + ');\n' implementation += function_space(2) + '} else {\n' implementation += function_space( 3) + 'thread_env->CallVoidMethod(\n' implementation += function_space(4) + 'global_failure_listener,\n' implementation += function_space(4) + 'failureMethod,\n' implementation += function_space(4) + 'jerror);\n' implementation += function_space(2) + '}\n' if len(api.output_var_list) != 0: implementation += function_space(2) + 'thread_env->DeleteGlobalRef(global_on_{0}_jinterface);\n'\ .format(api.function_name) implementation += function_space( 2) + 'thread_env->DeleteGlobalRef(global_failure_listener);\n' implementation += function_space( 2) + 'thread_env->DeleteGlobalRef(global_success_listener);\n' implementation += function_space(1) + '});\n' implementation += '}\n\n' return implementation
def __fetch_implementation(self, fetch_command): by_list = [] where_sql = 'string where_condition = "";' if fetch_command.where != '': by_list = re.split(',', fetch_command.where) where_sql = self.__convert_bys_to_sql_where(by_list) if not fetch_command.is_plural: impl = '' if fetch_command.alias_or_none is None: impl += 'std::unique_ptr<{0}> {2}::Fetch{0}FromCache{1} const {{\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list), self.manager_name) else: impl += 'std::unique_ptr<{0}> {2}::{3}{1} const {{\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list), self.manager_name, fetch_command.alias_or_none) impl += _CPP_SPACE + where_sql + _CPP_BR impl += ' LCC_DB_LOCK_GUARD;' + _CPP_BR tb_list = copy.copy(fetch_command.table_name_list) if len(tb_list) == 0: impl += ' {0}->open(where_condition);\n'.format( self.__sqlite_tb_name()) impl += ' if ({0}->recordCount() != 0) {{\n'.format( self.__sqlite_tb_name()) impl += ' sql::Record* record = {0}->getRecord(0);\n'.format( self.__sqlite_tb_name()) impl += ' unique_ptr<{0}> rtn({0}FromRecord(record));\n'.format( self.object_name) impl += ' return rtn;\n' impl += ' }' + _CPP_BR else: for table_name in tb_list: impl += ' {0}->open(where_condition);\n'.format( self.__sqlite_tb_name(table_name)) impl += ' if ({0}->recordCount() != 0) {{\n'.format( self.__sqlite_tb_name(table_name)) impl += ' sql::Record* record = {0}->getRecord(0);\n'.format( self.__sqlite_tb_name(table_name)) impl += ' unique_ptr<{0}> rtn({0}FromRecord(record));\n'.format( self.object_name) impl += ' return rtn;\n' impl += ' }' + _CPP_BR impl += ' return nullptr;\n' impl += '}' return impl else: # validations # table_name_list is not supported in plural case. if len(fetch_command.table_name_list ) > 0 and fetch_command.alias_or_none is None: skr_log_warning( '"tables" attribute only supported <fetch singular="true"/>' ) elif len(fetch_command.table_name_list ) > 1 and fetch_command.alias_or_none is not None: skr_log_warning( '"tables" attribute only supported at most 1 override name' ) # logic override_table_name = '' if len(fetch_command.table_name_list) > 0: override_table_name = fetch_command.table_name_list[0] impl = '' if fetch_command.alias_or_none is None: impl += 'std::vector<std::unique_ptr<{0}>> {3}::Fetch{1}FromCache{2} const {{\n\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list), self.manager_name) else: impl += 'std::vector<std::unique_ptr<{0}>> {3}::{1}{2} const {{\n\n'\ .format(self.object_name, fetch_command.alias_or_none, self.__convert_bys_to_string(by_list), self.manager_name) impl += ' vector<unique_ptr<{0}>> {1};\n\n'.format( self.object_name, self.plural_object_name.lower()) impl += _CPP_SPACE + where_sql + _CPP_BR impl += ' LockMainDatabase();' + _CPP_BR impl += ' {0}->open(where_condition{1});\n\n'.format( self.__sqlite_tb_name(override_table_name), fetch_command.sort_sql()) impl += ' for (int i = 0; i < {0}->recordCount(); ++i) {{\n'.format( self.__sqlite_tb_name(override_table_name)) impl += ' sql::Record* record = {0}->getRecord(i);\n'.format( self.__sqlite_tb_name(override_table_name)) impl += ' {0}.push_back({1}FromRecord(record));\n'.format( self.plural_object_name.lower(), self.object_name) impl += ' }\n\n' impl += ' UnlockMainDatabase();' + _CPP_BR impl += ' return {0};\n'.format(self.plural_object_name.lower()) impl += '}' return impl
def __fetch_implementation_v2(self, fetch_command, config): by_list = [] impl = '' if fetch_command.where != '': by_list = re.split(',', fetch_command.where) bys = self.__convert_bys_to_string_impl(by_list, self.fetch_has_sign) if fetch_command.alias != '': fetch_fun_name = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'Fetch{0}FromCache'.format(self.object_name) else: fetch_fun_name = 'Fetch{0}FromCache'.format( self.plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') impl += 'JNIEXPORT jobject JNICALL Java_{2}_{0}_{1}_native' \ .format(self.group_name, self.manager_name, config.jni_package_path) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const {1}::{0}* coreManager = reinterpret_cast<{1}::{0}*>(handler);' \ .format(self.manager_name, config.cpp_namespace) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.var_type.to_getter_string( ) + " cpp" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable( jni_var.to_param_style_name(), config) + ';' cpp_method_param += 'cpp' + jni_var.to_param_style_name( ) + ', ' impl += _JNI_BR if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE + 'std::unique_ptr<{3}::{0}> coreObject = coreManager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param, config.cpp_namespace) impl += _JNI_SPACE + 'if(coreObject == nullptr){\n return NULL;\n }\n' impl += _JNI_SPACE + 'return {1}::JniHelper::GetJ{0}ByCore{0}(*coreObject);\n}}'.format( self.object_name, config.cpp_namespace) else: impl += 'JNIEXPORT jobjectArray JNICALL Java_{2}_{0}_{1}_native' \ .format(self.group_name, self.manager_name, config.jni_package_path) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const {1}::{0}* coreManager = reinterpret_cast<{1}::{0}*>(handler);' \ .format(self.manager_name, config.cpp_namespace) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.to_getter_string( ) + " cpp" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable( jni_var.to_param_style_name(), config) + ';\n' cpp_method_param += 'cpp' + jni_var.to_param_style_name( ) + ', ' if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE impl += 'std::vector<std::unique_ptr<{3}::{0}>> coreObjects = coreManager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param, config.cpp_namespace) impl += _JNI_SPACE + 'return {1}::JniHelper::GetJ{0}sArrayByCore{0}s(coreObjects);\n}}'.format( self.object_name, config.cpp_namespace) return impl
def __fetch_implementation(self, fetch_command): by_list = [] impl = '' if fetch_command.where != '': by_list = re.split(',', fetch_command.where) bys = self.__convert_bys_to_string_impl(by_list, self.fetch_has_sign) if fetch_command.alias != '': fetch_fun_name = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'Fetch{0}FromCache'.format(self.object_name) else: fetch_fun_name = 'Fetch{0}FromCache'.format( self.plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') impl += 'JNIEXPORT jlong JNICALL Java_com_lesschat_core_{0}_{1}_native' \ .format(self.group_name, self.manager_name) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const lesschat::{0}* core_manager = reinterpret_cast<lesschat::{0}*>(handler);' \ .format(self.manager_name) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.var_type.to_getter_string( ) + " cpp_" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable( jni_var.to_param_style_name()) + ';' cpp_method_param += 'cpp_' + jni_var.to_param_style_name( ) + ', ' impl += _JNI_BR if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE + 'std::unique_ptr<lesschat::{0}> core_object = core_manager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param) impl += _JNI_SPACE + 'if(core_object == nullptr){\n return 0;\n }\n' impl += _JNI_SPACE + 'return reinterpret_cast<long>(core_object.release());\n}' else: impl += 'JNIEXPORT jlongArray JNICALL Java_com_lesschat_core_{0}_{1}_native' \ .format(self.group_name, self.manager_name) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const lesschat::{0}* core_manager = reinterpret_cast<lesschat::{0}*>(handler);' \ .format(self.manager_name) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.to_getter_string( ) + " cpp_" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable( jni_var.to_param_style_name()) + ';\n' cpp_method_param += 'cpp_' + jni_var.to_param_style_name( ) + ', ' if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE impl += 'std::vector<std::unique_ptr<lesschat::{0}>> core_objects = core_manager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param) impl += _JNI_SPACE + 'return lesschat::JniHelper::JlongArrayFromNativeArray(std::move(core_objects));\n}' return impl
def generate_fetch(self): """Gets fetch method implementation code. Paris with <generate_fetch_native>. New development should use <generate_fetch_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower( fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format( self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format( self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') fetch_function += indent(4) + 'public {0} '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + '{\n' fetch_function += indent( 8) + 'long handler = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string( by_list, False, True) + ';\n\n' fetch_function += indent( 8) + 'if (handler == JniHelper.sNullPointer) {\n' fetch_function += indent(12) + 'return null;\n' fetch_function += indent(8) + '}\n\n' fetch_function += indent( 8) + 'return new {0}(handler);\n'.format( self.__java_object_name()) fetch_function += indent(4) + '}' + _JAVA_BR else: fetch_function += indent(4) + 'public List<{0}> '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + ' {\n' fetch_function += indent( 8) + 'long[] handlers = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string( by_list, False, True) + ';\n\n' fetch_function += indent(8) + 'List<{0}> {1} = new ArrayList<>();\n'\ .format(self.__java_object_name(), self.__to_object_name_java_style() + 's') fetch_function += indent( 8) + 'for (long handler: handlers) {\n' fetch_function += indent(12) + '{0}.add(new {1}(handler));\n'\ .format(self.__to_object_name_java_style() + 's', self.__java_object_name()) fetch_function += indent(8) + '}\n\n' fetch_function += indent(8) + 'return {0};\n'\ .format(self.__to_object_name_java_style() + 's') fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def generate_http_function_implementations(self, config): """Generates HTTP - Cache methods. Args: config: A <Config> object represents user-defined config including namespace and package path. Returns: A string that is JNI http methods. """ namespace = config.cpp_namespace implementation = '' for api in self.apis: implementation += 'JNIEXPORT void JNICALL Java_{3}_{0}_{1}_native{2}\n'\ .format(self.group_name, self.manager_name, api.function_name, config.jni_package_path) implementation += ' (JNIEnv* env, jobject thiz, jlong handler' for input_var in api.input_var_list: implementation += ', {0} {1}'.format( input_var.var_type.to_jni_getter_string(), input_var.to_param_style_name()) implementation += ') {\n' implementation += function_space(1) + 'const {1}::{0}* core_manager = ' \ 'reinterpret_cast<{1}::{0}*>(handler);\n\n' \ .format(self.manager_name, namespace) implementation += function_space( 1) + 'jobject global_thiz = env->NewGlobalRef(thiz);\n' implementation += function_space( 1) + 'jobject global_null = env->NewGlobalRef(NULL);\n\n' for input_var in api.input_var_list: implementation += _JNI_SPACE + input_var.to_getter_string( ) + " cpp_" + input_var.to_param_style_name() implementation += ' = ' + input_var.cpp_variable_from_jni_variable( input_var.to_param_style_name(), config) implementation += ';\n' implementation += '\n' implementation += function_space(1) + 'core_manager->{0}('.format( api.function_name) space_length = len( function_space(1) + 'core_manager->{0}('.format(api.function_name)) space = '' for i in range(space_length): space += " " index = 0 for input_var in api.input_var_list: if index == 0: implementation += 'cpp_{0},\n'.format( input_var.to_param_style_name()) else: implementation += space + 'cpp_{0},\n'.format( input_var.to_param_style_name()) index += 1 if len(api.input_var_list) > 0: implementation += space + '[env, global_thiz, global_null](' else: implementation += '[env, global_thiz, global_null](' space_length = len('[env, global_thiz, global_null](') for i in range(space_length): space += " " implementation += 'bool success,\n' implementation += space + '{0} error'.format(config.cpp_error_type) for i in range(len(api.output_var_list)): implementation += ',\n{0}{1} {2}'.format( space, api.output_var_list[i].to_getter_string(namespace), api.output_var_list[i].name) implementation += '){\n' implementation += function_space( 2) + 'JNIEnv *jni_env = {0}::JniHelper::GetJniEnv();\n'.format( namespace) implementation += function_space( 2 ) + 'jclass jclazz = jni_env->GetObjectClass(global_thiz);\n\n' implementation += function_space(2) + 'if(jclazz == NULL) {\n' implementation += function_space(3) + 'return;\n' implementation += function_space(2) + '}\n' java_callback_function_name_str = 'on{0}'.format(api.function_name) java_callback_function_sign_str = 'ZLjava/lang/String;' for output_var in api.output_var_list: java_callback_function_sign_str += output_var.var_type.to_jni_signature_v2( ) implementation += function_space(2) + 'jmethodID method_id = jni_env->GetMethodID(jclazz, "{0}", "({1})V");\n'\ .format(java_callback_function_name_str, java_callback_function_sign_str) implementation += function_space(2) + 'if(method_id == NULL) {\n' implementation += function_space(3) + 'return;\n' implementation += function_space(2) + '}\n' # TODO([email protected]): We should improve it in future. if config.cpp_error_type == 'const std::string&': implementation += function_space( 2 ) + 'jstring jerror = jni_env->NewStringUTF(error.c_str());\n\n' elif config.cpp_error_type == 'worktile::WebApi::Error': implementation += function_space( 2 ) + 'jstring jerror = jni_env->NewStringUTF(error.description.c_str());\n\n' else: skr_log_warning( 'This is a tmp ugly implementation, you should support your own type here.' ) if len(api.output_var_list) != 0: implementation += function_space(2) + 'if (success){\n' implementation += function_space( 3 ) + 'jni_env->CallVoidMethod(global_thiz, method_id, success, jerror' space_length = len( function_space(3) + 'jni_env->CallVoidMethod(') space = '' for i in range(space_length): space += " " for output_var in api.output_var_list: implementation += ',\n' + space + output_var.jni_variable_from_cpp_variable_v2( output_var.name, namespace) implementation += ');\n' implementation += function_space(2) + '} else {\n' implementation += function_space( 3 ) + 'jni_env->CallVoidMethod(global_thiz, method_id, success, jerror' for output_var in api.output_var_list: implementation += ', global_null' implementation += ');\n' implementation += function_space(2) + '}\n\n' else: implementation += function_space( 2 ) + 'jni_env->CallVoidMethod(global_thiz, method_id, success, jerror);\n\n' implementation += function_space( 2) + 'jni_env->DeleteGlobalRef(global_thiz);\n' implementation += function_space( 2) + 'jni_env->DeleteGlobalRef(global_null);\n' implementation += function_space(1) + '});\n' implementation += '}\n\n' return implementation
def __fetch_implementation(self, fetch_command): by_list = [] impl = '' if fetch_command.where != '': by_list = re.split(',', fetch_command.where) bys = self.__convert_bys_to_string_impl(by_list, self.fetch_has_sign) if fetch_command.alias != '': fetch_fun_name = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'Fetch{0}FromCache'.format(self.object_name) else: fetch_fun_name = 'Fetch{0}FromCache'.format(self.plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') impl += 'JNIEXPORT jlong JNICALL Java_com_lesschat_core_{0}_{1}_native' \ .format(self.group_name, self.manager_name) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const lesschat::{0}* core_manager = reinterpret_cast<lesschat::{0}*>(handler);' \ .format(self.manager_name) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.var_type.to_getter_string() + " cpp_" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable(jni_var.to_param_style_name()) + ';' cpp_method_param += 'cpp_' + jni_var.to_param_style_name() + ', ' impl += _JNI_BR if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE + 'std::unique_ptr<lesschat::{0}> core_object = core_manager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param) impl += _JNI_SPACE + 'if(core_object == nullptr){\n return 0;\n }\n' impl += _JNI_SPACE + 'return reinterpret_cast<long>(core_object.release());\n}' else: impl += 'JNIEXPORT jlongArray JNICALL Java_com_lesschat_core_{0}_{1}_native' \ .format(self.group_name, self.manager_name) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const lesschat::{0}* core_manager = reinterpret_cast<lesschat::{0}*>(handler);' \ .format(self.manager_name) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.to_getter_string() + " cpp_" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable(jni_var.to_param_style_name()) + ';\n' cpp_method_param += 'cpp_' + jni_var.to_param_style_name() + ', ' if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE impl += 'std::vector<std::unique_ptr<lesschat::{0}>> core_objects = core_manager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param) impl += _JNI_SPACE + 'return lesschat::JniHelper::JlongArrayFromNativeArray(std::move(core_objects));\n}' return impl
def __fetch_implementation_v2(self, fetch_command, config): by_list = [] impl = '' if fetch_command.where != '': by_list = re.split(',', fetch_command.where) bys = self.__convert_bys_to_string_impl(by_list, self.fetch_has_sign) if fetch_command.alias != '': fetch_fun_name = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'Fetch{0}FromCache'.format(self.object_name) else: fetch_fun_name = 'Fetch{0}FromCache'.format(self.plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') impl += 'JNIEXPORT jobject JNICALL Java_{2}_{0}_{1}_native' \ .format(self.group_name, self.manager_name, config.jni_package_path) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const {1}::{0}* coreManager = reinterpret_cast<{1}::{0}*>(handler);' \ .format(self.manager_name, config.cpp_namespace) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.var_type.to_getter_string() + " cpp" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable(jni_var.to_param_style_name(), config) + ';' cpp_method_param += 'cpp' + jni_var.to_param_style_name() + ', ' impl += _JNI_BR if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE + 'std::unique_ptr<{3}::{0}> coreObject = coreManager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param, config.cpp_namespace) impl += _JNI_SPACE + 'if(coreObject == nullptr){\n return NULL;\n }\n' impl += _JNI_SPACE + 'return {1}::JniHelper::GetJ{0}ByCore{0}(*coreObject);\n}}'.format(self.object_name, config.cpp_namespace) else: impl += 'JNIEXPORT jobjectArray JNICALL Java_{2}_{0}_{1}_native' \ .format(self.group_name, self.manager_name, config.jni_package_path) impl += fetch_fun_name + bys impl += _JNI_SPACE + 'const {1}::{0}* coreManager = reinterpret_cast<{1}::{0}*>(handler);' \ .format(self.manager_name, config.cpp_namespace) impl += _JNI_BR cpp_method_param = '' for by_string in by_list: jni_var = self.__jni_var_by_name(by_string) if jni_var is not None: impl += _JNI_SPACE + jni_var.to_getter_string() + " cpp" + jni_var.to_param_style_name() impl += ' = ' + jni_var.cpp_variable_from_jni_variable(jni_var.to_param_style_name(), config) + ';\n' cpp_method_param += 'cpp' + jni_var.to_param_style_name() + ', ' if len(by_list) == 1: cpp_method_by = 'By' + jni_var.to_title_style_name() else: cpp_method_by = '' cpp_method_name = fetch_fun_name + cpp_method_by cpp_method_param = cpp_method_param[:-2] impl += _JNI_SPACE impl += 'std::vector<std::unique_ptr<{3}::{0}>> coreObjects = coreManager->{1}({2});\n\n' \ .format(self.object_name, cpp_method_name, cpp_method_param, config.cpp_namespace) impl += _JNI_SPACE + 'return {1}::JniHelper::GetJ{0}sArrayByCore{0}s(coreObjects);\n}}'.format( self.object_name, config.cpp_namespace) return impl
def generate_http_function_implementations(self, config): """Generates HTTP - Cache methods. Args: config: A <Config> object represents user-defined config including namespace and package path. Returns: A string that is JNI http methods. """ namespace = config.cpp_namespace implementation = '' for api in self.apis: implementation += 'JNIEXPORT void JNICALL Java_{3}_{0}_{1}_native{2}\n'\ .format(self.group_name, self.manager_name, api.function_name, config.jni_package_path) implementation += ' (JNIEnv* env, jobject thiz, jlong handler' for input_var in api.input_var_list: implementation += ', {0} {1}'.format(input_var.var_type.to_jni_getter_string(), input_var.to_param_style_name()) implementation += ') {\n' implementation += function_space(1) + 'const {1}::{0}* core_manager = ' \ 'reinterpret_cast<{1}::{0}*>(handler);\n\n' \ .format(self.manager_name, namespace) implementation += function_space(1) + 'jobject global_thiz = env->NewGlobalRef(thiz);\n' implementation += function_space(1) + 'jobject global_null = env->NewGlobalRef(NULL);\n\n' for input_var in api.input_var_list: implementation += _JNI_SPACE + input_var.to_getter_string() + " cpp_" + input_var.to_param_style_name() implementation += ' = ' + input_var.cpp_variable_from_jni_variable(input_var.to_param_style_name(), config) implementation += ';\n' implementation += '\n' implementation += function_space(1) + 'core_manager->{0}('.format(api.function_name) space_length = len(function_space(1) + 'core_manager->{0}('.format(api.function_name)) space = '' for i in range(space_length): space += " " index = 0 for input_var in api.input_var_list: if index == 0: implementation += 'cpp_{0},\n'.format(input_var.to_param_style_name()) else: implementation += space + 'cpp_{0},\n'.format(input_var.to_param_style_name()) index += 1 if len(api.input_var_list) > 0: implementation += space + '[env, global_thiz, global_null](' else: implementation += '[env, global_thiz, global_null](' space_length = len('[env, global_thiz, global_null](') for i in range(space_length): space += " " implementation += 'bool success,\n' implementation += space + '{0} error'.format(config.cpp_error_type) for i in range(len(api.output_var_list)): implementation += ',\n{0}{1} {2}'.format(space, api.output_var_list[i].to_getter_string(namespace), api.output_var_list[i].name) implementation += '){\n' implementation += function_space(2) + 'JNIEnv *jni_env = {0}::JniHelper::GetJniEnv();\n'.format(namespace) implementation += function_space(2) + 'jclass jclazz = jni_env->GetObjectClass(global_thiz);\n\n' implementation += function_space(2) + 'if(jclazz == NULL) {\n' implementation += function_space(3) + 'return;\n' implementation += function_space(2) + '}\n' java_callback_function_name_str = 'on{0}'.format(api.function_name) java_callback_function_sign_str = 'ZLjava/lang/String;' for output_var in api.output_var_list: java_callback_function_sign_str += output_var.var_type.to_jni_signature_v2() implementation += function_space(2) + 'jmethodID method_id = jni_env->GetMethodID(jclazz, "{0}", "({1})V");\n'\ .format(java_callback_function_name_str, java_callback_function_sign_str) implementation += function_space(2) + 'if(method_id == NULL) {\n' implementation += function_space(3) + 'return;\n' implementation += function_space(2) + '}\n' # TODO([email protected]): We should improve it in future. if config.cpp_error_type == 'const std::string&': implementation += function_space(2) + 'jstring jerror = jni_env->NewStringUTF(error.c_str());\n\n' elif config.cpp_error_type == 'worktile::WebApi::Error': implementation += function_space(2) + 'jstring jerror = jni_env->NewStringUTF(error.description.c_str());\n\n' else: skr_log_warning('This is a tmp ugly implementation, you should support your own type here.') if len(api.output_var_list) != 0: implementation += function_space(2) + 'if (success){\n' implementation += function_space(3) + 'jni_env->CallVoidMethod(global_thiz, method_id, success, jerror' space_length = len(function_space(3) + 'jni_env->CallVoidMethod(') space = '' for i in range(space_length): space += " " for output_var in api.output_var_list: implementation += ',\n' + space + output_var.jni_variable_from_cpp_variable_v2(output_var.name, namespace) implementation += ');\n' implementation += function_space(2) + '} else {\n' implementation += function_space(3) + 'jni_env->CallVoidMethod(global_thiz, method_id, success, jerror' for output_var in api.output_var_list: implementation += ', global_null' implementation += ');\n' implementation += function_space(2) + '}\n\n' else: implementation += function_space(2) + 'jni_env->CallVoidMethod(global_thiz, method_id, success, jerror);\n\n' implementation += function_space(2) + 'jni_env->DeleteGlobalRef(global_thiz);\n' implementation += function_space(2) + 'jni_env->DeleteGlobalRef(global_null);\n' implementation += function_space(1) + '});\n' implementation += '}\n\n' return implementation
def __fetch_implementation(self, fetch_command): by_list = [] where_sql = 'string where_condition = "";' if fetch_command.where != '': by_list = re.split(',', fetch_command.where) where_sql = self.__convert_bys_to_sql_where(by_list) if not fetch_command.is_plural: impl = '' if fetch_command.alias_or_none is None: impl += 'std::unique_ptr<{0}> {2}::Fetch{0}FromCache{1} const {{\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list), self.manager_name) else: impl += 'std::unique_ptr<{0}> {2}::{3}{1} const {{\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list), self.manager_name, fetch_command.alias_or_none) impl += _CPP_SPACE + where_sql + _CPP_BR impl += ' LCC_DB_LOCK_GUARD;' + _CPP_BR tb_list = copy.copy(fetch_command.table_name_list) if len(tb_list) == 0: impl += ' {0}->open(where_condition);\n'.format(self.__sqlite_tb_name()) impl += ' if ({0}->recordCount() != 0) {{\n'.format(self.__sqlite_tb_name()) impl += ' sql::Record* record = {0}->getRecord(0);\n'.format(self.__sqlite_tb_name()) impl += ' unique_ptr<{0}> rtn({0}FromRecord(record));\n'.format(self.object_name) impl += ' return rtn;\n' impl += ' }' + _CPP_BR else: for table_name in tb_list: impl += ' {0}->open(where_condition);\n'.format(self.__sqlite_tb_name(table_name)) impl += ' if ({0}->recordCount() != 0) {{\n'.format(self.__sqlite_tb_name(table_name)) impl += ' sql::Record* record = {0}->getRecord(0);\n'.format(self.__sqlite_tb_name(table_name)) impl += ' unique_ptr<{0}> rtn({0}FromRecord(record));\n'.format(self.object_name) impl += ' return rtn;\n' impl += ' }' + _CPP_BR impl += ' return nullptr;\n' impl += '}' return impl else: # validations # table_name_list is not supported in plural case. if len(fetch_command.table_name_list) > 0 and fetch_command.alias_or_none is None: skr_log_warning('"tables" attribute only supported <fetch singular="true"/>') elif len(fetch_command.table_name_list) > 1 and fetch_command.alias_or_none is not None: skr_log_warning('"tables" attribute only supported at most 1 override name') # logic override_table_name = '' if len(fetch_command.table_name_list) > 0: override_table_name = fetch_command.table_name_list[0] impl = '' if fetch_command.alias_or_none is None: impl += 'std::vector<std::unique_ptr<{0}>> {3}::Fetch{1}FromCache{2} const {{\n\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list), self.manager_name) else: impl += 'std::vector<std::unique_ptr<{0}>> {3}::{1}{2} const {{\n\n'\ .format(self.object_name, fetch_command.alias_or_none, self.__convert_bys_to_string(by_list), self.manager_name) impl += ' vector<unique_ptr<{0}>> {1};\n\n'.format(self.object_name, self.plural_object_name.lower()) impl += _CPP_SPACE + where_sql + _CPP_BR impl += ' LockMainDatabase();' + _CPP_BR impl += ' {0}->open(where_condition{1});\n\n'.format(self.__sqlite_tb_name(override_table_name), fetch_command.sort_sql()) impl += ' for (int i = 0; i < {0}->recordCount(); ++i) {{\n'.format(self.__sqlite_tb_name(override_table_name)) impl += ' sql::Record* record = {0}->getRecord(i);\n'.format(self.__sqlite_tb_name(override_table_name)) impl += ' {0}.push_back({1}FromRecord(record));\n'.format(self.plural_object_name.lower(), self.object_name) impl += ' }\n\n' impl += ' UnlockMainDatabase();' + _CPP_BR impl += ' return {0};\n'.format(self.plural_object_name.lower()) impl += '}' return impl