Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Won't Fix
-
1.8-beta-2, 3.0.0-alpha-1
-
None
-
None
-
all
Description
The current Groovy bean syntax to define a public property with a public setter & getter and a private backing field is as described in http://groovy.codehaus.org/Groovy+Beans.
I propose a new, optional syntax that allows the class author to control the access modifier of the getter method, setter method, and field. The default, if the access modifier specification is not present, would be to produce a public setter & getter with a private backing field, just as it always has been. The proposed syntax can be specified using the following symbols.
+ public (pretty intuitive)
~ protected (similar to Unix's "home" concept: the class's "home" package, this class or its subclasses)
= package (the package of the accessing code must be equal to this element's package)
- private (pretty intuitive)
! for getter/setter only: tells compiler not to generate method (common negation symbol)
I propose that the scope specifier expression is of the following form:
[<setterAccessSpecifier><getterAccessSpecifier><fieldAccessSpecifier>]
(square brackets indicate optional elements). The most common use cases outside Groovy's current default are those with a non-public setter, a public getter, and a private field.
I invite discussion on whether it should be a compiler error if the field specifier were ever more accessible than or equal to the scope of the setter or getter method. Basically, it might require callers in different packages to use @field syntax; I leave that consideration to the Groovy compiler experts.
While the proposed form could be modified to make getter & field access specifications optional, making it terser, it also might be less intuitive, especially if specifying a non-public scope for the setter or when not generating a setter method. While the proposed form is more verbose, it does have the advantage of being pretty explicit, once you know that the order is setter-getter-field, and the class author, if choosing to specify something other than public/public/private, is already stepping a bit outside the box.
The default value of the access specifier is "++-".
Here are some examples:
class Person { String name // public setter & getter, private field; same as ++- }
=====
class Person { ++- String name // public setter & getter, private field; same as current Groovy compiler }
=====
class Person { ~+- String name // common: protected setter, public getter, private field }
=====
class Person { -+- String name // common: private setter, public getter, private field }
=====
class Person { !+- String name // no setter, public getter, private field }
=====
class Person { --+ String name // ERROR? private setter, private getter, public field }
=====
class Person { ++~ String name // public setter, public getter, protected field }
Attachments
Issue Links
- relates to
-
GROOVY-4323 Convention for read-only properties
- Open