| 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 hierachy.append(GetNameForElement(kind)) | 227 hierachy.append(GetNameForElement(kind)) |
| 228 return hierachy | 228 return hierachy |
| 229 | 229 |
| 230 module = context.resolve('module') | 230 module = context.resolve('module') |
| 231 elements = [] | 231 elements = [] |
| 232 if GetPackage(module) != GetPackage(kind.module): | 232 if GetPackage(module) != GetPackage(kind.module): |
| 233 elements += [GetPackage(kind.module)] | 233 elements += [GetPackage(kind.module)] |
| 234 elements += _GetNameHierachy(kind) | 234 elements += _GetNameHierachy(kind) |
| 235 return '.'.join(elements) | 235 return '.'.join(elements) |
| 236 | 236 |
| 237 def GetBoxedJavaType(context, kind): | 237 def GetBoxedJavaType(context, kind, with_generics=True): |
| 238 unboxed_type = GetJavaType(context, kind, False) | 238 unboxed_type = GetJavaType(context, kind, False, with_generics) |
| 239 if unboxed_type in _java_primitive_to_boxed_type: | 239 if unboxed_type in _java_primitive_to_boxed_type: |
| 240 return _java_primitive_to_boxed_type[unboxed_type] | 240 return _java_primitive_to_boxed_type[unboxed_type] |
| 241 return unboxed_type | 241 return unboxed_type |
| 242 | 242 |
| 243 @contextfilter | 243 @contextfilter |
| 244 def GetJavaType(context, kind, boxed=False): | 244 def GetJavaType(context, kind, boxed=False, with_generics=True): |
| 245 if boxed: | 245 if boxed: |
| 246 return GetBoxedJavaType(context, kind) | 246 return GetBoxedJavaType(context, kind) |
| 247 if mojom.IsStructKind(kind) or mojom.IsInterfaceKind(kind): | 247 if mojom.IsStructKind(kind) or mojom.IsInterfaceKind(kind): |
| 248 return GetNameForKind(context, kind) | 248 return GetNameForKind(context, kind) |
| 249 if mojom.IsInterfaceRequestKind(kind): | 249 if mojom.IsInterfaceRequestKind(kind): |
| 250 return ('org.chromium.mojo.bindings.InterfaceRequest<%s>' % | 250 return ('org.chromium.mojo.bindings.InterfaceRequest<%s>' % |
| 251 GetNameForKind(context, kind.kind)) | 251 GetNameForKind(context, kind.kind)) |
| 252 if mojom.IsMapKind(kind): | 252 if mojom.IsMapKind(kind): |
| 253 return 'java.util.Map<%s, %s>' % ( | 253 if with_generics: |
| 254 GetBoxedJavaType(context, kind.key_kind), | 254 return 'java.util.Map<%s, %s>' % ( |
| 255 GetBoxedJavaType(context, kind.value_kind)) | 255 GetBoxedJavaType(context, kind.key_kind), |
| 256 GetBoxedJavaType(context, kind.value_kind)) |
| 257 else: |
| 258 return 'java.util.Map' |
| 256 if mojom.IsArrayKind(kind): | 259 if mojom.IsArrayKind(kind): |
| 257 return '%s[]' % GetJavaType(context, kind.kind) | 260 return '%s[]' % GetJavaType(context, kind.kind, boxed, with_generics) |
| 258 if mojom.IsEnumKind(kind): | 261 if mojom.IsEnumKind(kind): |
| 259 return 'int' | 262 return 'int' |
| 260 return _spec_to_java_type[kind.spec] | 263 return _spec_to_java_type[kind.spec] |
| 261 | 264 |
| 262 @contextfilter | 265 @contextfilter |
| 263 def DefaultValue(context, field): | 266 def DefaultValue(context, field): |
| 264 assert field.default | 267 assert field.default |
| 265 if isinstance(field.kind, mojom.Struct): | 268 if isinstance(field.kind, mojom.Struct): |
| 266 assert field.default == 'default' | 269 assert field.default == 'default' |
| 267 return 'new %s()' % GetJavaType(context, field.kind) | 270 return 'new %s()' % GetJavaType(context, field.kind) |
| 268 return '(%s) %s' % ( | 271 return '(%s) %s' % ( |
| 269 GetJavaType(context, field.kind), | 272 GetJavaType(context, field.kind), |
| 270 ExpressionToText(context, field.default, kind_spec=field.kind.spec)) | 273 ExpressionToText(context, field.default, kind_spec=field.kind.spec)) |
| 271 | 274 |
| 272 @contextfilter | 275 @contextfilter |
| 273 def ConstantValue(context, constant): | 276 def ConstantValue(context, constant): |
| 274 return '(%s) %s' % ( | 277 return '(%s) %s' % ( |
| 275 GetJavaType(context, constant.kind), | 278 GetJavaType(context, constant.kind), |
| 276 ExpressionToText(context, constant.value, kind_spec=constant.kind.spec)) | 279 ExpressionToText(context, constant.value, kind_spec=constant.kind.spec)) |
| 277 | 280 |
| 278 @contextfilter | 281 @contextfilter |
| 279 def NewArray(context, kind, size): | 282 def NewArray(context, kind, size): |
| 280 if mojom.IsArrayKind(kind.kind): | 283 if mojom.IsArrayKind(kind.kind): |
| 281 return NewArray(context, kind.kind, size) + '[]' | 284 return NewArray(context, kind.kind, size) + '[]' |
| 282 return 'new %s[%s]' % (GetJavaType(context, kind.kind), size) | 285 return 'new %s[%s]' % ( |
| 286 GetJavaType(context, kind.kind, boxed=False, with_generics=False), size) |
| 283 | 287 |
| 284 @contextfilter | 288 @contextfilter |
| 285 def ExpressionToText(context, token, kind_spec=''): | 289 def ExpressionToText(context, token, kind_spec=''): |
| 286 def _TranslateNamedValue(named_value): | 290 def _TranslateNamedValue(named_value): |
| 287 entity_name = GetNameForElement(named_value) | 291 entity_name = GetNameForElement(named_value) |
| 288 if named_value.parent_kind: | 292 if named_value.parent_kind: |
| 289 return GetJavaType(context, named_value.parent_kind) + '.' + entity_name | 293 return GetJavaType(context, named_value.parent_kind) + '.' + entity_name |
| 290 # Handle the case where named_value is a module level constant: | 294 # Handle the case where named_value is a module level constant: |
| 291 if not isinstance(named_value, mojom.EnumValue): | 295 if not isinstance(named_value, mojom.EnumValue): |
| 292 entity_name = (GetConstantsMainEntityName(named_value.module) + '.' + | 296 entity_name = (GetConstantsMainEntityName(named_value.module) + '.' + |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 return { | 522 return { |
| 519 'lstrip_blocks': True, | 523 'lstrip_blocks': True, |
| 520 'trim_blocks': True, | 524 'trim_blocks': True, |
| 521 } | 525 } |
| 522 | 526 |
| 523 def GetGlobals(self): | 527 def GetGlobals(self): |
| 524 return { | 528 return { |
| 525 'namespace': self.module.namespace, | 529 'namespace': self.module.namespace, |
| 526 'module': self.module, | 530 'module': self.module, |
| 527 } | 531 } |
| OLD | NEW |