Groovy map强制产生<class> _groovyProxy

时间:2017-09-28 22:01:20

标签: java groovy

我有一个我试图实例化的java类。

PackageGenerator gen = [
      fileName: "file.xml",
      platform: "windows",
      version: "1.0"]
println ReflectionToStringBuilder.toString(gen);

产生

PackageGenerator_groovyProxy[fileName=<null>, platform=<null>, version=<null>]

但如果我用.with方式写它:

PackageGenerator gen = new PackageGenerator()
gen.with {
    fileName = "file.xml"
    platform = "windows"
    version = "1.0"
}
println ReflectionToStringBuilder.toString(gen);

产生

PackageGenerator[fileName="file.xml", platform="windows", version="1.0"]

是什么导致使用groovy代理类而不是实际的类?

1 个答案:

答案 0 :(得分:1)

如果无法将原始对象分配给所需的类型,则在创建*** Settings *** Documentation This is the Suite Level Documentation Metadata Version 1.0.0.1 #This the first patch on the initial version. Metadata Author Stack Overflow #The core developer *** Test Cases *** Test Case 1 [Documentation] This is the test case documentation [Tags] Test Tag 1 Set Suite Documentation Test Level addition to Suite Documenation append=${true} Set Suite Metadata Test Case 1 A Suite Level Metadata item from the Test Case append=${true} Set Suite Metadata Link http://www.microsoft.com append=${true} Set Test Documentation My Test Level documentation append=${true} Set Test Message *HTML* My Test level Message<br/> append=${true} Comment My Test Case Comment Log My Test Case Log Fail [Teardown] Set Jira Link 1234 *** Keywords *** Set Jira Link [Arguments] ${jira_Id} ${prev_level} Set Log Level WARN Set Test Message *HTML* <br/><b>Link:</b> <a href='http://www.jira.com/issue/${jira_Id}'>Jira Issue ${jira_Id}</a><br/> append=${true} Set Log Level ${prev_level} 实现时会添加_groovyProxy后缀。我相信这里发生的是

Proxy

相同
PackageGenerator gen = [
  fileName: "file.xml",
  platform: "windows",
  version: "1.0"]

并且由于无法将PackageGenerator gen = [ fileName: "file.xml", platform: "windows", version: "1.0"] as PackageGenerator 强制转换为Map,因此会生成代理。

您可以使用地图构造函数轻松地克服这一点

PackageGenerator

哪种实现与您在第二个示例中编写的实现非常接近