Skip to content

Commit 93b73f2

Browse files
author
Stephen Cobbe
committed
Improved union implementation.
1 parent 79d5aff commit 93b73f2

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

generator/java.stoneg.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ 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):
311+
def union_create_with_method_name(data_type, value_fields_subset):
312312
if len(value_fields_subset) > 0:
313313
method_suffix = 'And%s' % _capwords(value_fields_subset[0].name)
314314
else:
@@ -2976,8 +2976,8 @@ def generate_data_type_union(self, data_type):
29762976
for field in static_fields:
29772977
singleton_args = ', '.join(["Tag.%s" % j.field_tag_enum_name(field)])
29782978
w.javadoc(field)
2979-
method_name = union_factory_create_method_name(data_type, [])
2980-
w.out('public static final %s %s = %s.%s(%s);',
2979+
method_name = union_create_with_method_name(data_type, [])
2980+
w.out('public static final %s %s = new %s().%s(%s);',
29812981
j.java_class(data_type),
29822982
j.field_static_instance(field),
29832983
j.java_class(data_type),
@@ -2997,7 +2997,14 @@ def generate_data_type_union(self, data_type):
29972997
#
29982998
# Constructors
29992999
#
3000-
def _gen_factory_method(data_type, value_fields_subset):
3000+
3001+
w.out('')
3002+
w.javadoc('Private default constructor, so that object is uninitializable publicly.')
3003+
with w.block('private %s()', j.java_class(data_type)):
3004+
pass
3005+
w.out('')
3006+
3007+
def _gen_create_with_method(data_type, value_fields_subset):
30013008
w.out('')
30023009
w.javadoc(data_type,
30033010
fields=value_fields_subset,
@@ -3009,18 +3016,18 @@ def _gen_factory_method(data_type, value_fields_subset):
30093016
for f in value_fields_subset
30103017
],
30113018
))
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))
3019+
method_name = union_create_with_method_name(data_type, value_fields_subset)
3020+
with w.block('private %s %s(%s)', j.java_class(data_type), method_name, formatted_args):
3021+
w.out('%s result = new %s();', j.java_class(data_type), j.java_class(data_type))
30153022
w.out('result._tag = _tag;')
30163023
for field in value_fields_subset:
30173024
# don't perform validation in the private constructor
30183025
w.out('result.%s = %s;', j.param_name(field), j.param_name(field))
30193026
w.out('return result;')
30203027

3021-
_gen_factory_method(data_type, [])
3028+
_gen_create_with_method(data_type, [])
30223029
for f in value_fields:
3023-
_gen_factory_method(data_type, [f])
3030+
_gen_create_with_method(data_type, [f])
30243031

30253032
#
30263033
# Field getters/constructors
@@ -3120,8 +3127,8 @@ def generate_data_type_union_field_methods(self, data_type):
31203127
j.java_class(field),
31213128
):
31223129
self.generate_field_validation(field, value_name="value", omit_arg_name=True, allow_default=False)
3123-
method_name = union_factory_create_method_name(data_type, [field])
3124-
w.out('return %s.%s(Tag.%s, %s);',
3130+
method_name = union_create_with_method_name(data_type, [field])
3131+
w.out('return new %s().%s(Tag.%s, %s);',
31253132
j.java_class(data_type),
31263133
method_name,
31273134
j.field_tag_enum_name(field),

0 commit comments

Comments
 (0)