OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generates java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
6 | 6 |
7 import argparse | 7 import argparse |
8 import ast | 8 import ast |
9 import contextlib | 9 import contextlib |
10 import os | 10 import os |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 def GetArrayExpectedLength(kind): | 339 def GetArrayExpectedLength(kind): |
340 if mojom.IsArrayKind(kind) and kind.length is not None: | 340 if mojom.IsArrayKind(kind) and kind.length is not None: |
341 return getattr(kind, 'java_map_size', str(kind.length)) | 341 return getattr(kind, 'java_map_size', str(kind.length)) |
342 else: | 342 else: |
343 return 'org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH' | 343 return 'org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH' |
344 | 344 |
345 def IsPointerArrayKind(kind): | 345 def IsPointerArrayKind(kind): |
346 if not mojom.IsArrayKind(kind): | 346 if not mojom.IsArrayKind(kind): |
347 return False | 347 return False |
348 sub_kind = kind.kind | 348 sub_kind = kind.kind |
349 return mojom.IsObjectKind(sub_kind) | 349 return mojom.IsObjectKind(sub_kind) and not mojom.IsUnionKind(sub_kind) |
| 350 |
| 351 def IsUnionArrayKind(kind): |
| 352 if not mojom.IsArrayKind(kind): |
| 353 return False |
| 354 sub_kind = kind.kind |
| 355 return mojom.IsUnionKind(sub_kind) |
350 | 356 |
351 def GetConstantsMainEntityName(module): | 357 def GetConstantsMainEntityName(module): |
352 if module.attributes and 'JavaConstantsClassName' in module.attributes: | 358 if module.attributes and 'JavaConstantsClassName' in module.attributes: |
353 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) | 359 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
354 # This constructs the name of the embedding classes for module level constants | 360 # This constructs the name of the embedding classes for module level constants |
355 # by extracting the mojom's filename and prepending it to Constants. | 361 # by extracting the mojom's filename and prepending it to Constants. |
356 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 362 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
357 'Constants') | 363 'Constants') |
358 | 364 |
359 def GetMethodOrdinalName(method): | 365 def GetMethodOrdinalName(method): |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 'has_method_with_response': HasMethodWithResponse, | 407 'has_method_with_response': HasMethodWithResponse, |
402 'interface_response_name': GetInterfaceResponseName, | 408 'interface_response_name': GetInterfaceResponseName, |
403 'is_array_kind': mojom.IsArrayKind, | 409 'is_array_kind': mojom.IsArrayKind, |
404 'is_any_handle_kind': mojom.IsAnyHandleKind, | 410 'is_any_handle_kind': mojom.IsAnyHandleKind, |
405 'is_interface_request_kind': mojom.IsInterfaceRequestKind, | 411 'is_interface_request_kind': mojom.IsInterfaceRequestKind, |
406 'is_map_kind': mojom.IsMapKind, | 412 'is_map_kind': mojom.IsMapKind, |
407 'is_nullable_kind': mojom.IsNullableKind, | 413 'is_nullable_kind': mojom.IsNullableKind, |
408 'is_pointer_array_kind': IsPointerArrayKind, | 414 'is_pointer_array_kind': IsPointerArrayKind, |
409 'is_reference_kind': mojom.IsReferenceKind, | 415 'is_reference_kind': mojom.IsReferenceKind, |
410 'is_struct_kind': mojom.IsStructKind, | 416 'is_struct_kind': mojom.IsStructKind, |
| 417 'is_union_array_kind': IsUnionArrayKind, |
411 'is_union_kind': mojom.IsUnionKind, | 418 'is_union_kind': mojom.IsUnionKind, |
412 'java_true_false': GetJavaTrueFalse, | 419 'java_true_false': GetJavaTrueFalse, |
413 'java_type': GetJavaType, | 420 'java_type': GetJavaType, |
414 'method_ordinal_name': GetMethodOrdinalName, | 421 'method_ordinal_name': GetMethodOrdinalName, |
415 'name': GetNameForElement, | 422 'name': GetNameForElement, |
416 'new_array': NewArray, | 423 'new_array': NewArray, |
| 424 'ucc': lambda x: UpperCamelCase(x.name), |
417 } | 425 } |
418 | 426 |
419 def GetJinjaExports(self): | 427 def GetJinjaExports(self): |
420 return { | 428 return { |
421 'package': GetPackage(self.module), | 429 'package': GetPackage(self.module), |
422 } | 430 } |
423 | 431 |
424 def GetJinjaExportsForInterface(self, interface): | 432 def GetJinjaExportsForInterface(self, interface): |
425 exports = self.GetJinjaExports() | 433 exports = self.GetJinjaExports() |
426 exports.update({'interface': interface}) | 434 exports.update({'interface': interface}) |
427 return exports | 435 return exports |
428 | 436 |
429 @UseJinja('java_templates/enum.java.tmpl', filters=java_filters) | 437 @UseJinja('java_templates/enum.java.tmpl', filters=java_filters) |
430 def GenerateEnumSource(self, enum): | 438 def GenerateEnumSource(self, enum): |
431 exports = self.GetJinjaExports() | 439 exports = self.GetJinjaExports() |
432 exports.update({'enum': enum}) | 440 exports.update({'enum': enum}) |
433 return exports | 441 return exports |
434 | 442 |
435 @UseJinja('java_templates/struct.java.tmpl', filters=java_filters) | 443 @UseJinja('java_templates/struct.java.tmpl', filters=java_filters) |
436 def GenerateStructSource(self, struct): | 444 def GenerateStructSource(self, struct): |
437 exports = self.GetJinjaExports() | 445 exports = self.GetJinjaExports() |
438 exports.update({'struct': struct}) | 446 exports.update({'struct': struct}) |
439 return exports | 447 return exports |
440 | 448 |
| 449 @UseJinja('java_templates/union.java.tmpl', filters=java_filters) |
| 450 def GenerateUnionSource(self, union): |
| 451 exports = self.GetJinjaExports() |
| 452 exports.update({'union': union}) |
| 453 return exports |
| 454 |
441 @UseJinja('java_templates/interface.java.tmpl', filters=java_filters) | 455 @UseJinja('java_templates/interface.java.tmpl', filters=java_filters) |
442 def GenerateInterfaceSource(self, interface): | 456 def GenerateInterfaceSource(self, interface): |
443 return self.GetJinjaExportsForInterface(interface) | 457 return self.GetJinjaExportsForInterface(interface) |
444 | 458 |
445 @UseJinja('java_templates/interface_internal.java.tmpl', filters=java_filters) | 459 @UseJinja('java_templates/interface_internal.java.tmpl', filters=java_filters) |
446 def GenerateInterfaceInternalSource(self, interface): | 460 def GenerateInterfaceInternalSource(self, interface): |
447 return self.GetJinjaExportsForInterface(interface) | 461 return self.GetJinjaExportsForInterface(interface) |
448 | 462 |
449 @UseJinja('java_templates/constants.java.tmpl', filters=java_filters) | 463 @UseJinja('java_templates/constants.java.tmpl', filters=java_filters) |
450 def GenerateConstantsSource(self, module): | 464 def GenerateConstantsSource(self, module): |
451 exports = self.GetJinjaExports() | 465 exports = self.GetJinjaExports() |
452 exports.update({'main_entity': GetConstantsMainEntityName(module), | 466 exports.update({'main_entity': GetConstantsMainEntityName(module), |
453 'constants': module.constants}) | 467 'constants': module.constants}) |
454 return exports | 468 return exports |
455 | 469 |
456 def DoGenerateFiles(self): | 470 def DoGenerateFiles(self): |
457 fileutil.EnsureDirectoryExists(self.output_dir) | 471 fileutil.EnsureDirectoryExists(self.output_dir) |
458 | 472 |
459 # Keep this above the others as .GetStructs() changes the state of the | 473 # Keep this above the others as .GetStructs() changes the state of the |
460 # module, annotating structs with required information. | 474 # module, annotating structs with required information. |
461 for struct in self.GetStructs(): | 475 for struct in self.GetStructs(): |
462 self.Write(self.GenerateStructSource(struct), | 476 self.Write(self.GenerateStructSource(struct), |
463 '%s.java' % GetNameForElement(struct)) | 477 '%s.java' % GetNameForElement(struct)) |
464 | 478 |
| 479 for union in self.module.unions: |
| 480 self.Write(self.GenerateUnionSource(union), |
| 481 '%s.java' % GetNameForElement(union)) |
| 482 |
465 for enum in self.module.enums: | 483 for enum in self.module.enums: |
466 self.Write(self.GenerateEnumSource(enum), | 484 self.Write(self.GenerateEnumSource(enum), |
467 '%s.java' % GetNameForElement(enum)) | 485 '%s.java' % GetNameForElement(enum)) |
468 | 486 |
469 for interface in self.GetInterfaces(): | 487 for interface in self.GetInterfaces(): |
470 self.Write(self.GenerateInterfaceSource(interface), | 488 self.Write(self.GenerateInterfaceSource(interface), |
471 '%s.java' % GetNameForElement(interface)) | 489 '%s.java' % GetNameForElement(interface)) |
472 self.Write(self.GenerateInterfaceInternalSource(interface), | 490 self.Write(self.GenerateInterfaceInternalSource(interface), |
473 '%s_Internal.java' % GetNameForElement(interface)) | 491 '%s_Internal.java' % GetNameForElement(interface)) |
474 | 492 |
(...skipping 25 matching lines...) Expand all Loading... |
500 return { | 518 return { |
501 'lstrip_blocks': True, | 519 'lstrip_blocks': True, |
502 'trim_blocks': True, | 520 'trim_blocks': True, |
503 } | 521 } |
504 | 522 |
505 def GetGlobals(self): | 523 def GetGlobals(self): |
506 return { | 524 return { |
507 'namespace': self.module.namespace, | 525 'namespace': self.module.namespace, |
508 'module': self.module, | 526 'module': self.module, |
509 } | 527 } |
OLD | NEW |