Hi Daniel,
yes, I followed the bug report thank you!
Indeed, I would have at least another GroovyCastException case when using @CompileStatic, which however is unrelated to that fix. I can easily reproduce with a project we use internally, which is however tightly bound to our internal services. Today I spent almost two hours to try to isolate the problem in a self-contained test case, but I could not succeed :-(

Any idea on what path I may follow to help you further investigate the problem?

The code breaks at a point like this:

public MyClass {
  // [...]

  private static Foo createFoo(final String type, final Object value) {
    if(!type || !value)
      return null;
    def result = new Foo()
    result.with {
      try {
        prop1 = anotherStaticMethodReturningString('blah')
        if(value instanceof String)
          prop2 = anotherStaticMethodReturningString('oh')
        else if (value instanceof BigDecimal)
          prop3 = (BigDecimal) value
       // else if - other cases of instanceof
      } finally {
    return result;

  // [...]


The exception occurs at the invocation of endUpdate() with the following exception:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class test.MyClass' with class 'java.lang.Class' to class 'anotherpackage.Foo'
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:414)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:328)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:242)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:615)
    at test.MyClass$_createFoo_closure6.doCall(MyClass.groovy:295)
    at test.MyClass$_createFoo_closure6.call(MyClass.groovy)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.with(DefaultGroovyMethods.java:369)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.with(DefaultGroovyMethods.java:316)
    at test.MyClass.createFoo(MyClass.groovy:275)

So, it seems like it's trying to invoke endUpdate() on MyClass class instance rather than on the Foo instance to which with should delegate to.

The problem goes away if either:
Any idea?


Il 20/02/2019 11:34, Daniel Sun ha scritto:
Hi Mauro,

The issue you most want to fix is fixed:


Sent from: http://groovy.329449.n5.nabble.com/Groovy-Users-f329450.html