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

Yuki Goto updated GROOVY-7758:
------------------------------
    Description: 
The following code gives {{GroovyCastException}}.
{code}
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
  }
}
{code}
The code below is a part of the decompiled {{Test.class}}. Groovy tries to cast 
{{this}} instead of {{a}}.
{code}
public Object doCall(String string, A a) {
  ((A)ScriptBytecodeAdapter.castToType(((_test_closure1)this).getThisObject(), 
A.class)).printString();
  return null;
}
{code}
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.

  was:
The following code gives {{GroovyCastException}}.
{code}
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
  }
}
{code}
The code blow is a part of the decompiled {{Test.class}}. Groovy trys to cast 
{{this}} instead of {{a}}.
{code}
public Object doCall(String string, A a) {
  ((A)ScriptBytecodeAdapter.castToType(((_test_closure1)this).getThisObject(), 
A.class)).printString();
  return null;
}
{code}
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.


> GroovyCastException when both @CompileStatic and @Newify are used
> -----------------------------------------------------------------
>
>                 Key: GROOVY-7758
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7758
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>    Affects Versions: 2.4.5
>            Reporter: Yuki Goto
>
> The following code gives {{GroovyCastException}}.
> {code}
> 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
>   }
> }
> {code}
> The code below is a part of the decompiled {{Test.class}}. Groovy tries to 
> cast {{this}} instead of {{a}}.
> {code}
> public Object doCall(String string, A a) {
>   
> ((A)ScriptBytecodeAdapter.castToType(((_test_closure1)this).getThisObject(), 
> A.class)).printString();
>   return null;
> }
> {code}
> 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.



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

Reply via email to