Groovy - type checking in script is not working properly

I have an application Groovyin which I allow the user to add custom behavior through scripts Groovy. I include these scripts through GroovyShelland test them through Type Checking Extensions. Full code of how I include the script in my application:

def config = new CompilerConfiguration()
config.addCompilationCustomizers(
    new ASTTransformationCustomizer(TypeChecked)
)
def shell = new GroovyShell(config)
shell.evaluate(new File("path/to/some/file.groovy"))

It works great. However , type checking in a script seems seriously broken. For example, I can include the following scripts without any compiler complaint:

String test = getTestValue() // automatic conversion from Integer to String. But WHY?
println "The value is $test" // shows as "The value is 0" on the console

private Integer getTestValue(){
    return 0
}

I can even go further. When creating classinside a script, I can assign it Stringwithout errors:

String y = new Test()
println y // shows Test@somenr on the console

class Test { }

do . , .

+1
2

, . , : String x = new T():

   0: invokestatic  #17                 // Method $getCallSiteArray:()[Lorg/codehaus/groovy/runtime/callsite/CallSite;
   3: astore_1
   4: aload_1
   5: ldc           #40                 // int 1
   7: aaload
   8: ldc           #42                 // class T
  10: invokeinterface #46,  2           // InterfaceMethod org/codehaus/groovy/runtime/callsite/CallSite.callConstructor:(Ljava/lang/Object;)Ljava/lang/Object;
  15: invokestatic  #52                 // Method org/codehaus/groovy/runtime/typehandling/ShortTypeHandling.castToString:(Ljava/lang/Object;)Ljava/lang/String;
  18: checkcast     #54                 // class java/lang/String

. @TypeChecked/@CompileStatic.

+1

, , Static Type Checker. LHS String, RHS , ShortTypeHandling.castToString().

Groovy 2.4.13.

0

Source: https://habr.com/ru/post/1617380/


All Articles