You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Jochen Theodorou (JIRA)" <ji...@apache.org> on 2015/08/26 09:54:45 UTC
[jira] [Resolved] (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 ]
Jochen Theodorou resolved GROOVY-7559.
--------------------------------------
Resolution: Not A Problem
Assignee: Jochen Theodorou
That's exactly how it is supposed to behave in Groovy. You are allowed to access private members from outside of the class, as long as you work on a direct instance of the class. So if you have class A{private foo}, then you are allowed to access foo on an instance new A(), since it is a direct instance. But in case of class B extends A{} you cannot access foo on a new B(). If B defines a private foo on its own, then you can access that foo through the instance new B().
> 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.4#6332)