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

Eric Milles edited comment on GROOVY-9244 at 10/3/19 5:13 PM:
--------------------------------------------------------------

I gave this another look and was able to see that the runtime system has enough 
to select the proper method when typecasts are added (as you requested in your 
issue title).  I can experiment with adding them into "this" and "super" 
constructor call expressions to handle null arguments better.

{code:groovy}
import java.util.regex.Pattern

abstract class Groovy3 {
    Groovy3(String s) { println "String" }
    Groovy3(Pattern p) { println "Pattern" }
}
class Groovy4 extends Groovy3 {
    Groovy4(String s) { super((String)s) }
    Groovy4(Pattern p) { super((Pattern)p) }
}

new Groovy4((String)null)
{code}

The anon. inner class version cannot be fixed with typecasts.  The constructor 
calls into 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.selectConstructorAndTransformArguments
 and there is no type information left for the null argument.


was (Author: emilles):
I gave this another look and was able to see that the runtime system has enough 
to select the proper method when typecasts are added (as you requested in your 
issue title).  I can experiment with adding them into "this" and "super" 
constructor call expressions to handle null arguments better.

{code:groovy}
import java.util.regex.Pattern

abstract class Groovy3 {
    Groovy3(String s) { println "String" }
    Groovy3(Pattern p) { println "Pattern" }
}
class Groovy4 extends Groovy3 {
    Groovy4(String s) { super((String)s) }
    Groovy4(Pattern p) { super((Pattern)p) }
}

new Groovy4((String)null)
{code}

> Anonymous subclasses should cast their super-parameters
> -------------------------------------------------------
>
>                 Key: GROOVY-9244
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9244
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.7, 2.5.7
>         Environment: I tried in GroovyConsole 2.4.7 and Groovy web console 
> 2.5.7
>            Reporter: Björn Kautler
>            Priority: Major
>
> Given this code:
> {noformat}
>     import java.util.regex.Pattern
>     abstract class Groovy3 {
>         Groovy3(String s) { println "String" }
>         Groovy3(Pattern p) { println "Pattern" }
>     }
>     class Groovy4 extends Groovy3 {
>         Groovy4(String s) { super(s) }
>         Groovy4(Pattern p) { super(p) }
>     }
>     class Groovy5 extends Groovy3 {
>         Groovy5(String s) { super(s as String) }
>         Groovy5(Pattern p) { super(p as Pattern) }
>     }
>     class Groovy6 {
>         Groovy6(String s) { println "String" }
>         Groovy6(Pattern p) { println "Pattern" }
>     }
>     new Groovy3(null as String) { }
>     new Groovy4(null as String)
>     new Groovy5(null as String)
>     new Groovy6(null as String)
> {noformat}
> Groovy3ish and Groovy4 instantiation fail
> Groovy5 and Groovy6 instantiation succeed and print {{String}}.
> I did not find a way to make the Groovy3ish one work, except for doing it the 
> Groovy4 way, but no chance with an anonymous subclass.
> From what I observed, I guess the anonymous subclass does it like Groovy4 but 
> should do it like Groovy5 to properly select the super constructor.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to