You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2019/11/27 14:13:00 UTC

[jira] [Resolved] (GROOVY-8468) @CompileStatic runtime error when call java implemented method that returns generic-array value

     [ https://issues.apache.org/jira/browse/GROOVY-8468?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles resolved GROOVY-8468.
---------------------------------
    Fix Version/s: 2.5.x
                   3.x
       Resolution: Fixed

Not sure when this got fixed, but it works in current 2.5 and 3.0 branches.

> @CompileStatic runtime error when call java implemented method that returns generic-array value 
> ------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-8468
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8468
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime, Static compilation
>    Affects Versions: 2.4.13
>         Environment: JVM 1.8.0_162,  windows 8.1
> 1.8.0_152-ea, 3.10.0-514.10.2.el7.x86_64 GNU/Linux
>            Reporter: Andrey Artemev
>            Priority: Critical
>             Fix For: 3.x, 2.5.x
>
>         Attachments: AnyClass.java, AnyInterface.java, Hello.java, HelloInterface.java, test.groovy
>
>
> Consider the following java classes:
> Absolutly any interface:
> {code:java|title=AnyInterface.java|borderStyle=solid}
> public interface AnyInterface {}
> {code}
> Any class that implements interface :
> {code:java|title=AnyClass.java|borderStyle=solid}
> public class AnyClass implements AnyInterface {}
> {code}
> Interface with following signature (important)  :
> {code:java|title=HelloInterface.java|borderStyle=solid}
> public interface HelloInterface {
>     public <T extends AnyInterface> T[] f(Class<T> clazz);	
> }
> {code}
>  And the main java class where located the method that we want call from groovy ( *f* ):
> {code:java|title=Hello.java|borderStyle=solid}
> import java.lang.reflect.Array;
> public class Hello implements HelloInterface {
>     @Override
>     public <T extends AnyInterface> T[] f(Class<T> clazz){
>         return (T[]) Array.newInstance(clazz, 0);
>     }
>     //To test f call from inside Java
>     public static void main(String[] args) {
>         Hello h = new Hello();
>         AnyInterface[] s = h.f(AnyClass.class);
>     }
> }
> {code}
>  
> So if we try to call f method (Hello.java) from groovy with @CompileStatic:
> {code:title=test.groovy|borderStyle=solid}
> import Hello
> import groovy.transform.CompileStatic
> @CompileStatic
> class Test {
>     static void test() {
>         Hello h = new Hello()
>         AnyInterface[] o = h.f(AnyClass.class)
>     }
> }
> Test.test()
> {code}
>  
> we will get NoSuchMethodError : 
> {code}
> Caught: java.lang.NoSuchMethodError: Hello.f(Ljava/lang/Class;)[Ljava/lang/Object;
> java.lang.NoSuchMethodError: Hello.f(Ljava/lang/Class;)[Ljava/lang/Object;
> at Test.test(test.groovy:8)
> at Test$test.call(Unknown Source)
> at test.run(test.groovy:12)
> {code}
> But without @CompileStatic annotation the code executes successfully
>  All sources are attached.



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