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

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: Fix line length in mojom_java_generator in a way that works. 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 = \
151 "org.chromium.mojo.bindings.BindingsHelper.ARRAY_NULLABLE"
152 ELEMENT_NULLABLE = \
153 "org.chromium.mojo.bindings.BindingsHelper.ELEMENT_NULLABLE"
154 NOTHING_NULLABLE = \
155 "org.chromium.mojo.bindings.BindingsHelper.NOTHING_NULLABLE"
156
157 flags_to_set = []
158 if mojom.IsNullableKind(kind):
159 flags_to_set.append(ARRAY_NULLABLE)
160 if mojom.IsNullableKind(kind.kind):
161 flags_to_set.append(ELEMENT_NULLABLE)
162
163 if not flags_to_set:
164 flags_to_set = [NOTHING_NULLABLE]
165 return " | ".join(flags_to_set)
166
167
139 @contextfilter 168 @contextfilter
140 def DecodeMethod(context, kind, offset, bit): 169 def DecodeMethod(context, kind, offset, bit):
141 def _DecodeMethodName(kind): 170 def _DecodeMethodName(kind):
142 if mojom.IsAnyArrayKind(kind): 171 if mojom.IsAnyArrayKind(kind):
143 return _DecodeMethodName(kind.kind) + 's' 172 return _DecodeMethodName(kind.kind) + 's'
144 if mojom.IsEnumKind(kind): 173 if mojom.IsEnumKind(kind):
145 return _DecodeMethodName(mojom.INT32) 174 return _DecodeMethodName(mojom.INT32)
146 if mojom.IsInterfaceRequestKind(kind): 175 if mojom.IsInterfaceRequestKind(kind):
147 return "readInterfaceRequest" 176 return "readInterfaceRequest"
148 if mojom.IsInterfaceKind(kind): 177 if mojom.IsInterfaceKind(kind):
149 return "readServiceInterface" 178 return "readServiceInterface"
150 return _spec_to_decode_method[kind.spec] 179 return _spec_to_decode_method[kind.spec]
151 methodName = _DecodeMethodName(kind) 180 methodName = _DecodeMethodName(kind)
152 params = [ str(offset) ] 181 params = [ str(offset) ]
153 if (kind == mojom.BOOL): 182 if (kind == mojom.BOOL):
154 params.append(str(bit)) 183 params.append(str(bit))
184 if mojom.IsReferenceKind(kind):
185 if mojom.IsAnyArrayKind(kind):
186 params.append(GetArrayNullabilityFlags(kind))
187 else:
188 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind)))
155 if mojom.IsInterfaceKind(kind): 189 if mojom.IsInterfaceKind(kind):
156 params.append('%s.MANAGER' % GetJavaType(context, kind)) 190 params.append('%s.MANAGER' % GetJavaType(context, kind))
157 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): 191 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind):
158 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) 192 params.append('%s.MANAGER' % GetJavaType(context, kind.kind))
159 return '%s(%s)' % (methodName, ', '.join(params)) 193 return '%s(%s)' % (methodName, ', '.join(params))
160 194
161 @contextfilter 195 @contextfilter
162 def EncodeMethod(context, kind, variable, offset, bit): 196 def EncodeMethod(context, kind, variable, offset, bit):
163 params = [ variable, str(offset) ] 197 params = [ variable, str(offset) ]
164 if (kind == mojom.BOOL): 198 if (kind == mojom.BOOL):
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 java_filters = { 351 java_filters = {
318 "interface_response_name": GetInterfaceResponseName, 352 "interface_response_name": GetInterfaceResponseName,
319 "constant_value": ConstantValue, 353 "constant_value": ConstantValue,
320 "default_value": DefaultValue, 354 "default_value": DefaultValue,
321 "decode_method": DecodeMethod, 355 "decode_method": DecodeMethod,
322 "expression_to_text": ExpressionToText, 356 "expression_to_text": ExpressionToText,
323 "encode_method": EncodeMethod, 357 "encode_method": EncodeMethod,
324 "has_method_with_response": HasMethodWithResponse, 358 "has_method_with_response": HasMethodWithResponse,
325 "has_method_without_response": HasMethodWithoutResponse, 359 "has_method_without_response": HasMethodWithoutResponse,
326 "is_handle": mojom.IsNonInterfaceHandleKind, 360 "is_handle": mojom.IsNonInterfaceHandleKind,
361 "is_nullable_kind": mojom.IsNullableKind,
327 "is_pointer_array_kind": IsPointerArrayKind, 362 "is_pointer_array_kind": IsPointerArrayKind,
328 "is_struct_kind": mojom.IsStructKind, 363 "is_struct_kind": mojom.IsStructKind,
329 "java_type": GetJavaType, 364 "java_type": GetJavaType,
365 "java_true_false": GetJavaTrueFalse,
330 "method_ordinal_name": GetMethodOrdinalName, 366 "method_ordinal_name": GetMethodOrdinalName,
331 "name": GetNameForElement, 367 "name": GetNameForElement,
332 "new_array": NewArray, 368 "new_array": NewArray,
333 "response_struct_from_method": GetResponseStructFromMethod, 369 "response_struct_from_method": GetResponseStructFromMethod,
334 "struct_from_method": GetStructFromMethod, 370 "struct_from_method": GetStructFromMethod,
335 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, 371 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
336 } 372 }
337 373
338 def GetJinjaExports(self): 374 def GetJinjaExports(self):
339 return { 375 return {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 def GetJinjaParameters(self): 448 def GetJinjaParameters(self):
413 return { 449 return {
414 'lstrip_blocks': True, 450 'lstrip_blocks': True,
415 'trim_blocks': True, 451 'trim_blocks': True,
416 } 452 }
417 453
418 def GetGlobals(self): 454 def GetGlobals(self):
419 return { 455 return {
420 'module': self.module, 456 'module': self.module,
421 } 457 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698