Chromium Code Reviews| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 return java_pod_type_map[java_type[:-2]] + 'Array' | 117 return java_pod_type_map[java_type[:-2]] + 'Array' |
| 118 return 'jobjectArray' | 118 return 'jobjectArray' |
| 119 elif java_type.startswith('Class'): | 119 elif java_type.startswith('Class'): |
| 120 # Checking just the start of the name, rather than a direct comparison, | 120 # Checking just the start of the name, rather than a direct comparison, |
| 121 # in order to handle generics. | 121 # in order to handle generics. |
| 122 return 'jclass' | 122 return 'jclass' |
| 123 else: | 123 else: |
| 124 return 'jobject' | 124 return 'jobject' |
| 125 | 125 |
| 126 | 126 |
| 127 def JavaDataTypeToCForCalledByNativeParam(java_type): | |
| 128 """Returns a C datatype to be when calling from native.""" | |
| 129 if java_type == 'int': | |
| 130 return 'JniIntWrapper' | |
| 131 else: | |
| 132 return JavaDataTypeToC(java_type) | |
| 133 | |
| 134 | |
| 127 def JavaReturnValueToC(java_type): | 135 def JavaReturnValueToC(java_type): |
| 128 """Returns a valid C return value for the given java type.""" | 136 """Returns a valid C return value for the given java type.""" |
| 129 java_pod_type_map = { | 137 java_pod_type_map = { |
| 130 'int': '0', | 138 'int': '0', |
| 131 'byte': '0', | 139 'byte': '0', |
| 132 'char': '0', | 140 'char': '0', |
| 133 'short': '0', | 141 'short': '0', |
| 134 'boolean': 'false', | 142 'boolean': 'false', |
| 135 'long': '0', | 143 'long': '0', |
| 136 'double': '0', | 144 'double': '0', |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 665 // For | 673 // For |
| 666 // ${FULLY_QUALIFIED_CLASS} | 674 // ${FULLY_QUALIFIED_CLASS} |
| 667 | 675 |
| 668 #ifndef ${HEADER_GUARD} | 676 #ifndef ${HEADER_GUARD} |
| 669 #define ${HEADER_GUARD} | 677 #define ${HEADER_GUARD} |
| 670 | 678 |
| 671 #include <jni.h> | 679 #include <jni.h> |
| 672 | 680 |
| 673 ${INCLUDES} | 681 ${INCLUDES} |
| 674 | 682 |
| 683 #include "base/android/jni_int_wrapper.h" | |
| 684 | |
| 675 // Step 1: forward declarations. | 685 // Step 1: forward declarations. |
| 676 namespace { | 686 namespace { |
| 677 $CLASS_PATH_DEFINITIONS | 687 $CLASS_PATH_DEFINITIONS |
| 678 $METHOD_ID_DEFINITIONS | 688 $METHOD_ID_DEFINITIONS |
| 679 } // namespace | 689 } // namespace |
| 680 | 690 |
| 681 $OPEN_NAMESPACE | 691 $OPEN_NAMESPACE |
| 682 $FORWARD_DECLARATIONS | 692 $FORWARD_DECLARATIONS |
| 683 | 693 |
| 684 $CONSTANT_FIELDS | 694 $CONSTANT_FIELDS |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 925 | 935 |
| 926 Returns: | 936 Returns: |
| 927 A string containing the params. | 937 A string containing the params. |
| 928 """ | 938 """ |
| 929 return ',\n '.join(self.GetJNIFirstParam(native) + | 939 return ',\n '.join(self.GetJNIFirstParam(native) + |
| 930 [JavaDataTypeToC(param.datatype) + ' ' + | 940 [JavaDataTypeToC(param.datatype) + ' ' + |
| 931 param.name | 941 param.name |
| 932 for param in native.params]) | 942 for param in native.params]) |
| 933 | 943 |
| 934 def GetCalledByNativeParamsInDeclaration(self, called_by_native): | 944 def GetCalledByNativeParamsInDeclaration(self, called_by_native): |
| 935 return ',\n '.join([JavaDataTypeToC(param.datatype) + ' ' + | 945 return ',\n '.join([ |
| 936 param.name | 946 JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' + |
| 937 for param in called_by_native.params]) | 947 param.name |
| 948 for param in called_by_native.params]) | |
| 938 | 949 |
| 939 def GetForwardDeclaration(self, native): | 950 def GetForwardDeclaration(self, native): |
| 940 template = Template(""" | 951 template = Template(""" |
| 941 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); | 952 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); |
| 942 """) | 953 """) |
| 943 values = {'RETURN': JavaDataTypeToC(native.return_type), | 954 values = {'RETURN': JavaDataTypeToC(native.return_type), |
| 944 'NAME': native.name, | 955 'NAME': native.name, |
| 945 'PARAMS': self.GetParamsInDeclaration(native)} | 956 'PARAMS': self.GetParamsInDeclaration(native)} |
| 946 return template.substitute(values) | 957 return template.substitute(values) |
| 947 | 958 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 971 'OPTIONAL_ERROR_RETURN': optional_error_return, | 982 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| 972 'NAME': native.name, | 983 'NAME': native.name, |
| 973 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), | 984 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), |
| 974 'PARAM0_NAME': native.params[0].name, | 985 'PARAM0_NAME': native.params[0].name, |
| 975 'P0_TYPE': native.p0_type, | 986 'P0_TYPE': native.p0_type, |
| 976 'PARAMS_IN_CALL': params_in_call, | 987 'PARAMS_IN_CALL': params_in_call, |
| 977 'POST_CALL': post_call | 988 'POST_CALL': post_call |
| 978 } | 989 } |
| 979 return template.substitute(values) | 990 return template.substitute(values) |
| 980 | 991 |
| 992 def GetArgument(self, param): | |
| 993 return ("as_jint(" + param.name + ")" | |
|
bulach
2014/05/06 14:57:55
nit: s/"/'/
| |
| 994 if param.datatype == 'int' else param.name) | |
| 995 | |
| 996 def GetArgumentsInCall(self, params): | |
| 997 """Return a string of arguments to call from native into Java""" | |
| 998 return [self.GetArgument(p) for p in params] | |
| 999 | |
| 981 def GetCalledByNativeValues(self, called_by_native): | 1000 def GetCalledByNativeValues(self, called_by_native): |
| 982 """Fills in necessary values for the CalledByNative methods.""" | 1001 """Fills in necessary values for the CalledByNative methods.""" |
| 983 if called_by_native.static or called_by_native.is_constructor: | 1002 if called_by_native.static or called_by_native.is_constructor: |
| 984 first_param_in_declaration = '' | 1003 first_param_in_declaration = '' |
| 985 first_param_in_call = ('g_%s_clazz' % | 1004 first_param_in_call = ('g_%s_clazz' % |
| 986 (called_by_native.java_class_name or | 1005 (called_by_native.java_class_name or |
| 987 self.class_name)) | 1006 self.class_name)) |
| 988 else: | 1007 else: |
| 989 first_param_in_declaration = ', jobject obj' | 1008 first_param_in_declaration = ', jobject obj' |
| 990 first_param_in_call = 'obj' | 1009 first_param_in_call = 'obj' |
| 991 params_in_declaration = self.GetCalledByNativeParamsInDeclaration( | 1010 params_in_declaration = self.GetCalledByNativeParamsInDeclaration( |
| 992 called_by_native) | 1011 called_by_native) |
| 993 if params_in_declaration: | 1012 if params_in_declaration: |
| 994 params_in_declaration = ', ' + params_in_declaration | 1013 params_in_declaration = ', ' + params_in_declaration |
| 995 params_in_call = ', '.join(param.name for param in called_by_native.params) | 1014 params_in_call = ', '.join(self.GetArgumentsInCall(called_by_native.params)) |
| 996 if params_in_call: | 1015 if params_in_call: |
| 997 params_in_call = ', ' + params_in_call | 1016 params_in_call = ', ' + params_in_call |
| 998 pre_call = '' | 1017 pre_call = '' |
| 999 post_call = '' | 1018 post_call = '' |
| 1000 if called_by_native.static_cast: | 1019 if called_by_native.static_cast: |
| 1001 pre_call = 'static_cast<%s>(' % called_by_native.static_cast | 1020 pre_call = 'static_cast<%s>(' % called_by_native.static_cast |
| 1002 post_call = ')' | 1021 post_call = ')' |
| 1003 check_exception = '' | 1022 check_exception = '' |
| 1004 if not called_by_native.unchecked: | 1023 if not called_by_native.unchecked: |
| 1005 check_exception = 'jni_generator::CheckException(env);' | 1024 check_exception = 'jni_generator::CheckException(env);' |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1343 root_name = os.path.splitext(os.path.basename(input_file))[0] | 1362 root_name = os.path.splitext(os.path.basename(input_file))[0] |
| 1344 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 1363 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
| 1345 if options.jarjar: | 1364 if options.jarjar: |
| 1346 with open(options.jarjar) as f: | 1365 with open(options.jarjar) as f: |
| 1347 JniParams.SetJarJarMappings(f.read()) | 1366 JniParams.SetJarJarMappings(f.read()) |
| 1348 GenerateJNIHeader(input_file, output_file, options) | 1367 GenerateJNIHeader(input_file, output_file, options) |
| 1349 | 1368 |
| 1350 | 1369 |
| 1351 if __name__ == '__main__': | 1370 if __name__ == '__main__': |
| 1352 sys.exit(main(sys.argv)) | 1371 sys.exit(main(sys.argv)) |
| OLD | NEW |