You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Christopher Smith (JIRA)" <ji...@apache.org> on 2015/08/25 23:42:45 UTC

[jira] [Created] (GROOVY-7559) dynamic code can write to a read-only property

Christopher Smith created GROOVY-7559:
-----------------------------------------

             Summary: 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


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.4#6332)