Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4519

Add shorthand syntax for specifying access modifiers for property methods (setters & getters) & fields



    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 1.8-beta-2, 3.0.0-alpha-1
    • Fix Version/s: None
    • Labels:
    • Environment:


      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:


      (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


          Issue Links



              • Assignee:
                matthewadams Matthew T. Adams
              • Votes:
                0 Vote for this issue
                0 Start watching this issue


                • Created: