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

GEP: Adding Bound Properties to GroovyBeans

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.1-beta-2
    • 1.6-beta-1
    • GEP
    • None

    Description

      Abstract:

      The current support of properties in Groovy beans does not auto-generate support for bound properties, a standard feature defined in the JavaBeans specification. This GEP proposes that there be some standard terse Groovy way to indicate to the compiler that such property support should be generated automatically

      Specification:

      The proposed solution is to introduce a new annotation groovy.lang.BoundProperty. This annotation will have source level retention and will be valid only on fields.

      When this property is present on one or more GroovyBeans properties then the class that contains the property will add a field $propertyChangeSupport of type java.beans.PropertyChangeSupport. Five methods will also be added, addPropertyChangeListener(PropertyChagneLsitener), removePropertyChangeListener(PropertyChagneListener), getPropertyChangeListeners(), addPropertyChangeListener(String, PropertyChagneLsitener), and removePropertyChangeListener(String, PropertyChagneListener). All of these methods will delegate directly to the appropriate method on $propertyChangeSupport. The auto-generated setter method will also call the firePropertyChanged(String, Object, Object} method of $propertyChangeSupport.

      A compile time error MUST be raised when a property is declared as @BoundProperty where the default named setter method is also declared. (i.e.

      @BoundProperty int x
      setX(int i) { x = i }

      is invalid)

      A compile time error MUST be raised when a class has any properties declared as a @BoundProperty and any or all of the five automatically added methods are also declared in the class.

      A compile time error SHOULD be raised when the @BoundProperty is used on a field with declared visibility, hence not a GroovyBean Property.

      Classes compiles with this annotation SHOULD execute under a 1.4 JVM, although compilation using a Java 5 JVM MAY be required.

      Motivation:

      While it is possible to add such wiring today to a GroovyBeans property by essentially implementing it the way Java does, (i.e. explicitly do everything yourself), it would be cleaner if there was a way to declare a property as bound, and have groovy handle the wiring like it already does for simple property declarations. Either a language keyword or annotation would be possible ways to do it.

      I am proposing the built in annotation because the addition of an additional language keyword may break older scripts where a previously unreserved keyword is now reserved. The use of syntactical punctuation is also not recommended since (a) there is no other example of using punctuation to add this aspect to a property and (b) because of that readability would suffer for those not familiar with the Groovy language.

      I am also proposing annotations because I will be able to submit a proof of concept implementation myself much easier.

      Backwards Compatibility:

      This enhancement will not be source code compatible with versions of Groovy prior to 1.1. The sole reason for this is the use of annotations. (use of a keyword or punctuation would similarly cause incompatibility).

      In instances where a user wants to use a Groovy script with @BoundProperty notation they user will need to remove the annotation, and either (a) not use automatically generated PropertyChangeListeners or (b) implement them by hand.

      Attachments

        1. GEP-BoundPropertries-java14safe.txt
          23 kB
          Daniel Ferrin
        2. BuiltinAnnotations.java
          1 kB
          Daniel Ferrin
        3. BoundProperty.java
          0.5 kB
          Daniel Ferrin
        4. GEP-BoundProperties.txt
          11 kB
          Daniel Ferrin

        Activity

          People

            shemnon Daniel Ferrin
            shemnon Daniel Ferrin
            Votes:
            4 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: