Skip to content

Commit

Permalink
Only generate virtual setters where necessary LOXI-76
Browse files Browse the repository at this point in the history
The previous commit would add virtual Exception-raising setters
globally. This commit adds a method that specifically checks whether
a setter is necessary.

A setter must be generated if the field is writeable in this class
or any super class. Fields can turn non-writeable in a subclass
if a superclass data member is used as a discriminator.
  • Loading branch information
andi-bigswitch committed Dec 2, 2015
1 parent 1c8f7ee commit 99c6d9d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions java_gen/java_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,16 @@ def priv_value(self):
else:
return self.java_type.format_value(self.member.value, pub_type=False)

@property
def needs_setter(self):
if self.is_writeable:
return True
super_class = self.msg.super_class
if super_class:
super_member = super_class.member_by_name(self.name)
if super_member:
return super_member.needs_setter
return False

@property
def is_writeable(self):
Expand Down
2 changes: 1 addition & 1 deletion java_gen/templates/_field_accessors.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
//:: #endif

//:: if generate_setters:
//:: if generate_setters and prop.needs_setter:
//:: setter_template_file_name = "%s/custom/%s_%s.java" % (template_dir, msg.name if not builder else msg.name + '.Builder', prop.setter_name)
//:: if os.path.exists(setter_template_file_name):
//:: include(setter_template_file_name, msg=msg, builder=builder, has_parent=has_parent)
Expand Down
2 changes: 2 additions & 0 deletions java_gen/templates/of_interface.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public interface Builder${ "<%s>" % msg.type_annotation if msg.type_annotation e
${msg.name}${msg.type_variable} build();
//:: for prop in msg.members:
${prop.java_type.public_type} ${prop.getter_name}()${ "" if prop.is_universal else " throws UnsupportedOperationException"};
//:: if prop.needs_setter:
Builder${msg.type_variable} ${prop.setter_name}(${prop.java_type.public_type} ${prop.name})${ "" if prop.is_universal else " throws UnsupportedOperationException"};
//:: #endif
//:: #endfor
}
}

0 comments on commit 99c6d9d

Please sign in to comment.