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

Groovy could implement an @AutoImplement transform

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.5.0-alpha-1
    • None
    • None

    Description

      Groovy provides numerous facilities for dynamically creating 'Proxy' implementations of interfaces, e.g.:

      def emptyIterator = [hasNext: { false }] as Iterator
      

      There is special support for Closures, maps of Closures, SAM method coercion and various proxy generator classes. Typically such dynamic creation is exactly what is required, e.g. a one-off usage object or a testing stub of some kind. But other times, compile time creation of such classes would be useful. This proposal suggests a transform to reduce boilerplate code for a number of common scenarios where code can be created. The proposal has numerous configuration options but doesn't try to support everything that the dynamic options provide. E.g. no map of Closures is supported; you can just create a class manually in that case.

      The transform allows the above example to be as follows:

      @AutoImplement
      class EmptyStringIterator implements Iterator<String> {
          boolean hasNext() { false }
      }
      

      which provides a method having signature 'String next()' with an implementation that returns the default value for the return type (null for String).

      Alternatively, we can make it throw an exception as follows:

      @AutoImplement(exception=UnsupportedOperationException)
      class EmptyStringIterator implements Iterator<String> {
          boolean hasNext() { false }
      }
      

      This would in fact be a closer match to the initial dynamic case shown above.

      Attachments

        Activity

          People

            paulk Paul King
            paulk Paul King
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: