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

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

Issue 317273006: Add serialization/deserialization of structs for mojo java bindings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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
« no previous file with comments | « mojo/public/tools/bindings/generators/java_templates/struct_definition.tmpl ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 cc1569f03cc06d710c9d79197f051db3259e4ad8..7364afc56ad725ff594c1180e68fc377d69071ce 100644
--- a/mojo/public/tools/bindings/generators/mojom_java_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_java_generator.py
@@ -17,6 +17,8 @@ from mojom.generate.template_expander import UseJinja
GENERATOR_PREFIX = 'java'
+_HEADER_SIZE = 8
+
_spec_to_java_type = {
'b': 'boolean',
'd': 'double',
@@ -37,6 +39,26 @@ _spec_to_java_type = {
'u8': 'byte',
}
+_spec_to_decode_method = {
+ 'b': 'readBoolean',
+ 'd': 'readDouble',
+ 'f': 'readFloat',
+ 'h:d:c': 'readConsumerHandle',
+ 'h:d:p': 'readProducerHandle',
+ 'h:m': 'readMessagePipeHandle',
+ 'h': 'readUntypedHandle',
+ 'h:s': 'readSharedBufferHandle',
+ 'i16': 'readShort',
+ 'i32': 'readInt',
+ 'i64': 'readLong',
+ 'i8': 'readByte',
+ 's': 'readString',
+ 'u16': 'readShort',
+ 'u32': 'readInt',
+ 'u64': 'readLong',
+ 'u8': 'readByte',
+}
+
_java_primitive_to_boxed_type = {
'boolean': 'Boolean',
'byte': 'Byte',
@@ -99,6 +121,42 @@ def ParseStringAttribute(attribute):
assert isinstance(attribute, basestring)
return attribute
+def IsArray(kind):
+ return isinstance(kind, (mojom.Array, mojom.FixedArray))
+
+@contextfilter
+def DecodeMethod(context, kind, offset, bit):
+ def _DecodeMethodName(kind):
+ if IsArray(kind):
+ return _DecodeMethodName(kind.kind) + 's'
+ if isinstance(kind, mojom.Enum):
+ return _DecodeMethodName(mojom.INT32)
+ if isinstance(kind, mojom.InterfaceRequest):
+ return "readInterfaceRequest"
+ if isinstance(kind, mojom.Interface):
+ return "readServiceInterface"
+ return _spec_to_decode_method[kind.spec]
+ methodName = _DecodeMethodName(kind)
+ additionalParams = ''
+ if (kind == mojom.BOOL):
+ additionalParams = ', %d' % bit
+ if isinstance(kind, mojom.Interface):
+ additionalParams = ', %s.BUILDER' % GetJavaType(context, kind)
+ if IsArray(kind) and isinstance(kind.kind, mojom.Interface):
+ additionalParams = ', %s.BUILDER' % GetJavaType(context, kind.kind)
+ return '%s(%s%s)' % (methodName, offset, additionalParams)
+
+@contextfilter
+def EncodeMethod(context, kind, variable, offset, bit):
+ additionalParams = ''
+ if (kind == mojom.BOOL):
+ additionalParams = ', %d' % bit
+ if isinstance(kind, mojom.Interface):
+ additionalParams = ', %s.BUILDER' % GetJavaType(context, kind)
+ if IsArray(kind) and isinstance(kind.kind, mojom.Interface):
+ additionalParams = ', %s.BUILDER' % GetJavaType(context, kind.kind)
+ return 'encode(%s, %s%s)' % (variable, offset, additionalParams)
+
def GetPackage(module):
if 'JavaPackage' in module.attributes:
return ParseStringAttribute(module.attributes['JavaPackage'])
@@ -135,7 +193,7 @@ def GetJavaType(context, kind, boxed=False):
if isinstance(kind, mojom.InterfaceRequest):
return ("org.chromium.mojo.bindings.InterfaceRequest<%s>" %
GetNameForKind(context, kind.kind))
- if isinstance(kind, (mojom.Array, mojom.FixedArray)):
+ if IsArray(kind):
return "%s[]" % GetJavaType(context, kind.kind)
if isinstance(kind, mojom.Enum):
return "int"
@@ -154,6 +212,12 @@ def DefaultValue(context, field):
ExpressionToText(context, field.default))
@contextfilter
+def NewArray(context, kind, size):
+ if IsArray(kind.kind):
+ return NewArray(context, kind.kind, size) + '[]'
+ return 'new %s[%s]' % (GetJavaType(context, kind.kind), size)
+
+@contextfilter
def ExpressionToText(context, token):
def _TranslateNamedValue(named_value):
entity_name = GetNameForElement(named_value)
@@ -174,6 +238,12 @@ def ExpressionToText(context, token):
return token + 'L'
return token
+def IsPointerArrayKind(kind):
+ if not IsArray(kind):
+ return False
+ sub_kind = kind.kind
+ return generator.IsObjectKind(sub_kind)
+
def GetConstantsMainEntityName(module):
if 'JavaConstantsClassName' in module.attributes:
return ParseStringAttribute(module.attributes['JavaConstantsClassName'])
@@ -187,10 +257,16 @@ class Generator(generator.Generator):
java_filters = {
"interface_response_name": GetInterfaceResponseName,
"default_value": DefaultValue,
+ "decode_method": DecodeMethod,
"expression_to_text": ExpressionToText,
+ "encode_method": EncodeMethod,
"is_handle": IsHandle,
+ "is_pointer_array_kind": IsPointerArrayKind,
+ "is_struct_kind": lambda kind: isinstance(kind, mojom.Struct),
"java_type": GetJavaType,
"name": GetNameForElement,
+ "new_array": NewArray,
+ "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
}
def GetJinjaExports(self):
« no previous file with comments | « mojo/public/tools/bindings/generators/java_templates/struct_definition.tmpl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698