You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2016/06/12 11:20:20 UTC

[jira] [Created] (GROOVY-7860) Groovy could implement an @AutoImplement transform

Paul King created GROOVY-7860:
---------------------------------

             Summary: Groovy could implement an @AutoImplement transform
                 Key: GROOVY-7860
                 URL: https://issues.apache.org/jira/browse/GROOVY-7860
             Project: Groovy
          Issue Type: New Feature
            Reporter: Paul King
            Assignee: Paul King


Groovy provides numerous facilities for dynamically creating 'Proxy' implementations of interfaces, e.g.:
{code}
def emptyIterator = [hasNext: { false }] as Iterator
{code}
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:
{code}
@AutoImplement
class EmptyStringIterator implements Iterator<String> {
    boolean hasNext() { false }
}
{code}
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:
{code}
@AutoImplement(exception=UnsupportedOperationException)
class EmptyStringIterator implements Iterator<String> {
    boolean hasNext() { false }
}
{code}
This would in fact be a closer match to the initial dynamic case shown above.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)