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

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

Issue 514293002: Mojo: validate nullability in Java bindings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add example Foo.java. Created 6 years, 3 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 os 9 import os
10 import re 10 import re
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 return ConstantStyle(element.name) 129 return ConstantStyle(element.name)
130 raise Exception("Unexpected element: " % element) 130 raise Exception("Unexpected element: " % element)
131 131
132 def GetInterfaceResponseName(method): 132 def GetInterfaceResponseName(method):
133 return UpperCamelCase(method.name + 'Response') 133 return UpperCamelCase(method.name + 'Response')
134 134
135 def ParseStringAttribute(attribute): 135 def ParseStringAttribute(attribute):
136 assert isinstance(attribute, basestring) 136 assert isinstance(attribute, basestring)
137 return attribute 137 return attribute
138 138
139 def GetJavaTrueFalse(value):
140 return "true" if value else "false"
141
142 def GetArrayNullabilityFlags(kind):
143 """Returns nullability flags for an array type, see Decoder.java.
144
145 As we have dedicated decoding functions for arrays, we have to pass
146 nullability information about both the array itself, as well as the array
147 element type there.
148 """
149 assert mojom.IsAnyArrayKind(kind)
150 ARRAY_NULLABLE = "org.chromium.mojo.bindings.Decoder.ARRAY_NULLABLE"
151 ELEMENT_NULLABLE = "org.chromium.mojo.bindings.Decoder.ELEMENT_NULLABLE"
152 NOTHING_NULLABLE = "org.chromium.mojo.bindings.Decoder.NOTHING_NULLABLE"
153
154 flags_to_set = []
155 if mojom.IsNullableKind(kind):
156 flags_to_set.append(ARRAY_NULLABLE)
157 if mojom.IsNullableKind(kind.kind):
158 flags_to_set.append(ELEMENT_NULLABLE)
159
160 if not flags_to_set:
161 flags_to_set = [NOTHING_NULLABLE]
162 return " | ".join(flags_to_set)
163
164
139 @contextfilter 165 @contextfilter
140 def DecodeMethod(context, kind, offset, bit): 166 def DecodeMethod(context, kind, offset, bit):
141 def _DecodeMethodName(kind): 167 def _DecodeMethodName(kind):
142 if mojom.IsAnyArrayKind(kind): 168 if mojom.IsAnyArrayKind(kind):
143 return _DecodeMethodName(kind.kind) + 's' 169 return _DecodeMethodName(kind.kind) + 's'
144 if mojom.IsEnumKind(kind): 170 if mojom.IsEnumKind(kind):
145 return _DecodeMethodName(mojom.INT32) 171 return _DecodeMethodName(mojom.INT32)
146 if mojom.IsInterfaceRequestKind(kind): 172 if mojom.IsInterfaceRequestKind(kind):
147 return "readInterfaceRequest" 173 return "readInterfaceRequest"
148 if mojom.IsInterfaceKind(kind): 174 if mojom.IsInterfaceKind(kind):
149 return "readServiceInterface" 175 return "readServiceInterface"
150 return _spec_to_decode_method[kind.spec] 176 return _spec_to_decode_method[kind.spec]
151 methodName = _DecodeMethodName(kind) 177 methodName = _DecodeMethodName(kind)
152 params = [ str(offset) ] 178 params = [ str(offset) ]
153 if (kind == mojom.BOOL): 179 if (kind == mojom.BOOL):
154 params.append(str(bit)) 180 params.append(str(bit))
181 if mojom.IsReferenceKind(kind):
182 if mojom.IsAnyArrayKind(kind):
183 params.append(GetArrayNullabilityFlags(kind))
184 else:
185 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind)))
155 if mojom.IsInterfaceKind(kind): 186 if mojom.IsInterfaceKind(kind):
156 params.append('%s.MANAGER' % GetJavaType(context, kind)) 187 params.append('%s.MANAGER' % GetJavaType(context, kind))
157 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): 188 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind):
158 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) 189 params.append('%s.MANAGER' % GetJavaType(context, kind.kind))
159 return '%s(%s)' % (methodName, ', '.join(params)) 190 return '%s(%s)' % (methodName, ', '.join(params))
160 191
161 @contextfilter 192 @contextfilter
162 def EncodeMethod(context, kind, variable, offset, bit): 193 def EncodeMethod(context, kind, variable, offset, bit):
163 params = [ variable, str(offset) ] 194 params = [ variable, str(offset) ]
164 if (kind == mojom.BOOL): 195 if (kind == mojom.BOOL):
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 java_filters = { 348 java_filters = {
318 "interface_response_name": GetInterfaceResponseName, 349 "interface_response_name": GetInterfaceResponseName,
319 "constant_value": ConstantValue, 350 "constant_value": ConstantValue,
320 "default_value": DefaultValue, 351 "default_value": DefaultValue,
321 "decode_method": DecodeMethod, 352 "decode_method": DecodeMethod,
322 "expression_to_text": ExpressionToText, 353 "expression_to_text": ExpressionToText,
323 "encode_method": EncodeMethod, 354 "encode_method": EncodeMethod,
324 "has_method_with_response": HasMethodWithResponse, 355 "has_method_with_response": HasMethodWithResponse,
325 "has_method_without_response": HasMethodWithoutResponse, 356 "has_method_without_response": HasMethodWithoutResponse,
326 "is_handle": mojom.IsNonInterfaceHandleKind, 357 "is_handle": mojom.IsNonInterfaceHandleKind,
358 "is_nullable_kind": mojom.IsNullableKind,
327 "is_pointer_array_kind": IsPointerArrayKind, 359 "is_pointer_array_kind": IsPointerArrayKind,
328 "is_struct_kind": mojom.IsStructKind, 360 "is_struct_kind": mojom.IsStructKind,
329 "java_type": GetJavaType, 361 "java_type": GetJavaType,
362 "java_true_false": GetJavaTrueFalse,
330 "method_ordinal_name": GetMethodOrdinalName, 363 "method_ordinal_name": GetMethodOrdinalName,
331 "name": GetNameForElement, 364 "name": GetNameForElement,
332 "new_array": NewArray, 365 "new_array": NewArray,
333 "response_struct_from_method": GetResponseStructFromMethod, 366 "response_struct_from_method": GetResponseStructFromMethod,
334 "struct_from_method": GetStructFromMethod, 367 "struct_from_method": GetStructFromMethod,
335 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, 368 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
336 } 369 }
337 370
338 def GetJinjaExports(self): 371 def GetJinjaExports(self):
339 return { 372 return {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 def GetJinjaParameters(self): 445 def GetJinjaParameters(self):
413 return { 446 return {
414 'lstrip_blocks': True, 447 'lstrip_blocks': True,
415 'trim_blocks': True, 448 'trim_blocks': True,
416 } 449 }
417 450
418 def GetGlobals(self): 451 def GetGlobals(self):
419 return { 452 return {
420 'module': self.module, 453 'module': self.module,
421 } 454 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698