Groovy - 脚本中的类型检查未按预期工作
Groovy - Type checking in script not working as expected
我有一个 Groovy
应用程序,我允许用户在其中通过 Groovy
脚本添加自定义行为。我通过 GroovyShell
包含这些脚本并通过 Type Checking Extensions
输入检查它们。我如何将脚本包含在我的应用程序中的完整代码是:
def config = new CompilerConfiguration()
config.addCompilationCustomizers(
new ASTTransformationCustomizer(TypeChecked)
)
def shell = new GroovyShell(config)
shell.evaluate(new File("path/to/some/file.groovy"))
这很好用。 但是,脚本中的类型检查似乎严重损坏。例如,我可以包含以下脚本 而不会 编译器的任何投诉:
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
}
我什至可以走得更远。在脚本中创建 class
时,我可以将其分配给 String
而不会出现任何错误:
String y = new Test()
println y // shows Test@somenr on the console
class Test { }
其他类型的检查做 工作。我还没有发现它背后的任何逻辑,所以非常感谢任何正确方向的指示。
如有疑问,请反驳。这是与您的电话类似的电话: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
所以 this 是该演员表的罪魁祸首。这似乎也适用于 @TypeChecked
/@CompileStatic
.
这很可能是静态类型检查器中的错误。当表达式的 LHS 是字符串变量时,将调用 ShortTypeHandling.castToString()
的转换应用于 RHS。
这适用于 Groovy 2.4.13。
我有一个 Groovy
应用程序,我允许用户在其中通过 Groovy
脚本添加自定义行为。我通过 GroovyShell
包含这些脚本并通过 Type Checking Extensions
输入检查它们。我如何将脚本包含在我的应用程序中的完整代码是:
def config = new CompilerConfiguration()
config.addCompilationCustomizers(
new ASTTransformationCustomizer(TypeChecked)
)
def shell = new GroovyShell(config)
shell.evaluate(new File("path/to/some/file.groovy"))
这很好用。 但是,脚本中的类型检查似乎严重损坏。例如,我可以包含以下脚本 而不会 编译器的任何投诉:
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
}
我什至可以走得更远。在脚本中创建 class
时,我可以将其分配给 String
而不会出现任何错误:
String y = new Test()
println y // shows Test@somenr on the console
class Test { }
其他类型的检查做 工作。我还没有发现它背后的任何逻辑,所以非常感谢任何正确方向的指示。
如有疑问,请反驳。这是与您的电话类似的电话: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
所以 this 是该演员表的罪魁祸首。这似乎也适用于 @TypeChecked
/@CompileStatic
.
这很可能是静态类型检查器中的错误。当表达式的 LHS 是字符串变量时,将调用 ShortTypeHandling.castToString()
的转换应用于 RHS。
这适用于 Groovy 2.4.13。