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 os | 8 import os |
9 import re | 9 import re |
10 | 10 |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 if re.match('^[0-9]+$', token): | 237 if re.match('^[0-9]+$', token): |
238 return token + 'L' | 238 return token + 'L' |
239 return token | 239 return token |
240 | 240 |
241 def IsPointerArrayKind(kind): | 241 def IsPointerArrayKind(kind): |
242 if not IsArray(kind): | 242 if not IsArray(kind): |
243 return False | 243 return False |
244 sub_kind = kind.kind | 244 sub_kind = kind.kind |
245 return generator.IsObjectKind(sub_kind) | 245 return generator.IsObjectKind(sub_kind) |
246 | 246 |
| 247 def GetResponseStructFromMethod(method): |
| 248 return generator.GetDataHeader( |
| 249 False, generator.GetResponseStructFromMethod(method)) |
| 250 |
| 251 def GetStructFromMethod(method): |
| 252 return generator.GetDataHeader( |
| 253 False, generator.GetStructFromMethod(method)) |
| 254 |
247 def GetConstantsMainEntityName(module): | 255 def GetConstantsMainEntityName(module): |
248 if 'JavaConstantsClassName' in module.attributes: | 256 if 'JavaConstantsClassName' in module.attributes: |
249 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) | 257 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
250 # This constructs the name of the embedding classes for module level constants | 258 # This constructs the name of the embedding classes for module level constants |
251 # by extracting the mojom's filename and prepending it to Constants. | 259 # by extracting the mojom's filename and prepending it to Constants. |
252 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 260 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
253 'Constants') | 261 'Constants') |
254 | 262 |
255 class Generator(generator.Generator): | 263 class Generator(generator.Generator): |
256 | 264 |
257 java_filters = { | 265 java_filters = { |
258 "interface_response_name": GetInterfaceResponseName, | 266 "interface_response_name": GetInterfaceResponseName, |
259 "default_value": DefaultValue, | 267 "default_value": DefaultValue, |
260 "decode_method": DecodeMethod, | 268 "decode_method": DecodeMethod, |
261 "expression_to_text": ExpressionToText, | 269 "expression_to_text": ExpressionToText, |
262 "encode_method": EncodeMethod, | 270 "encode_method": EncodeMethod, |
263 "is_handle": IsHandle, | 271 "is_handle": IsHandle, |
264 "is_pointer_array_kind": IsPointerArrayKind, | 272 "is_pointer_array_kind": IsPointerArrayKind, |
265 "is_struct_kind": lambda kind: isinstance(kind, mojom.Struct), | 273 "is_struct_kind": lambda kind: isinstance(kind, mojom.Struct), |
266 "java_type": GetJavaType, | 274 "java_type": GetJavaType, |
267 "name": GetNameForElement, | 275 "name": GetNameForElement, |
268 "new_array": NewArray, | 276 "new_array": NewArray, |
| 277 "response_struct_from_method": GetResponseStructFromMethod, |
| 278 "struct_from_method": GetStructFromMethod, |
269 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 279 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
270 } | 280 } |
271 | 281 |
272 def GetJinjaExports(self): | 282 def GetJinjaExports(self): |
273 return { | 283 return { |
274 "module": self.module, | 284 "module": self.module, |
275 "package": GetPackage(self.module), | 285 "package": GetPackage(self.module), |
276 } | 286 } |
277 | 287 |
278 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) | 288 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) |
(...skipping 11 matching lines...) Expand all Loading... |
290 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) | 300 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) |
291 def GenerateInterfaceSource(self, interface): | 301 def GenerateInterfaceSource(self, interface): |
292 exports = self.GetJinjaExports() | 302 exports = self.GetJinjaExports() |
293 exports.update({"interface": interface}) | 303 exports.update({"interface": interface}) |
294 if interface.client: | 304 if interface.client: |
295 for client in self.module.interfaces: | 305 for client in self.module.interfaces: |
296 if client.name == interface.client: | 306 if client.name == interface.client: |
297 exports.update({"client": client}) | 307 exports.update({"client": client}) |
298 return exports | 308 return exports |
299 | 309 |
| 310 @UseJinja("java_templates/interface_internal.java.tmpl", filters=java_filters) |
| 311 def GenerateInterfaceInternalSource(self, interface): |
| 312 exports = self.GetJinjaExports() |
| 313 exports.update({"interface": interface}) |
| 314 return exports |
| 315 |
300 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) | 316 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) |
301 def GenerateConstantsSource(self, module): | 317 def GenerateConstantsSource(self, module): |
302 exports = self.GetJinjaExports() | 318 exports = self.GetJinjaExports() |
303 exports.update({"main_entity": GetConstantsMainEntityName(module), | 319 exports.update({"main_entity": GetConstantsMainEntityName(module), |
304 "constants": module.constants}) | 320 "constants": module.constants}) |
305 return exports | 321 return exports |
306 | 322 |
307 def GenerateFiles(self, unparsed_args): | 323 def GenerateFiles(self, unparsed_args): |
308 parser = argparse.ArgumentParser() | 324 parser = argparse.ArgumentParser() |
309 parser.add_argument("--java_output_directory", dest="java_output_directory") | 325 parser.add_argument("--java_output_directory", dest="java_output_directory") |
(...skipping 12 matching lines...) Expand all Loading... |
322 self.Write(self.GenerateEnumSource(enum), | 338 self.Write(self.GenerateEnumSource(enum), |
323 "%s.java" % GetNameForElement(enum)) | 339 "%s.java" % GetNameForElement(enum)) |
324 | 340 |
325 for struct in self.module.structs: | 341 for struct in self.module.structs: |
326 self.Write(self.GenerateStructSource(struct), | 342 self.Write(self.GenerateStructSource(struct), |
327 "%s.java" % GetNameForElement(struct)) | 343 "%s.java" % GetNameForElement(struct)) |
328 | 344 |
329 for interface in self.module.interfaces: | 345 for interface in self.module.interfaces: |
330 self.Write(self.GenerateInterfaceSource(interface), | 346 self.Write(self.GenerateInterfaceSource(interface), |
331 "%s.java" % GetNameForElement(interface)) | 347 "%s.java" % GetNameForElement(interface)) |
| 348 if interface.methods: |
| 349 self.Write(self.GenerateInterfaceInternalSource(interface), |
| 350 "%sInternal.java" % GetNameForElement(interface)) |
332 | 351 |
333 if self.module.constants: | 352 if self.module.constants: |
334 self.Write(self.GenerateConstantsSource(self.module), | 353 self.Write(self.GenerateConstantsSource(self.module), |
335 "%s.java" % GetConstantsMainEntityName(self.module)) | 354 "%s.java" % GetConstantsMainEntityName(self.module)) |
336 | 355 |
337 def GetJinjaParameters(self): | 356 def GetJinjaParameters(self): |
338 return { | 357 return { |
339 'lstrip_blocks': True, | 358 'lstrip_blocks': True, |
340 'trim_blocks': True, | 359 'trim_blocks': True, |
341 } | 360 } |
342 | 361 |
343 def GetGlobals(self): | 362 def GetGlobals(self): |
344 return { | 363 return { |
345 'module': self.module, | 364 'module': self.module, |
346 } | 365 } |
OLD | NEW |