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:

|@CompileStatic||
|
|public MyClass {|
|  // [...]||
|
|
|
|  private static Foo createFoo(final String type, final Object value) {|
|    if(!type || !value) ||
|
|      return null;|
|    def result = new Foo()|
|    result.with {|
|      beginUpdate()|
|      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 {|
|        endUpdate()||
|
|      }||
|
|    }|
|    return result;||
|
|  }|
|
|
|  // [...]||
|
|}||
|

where:

 * |Foo| is a Java class coming from a JAR
 * |beginUpdate()| and |endUpdate()| are methods defined in |Foo|
 * |prop1|, |prop2| and |prop3| are properties defined in |Foo| (i.e.:
   there are getters and setters for them)

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:

 * I remove |@CompileStatic| (or I just add |@CompileDynamic| to
   |createFoo()| method)||
 * I apply to |MyClass| (or maybe to the consumer code using |MyClass|)
   a sufficient amount of changes (but I can't say exactly which ones)|
   |

Any idea?

Mauro

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

The issue you most want to fix is fixed:
https://github.com/apache/groovy/commit/f44449ebd615cb5d5f766a30b2a6e904084acece

Cheers,
Daniel.Sun




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


Reply via email to