Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_java_generator.py

Issue 2864543002: Mojo code generator: makes the filters member methods of generator if necessary. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 396
397 @contextlib.contextmanager 397 @contextlib.contextmanager
398 def TempDir(): 398 def TempDir():
399 dirname = tempfile.mkdtemp() 399 dirname = tempfile.mkdtemp()
400 try: 400 try:
401 yield dirname 401 yield dirname
402 finally: 402 finally:
403 shutil.rmtree(dirname) 403 shutil.rmtree(dirname)
404 404
405 class Generator(generator.Generator): 405 class Generator(generator.Generator):
406 406 def _GetJinjaExports(self):
407 java_filters = {
408 'array_expected_length': GetArrayExpectedLength,
409 'array': GetArrayKind,
410 'constant_value': ConstantValue,
411 'decode_method': DecodeMethod,
412 'default_value': DefaultValue,
413 'encode_method': EncodeMethod,
414 'expression_to_text': ExpressionToText,
415 'has_method_without_response': HasMethodWithoutResponse,
416 'has_method_with_response': HasMethodWithResponse,
417 'interface_response_name': GetInterfaceResponseName,
418 'is_array_kind': mojom.IsArrayKind,
419 'is_any_handle_kind': mojom.IsAnyHandleKind,
420 "is_enum_kind": mojom.IsEnumKind,
421 'is_interface_request_kind': mojom.IsInterfaceRequestKind,
422 'is_map_kind': mojom.IsMapKind,
423 'is_nullable_kind': mojom.IsNullableKind,
424 'is_pointer_array_kind': IsPointerArrayKind,
425 'is_reference_kind': mojom.IsReferenceKind,
426 'is_struct_kind': mojom.IsStructKind,
427 'is_union_array_kind': IsUnionArrayKind,
428 'is_union_kind': mojom.IsUnionKind,
429 'java_class_for_enum': GetJavaClassForEnum,
430 'java_true_false': GetJavaTrueFalse,
431 'java_type': GetJavaType,
432 'method_ordinal_name': GetMethodOrdinalName,
433 'name': GetNameForElement,
434 'new_array': NewArray,
435 'ucc': lambda x: UpperCamelCase(x.name),
436 }
437
438 def GetJinjaExports(self):
439 return { 407 return {
440 'package': GetPackage(self.module), 408 'package': GetPackage(self.module),
441 } 409 }
442 410
443 @staticmethod 411 @staticmethod
444 def GetTemplatePrefix(): 412 def GetTemplatePrefix():
445 return "java_templates" 413 return "java_templates"
446 414
447 @classmethod 415 def GetFilters(self):
448 def GetFilters(cls): 416 java_filters = {
449 return cls.java_filters 417 'array_expected_length': GetArrayExpectedLength,
418 'array': GetArrayKind,
419 'constant_value': ConstantValue,
420 'decode_method': DecodeMethod,
421 'default_value': DefaultValue,
422 'encode_method': EncodeMethod,
423 'expression_to_text': ExpressionToText,
424 'has_method_without_response': HasMethodWithoutResponse,
425 'has_method_with_response': HasMethodWithResponse,
426 'interface_response_name': GetInterfaceResponseName,
427 'is_array_kind': mojom.IsArrayKind,
428 'is_any_handle_kind': mojom.IsAnyHandleKind,
429 "is_enum_kind": mojom.IsEnumKind,
430 'is_interface_request_kind': mojom.IsInterfaceRequestKind,
431 'is_map_kind': mojom.IsMapKind,
432 'is_nullable_kind': mojom.IsNullableKind,
433 'is_pointer_array_kind': IsPointerArrayKind,
434 'is_reference_kind': mojom.IsReferenceKind,
435 'is_struct_kind': mojom.IsStructKind,
436 'is_union_array_kind': IsUnionArrayKind,
437 'is_union_kind': mojom.IsUnionKind,
438 'java_class_for_enum': GetJavaClassForEnum,
439 'java_true_false': GetJavaTrueFalse,
440 'java_type': GetJavaType,
441 'method_ordinal_name': GetMethodOrdinalName,
442 'name': GetNameForElement,
443 'new_array': NewArray,
444 'ucc': lambda x: UpperCamelCase(x.name),
445 }
446 return java_filters
450 447
451 def GetJinjaExportsForInterface(self, interface): 448 def _GetJinjaExportsForInterface(self, interface):
452 exports = self.GetJinjaExports() 449 exports = self._GetJinjaExports()
453 exports.update({'interface': interface}) 450 exports.update({'interface': interface})
454 return exports 451 return exports
455 452
456 @UseJinja('enum.java.tmpl') 453 @UseJinja('enum.java.tmpl')
457 def GenerateEnumSource(self, enum): 454 def _GenerateEnumSource(self, enum):
458 exports = self.GetJinjaExports() 455 exports = self._GetJinjaExports()
459 exports.update({'enum': enum}) 456 exports.update({'enum': enum})
460 return exports 457 return exports
461 458
462 @UseJinja('struct.java.tmpl') 459 @UseJinja('struct.java.tmpl')
463 def GenerateStructSource(self, struct): 460 def _GenerateStructSource(self, struct):
464 exports = self.GetJinjaExports() 461 exports = self._GetJinjaExports()
465 exports.update({'struct': struct}) 462 exports.update({'struct': struct})
466 return exports 463 return exports
467 464
468 @UseJinja('union.java.tmpl') 465 @UseJinja('union.java.tmpl')
469 def GenerateUnionSource(self, union): 466 def _GenerateUnionSource(self, union):
470 exports = self.GetJinjaExports() 467 exports = self._GetJinjaExports()
471 exports.update({'union': union}) 468 exports.update({'union': union})
472 return exports 469 return exports
473 470
474 @UseJinja('interface.java.tmpl') 471 @UseJinja('interface.java.tmpl')
475 def GenerateInterfaceSource(self, interface): 472 def _GenerateInterfaceSource(self, interface):
476 return self.GetJinjaExportsForInterface(interface) 473 return self._GetJinjaExportsForInterface(interface)
477 474
478 @UseJinja('interface_internal.java.tmpl') 475 @UseJinja('interface_internal.java.tmpl')
479 def GenerateInterfaceInternalSource(self, interface): 476 def _GenerateInterfaceInternalSource(self, interface):
480 return self.GetJinjaExportsForInterface(interface) 477 return self._GetJinjaExportsForInterface(interface)
481 478
482 @UseJinja('constants.java.tmpl') 479 @UseJinja('constants.java.tmpl')
483 def GenerateConstantsSource(self, module): 480 def _GenerateConstantsSource(self, module):
484 exports = self.GetJinjaExports() 481 exports = self._GetJinjaExports()
485 exports.update({'main_entity': GetConstantsMainEntityName(module), 482 exports.update({'main_entity': GetConstantsMainEntityName(module),
486 'constants': module.constants}) 483 'constants': module.constants})
487 return exports 484 return exports
488 485
489 def DoGenerateFiles(self): 486 def _DoGenerateFiles(self):
490 fileutil.EnsureDirectoryExists(self.output_dir) 487 fileutil.EnsureDirectoryExists(self.output_dir)
491 488
492 # Keep this above the others as .GetStructs() changes the state of the 489 # Keep this above the others as .GetStructs() changes the state of the
493 # module, annotating structs with required information. 490 # module, annotating structs with required information.
494 for struct in self.GetStructs(): 491 for struct in self.GetStructs():
495 self.Write(self.GenerateStructSource(struct), 492 self.Write(self._GenerateStructSource(struct),
496 '%s.java' % GetNameForElement(struct)) 493 '%s.java' % GetNameForElement(struct))
497 494
498 for union in self.module.unions: 495 for union in self.module.unions:
499 self.Write(self.GenerateUnionSource(union), 496 self.Write(self._GenerateUnionSource(union),
500 '%s.java' % GetNameForElement(union)) 497 '%s.java' % GetNameForElement(union))
501 498
502 for enum in self.module.enums: 499 for enum in self.module.enums:
503 self.Write(self.GenerateEnumSource(enum), 500 self.Write(self._GenerateEnumSource(enum),
504 '%s.java' % GetNameForElement(enum)) 501 '%s.java' % GetNameForElement(enum))
505 502
506 for interface in self.GetInterfaces(): 503 for interface in self.GetInterfaces():
507 self.Write(self.GenerateInterfaceSource(interface), 504 self.Write(self._GenerateInterfaceSource(interface),
508 '%s.java' % GetNameForElement(interface)) 505 '%s.java' % GetNameForElement(interface))
509 self.Write(self.GenerateInterfaceInternalSource(interface), 506 self.Write(self._GenerateInterfaceInternalSource(interface),
510 '%s_Internal.java' % GetNameForElement(interface)) 507 '%s_Internal.java' % GetNameForElement(interface))
511 508
512 if self.module.constants: 509 if self.module.constants:
513 self.Write(self.GenerateConstantsSource(self.module), 510 self.Write(self._GenerateConstantsSource(self.module),
514 '%s.java' % GetConstantsMainEntityName(self.module)) 511 '%s.java' % GetConstantsMainEntityName(self.module))
515 512
516 def GenerateFiles(self, unparsed_args): 513 def GenerateFiles(self, unparsed_args):
517 # TODO(rockot): Support variant output for Java. 514 # TODO(rockot): Support variant output for Java.
518 if self.variant: 515 if self.variant:
519 raise Exception("Variants not supported in Java bindings.") 516 raise Exception("Variants not supported in Java bindings.")
520 517
521 parser = argparse.ArgumentParser() 518 parser = argparse.ArgumentParser()
522 parser.add_argument('--java_output_directory', dest='java_output_directory') 519 parser.add_argument('--java_output_directory', dest='java_output_directory')
523 args = parser.parse_args(unparsed_args) 520 args = parser.parse_args(unparsed_args)
524 package_path = GetPackage(self.module).replace('.', '/') 521 package_path = GetPackage(self.module).replace('.', '/')
525 522
526 # Generate the java files in a temporary directory and place a single 523 # Generate the java files in a temporary directory and place a single
527 # srcjar in the output directory. 524 # srcjar in the output directory.
528 basename = self.MatchMojomFilePath("%s.srcjar" % self.module.name) 525 basename = self.MatchMojomFilePath("%s.srcjar" % self.module.name)
529 zip_filename = os.path.join(self.output_dir, basename) 526 zip_filename = os.path.join(self.output_dir, basename)
530 with TempDir() as temp_java_root: 527 with TempDir() as temp_java_root:
531 self.output_dir = os.path.join(temp_java_root, package_path) 528 self.output_dir = os.path.join(temp_java_root, package_path)
532 self.DoGenerateFiles(); 529 self._DoGenerateFiles();
533 build_utils.ZipDir(zip_filename, temp_java_root) 530 build_utils.ZipDir(zip_filename, temp_java_root)
534 531
535 if args.java_output_directory: 532 if args.java_output_directory:
536 # If requested, generate the java files directly into indicated directory. 533 # If requested, generate the java files directly into indicated directory.
537 self.output_dir = os.path.join(args.java_output_directory, package_path) 534 self.output_dir = os.path.join(args.java_output_directory, package_path)
538 self.DoGenerateFiles(); 535 self._DoGenerateFiles();
539 536
540 def GetJinjaParameters(self): 537 def GetJinjaParameters(self):
541 return { 538 return {
542 'lstrip_blocks': True, 539 'lstrip_blocks': True,
543 'trim_blocks': True, 540 'trim_blocks': True,
544 } 541 }
545 542
546 def GetGlobals(self): 543 def GetGlobals(self):
547 return { 544 return {
548 'namespace': self.module.namespace, 545 'namespace': self.module.namespace,
549 'module': self.module, 546 'module': self.module,
550 } 547 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698