Skip to content

Commit 33f3655

Browse files
author
Stephen Cobbe
committed
Changed internal Union constructors to avoid compile issues for large number of args.
1 parent c6aeb4b commit 33f3655

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

generator/java.stoneg.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ def get_underlying_type(data_type, allow_lists=True):
308308
return data_type
309309

310310

311+
def union_factory_create_method_name(data_type, value_fields_subset):
312+
if len(value_fields_subset) > 0:
313+
method_suffix = 'And%s' % _capwords(value_fields_subset[0].name)
314+
else:
315+
method_suffix = ''
316+
return 'withTag%s' % method_suffix
317+
318+
311319
@total_ordering
312320
class JavaClass(object):
313321
"""
@@ -2962,49 +2970,57 @@ def generate_data_type_union(self, data_type):
29622970
all_fields = data_type.all_fields
29632971
static_fields = [f for f in all_fields if is_void_type(f.data_type)]
29642972
value_fields = [f for f in all_fields if not is_void_type(f.data_type)]
2965-
nulls = ["null" for f in value_fields]
29662973

29672974
if static_fields:
29682975
w.out('')
29692976
for field in static_fields:
2970-
singleton_args = ', '.join(["Tag.%s" % j.field_tag_enum_name(field)] + nulls)
2977+
singleton_args = ', '.join(["Tag.%s" % j.field_tag_enum_name(field)])
29712978
w.javadoc(field)
2972-
w.out('public static final %s %s = new %s(%s);',
2979+
method_name = union_factory_create_method_name(data_type, [])
2980+
w.out('public static final %s %s = %s.%s(%s);',
29732981
j.java_class(data_type),
29742982
j.field_static_instance(field),
29752983
j.java_class(data_type),
2984+
method_name,
29762985
singleton_args,
29772986
)
29782987

29792988
#
29802989
# Instance fields
29812990
#
29822991
w.out('')
2983-
w.out('private final Tag _tag;')
2992+
w.out('private Tag _tag;')
29842993
for field in all_fields:
29852994
if j.has_value(field):
2986-
w.out('private final %s %s;', j.java_class(field, boxed=True), j.param_name(field))
2995+
w.out('private %s %s;', j.java_class(field, boxed=True), j.param_name(field))
29872996

29882997
#
2989-
# Constructor
2998+
# Constructors
29902999
#
2991-
args = ', '.join(chain(
2992-
['Tag _tag'],
2993-
[
2994-
w.fmt('%s %s', j.java_class(f, boxed=True), j.param_name(f))
2995-
for f in value_fields
2996-
],
2997-
))
2998-
w.out('')
2999-
w.javadoc(data_type,
3000-
fields=value_fields,
3001-
params=OrderedDict(_tag="Discriminating tag for this instance."))
3002-
with w.block('private %s(%s)', j.java_class(data_type), args):
3003-
w.out('this._tag = _tag;')
3004-
for field in value_fields:
3005-
# don't perform validation in the private constructor
3006-
w.out('this.%s = %s;', j.param_name(field), j.param_name(field))
3007-
3000+
def _gen_factory_method(data_type, value_fields_subset):
3001+
w.out('')
3002+
w.javadoc(data_type,
3003+
fields=value_fields_subset,
3004+
params=OrderedDict(_tag="Discriminating tag for this instance."))
3005+
formatted_args = ', '.join(chain(
3006+
['Tag _tag'],
3007+
[
3008+
w.fmt('%s %s', j.java_class(f, boxed=True), j.param_name(f))
3009+
for f in value_fields_subset
3010+
],
3011+
))
3012+
method_name = union_factory_create_method_name(data_type, value_fields_subset)
3013+
with w.block('private static %s %s(%s)', j.java_class(data_type), method_name, formatted_args):
3014+
w.out('final %s result = new %s();', j.java_class(data_type), j.java_class(data_type))
3015+
w.out('result._tag = _tag;')
3016+
for field in value_fields_subset:
3017+
# don't perform validation in the private constructor
3018+
w.out('result.%s = %s;', j.param_name(field), j.param_name(field))
3019+
w.out('return result;')
3020+
3021+
_gen_factory_method(data_type, [])
3022+
for f in value_fields:
3023+
_gen_factory_method(data_type, [f])
30083024

30093025
#
30103026
# Field getters/constructors
@@ -3104,14 +3120,12 @@ def generate_data_type_union_field_methods(self, data_type):
31043120
j.java_class(field),
31053121
):
31063122
self.generate_field_validation(field, value_name="value", omit_arg_name=True, allow_default=False)
3107-
args = ", ".join(
3108-
"value" if f is field else "null"
3109-
for f in value_fields
3110-
)
3111-
w.out('return new %s(Tag.%s, %s);',
3123+
method_name = union_factory_create_method_name(data_type, [field])
3124+
w.out('return %s.%s(Tag.%s, %s);',
31123125
j.java_class(data_type),
3126+
method_name,
31133127
j.field_tag_enum_name(field),
3114-
args)
3128+
"value")
31153129

31163130
if is_nullable_type(field.data_type):
31173131
w.out('')

0 commit comments

Comments
 (0)