Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: base/android/jni_generator/jni_generator.py

Issue 938373002: android: Refactor function name template in JNI generator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Extracts native methods from a Java file and generates the JNI bindings. 6 """Extracts native methods from a Java file and generates the JNI bindings.
7 If you change this, please run and update the tests.""" 7 If you change this, please run and update the tests."""
8 8
9 import collections 9 import collections
10 import errno 10 import errno
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 [JavaDataTypeToC(param.datatype) + ' ' + 1028 [JavaDataTypeToC(param.datatype) + ' ' +
1029 param.name 1029 param.name
1030 for param in native.params]) 1030 for param in native.params])
1031 1031
1032 def GetCalledByNativeParamsInDeclaration(self, called_by_native): 1032 def GetCalledByNativeParamsInDeclaration(self, called_by_native):
1033 return ',\n '.join([ 1033 return ',\n '.join([
1034 JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' + 1034 JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' +
1035 param.name 1035 param.name
1036 for param in called_by_native.params]) 1036 for param in called_by_native.params])
1037 1037
1038 def GetStubName(self, native):
rmcilroy 2015/02/20 10:56:02 could you add a comment to explain what the StubNa
1039 if self.options.native_exports:
1040 template = Template("Java_${JAVA_NAME}_native${NAME}")
1041
1042 java_name = JniParams.RemapClassName(self.fully_qualified_class)
1043 java_name = java_name.replace('_', '_1').replace('/', '_')
1044 if native.java_class_name:
1045 java_name += '_00024' + native.java_class_name
1046
1047 values = {'NAME': native.name,
1048 'JAVA_NAME': java_name}
1049 return template.substitute(values)
1050 else:
1051 return native.name
1052
1038 def GetForwardDeclaration(self, native): 1053 def GetForwardDeclaration(self, native):
1039 template_str = """ 1054 template_str = """
1040 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); 1055 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS});
1041 """ 1056 """
1042 if self.options.native_exports: 1057 if self.options.native_exports:
1043 template_str += """ 1058 template_str += """
1044 __attribute__((visibility("default"), alias("${NAME}"))) 1059 __attribute__((visibility("default"), alias("${NAME}")))
1045 ${RETURN} Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, ${PARAMS}); 1060 ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS});
1046 """ 1061 """
1047 template = Template(template_str) 1062 template = Template(template_str)
1048 params_in_call = [] 1063 params_in_call = []
1049 if not self.options.pure_native_methods: 1064 if not self.options.pure_native_methods:
1050 params_in_call = ['env', 'jcaller'] 1065 params_in_call = ['env', 'jcaller']
1051 params_in_call = ', '.join(params_in_call + [p.name for p in native.params]) 1066 params_in_call = ', '.join(params_in_call + [p.name for p in native.params])
1052 1067
1053 java_name = JniParams.RemapClassName(self.fully_qualified_class)
1054 java_name = java_name.replace('_', '_1').replace('/', '_')
1055 if native.java_class_name:
1056 java_name += '_00024' + native.java_class_name
1057
1058 values = {'RETURN': JavaDataTypeToC(native.return_type), 1068 values = {'RETURN': JavaDataTypeToC(native.return_type),
1059 'NAME': native.name, 1069 'NAME': native.name,
1060 'JAVA_NAME': java_name,
1061 'PARAMS': self.GetParamsInDeclaration(native), 1070 'PARAMS': self.GetParamsInDeclaration(native),
1062 'PARAMS_IN_CALL': params_in_call} 1071 'PARAMS_IN_CALL': params_in_call,
1072 'STUB_NAME': self.GetStubName(native)}
1063 return template.substitute(values) 1073 return template.substitute(values)
1064 1074
1065 def GetNativeMethodStubString(self, native): 1075 def GetNativeMethodStubString(self, native):
1066 """Returns stubs for native methods.""" 1076 """Returns stubs for native methods."""
1067 if self.options.native_exports: 1077 if self.options.native_exports:
1068 template_str = """\ 1078 template_str = """\
1069 __attribute__((visibility("default"))) 1079 __attribute__((visibility("default")))
1070 ${RETURN} Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, 1080 ${RETURN} ${STUB_NAME}(JNIEnv* env,
1071 ${PARAMS_IN_DECLARATION}) {""" 1081 ${PARAMS_IN_DECLARATION}) {"""
1072 else: 1082 else:
1073 template_str = """\ 1083 template_str = """\
1074 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {""" 1084 static ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {"""
1075 template_str += """ 1085 template_str += """
1076 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); 1086 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME});
1077 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); 1087 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN});
1078 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; 1088 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL};
1079 } 1089 }
1080 """ 1090 """
1081 1091
1082 template = Template(template_str) 1092 template = Template(template_str)
1083 params = [] 1093 params = []
1084 if not self.options.pure_native_methods: 1094 if not self.options.pure_native_methods:
1085 params = ['env', 'jcaller'] 1095 params = ['env', 'jcaller']
1086 params_in_call = ', '.join(params + [p.name for p in native.params[1:]]) 1096 params_in_call = ', '.join(params + [p.name for p in native.params[1:]])
1087 1097
1088 return_type = JavaDataTypeToC(native.return_type) 1098 return_type = JavaDataTypeToC(native.return_type)
1089 optional_error_return = JavaReturnValueToC(native.return_type) 1099 optional_error_return = JavaReturnValueToC(native.return_type)
1090 if optional_error_return: 1100 if optional_error_return:
1091 optional_error_return = ', ' + optional_error_return 1101 optional_error_return = ', ' + optional_error_return
1092 post_call = '' 1102 post_call = ''
1093 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): 1103 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type):
1094 post_call = '.Release()' 1104 post_call = '.Release()'
1095 1105
1096 if self.options.native_exports:
1097 java_name = JniParams.RemapClassName(self.fully_qualified_class)
1098 java_name = java_name.replace('_', '_1').replace('/', '_')
1099 if native.java_class_name:
1100 java_name += '_00024' + native.java_class_name
1101 else:
1102 java_name = ''
1103
1104 values = { 1106 values = {
1105 'RETURN': return_type, 1107 'RETURN': return_type,
1106 'OPTIONAL_ERROR_RETURN': optional_error_return, 1108 'OPTIONAL_ERROR_RETURN': optional_error_return,
1107 'JAVA_NAME': java_name,
1108 'NAME': native.name, 1109 'NAME': native.name,
1109 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), 1110 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native),
1110 'PARAM0_NAME': native.params[0].name, 1111 'PARAM0_NAME': native.params[0].name,
1111 'P0_TYPE': native.p0_type, 1112 'P0_TYPE': native.p0_type,
1112 'PARAMS_IN_CALL': params_in_call, 1113 'PARAMS_IN_CALL': params_in_call,
1113 'POST_CALL': post_call 1114 'POST_CALL': post_call,
1115 'STUB_NAME': self.GetStubName(native),
1114 } 1116 }
1115 return template.substitute(values) 1117 return template.substitute(values)
1116 1118
1117 def GetArgument(self, param): 1119 def GetArgument(self, param):
1118 return ('as_jint(' + param.name + ')' 1120 return ('as_jint(' + param.name + ')'
1119 if param.datatype == 'int' else param.name) 1121 if param.datatype == 'int' else param.name)
1120 1122
1121 def GetArgumentsInCall(self, params): 1123 def GetArgumentsInCall(self, params):
1122 """Return a string of arguments to call from native into Java""" 1124 """Return a string of arguments to call from native into Java"""
1123 return [self.GetArgument(p) for p in params] 1125 return [self.GetArgument(p) for p in params]
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 values['FUNCTION_SIGNATURE'] = ( 1220 values['FUNCTION_SIGNATURE'] = (
1219 function_signature_template.substitute(values)) 1221 function_signature_template.substitute(values))
1220 if called_by_native.system_class: 1222 if called_by_native.system_class:
1221 values['FUNCTION_HEADER'] = ( 1223 values['FUNCTION_HEADER'] = (
1222 function_header_with_unused_template.substitute(values)) 1224 function_header_with_unused_template.substitute(values))
1223 else: 1225 else:
1224 values['FUNCTION_HEADER'] = function_header_template.substitute(values) 1226 values['FUNCTION_HEADER'] = function_header_template.substitute(values)
1225 return template.substitute(values) 1227 return template.substitute(values)
1226 1228
1227 def GetKMethodArrayEntry(self, native): 1229 def GetKMethodArrayEntry(self, native):
1228 template = Template("""\ 1230 template = Template(' { "native${NAME}", ${JNI_SIGNATURE}, ' +
1229 { "native${NAME}", ${JNI_SIGNATURE}, reinterpret_cast<void*>(${NAME}) },""") 1231 'reinterpret_cast<void*>(${STUB_NAME}) },')
1230 values = {'NAME': native.name, 1232 values = {'NAME': native.name,
1231 'JNI_SIGNATURE': JniParams.Signature(native.params, 1233 'JNI_SIGNATURE': JniParams.Signature(native.params,
1232 native.return_type, 1234 native.return_type,
1233 True)} 1235 True),
1236 'STUB_NAME': self.GetStubName(native)}
1234 return template.substitute(values) 1237 return template.substitute(values)
1235 1238
1236 def GetUniqueClasses(self, origin): 1239 def GetUniqueClasses(self, origin):
1237 ret = {self.class_name: self.fully_qualified_class} 1240 ret = {self.class_name: self.fully_qualified_class}
1238 for entry in origin: 1241 for entry in origin:
1239 class_name = self.class_name 1242 class_name = self.class_name
1240 jni_class_path = self.fully_qualified_class 1243 jni_class_path = self.fully_qualified_class
1241 if entry.java_class_name: 1244 if entry.java_class_name:
1242 class_name = entry.java_class_name 1245 class_name = entry.java_class_name
1243 jni_class_path = self.fully_qualified_class + '$' + class_name 1246 jni_class_path = self.fully_qualified_class + '$' + class_name
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
1520 GenerateJNIHeader(input_file, output_file, options) 1523 GenerateJNIHeader(input_file, output_file, options)
1521 1524
1522 if options.depfile: 1525 if options.depfile:
1523 build_utils.WriteDepfile( 1526 build_utils.WriteDepfile(
1524 options.depfile, 1527 options.depfile,
1525 build_utils.GetPythonDependencies()) 1528 build_utils.GetPythonDependencies())
1526 1529
1527 1530
1528 if __name__ == '__main__': 1531 if __name__ == '__main__':
1529 sys.exit(main(sys.argv)) 1532 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698