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 optparse | 10 import optparse |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 param = param[:-2] | 161 param = param[:-2] |
162 # Generic? | 162 # Generic? |
163 if '<' in param: | 163 if '<' in param: |
164 param = param[:param.index('<')] | 164 param = param[:param.index('<')] |
165 if param in pod_param_map: | 165 if param in pod_param_map: |
166 return prefix + pod_param_map[param] | 166 return prefix + pod_param_map[param] |
167 if '/' in param: | 167 if '/' in param: |
168 # Coming from javap, use the fully qualified param directly. | 168 # Coming from javap, use the fully qualified param directly. |
169 return 'L' + param + ';' | 169 return 'L' + param + ';' |
170 for qualified_name in (object_param_list + | 170 for qualified_name in (object_param_list + |
171 JniParams._imports + | |
172 [JniParams._fully_qualified_class] + | 171 [JniParams._fully_qualified_class] + |
173 JniParams._inner_classes): | 172 JniParams._inner_classes): |
174 if (qualified_name.endswith('/' + param) or | 173 if (qualified_name.endswith('/' + param) or |
175 qualified_name.endswith('$' + param.replace('.', '$')) or | 174 qualified_name.endswith('$' + param.replace('.', '$')) or |
176 qualified_name == 'L' + param): | 175 qualified_name == 'L' + param): |
177 return prefix + qualified_name + ';' | 176 return prefix + qualified_name + ';' |
| 177 |
| 178 # Is it from an import? (e.g. referecing Class from import pkg.Class; |
| 179 # note that referencing an inner class Inner from import pkg.Class.Inner |
| 180 # is not supported). |
| 181 for qualified_name in JniParams._imports: |
| 182 if qualified_name.endswith('/' + param): |
| 183 # Ensure it's not an inner class. |
| 184 components = qualified_name.split('/') |
| 185 if len(components) > 2 and components[-2][0].isupper(): |
| 186 raise SyntaxError('Inner class (%s) can not be imported ' |
| 187 'and used by JNI (%s). Please import the outer ' |
| 188 'class and use Outer.Inner instead.' % |
| 189 (qualified_name, param)) |
| 190 return prefix + qualified_name + ';' |
| 191 |
| 192 # Is it an inner class from an outer class import? (e.g. referencing |
| 193 # Class.Inner from import pkg.Class). |
| 194 if '.' in param: |
| 195 components = param.split('.') |
| 196 outer = '/'.join(components[:-1]) |
| 197 inner = components[-1] |
| 198 for qualified_name in JniParams._imports: |
| 199 if qualified_name.endswith('/' + outer): |
| 200 return prefix + qualified_name + '$' + inner |
| 201 |
178 # Type not found, falling back to same package as this class. | 202 # Type not found, falling back to same package as this class. |
179 return prefix + 'L' + JniParams._package + '/' + param + ';' | 203 return prefix + 'L' + JniParams._package + '/' + param + ';' |
180 | 204 |
181 @staticmethod | 205 @staticmethod |
182 def Signature(params, returns, wrap): | 206 def Signature(params, returns, wrap): |
183 """Returns the JNI signature for the given datatypes.""" | 207 """Returns the JNI signature for the given datatypes.""" |
184 items = ['('] | 208 items = ['('] |
185 items += [JniParams.JavaToJni(param.datatype) for param in params] | 209 items += [JniParams.JavaToJni(param.datatype) for param in params] |
186 items += [')'] | 210 items += [')'] |
187 items += [JniParams.JavaToJni(returns)] | 211 items += [JniParams.JavaToJni(returns)] |
(...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
968 input_file = options.input_file | 992 input_file = options.input_file |
969 output_file = None | 993 output_file = None |
970 if options.output_dir: | 994 if options.output_dir: |
971 root_name = os.path.splitext(os.path.basename(input_file))[0] | 995 root_name = os.path.splitext(os.path.basename(input_file))[0] |
972 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' | 996 output_file = os.path.join(options.output_dir, root_name) + '_jni.h' |
973 GenerateJNIHeader(input_file, output_file, options.namespace) | 997 GenerateJNIHeader(input_file, output_file, options.namespace) |
974 | 998 |
975 | 999 |
976 if __name__ == '__main__': | 1000 if __name__ == '__main__': |
977 sys.exit(main(sys.argv)) | 1001 sys.exit(main(sys.argv)) |
OLD | NEW |