OLD | NEW |
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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 def JavaDataTypeToCForDeclaration(java_type): | 146 def JavaDataTypeToCForDeclaration(java_type): |
147 """Returns a JavaRef-wrapped C datatype for the given java type.""" | 147 """Returns a JavaRef-wrapped C datatype for the given java type.""" |
148 return WrapCTypeForDeclaration(JavaDataTypeToC(java_type)) | 148 return WrapCTypeForDeclaration(JavaDataTypeToC(java_type)) |
149 | 149 |
150 | 150 |
151 def JavaDataTypeToCForCalledByNativeParam(java_type): | 151 def JavaDataTypeToCForCalledByNativeParam(java_type): |
152 """Returns a C datatype to be when calling from native.""" | 152 """Returns a C datatype to be when calling from native.""" |
153 if java_type == 'int': | 153 if java_type == 'int': |
154 return 'JniIntWrapper' | 154 return 'JniIntWrapper' |
155 else: | 155 else: |
156 return JavaDataTypeToC(java_type) | 156 c_type = JavaDataTypeToC(java_type) |
| 157 if re.match(RE_SCOPED_JNI_TYPES, c_type): |
| 158 return 'const base::android::JavaRefOrBare<' + c_type + '>&' |
| 159 else: |
| 160 return c_type |
157 | 161 |
158 | 162 |
159 def JavaReturnValueToC(java_type): | 163 def JavaReturnValueToC(java_type): |
160 """Returns a valid C return value for the given java type.""" | 164 """Returns a valid C return value for the given java type.""" |
161 java_pod_type_map = { | 165 java_pod_type_map = { |
162 'int': '0', | 166 'int': '0', |
163 'byte': '0', | 167 'byte': '0', |
164 'char': '0', | 168 'char': '0', |
165 'short': '0', | 169 'short': '0', |
166 'boolean': 'false', | 170 'boolean': 'false', |
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 | 1029 |
1026 extern "C" __attribute__((visibility("default"))) | 1030 extern "C" __attribute__((visibility("default"))) |
1027 ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_STUB}) { | 1031 ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_STUB}) { |
1028 return ${NAME}(${PARAMS_IN_CALL})${POST_CALL}; | 1032 return ${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
1029 } | 1033 } |
1030 """) | 1034 """) |
1031 | 1035 |
1032 return template.substitute(values) | 1036 return template.substitute(values) |
1033 | 1037 |
1034 def GetArgument(self, param): | 1038 def GetArgument(self, param): |
1035 return ('as_jint(' + param.name + ')' | 1039 if param.datatype == 'int': |
1036 if param.datatype == 'int' else param.name) | 1040 return 'as_jint(' + param.name + ')' |
| 1041 elif re.match(RE_SCOPED_JNI_TYPES, JavaDataTypeToC(param.datatype)): |
| 1042 return param.name + '.obj()' |
| 1043 else: |
| 1044 return param.name |
1037 | 1045 |
1038 def GetArgumentsInCall(self, params): | 1046 def GetArgumentsInCall(self, params): |
1039 """Return a string of arguments to call from native into Java""" | 1047 """Return a string of arguments to call from native into Java""" |
1040 return [self.GetArgument(p) for p in params] | 1048 return [self.GetArgument(p) for p in params] |
1041 | 1049 |
1042 def GetCalledByNativeValues(self, called_by_native): | 1050 def GetCalledByNativeValues(self, called_by_native): |
1043 """Fills in necessary values for the CalledByNative methods.""" | 1051 """Fills in necessary values for the CalledByNative methods.""" |
1044 java_class = called_by_native.java_class_name or self.class_name | 1052 java_class = called_by_native.java_class_name or self.class_name |
1045 if called_by_native.static or called_by_native.is_constructor: | 1053 if called_by_native.static or called_by_native.is_constructor: |
1046 first_param_in_declaration = '' | 1054 first_param_in_declaration = '' |
1047 first_param_in_call = ('%s_clazz(env)' % java_class) | 1055 first_param_in_call = ('%s_clazz(env)' % java_class) |
1048 else: | 1056 else: |
1049 first_param_in_declaration = ', jobject obj' | 1057 first_param_in_declaration = ( |
1050 first_param_in_call = 'obj' | 1058 ', const base::android::JavaRefOrBare<jobject>& obj') |
| 1059 first_param_in_call = 'obj.obj()' |
1051 params_in_declaration = self.GetCalledByNativeParamsInDeclaration( | 1060 params_in_declaration = self.GetCalledByNativeParamsInDeclaration( |
1052 called_by_native) | 1061 called_by_native) |
1053 if params_in_declaration: | 1062 if params_in_declaration: |
1054 params_in_declaration = ', ' + params_in_declaration | 1063 params_in_declaration = ', ' + params_in_declaration |
1055 params_in_call = ', '.join(self.GetArgumentsInCall(called_by_native.params)) | 1064 params_in_call = ', '.join(self.GetArgumentsInCall(called_by_native.params)) |
1056 if params_in_call: | 1065 if params_in_call: |
1057 params_in_call = ', ' + params_in_call | 1066 params_in_call = ', ' + params_in_call |
1058 pre_call = '' | 1067 pre_call = '' |
1059 post_call = '' | 1068 post_call = '' |
1060 if called_by_native.static_cast: | 1069 if called_by_native.static_cast: |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1364 GenerateJNIHeader(input_file, output_file, options) | 1373 GenerateJNIHeader(input_file, output_file, options) |
1365 | 1374 |
1366 if options.depfile: | 1375 if options.depfile: |
1367 build_utils.WriteDepfile( | 1376 build_utils.WriteDepfile( |
1368 options.depfile, | 1377 options.depfile, |
1369 build_utils.GetPythonDependencies()) | 1378 build_utils.GetPythonDependencies()) |
1370 | 1379 |
1371 | 1380 |
1372 if __name__ == '__main__': | 1381 if __name__ == '__main__': |
1373 sys.exit(main(sys.argv)) | 1382 sys.exit(main(sys.argv)) |
OLD | NEW |