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

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

Issue 607933002: mojo: Fix java generator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix findbugs and emptu package. 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
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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 101
102 def UpperCamelCase(name): 102 def UpperCamelCase(name):
103 return ''.join([x.capitalize() for x in NameToComponent(name)]) 103 return ''.join([x.capitalize() for x in NameToComponent(name)])
104 104
105 def CamelCase(name): 105 def CamelCase(name):
106 uccc = UpperCamelCase(name) 106 uccc = UpperCamelCase(name)
107 return uccc[0].lower() + uccc[1:] 107 return uccc[0].lower() + uccc[1:]
108 108
109 def ConstantStyle(name): 109 def ConstantStyle(name):
110 components = NameToComponent(name) 110 components = NameToComponent(name)
111 if components[0] == 'k': 111 if components[0] == 'k' and len(components) > 1:
112 components = components[1:] 112 components = components[1:]
113 # variable cannot starts with a digit.
114 if components[0][0].isdigit():
115 components[0] = '_' + components[0]
113 return '_'.join([x.upper() for x in components]) 116 return '_'.join([x.upper() for x in components])
114 117
115 def GetNameForElement(element): 118 def GetNameForElement(element):
116 if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element) or 119 if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element) or
117 mojom.IsStructKind(element)): 120 mojom.IsStructKind(element)):
118 return UpperCamelCase(element.name) 121 return UpperCamelCase(element.name)
119 if mojom.IsInterfaceRequestKind(element): 122 if mojom.IsInterfaceRequestKind(element):
120 return GetNameForElement(element.kind) 123 return GetNameForElement(element.kind)
121 if isinstance(element, (mojom.Method, 124 if isinstance(element, (mojom.Method,
122 mojom.Parameter, 125 mojom.Parameter,
123 mojom.Field)): 126 mojom.Field)):
124 return CamelCase(element.name) 127 return CamelCase(element.name)
125 if isinstance(element, mojom.EnumValue): 128 if isinstance(element, mojom.EnumValue):
126 return (GetNameForElement(element.enum) + '.' + 129 return (GetNameForElement(element.enum) + '.' +
127 ConstantStyle(element.name)) 130 ConstantStyle(element.name))
128 if isinstance(element, (mojom.NamedValue, 131 if isinstance(element, (mojom.NamedValue,
129 mojom.Constant)): 132 mojom.Constant,
133 mojom.EnumField)):
130 return ConstantStyle(element.name) 134 return ConstantStyle(element.name)
131 raise Exception('Unexpected element: ' % element) 135 raise Exception('Unexpected element: %s' % element)
132 136
133 def GetInterfaceResponseName(method): 137 def GetInterfaceResponseName(method):
134 return UpperCamelCase(method.name + 'Response') 138 return UpperCamelCase(method.name + 'Response')
135 139
136 def ParseStringAttribute(attribute): 140 def ParseStringAttribute(attribute):
137 assert isinstance(attribute, basestring) 141 assert isinstance(attribute, basestring)
138 return attribute 142 return attribute
139 143
140 def GetJavaTrueFalse(value): 144 def GetJavaTrueFalse(value):
141 return 'true' if value else 'false' 145 return 'true' if value else 'false'
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 @contextfilter 212 @contextfilter
209 def EncodeMethod(context, kind, variable, offset, bit): 213 def EncodeMethod(context, kind, variable, offset, bit):
210 params = AppendEncodeDecodeParams( 214 params = AppendEncodeDecodeParams(
211 [ variable, str(offset) ], context, kind, bit) 215 [ variable, str(offset) ], context, kind, bit)
212 return 'encode(%s)' % ', '.join(params) 216 return 'encode(%s)' % ', '.join(params)
213 217
214 def GetPackage(module): 218 def GetPackage(module):
215 if 'JavaPackage' in module.attributes: 219 if 'JavaPackage' in module.attributes:
216 return ParseStringAttribute(module.attributes['JavaPackage']) 220 return ParseStringAttribute(module.attributes['JavaPackage'])
217 # Default package. 221 # Default package.
218 return 'org.chromium.mojom.' + module.namespace 222 if module.namespace:
223 return 'org.chromium.mojom.' + module.namespace
224 return 'org.chromium.mojom'
219 225
220 def GetNameForKind(context, kind): 226 def GetNameForKind(context, kind):
221 def _GetNameHierachy(kind): 227 def _GetNameHierachy(kind):
222 hierachy = [] 228 hierachy = []
223 if kind.parent_kind: 229 if kind.parent_kind:
224 hierachy = _GetNameHierachy(kind.parent_kind) 230 hierachy = _GetNameHierachy(kind.parent_kind)
225 hierachy.append(GetNameForElement(kind)) 231 hierachy.append(GetNameForElement(kind))
226 return hierachy 232 return hierachy
227 233
228 module = context.resolve('module') 234 module = context.resolve('module')
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 # This constructs the name of the embedding classes for module level constants 343 # This constructs the name of the embedding classes for module level constants
338 # by extracting the mojom's filename and prepending it to Constants. 344 # by extracting the mojom's filename and prepending it to Constants.
339 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + 345 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) +
340 'Constants') 346 'Constants')
341 347
342 def GetMethodOrdinalName(method): 348 def GetMethodOrdinalName(method):
343 return ConstantStyle(method.name) + '_ORDINAL' 349 return ConstantStyle(method.name) + '_ORDINAL'
344 350
345 def HasMethodWithResponse(interface): 351 def HasMethodWithResponse(interface):
346 for method in interface.methods: 352 for method in interface.methods:
347 if method.response_parameters: 353 if method.response_parameters is not None:
348 return True 354 return True
349 return False 355 return False
350 356
351 def HasMethodWithoutResponse(interface): 357 def HasMethodWithoutResponse(interface):
352 for method in interface.methods: 358 for method in interface.methods:
353 if not method.response_parameters: 359 if method.response_parameters is None:
354 return True 360 return True
355 return False 361 return False
356 362
357 @contextlib.contextmanager 363 @contextlib.contextmanager
358 def TempDir(): 364 def TempDir():
359 dirname = tempfile.mkdtemp() 365 dirname = tempfile.mkdtemp()
360 try: 366 try:
361 yield dirname 367 yield dirname
362 finally: 368 finally:
363 shutil.rmtree(dirname) 369 shutil.rmtree(dirname)
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 return { 495 return {
490 'lstrip_blocks': True, 496 'lstrip_blocks': True,
491 'trim_blocks': True, 497 'trim_blocks': True,
492 } 498 }
493 499
494 def GetGlobals(self): 500 def GetGlobals(self):
495 return { 501 return {
496 'namespace': self.module.namespace, 502 'namespace': self.module.namespace,
497 'module': self.module, 503 'module': self.module,
498 } 504 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698