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

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

Issue 648683003: mojo: Add maps to java bindings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Follow review Created 6 years, 2 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
« no previous file with comments | « mojo/public/tools/bindings/generators/java_templates/struct_definition.tmpl ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 """ Appends standard parameters shared between encode and decode calls. """ 174 """ Appends standard parameters shared between encode and decode calls. """
175 params = list(initial_params) 175 params = list(initial_params)
176 if (kind == mojom.BOOL): 176 if (kind == mojom.BOOL):
177 params.append(str(bit)) 177 params.append(str(bit))
178 if mojom.IsReferenceKind(kind): 178 if mojom.IsReferenceKind(kind):
179 if mojom.IsAnyArrayKind(kind): 179 if mojom.IsAnyArrayKind(kind):
180 params.append(GetArrayNullabilityFlags(kind)) 180 params.append(GetArrayNullabilityFlags(kind))
181 else: 181 else:
182 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) 182 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind)))
183 if mojom.IsAnyArrayKind(kind): 183 if mojom.IsAnyArrayKind(kind):
184 if mojom.IsFixedArrayKind(kind): 184 params.append(GetArrayExpectedLength(kind))
185 params.append(str(kind.length))
186 else:
187 params.append(
188 'org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH');
189 if mojom.IsInterfaceKind(kind): 185 if mojom.IsInterfaceKind(kind):
190 params.append('%s.MANAGER' % GetJavaType(context, kind)) 186 params.append('%s.MANAGER' % GetJavaType(context, kind))
191 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): 187 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind):
192 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) 188 params.append('%s.MANAGER' % GetJavaType(context, kind.kind))
193 return params 189 return params
194 190
195 191
196 @contextfilter 192 @contextfilter
197 def DecodeMethod(context, kind, offset, bit): 193 def DecodeMethod(context, kind, offset, bit):
198 def _DecodeMethodName(kind): 194 def _DecodeMethodName(kind):
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 242
247 @contextfilter 243 @contextfilter
248 def GetJavaType(context, kind, boxed=False): 244 def GetJavaType(context, kind, boxed=False):
249 if boxed: 245 if boxed:
250 return GetBoxedJavaType(context, kind) 246 return GetBoxedJavaType(context, kind)
251 if mojom.IsStructKind(kind) or mojom.IsInterfaceKind(kind): 247 if mojom.IsStructKind(kind) or mojom.IsInterfaceKind(kind):
252 return GetNameForKind(context, kind) 248 return GetNameForKind(context, kind)
253 if mojom.IsInterfaceRequestKind(kind): 249 if mojom.IsInterfaceRequestKind(kind):
254 return ('org.chromium.mojo.bindings.InterfaceRequest<%s>' % 250 return ('org.chromium.mojo.bindings.InterfaceRequest<%s>' %
255 GetNameForKind(context, kind.kind)) 251 GetNameForKind(context, kind.kind))
252 if mojom.IsMapKind(kind):
253 return 'java.util.Map<%s, %s>' % (
254 GetBoxedJavaType(context, kind.key_kind),
255 GetBoxedJavaType(context, kind.value_kind))
256 if mojom.IsAnyArrayKind(kind): 256 if mojom.IsAnyArrayKind(kind):
257 return '%s[]' % GetJavaType(context, kind.kind) 257 return '%s[]' % GetJavaType(context, kind.kind)
258 if mojom.IsEnumKind(kind): 258 if mojom.IsEnumKind(kind):
259 return 'int' 259 return 'int'
260 return _spec_to_java_type[kind.spec] 260 return _spec_to_java_type[kind.spec]
261 261
262 @contextfilter 262 @contextfilter
263 def DefaultValue(context, field): 263 def DefaultValue(context, field):
264 assert field.default 264 assert field.default
265 if isinstance(field.kind, mojom.Struct): 265 if isinstance(field.kind, mojom.Struct):
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 if token.value == 'double.NAN': 316 if token.value == 'double.NAN':
317 return 'java.lang.Double.NaN' 317 return 'java.lang.Double.NaN'
318 if token.value == 'float.INFINITY': 318 if token.value == 'float.INFINITY':
319 return 'java.lang.Float.POSITIVE_INFINITY' 319 return 'java.lang.Float.POSITIVE_INFINITY'
320 if token.value == 'float.NEGATIVE_INFINITY': 320 if token.value == 'float.NEGATIVE_INFINITY':
321 return 'java.lang.Float.NEGATIVE_INFINITY' 321 return 'java.lang.Float.NEGATIVE_INFINITY'
322 if token.value == 'float.NAN': 322 if token.value == 'float.NAN':
323 return 'java.lang.Float.NaN' 323 return 'java.lang.Float.NaN'
324 return token 324 return token
325 325
326 def GetArrayKind(kind, size = None):
327 if size is None:
328 return mojom.Array(kind)
329 else:
330 array = mojom.FixedArray(0, kind)
331 array.java_map_size = size
332 return array
333
334 def GetArrayExpectedLength(kind):
335 if mojom.IsFixedArrayKind(kind):
336 return getattr(kind, 'java_map_size', str(kind.length))
337 else:
338 return 'org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH'
339
326 def IsPointerArrayKind(kind): 340 def IsPointerArrayKind(kind):
327 if not mojom.IsAnyArrayKind(kind): 341 if not mojom.IsAnyArrayKind(kind):
328 return False 342 return False
329 sub_kind = kind.kind 343 sub_kind = kind.kind
330 return mojom.IsObjectKind(sub_kind) 344 return mojom.IsObjectKind(sub_kind)
331 345
332 def GetResponseStructFromMethod(method): 346 def GetResponseStructFromMethod(method):
333 return generator.GetDataHeader( 347 return generator.GetDataHeader(
334 False, generator.GetResponseStructFromMethod(method)) 348 False, generator.GetResponseStructFromMethod(method))
335 349
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 with zipfile.ZipFile(zip_filename, 'w') as zip_file: 386 with zipfile.ZipFile(zip_filename, 'w') as zip_file:
373 for dirname, _, files in os.walk(root): 387 for dirname, _, files in os.walk(root):
374 for filename in files: 388 for filename in files:
375 path = os.path.join(dirname, filename) 389 path = os.path.join(dirname, filename)
376 path_in_archive = os.path.relpath(path, root) 390 path_in_archive = os.path.relpath(path, root)
377 zip_file.write(path, path_in_archive) 391 zip_file.write(path, path_in_archive)
378 392
379 class Generator(generator.Generator): 393 class Generator(generator.Generator):
380 394
381 java_filters = { 395 java_filters = {
396 'array': GetArrayKind,
397 'array_expected_length': GetArrayExpectedLength,
382 'interface_response_name': GetInterfaceResponseName, 398 'interface_response_name': GetInterfaceResponseName,
383 'constant_value': ConstantValue, 399 'constant_value': ConstantValue,
384 'default_value': DefaultValue, 400 'default_value': DefaultValue,
385 'decode_method': DecodeMethod, 401 'decode_method': DecodeMethod,
386 'expression_to_text': ExpressionToText, 402 'expression_to_text': ExpressionToText,
387 'encode_method': EncodeMethod, 403 'encode_method': EncodeMethod,
388 'has_method_with_response': HasMethodWithResponse, 404 'has_method_with_response': HasMethodWithResponse,
389 'has_method_without_response': HasMethodWithoutResponse, 405 'has_method_without_response': HasMethodWithoutResponse,
390 'is_fixed_array_kind': mojom.IsFixedArrayKind, 406 'is_fixed_array_kind': mojom.IsFixedArrayKind,
391 'is_handle': mojom.IsNonInterfaceHandleKind, 407 'is_handle': mojom.IsNonInterfaceHandleKind,
408 'is_map_kind': mojom.IsMapKind,
392 'is_nullable_kind': mojom.IsNullableKind, 409 'is_nullable_kind': mojom.IsNullableKind,
393 'is_pointer_array_kind': IsPointerArrayKind, 410 'is_pointer_array_kind': IsPointerArrayKind,
394 'is_struct_kind': mojom.IsStructKind, 411 'is_struct_kind': mojom.IsStructKind,
395 'java_type': GetJavaType, 412 'java_type': GetJavaType,
396 'java_true_false': GetJavaTrueFalse, 413 'java_true_false': GetJavaTrueFalse,
397 'method_ordinal_name': GetMethodOrdinalName, 414 'method_ordinal_name': GetMethodOrdinalName,
398 'name': GetNameForElement, 415 'name': GetNameForElement,
399 'new_array': NewArray, 416 'new_array': NewArray,
400 'response_struct_from_method': GetResponseStructFromMethod, 417 'response_struct_from_method': GetResponseStructFromMethod,
401 'struct_from_method': GetStructFromMethod, 418 'struct_from_method': GetStructFromMethod,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 return { 512 return {
496 'lstrip_blocks': True, 513 'lstrip_blocks': True,
497 'trim_blocks': True, 514 'trim_blocks': True,
498 } 515 }
499 516
500 def GetGlobals(self): 517 def GetGlobals(self):
501 return { 518 return {
502 'namespace': self.module.namespace, 519 'namespace': self.module.namespace,
503 'module': self.module, 520 'module': self.module,
504 } 521 }
OLDNEW
« 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