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 'jni_int_wrapper' | |
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 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
648 def ExtractInitNative(self, options): | 656 def ExtractInitNative(self, options): |
649 for native in self.natives: | 657 for native in self.natives: |
650 if options.jni_init_native_name == 'native' + native.name: | 658 if options.jni_init_native_name == 'native' + native.name: |
651 self.natives.remove(native) | 659 self.natives.remove(native) |
652 return native | 660 return native |
653 return None | 661 return None |
654 | 662 |
655 def GetContent(self): | 663 def GetContent(self): |
656 """Returns the content of the JNI binding file.""" | 664 """Returns the content of the JNI binding file.""" |
657 template = Template("""\ | 665 template = Template("""\ |
658 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 666 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
bulach
2014/05/02 16:55:56
probably better to leave this as is?
| |
659 // Use of this source code is governed by a BSD-style license that can be | 667 // Use of this source code is governed by a BSD-style license that can be |
660 // found in the LICENSE file. | 668 // found in the LICENSE file. |
661 | 669 |
662 | 670 |
663 // This file is autogenerated by | 671 // This file is autogenerated by |
664 // ${SCRIPT_NAME} | 672 // ${SCRIPT_NAME} |
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 param.name + ".asJint()" if param.datatype == 'int' else param.name | |
bulach
2014/05/02 16:55:56
how about we make the jni_generator take a debug o
| |
994 | |
995 def GetArgumentsInCall(self, params): | |
996 """Return a string of arguments to call from native into Java""" | |
997 return [self.GetArgument(p) for p in params] | |
998 | |
981 def GetCalledByNativeValues(self, called_by_native): | 999 def GetCalledByNativeValues(self, called_by_native): |
982 """Fills in necessary values for the CalledByNative methods.""" | 1000 """Fills in necessary values for the CalledByNative methods.""" |
983 if called_by_native.static or called_by_native.is_constructor: | 1001 if called_by_native.static or called_by_native.is_constructor: |
984 first_param_in_declaration = '' | 1002 first_param_in_declaration = '' |
985 first_param_in_call = ('g_%s_clazz' % | 1003 first_param_in_call = ('g_%s_clazz' % |
986 (called_by_native.java_class_name or | 1004 (called_by_native.java_class_name or |
987 self.class_name)) | 1005 self.class_name)) |
988 else: | 1006 else: |
989 first_param_in_declaration = ', jobject obj' | 1007 first_param_in_declaration = ', jobject obj' |
990 first_param_in_call = 'obj' | 1008 first_param_in_call = 'obj' |
991 params_in_declaration = self.GetCalledByNativeParamsInDeclaration( | 1009 params_in_declaration = self.GetCalledByNativeParamsInDeclaration( |
992 called_by_native) | 1010 called_by_native) |
993 if params_in_declaration: | 1011 if params_in_declaration: |
994 params_in_declaration = ', ' + params_in_declaration | 1012 params_in_declaration = ', ' + params_in_declaration |
995 params_in_call = ', '.join(param.name for param in called_by_native.params) | 1013 params_in_call = ', '.join(self.GetArgumentsInCall(called_by_native.params)) |
996 if params_in_call: | 1014 if params_in_call: |
997 params_in_call = ', ' + params_in_call | 1015 params_in_call = ', ' + params_in_call |
998 pre_call = '' | 1016 pre_call = '' |
999 post_call = '' | 1017 post_call = '' |
1000 if called_by_native.static_cast: | 1018 if called_by_native.static_cast: |
1001 pre_call = 'static_cast<%s>(' % called_by_native.static_cast | 1019 pre_call = 'static_cast<%s>(' % called_by_native.static_cast |
1002 post_call = ')' | 1020 post_call = ')' |
1003 check_exception = '' | 1021 check_exception = '' |
1004 if not called_by_native.unchecked: | 1022 if not called_by_native.unchecked: |
1005 check_exception = 'jni_generator::CheckException(env);' | 1023 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] | 1361 root_name = os.path.splitext(os.path.basename(input_file))[0] |
1344 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 1362 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
1345 if options.jarjar: | 1363 if options.jarjar: |
1346 with open(options.jarjar) as f: | 1364 with open(options.jarjar) as f: |
1347 JniParams.SetJarJarMappings(f.read()) | 1365 JniParams.SetJarJarMappings(f.read()) |
1348 GenerateJNIHeader(input_file, output_file, options) | 1366 GenerateJNIHeader(input_file, output_file, options) |
1349 | 1367 |
1350 | 1368 |
1351 if __name__ == '__main__': | 1369 if __name__ == '__main__': |
1352 sys.exit(main(sys.argv)) | 1370 sys.exit(main(sys.argv)) |
OLD | NEW |