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

Unified Diff: mojo/public/tools/bindings/generators/mojom_java_generator.py

Issue 306893010: Generate java bindings for structs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Re-upload after revert Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/mojom_java_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_java_generator.py b/mojo/public/tools/bindings/generators/mojom_java_generator.py
index ca43b8456304a85a6b161f1afe6cfef75cac77a5..4755b0db6fc292d21fdd71184e410cf91c6e246e 100644
--- a/mojo/public/tools/bindings/generators/mojom_java_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_java_generator.py
@@ -8,6 +8,8 @@ import argparse
import os
import re
+from jinja2 import contextfilter
+
import mojom.generate.generator as generator
import mojom.generate.module as mojom
from mojom.generate.template_expander import UseJinja
@@ -62,19 +64,21 @@ def ConstantStyle(name):
return '_'.join([x.upper() for x in components])
def GetNameForElement(element):
- if (isinstance(element, mojom.Enum) or
- isinstance(element, mojom.Interface) or
- isinstance(element, mojom.Struct)):
+ if isinstance(element, (mojom.Enum,
+ mojom.Interface,
+ mojom.Struct)):
return UpperCamelCase(element.name)
- if (isinstance(element, mojom.Method) or
- isinstance(element, mojom.Parameter) or
- isinstance(element, mojom.Field)):
+ if isinstance(element, mojom.InterfaceRequest):
+ return GetNameForElement(element.kind)
+ if isinstance(element, (mojom.Method,
+ mojom.Parameter,
+ mojom.Field)):
return CamelCase(element.name)
if isinstance(element, mojom.EnumValue):
return (UpperCamelCase(element.enum_name) + '.' +
ConstantStyle(element.name))
- if (isinstance(element, mojom.NamedValue) or
- isinstance(element, mojom.Constant)):
+ if isinstance(element, (mojom.NamedValue,
+ mojom.Constant)):
return ConstantStyle(element.name)
raise Exception("Unexpected element: " % element)
@@ -88,36 +92,57 @@ def GetPackage(module):
# Default package.
return "org.chromium.mojom." + module.namespace
-def GetNameForKind(kind):
+def GetNameForKind(context, kind):
def _GetNameHierachy(kind):
hierachy = []
if kind.parent_kind:
hierachy = _GetNameHierachy(kind.parent_kind)
- hierachy.append(kind.name)
+ hierachy.append(GetNameForElement(kind))
return hierachy
- elements = [GetPackage(kind.module)]
+ module = context.resolve('module')
+ elements = []
+ if GetPackage(module) != GetPackage(kind.module):
+ elements += [GetPackage(kind.module)]
elements += _GetNameHierachy(kind)
return '.'.join(elements)
-def GetJavaType(kind):
+@contextfilter
+def GetJavaType(context, kind):
if isinstance(kind, (mojom.Struct, mojom.Interface)):
- return GetNameForKind(kind)
- if isinstance(kind, mojom.Array):
- return "%s[]" % GetJavaType(kind.kind)
+ return GetNameForKind(context, kind)
+ if isinstance(kind, mojom.InterfaceRequest):
+ return GetNameForKind(context, kind.kind)
+ if isinstance(kind, (mojom.Array, mojom.FixedArray)):
+ return "%s[]" % GetJavaType(context, kind.kind)
if isinstance(kind, mojom.Enum):
return "int"
return _spec_to_java_type[kind.spec]
-def ExpressionToText(token):
+def IsHandle(kind):
+ return kind.spec[0] == 'h'
+
+@contextfilter
+def DefaultValue(context, field):
+ assert field.default
+ if isinstance(field.kind, mojom.Struct):
+ assert field.default == "default"
+ return "new %s()" % GetJavaType(context, field.kind)
+ return "(%s) %s" % (GetJavaType(context, field.kind),
+ ExpressionToText(context, field.default))
+
+@contextfilter
+def ExpressionToText(context, token):
def _TranslateNamedValue(named_value):
entity_name = GetNameForElement(named_value)
if named_value.parent_kind:
- return GetJavaType(named_value.parent_kind) + '.' + entity_name
+ return GetJavaType(context, named_value.parent_kind) + '.' + entity_name
# Handle the case where named_value is a module level constant:
if not isinstance(named_value, mojom.EnumValue):
entity_name = (GetConstantsMainEntityName(named_value.module) + '.' +
entity_name)
+ if GetPackage(named_value.module) == GetPackage(context.resolve('module')):
+ return entity_name
return GetPackage(named_value.module) + '.' + entity_name
if isinstance(token, mojom.NamedValue):
@@ -138,7 +163,9 @@ def GetConstantsMainEntityName(module):
class Generator(generator.Generator):
java_filters = {
+ "default_value": DefaultValue,
"expression_to_text": ExpressionToText,
+ "is_handle": IsHandle,
"java_type": GetJavaType,
"name": GetNameForElement,
}
@@ -149,15 +176,19 @@ class Generator(generator.Generator):
"package": GetPackage(self.module),
}
- @UseJinja("java_templates/enum.java.tmpl", filters=java_filters,
- lstrip_blocks=True, trim_blocks=True)
+ @UseJinja("java_templates/enum.java.tmpl", filters=java_filters)
def GenerateEnumSource(self, enum):
exports = self.GetJinjaExports()
exports.update({"enum": enum})
return exports
- @UseJinja("java_templates/constants.java.tmpl", filters=java_filters,
- lstrip_blocks=True, trim_blocks=True)
+ @UseJinja("java_templates/struct.java.tmpl", filters=java_filters)
+ def GenerateStructSource(self, struct):
+ exports = self.GetJinjaExports()
+ exports.update({"struct": struct})
+ return exports
+
+ @UseJinja("java_templates/constants.java.tmpl", filters=java_filters)
def GenerateConstantsSource(self, module):
exports = self.GetJinjaExports()
exports.update({"main_entity": GetConstantsMainEntityName(module),
@@ -182,6 +213,21 @@ class Generator(generator.Generator):
self.Write(self.GenerateEnumSource(enum),
"%s.java" % GetNameForElement(enum))
+ for struct in self.module.structs:
+ self.Write(self.GenerateStructSource(struct),
+ "%s.java" % GetNameForElement(struct))
+
if self.module.constants:
self.Write(self.GenerateConstantsSource(self.module),
"%s.java" % GetConstantsMainEntityName(self.module))
+
+ def GetJinjaParameters(self):
+ return {
+ 'lstrip_blocks': True,
+ 'trim_blocks': True,
+ }
+
+ def GetGlobals(self):
+ return {
+ 'module': self.module,
+ }

Powered by Google App Engine
This is Rietveld 408576698