Quantcast

[jira] [Comment Edited] (GROOVY-8123) Wrong invokation target of static method

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[jira] [Comment Edited] (GROOVY-8123) Wrong invokation target of static method

JIRA jira@apache.org

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

Paul King edited comment on GROOVY-8123 at 4/19/17 2:27 AM:
------------------------------------------------------------

A potential workaround is to use {{@CompileStatic}}:
{code}
@Grab('com.google.guava:guava:17.0')
import com.google.common.collect.ImmutableSortedSet

@groovy.transform.CompileStatic
def method() {
  println ImmutableSortedSet.naturalOrder().build().dump()
  println ImmutableSortedSet.of().dump()
}

method()
{code}


was (Author: paulk):
The other workaround is to use {{@CompileStatic}}:
{code}
@Grab('com.google.guava:guava:17.0')
import com.google.common.collect.ImmutableSortedSet

@groovy.transform.CompileStatic
def method() {
  println ImmutableSortedSet.naturalOrder().build().dump()
  println ImmutableSortedSet.of().dump()
}

method()
{code}

> Wrong invokation target of static method
> ----------------------------------------
>
>                 Key: GROOVY-8123
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8123
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.4.9
>            Reporter: Jędrzej Dąbrowa
>
> Given following set of java classes:
> {code:title=Interface.java|borderStyle=solid}
> public interface Interface {
>     class ExternalInterface implements Interface {}
>     static Interface provide() {
>         new Throwable().printStackTrace();
>         return new ExternalInterface();
>     }
> }
> {code}
> {code:title=MyInterface.java|borderStyle=solid}
> public abstract class MyInterface implements Interface {
>     private static class MyInterfaceImpl extends MyInterface {}
>     public static MyInterface provide() {
>         return new MyInterfaceImpl();
>     }
> }
> {code}
> And below Groovy code:
> {code:title=Test.groovy|borderStyle=solid}
> class Test {
>     static void main(String[] args) {
>         MyInterface myInterface = MyInterface.provide()
>     }
> }
> {code}
> It yields following result:
> {noformat}
> Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Interface$ExternalInterface@24a35978' with class 'Interface$ExternalInterface' to class 'MyInterface'
> at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
> at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
> at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
> at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
> at Test.main(Test.groovy:9)
> {noformat}
> Stacktrace during creating object of invalid type is:
> {noformat}
> at Interface.provide(Interface.java:7)
> at Interface$provide.call(Unknown Source)
> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
> at Test.main(Test.groovy:9)
> {noformat}
> To me this seems to be a bug. Or is this expected behavior?



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)
Loading...