Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7758

GroovyCastException when both @CompileStatic and @Newify are used

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.5
    • Fix Version/s: 2.4.7
    • Component/s: Static compilation
    • Labels:
      None

      Description

      The following code gives GroovyCastException.

      class Main {
        public static void main(String... args) {
          Test test = new Test()
        }
      }
      
      @CompileStatic @Newify
      class Test {
        private HashMap<String, A> hashMap = [:]
        public Test() {
          set()
          test()
        }
        public void test() {
          hashMap.each() { String string, A a ->
            a.printString()
          }
        }
        public void set() {
          hashMap.put("aaa", B.new("xxx"))
          hashMap.put("bbb", B.new("yyy"))
        }
      }
      
      class A {
        public String string = ""
        public void printString() {
          println(string)
        }
      }
      
      class B extends A {
        public B(String string) {
          this.string = string
        }
      }
      

      The code below is a part of the decompiled Test.class. Groovy tries to cast this instead of a.

      public Object doCall(String string, A a) {
        ((A)ScriptBytecodeAdapter.castToType(((_test_closure1)this).getThisObject(), A.class)).printString();
        return null;
      }
      

      When @CompileStatic is omitted, or when @Newify is omitted and B.new is replaced by new B, the code above gives no exception and runs as expected.

        Activity

        Hide
        shils Shil Sinha added a comment -

        This occurs because 'implicitThis' is not set correctly in Newify transformed method calls. A simpler example:

        class A {
            String foo() { 'abc' }
        }
        
        @groovy.transform.CompileStatic
        @groovy.lang.Newify
        String test(A arg) {
            Closure<String> cl = { A it -> it.foo() }
            cl.call(arg)
        }
        
        assert test(new A()) == 'abc'
        

        The above fails with the same error (and for the same reason) as the example in the issue description.

        Show
        shils Shil Sinha added a comment - This occurs because 'implicitThis' is not set correctly in Newify transformed method calls. A simpler example: class A { String foo() { 'abc' } } @groovy.transform.CompileStatic @groovy.lang.Newify String test(A arg) { Closure< String > cl = { A it -> it.foo() } cl.call(arg) } assert test( new A()) == 'abc' The above fails with the same error (and for the same reason) as the example in the issue description.
        Hide
        githubbot ASF GitHub Bot added a comment -

        GitHub user shils opened a pull request:

        https://github.com/apache/groovy/pull/294

        GROOVY-7758: GroovyCastException when both @CompileStatic and @Newify…

        … are used

        You can merge this pull request into a Git repository by running:

        $ git pull https://github.com/shils/groovy GROOVY-7758

        Alternatively you can review and apply these changes as the patch at:

        https://github.com/apache/groovy/pull/294.patch

        To close this pull request, make a commit to your master/trunk branch
        with (at least) the following in the commit message:

        This closes #294


        commit da9666567fd5a6400521c1d2bc096e989f44bff5
        Author: Shil Sinha <shil.sinha@gmail.com>
        Date: 2016-03-14T19:21:30Z

        GROOVY-7758: GroovyCastException when both @CompileStatic and @Newify are used


        Show
        githubbot ASF GitHub Bot added a comment - GitHub user shils opened a pull request: https://github.com/apache/groovy/pull/294 GROOVY-7758 : GroovyCastException when both @CompileStatic and @Newify… … are used You can merge this pull request into a Git repository by running: $ git pull https://github.com/shils/groovy GROOVY-7758 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/294.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #294 commit da9666567fd5a6400521c1d2bc096e989f44bff5 Author: Shil Sinha <shil.sinha@gmail.com> Date: 2016-03-14T19:21:30Z GROOVY-7758 : GroovyCastException when both @CompileStatic and @Newify are used
        Hide
        githubbot ASF GitHub Bot added a comment -

        Github user asfgit closed the pull request at:

        https://github.com/apache/groovy/pull/294

        Show
        githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/groovy/pull/294

          People

          • Assignee:
            shils Shil Sinha
            Reporter:
            Ziphil Natsuki Takanashi
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development