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 # TODO (qinmin): Need to refactor this file as base should not know about | 6 # TODO (qinmin): Need to refactor this file as base should not know about |
7 # higher level concepts. Currently this file has knowledge about higher level | 7 # higher level concepts. Currently this file has knowledge about higher level |
8 # java classes. | 8 # java classes. |
9 | 9 |
10 """Extracts native methods from a Java file and generates the JNI bindings. | 10 """Extracts native methods from a Java file and generates the JNI bindings. |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 script_components = os.path.abspath(sys.argv[0]).split(os.path.sep) | 543 script_components = os.path.abspath(sys.argv[0]).split(os.path.sep) |
544 base_index = script_components.index('base') | 544 base_index = script_components.index('base') |
545 script_name = os.sep.join(script_components[base_index:]) | 545 script_name = os.sep.join(script_components[base_index:]) |
546 values = { | 546 values = { |
547 'SCRIPT_NAME': script_name, | 547 'SCRIPT_NAME': script_name, |
548 'FULLY_QUALIFIED_CLASS': self.fully_qualified_class, | 548 'FULLY_QUALIFIED_CLASS': self.fully_qualified_class, |
549 'CLASS_PATH_DEFINITIONS': self.GetClassPathDefinitionsString(), | 549 'CLASS_PATH_DEFINITIONS': self.GetClassPathDefinitionsString(), |
550 'FORWARD_DECLARATIONS': self.GetForwardDeclarationsString(), | 550 'FORWARD_DECLARATIONS': self.GetForwardDeclarationsString(), |
551 'METHOD_STUBS': self.GetMethodStubsString(), | 551 'METHOD_STUBS': self.GetMethodStubsString(), |
552 'OPEN_NAMESPACE': self.GetOpenNamespaceString(), | 552 'OPEN_NAMESPACE': self.GetOpenNamespaceString(), |
553 'NAMESPACE': self.GetNamespaceString(), | |
554 'GET_METHOD_IDS_IMPL': self.GetMethodIDsImplString(), | 553 'GET_METHOD_IDS_IMPL': self.GetMethodIDsImplString(), |
555 'REGISTER_NATIVES_IMPL': self.GetRegisterNativesImplString(), | 554 'REGISTER_NATIVES_IMPL': self.GetRegisterNativesImplString(), |
556 'CLOSE_NAMESPACE': self.GetCloseNamespaceString(), | 555 'CLOSE_NAMESPACE': self.GetCloseNamespaceString(), |
557 'HEADER_GUARD': self.header_guard, | 556 'HEADER_GUARD': self.header_guard, |
558 } | 557 } |
559 return WrapOutput(template.substitute(values)) | 558 return WrapOutput(template.substitute(values)) |
560 | 559 |
561 def GetClassPathDefinitionsString(self): | 560 def GetClassPathDefinitionsString(self): |
562 ret = [] | 561 ret = [] |
563 ret += [self.GetClassPathDefinitions()] | 562 ret += [self.GetClassPathDefinitions()] |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 kmethods = self.GetKMethodsString(clazz) | 615 kmethods = self.GetKMethodsString(clazz) |
617 if kmethods: | 616 if kmethods: |
618 values = {'JAVA_CLASS': clazz, | 617 values = {'JAVA_CLASS': clazz, |
619 'KMETHODS': kmethods} | 618 'KMETHODS': kmethods} |
620 ret += [template.substitute(values)] | 619 ret += [template.substitute(values)] |
621 if not ret: return '' | 620 if not ret: return '' |
622 return '\n' + '\n'.join(ret) | 621 return '\n' + '\n'.join(ret) |
623 | 622 |
624 def GetOpenNamespaceString(self): | 623 def GetOpenNamespaceString(self): |
625 if self.namespace: | 624 if self.namespace: |
626 return 'namespace %s {' % self.namespace | 625 all_namespaces = ['namespace %s {' % ns |
627 return '' | 626 for ns in self.namespace.split('::')] |
628 | 627 return '\n'.join(all_namespaces) |
629 def GetNamespaceString(self): | |
630 if self.namespace: | |
631 return '%s::' % self.namespace | |
632 return '' | 628 return '' |
633 | 629 |
634 def GetCloseNamespaceString(self): | 630 def GetCloseNamespaceString(self): |
635 if self.namespace: | 631 if self.namespace: |
636 return '} // namespace %s\n' % self.namespace | 632 all_namespaces = ['} // namespace %s' % ns |
| 633 for ns in self.namespace.split('::')] |
| 634 all_namespaces.reverse() |
| 635 return '\n'.join(all_namespaces) + '\n' |
637 return '' | 636 return '' |
638 | 637 |
639 def GetJNIFirstParam(self, native): | 638 def GetJNIFirstParam(self, native): |
640 ret = [] | 639 ret = [] |
641 if native.type == 'method': | 640 if native.type == 'method': |
642 ret = ['jobject obj'] | 641 ret = ['jobject obj'] |
643 elif native.type == 'function': | 642 elif native.type == 'function': |
644 if native.static: | 643 if native.static: |
645 ret = ['jclass clazz'] | 644 ret = ['jclass clazz'] |
646 else: | 645 else: |
(...skipping 16 matching lines...) Expand all Loading... |
663 | 662 |
664 def GetCalledByNativeParamsInDeclaration(self, called_by_native): | 663 def GetCalledByNativeParamsInDeclaration(self, called_by_native): |
665 return ',\n '.join([JavaDataTypeToC(param.datatype) + ' ' + | 664 return ',\n '.join([JavaDataTypeToC(param.datatype) + ' ' + |
666 param.name | 665 param.name |
667 for param in called_by_native.params]) | 666 for param in called_by_native.params]) |
668 | 667 |
669 def GetForwardDeclaration(self, native): | 668 def GetForwardDeclaration(self, native): |
670 template = Template(""" | 669 template = Template(""" |
671 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); | 670 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); |
672 """) | 671 """) |
673 values = {'NAMESPACE': self.GetNamespaceString(), | 672 values = {'RETURN': JavaDataTypeToC(native.return_type), |
674 'RETURN': JavaDataTypeToC(native.return_type), | |
675 'NAME': native.name, | 673 'NAME': native.name, |
676 'PARAMS': self.GetParamsInDeclaration(native)} | 674 'PARAMS': self.GetParamsInDeclaration(native)} |
677 return template.substitute(values) | 675 return template.substitute(values) |
678 | 676 |
679 def GetNativeMethodStub(self, native): | 677 def GetNativeMethodStub(self, native): |
680 """Returns stubs for native methods.""" | 678 """Returns stubs for native methods.""" |
681 template = Template("""\ | 679 template = Template("""\ |
682 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) { | 680 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) { |
683 DCHECK(${PARAM0_NAME}) << "${NAME}"; | 681 DCHECK(${PARAM0_NAME}) << "${NAME}"; |
684 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); | 682 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); |
685 return native->${NAME}(env, obj${PARAMS_IN_CALL})${POST_CALL}; | 683 return native->${NAME}(env, obj${PARAMS_IN_CALL})${POST_CALL}; |
686 } | 684 } |
687 """) | 685 """) |
688 params_for_call = ', '.join(p.name for p in native.params[1:]) | 686 params_for_call = ', '.join(p.name for p in native.params[1:]) |
689 if params_for_call: | 687 if params_for_call: |
690 params_for_call = ', ' + params_for_call | 688 params_for_call = ', ' + params_for_call |
691 | 689 |
692 return_type = JavaDataTypeToC(native.return_type) | 690 return_type = JavaDataTypeToC(native.return_type) |
693 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): | 691 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): |
694 scoped_return_type = 'ScopedJavaLocalRef<' + return_type + '>' | 692 scoped_return_type = 'ScopedJavaLocalRef<' + return_type + '>' |
695 post_call = '.Release()' | 693 post_call = '.Release()' |
696 else: | 694 else: |
697 scoped_return_type = return_type | 695 scoped_return_type = return_type |
698 post_call = '' | 696 post_call = '' |
699 values = { | 697 values = { |
700 'RETURN': return_type, | 698 'RETURN': return_type, |
701 'SCOPED_RETURN': scoped_return_type, | 699 'SCOPED_RETURN': scoped_return_type, |
702 'NAMESPACE': self.GetNamespaceString(), | |
703 'NAME': native.name, | 700 'NAME': native.name, |
704 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), | 701 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), |
705 'PARAM0_NAME': native.params[0].name, | 702 'PARAM0_NAME': native.params[0].name, |
706 'P0_TYPE': native.p0_type, | 703 'P0_TYPE': native.p0_type, |
707 'PARAMS_IN_CALL': params_for_call, | 704 'PARAMS_IN_CALL': params_for_call, |
708 'POST_CALL': post_call | 705 'POST_CALL': post_call |
709 } | 706 } |
710 return template.substitute(values) | 707 return template.substitute(values) |
711 | 708 |
712 def GetCalledByNativeMethodStub(self, called_by_native): | 709 def GetCalledByNativeMethodStub(self, called_by_native): |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
966 input_file = options.input_file | 963 input_file = options.input_file |
967 output_file = None | 964 output_file = None |
968 if options.output_dir: | 965 if options.output_dir: |
969 root_name = os.path.splitext(os.path.basename(input_file))[0] | 966 root_name = os.path.splitext(os.path.basename(input_file))[0] |
970 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 967 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
971 GenerateJNIHeader(input_file, output_file, options.namespace) | 968 GenerateJNIHeader(input_file, output_file, options.namespace) |
972 | 969 |
973 | 970 |
974 if __name__ == '__main__': | 971 if __name__ == '__main__': |
975 sys.exit(main(sys.argv)) | 972 sys.exit(main(sys.argv)) |
OLD | NEW |