You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2017/02/01 23:17:52 UTC
[jira] [Closed] (GROOVY-7559) dynamic code can write to a read-only
property
[ https://issues.apache.org/jira/browse/GROOVY-7559?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Paul King closed GROOVY-7559.
-----------------------------
> dynamic code can write to a read-only property
> ----------------------------------------------
>
> Key: GROOVY-7559
> URL: https://issues.apache.org/jira/browse/GROOVY-7559
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.4.4
> Reporter: Christopher Smith
> Assignee: Jochen Theodorou
>
> I have an {{Invoice}} class for which line items and charges can only be added through methods that enforce invariants:
> {code}
> class Invoice {
> void calculateBalance() {...}
> private List<LineItem> lineItems = []
> final List<LineItem> getLineItems() {
> lineItems.asImmutable()
> }
> void addLineItem(LineItem item) {
> lineItems << item
> calculateBalance()
> }
> private List<BillingTransaction> charges = []
> @Override
> final List<BillingTransaction> getCharges() {
> charges.asImmutable()
> }
> @Override
> void applyCharge(BillingTransaction charge) {
> charges << charge
> calculateBalance()
> }
> }
> {code}
> In my Spock test, I can successfully execute {{invoice.charges = []}}, even though there's no setter for the property. Strangely, I am migrating these fields from an abstract base class (pulling out into their own class), and the read-only is enforced correctly in the unit test in that case (I literally copied and pasted, and the test started failing).
> {{javap}} indicates that no setter method is being generated.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)