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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 _inner_classes = [] | 155 _inner_classes = [] |
156 _remappings = [] | 156 _remappings = [] |
157 _implicit_imports = [] | 157 _implicit_imports = [] |
158 | 158 |
159 @staticmethod | 159 @staticmethod |
160 def SetFullyQualifiedClass(fully_qualified_class): | 160 def SetFullyQualifiedClass(fully_qualified_class): |
161 JniParams._fully_qualified_class = 'L' + fully_qualified_class | 161 JniParams._fully_qualified_class = 'L' + fully_qualified_class |
162 JniParams._package = '/'.join(fully_qualified_class.split('/')[:-1]) | 162 JniParams._package = '/'.join(fully_qualified_class.split('/')[:-1]) |
163 | 163 |
164 @staticmethod | 164 @staticmethod |
| 165 def AddAdditionalImport(class_name): |
| 166 assert class_name.endswith('.class') |
| 167 raw_class_name = class_name[:-len('.class')] |
| 168 if '.' in raw_class_name: |
| 169 raise SyntaxError('%s cannot be used in @JNIAdditionalImport. ' |
| 170 'Only import unqualified outer classes.' % class_name) |
| 171 new_import = 'L%s/%s' % (JniParams._package, raw_class_name) |
| 172 if new_import in JniParams._imports: |
| 173 raise SyntaxError('Do not use JNIAdditionalImport on an already ' |
| 174 'imported class: %s' % (new_import.replace('/', '.'))) |
| 175 JniParams._imports += [new_import] |
| 176 |
| 177 @staticmethod |
165 def ExtractImportsAndInnerClasses(contents): | 178 def ExtractImportsAndInnerClasses(contents): |
166 if not JniParams._package: | 179 if not JniParams._package: |
167 raise RuntimeError('SetFullyQualifiedClass must be called before ' | 180 raise RuntimeError('SetFullyQualifiedClass must be called before ' |
168 'ExtractImportsAndInnerClasses') | 181 'ExtractImportsAndInnerClasses') |
169 contents = contents.replace('\n', '') | 182 contents = contents.replace('\n', '') |
170 re_import = re.compile(r'import.*?(?P<class>\S*?);') | 183 re_import = re.compile(r'import.*?(?P<class>\S*?);') |
171 for match in re.finditer(re_import, contents): | 184 for match in re.finditer(re_import, contents): |
172 JniParams._imports += ['L' + match.group('class').replace('.', '/')] | 185 JniParams._imports += ['L' + match.group('class').replace('.', '/')] |
173 | 186 |
174 re_inner = re.compile(r'(class|interface)\s+?(?P<name>\w+?)\W') | 187 re_inner = re.compile(r'(class|interface)\s+?(?P<name>\w+?)\W') |
175 for match in re.finditer(re_inner, contents): | 188 for match in re.finditer(re_inner, contents): |
176 inner = match.group('name') | 189 inner = match.group('name') |
177 if not JniParams._fully_qualified_class.endswith(inner): | 190 if not JniParams._fully_qualified_class.endswith(inner): |
178 JniParams._inner_classes += [JniParams._fully_qualified_class + '$' + | 191 JniParams._inner_classes += [JniParams._fully_qualified_class + '$' + |
179 inner] | 192 inner] |
180 | 193 |
181 re_additional_imports = re.compile( | 194 re_additional_imports = re.compile( |
182 r'@JNIAdditionalImport\((?P<class_name>\w+?)\.class\)') | 195 r'@JNIAdditionalImport\(\s*{?(?P<class_names>.*?)}?\s*\)') |
183 for match in re.finditer(re_additional_imports, contents): | 196 for match in re.finditer(re_additional_imports, contents): |
184 class_name = match.group('class_name') | 197 for class_name in match.group('class_names').split(','): |
185 if '.' in class_name: | 198 JniParams.AddAdditionalImport(class_name.strip()) |
186 raise SyntaxError('*.class cannot be used in @JNIAdditionalImport. ' | |
187 'Only import unqualified outer classes.' % class_name) | |
188 new_import = 'L%s/%s' % (JniParams._package, class_name) | |
189 if new_import in JniParams._imports: | |
190 raise SyntaxError('Do not use JNIAdditionalImport on an already ' | |
191 'imported class: %s' % (new_import.replace('/', '.'))) | |
192 JniParams._imports += [new_import] | |
193 | 199 |
194 @staticmethod | 200 @staticmethod |
195 def ParseJavaPSignature(signature_line): | 201 def ParseJavaPSignature(signature_line): |
196 prefix = 'Signature: ' | 202 prefix = 'Signature: ' |
197 return '"%s"' % signature_line[signature_line.index(prefix) + len(prefix):] | 203 return '"%s"' % signature_line[signature_line.index(prefix) + len(prefix):] |
198 | 204 |
199 @staticmethod | 205 @staticmethod |
200 def JavaToJni(param): | 206 def JavaToJni(param): |
201 """Converts a java param into a JNI signature type.""" | 207 """Converts a java param into a JNI signature type.""" |
202 pod_param_map = { | 208 pod_param_map = { |
(...skipping 1199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1402 root_name = os.path.splitext(os.path.basename(input_file))[0] | 1408 root_name = os.path.splitext(os.path.basename(input_file))[0] |
1403 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 1409 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
1404 if options.jarjar: | 1410 if options.jarjar: |
1405 with open(options.jarjar) as f: | 1411 with open(options.jarjar) as f: |
1406 JniParams.SetJarJarMappings(f.read()) | 1412 JniParams.SetJarJarMappings(f.read()) |
1407 GenerateJNIHeader(input_file, output_file, options) | 1413 GenerateJNIHeader(input_file, output_file, options) |
1408 | 1414 |
1409 | 1415 |
1410 if __name__ == '__main__': | 1416 if __name__ == '__main__': |
1411 sys.exit(main(sys.argv)) | 1417 sys.exit(main(sys.argv)) |
OLD | NEW |