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 if '.' in class_name: | |
167 raise SyntaxError('*.class cannot be used in @JNIAdditionalImport. ' | |
168 'Only import unqualified outer classes.' % class_name) | |
169 new_import = 'L%s/%s' % (JniParams._package, class_name) | |
170 if new_import in JniParams._imports: | |
171 raise SyntaxError('Do not use JNIAdditionalImport on an already ' | |
172 'imported class: %s' % (new_import.replace('/', '.'))) | |
173 print 'Adding ' + new_import | |
bulach
2014/05/22 16:40:00
nit: remove
qsr
2014/05/22 16:47:55
Done.
| |
174 JniParams._imports += [new_import] | |
175 | |
176 @staticmethod | |
165 def ExtractImportsAndInnerClasses(contents): | 177 def ExtractImportsAndInnerClasses(contents): |
166 if not JniParams._package: | 178 if not JniParams._package: |
167 raise RuntimeError('SetFullyQualifiedClass must be called before ' | 179 raise RuntimeError('SetFullyQualifiedClass must be called before ' |
168 'ExtractImportsAndInnerClasses') | 180 'ExtractImportsAndInnerClasses') |
169 contents = contents.replace('\n', '') | 181 contents = contents.replace('\n', '') |
170 re_import = re.compile(r'import.*?(?P<class>\S*?);') | 182 re_import = re.compile(r'import.*?(?P<class>\S*?);') |
171 for match in re.finditer(re_import, contents): | 183 for match in re.finditer(re_import, contents): |
172 JniParams._imports += ['L' + match.group('class').replace('.', '/')] | 184 JniParams._imports += ['L' + match.group('class').replace('.', '/')] |
173 | 185 |
174 re_inner = re.compile(r'(class|interface)\s+?(?P<name>\w+?)\W') | 186 re_inner = re.compile(r'(class|interface)\s+?(?P<name>\w+?)\W') |
175 for match in re.finditer(re_inner, contents): | 187 for match in re.finditer(re_inner, contents): |
176 inner = match.group('name') | 188 inner = match.group('name') |
177 if not JniParams._fully_qualified_class.endswith(inner): | 189 if not JniParams._fully_qualified_class.endswith(inner): |
178 JniParams._inner_classes += [JniParams._fully_qualified_class + '$' + | 190 JniParams._inner_classes += [JniParams._fully_qualified_class + '$' + |
179 inner] | 191 inner] |
180 | 192 |
193 re_additional_import = re.compile( | |
194 r'@JNIAdditionalImport\((?P<class_name>\w+?)\.class\)') | |
195 for match in re.finditer(re_additional_import, contents): | |
196 JniParams.AddAdditionalImport(match.group('class_name')) | |
197 | |
181 re_additional_imports = re.compile( | 198 re_additional_imports = re.compile( |
182 r'@JNIAdditionalImport\((?P<class_name>\w+?)\.class\)') | 199 r'@JNIAdditionalImport\({(?P<class_names>.*?)}\)') |
bulach
2014/05/22 16:40:00
shouldn't this be sufficient, i.e., remove 193-197
qsr
2014/05/22 16:47:55
Not necessarly, you can use @JNIAdditionalImport(S
| |
183 for match in re.finditer(re_additional_imports, contents): | 200 for match in re.finditer(re_additional_imports, contents): |
184 class_name = match.group('class_name') | 201 for class_name in match.group('class_names').split(','): |
185 if '.' in class_name: | 202 JniParams.AddAdditionalImport(class_name.strip()[:-6]) |
bulach
2014/05/22 16:40:00
hmm, the -6 seems out of place here.
how about jus
qsr
2014/05/22 16:47:55
Done.
| |
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 | 203 |
194 @staticmethod | 204 @staticmethod |
195 def ParseJavaPSignature(signature_line): | 205 def ParseJavaPSignature(signature_line): |
196 prefix = 'Signature: ' | 206 prefix = 'Signature: ' |
197 return '"%s"' % signature_line[signature_line.index(prefix) + len(prefix):] | 207 return '"%s"' % signature_line[signature_line.index(prefix) + len(prefix):] |
198 | 208 |
199 @staticmethod | 209 @staticmethod |
200 def JavaToJni(param): | 210 def JavaToJni(param): |
201 """Converts a java param into a JNI signature type.""" | 211 """Converts a java param into a JNI signature type.""" |
202 pod_param_map = { | 212 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] | 1412 root_name = os.path.splitext(os.path.basename(input_file))[0] |
1403 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 1413 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
1404 if options.jarjar: | 1414 if options.jarjar: |
1405 with open(options.jarjar) as f: | 1415 with open(options.jarjar) as f: |
1406 JniParams.SetJarJarMappings(f.read()) | 1416 JniParams.SetJarJarMappings(f.read()) |
1407 GenerateJNIHeader(input_file, output_file, options) | 1417 GenerateJNIHeader(input_file, output_file, options) |
1408 | 1418 |
1409 | 1419 |
1410 if __name__ == '__main__': | 1420 if __name__ == '__main__': |
1411 sys.exit(main(sys.argv)) | 1421 sys.exit(main(sys.argv)) |
OLD | NEW |