[ 
https://issues.apache.org/jira/browse/GROOVY-1724?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15504110#comment-15504110
 ] 

Abel Salgado Romero commented on GROOVY-1724:
---------------------------------------------

Sorry to comment on a closed issue, but I stumbled upon a thing related to this 
issue and I'd like to understand the scope of the implementation to see if this 
is a bug or intended.
My case is not 100% the same but I'm confused with the sentence "allow 
extending classes as well"

I've seen that when coercing a closure to an abstract class with a single 
abstract method, no real extend is done. Instead a Proxy is created to delegate 
calls to the method to the closure.
This means that methods and attributes of the abstract class are not 
accessible. While it makes sense to some degree, this makes the use of this 
feature dangerous.
For instance IntelliJ recognizes the coercion as a real subclass and provides 
completion for methods and attributes found in the abstract super, but they 
won't be available on runtime.

For instance, this compiles but fails saying that cannot find the "reverse" 
method, obviously, since the delegate in the closure points to the script where 
it was declared.

Abstract Java class with a single abstract method
{noformat}
// AbstractStuff.java
public abstract class AbstractStuff {

    public String reverse(String value) {
        return new StringBuilder(value).reverse().toString();
    }

    public abstract String concat(String one, String two);

}
{noformat}

{noformat}
// CoercionPocScript.groovy
    AbstractStuff stuff = { String one, String two ->
        reverse(one + two)
    }

    stuff.concat('1','2')
{noformat}

Using Groovy 2.4.7

> Extend Map coercion to classes
> ------------------------------
>
>                 Key: GROOVY-1724
>                 URL: https://issues.apache.org/jira/browse/GROOVY-1724
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Guillaume Delcroix
>            Assignee: Jochen Theodorou
>             Fix For: 1.1-beta-3
>
>
> Currently, it is possible to define Map coercion to interfaces:
> [foo: {}, bar: {}] as SomeInterface
> The mechanism leverages Java proxies and invocation handlers.
> The mechanism should be enhanced to allow extending classes as well.



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

Reply via email to