使用Groovy中的GStrings设置/获取系统属性时的结果不同 [英] Different results when setting/getting System properties with GStrings in Groovy

查看:140
本文介绍了使用Groovy中的GStrings设置/获取系统属性时的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy在这里的行为使我感到困惑。我已经运行了一些调试器,试图确定动态混合器中的哪些位置,这些代码路径可能会越过,但是想知道是否有人可以直接在这里设置我。

基本上,在设置系统属性时使用GString作为值,这取决于我如何设置属性,该属性并不总是通过某些方法可读。



我见过为什么Map不能在Groovy中使用GString?为什么groovy在字典中看不到某些值?但我的问题具体适用于地图值,所以不确定那些适用于或不是?



片段:

  def tdollar ='dollar'

System.setProperty('key1','value1')
System.setProperty('key2',value2 $ tdollar )
//替换下面的内容使用除
以外的任何属性设置方法//上述结果相同
System.properties ['key4'] ='value4'
System.properties ['key5'] =value5 $ t $

println System.hasProperty('key1')
println System.hasProperty('key2')
println System.hasProperty('key4')
println System.hasProperty('key5')
println
$ b println System.getProperty('key1')
println System.getProperty('key2')
println系统.getProperty('key4')
println System.getProperty('key5')
println

println System.properties.keySet()
println

println System.properties ['key1']
println System.properties ['key2']
println System.properties ['key4']
println System.properties ['key5 ']

输出:

  null 
null
null
null

value1
value2dollar
value4
null

[java.runtime.n ame,sun.boot.library.path,java.vm.version,gopherProxySet,java.vm.vendor,java.vendor.url,path.separator,java.vm.name,file.encoding.pkg,user.country, sun.java.launcher,sun.os.patch.level,program.name,key5,key4,java.vm.specification.name,user.dir,key2,java.runtime.version,key1,java.awt.graphicsenv, java.endorsed.dirs,os.arch,java.io.tmpdir,line.separator,java.vm.specification.vendor,os.name,tools.jar,sun.jnu.encoding,script.name,java.library。路径,java.specification.name,java.class.version,sun.management.compiler,os.version,user.home,user.timezone,java.awt.printerjob,file.encoding,java.specification.version,java。 class.path,user.name,java.vm.specification.version,sun.java.command,java.home,sun.arch.data.model,user.language,java.specification.vendor,awt.toolkit,java。 vm.info,java.version,java.ext.dirs,sun.boot.class.path,java.vendor,file.separator,java.vendor.url.bug,sun.io.unicode.encoding,sun.cpu。 endian,groovy.starter.conf,groovy.home ,sun.cpu.isalist]

value1
value2dollar
value4
value5dollar

为什么如果我不使用 System.setProperty(key,value)语法,则属性是不可读的 System.getProperty(key),但仍然可以通过任何其他方法读取?

鉴于此行为,是否有最佳做法记录有关Groovy中的系统属性。



写下这些,我想知道这是否只是一个普遍的地图问题。将测试。

解决方案

  System.setProperty('key2',value2 $ tdollar )

当您使用上述方法进行操作时,第二个参数会从GString隐式转换为String

  System.properties ['key5'] =value5 $ tdollar

这会使用System类中的 setProperties 方法(而不是 setProperty ),并因此得到解决不同地导致问题。在发送到底层java类之前,GString可能无法正确转换或未转换为字符串。如果您将值从GString更改为像这样的字符串:

  System.properties ['key5'] =value5 + tdollar 

问题消失


I'm perplexed by Groovy's behavior here. I've run through the debugger some to try to determine where in the dynamic mixins these code paths may be getting crossed but wondered if anyone could set me straight here.

Basically, in setting a system property with a GString for a value, depending on how I set the property, the property is not always readable back via certain methods.

I have seen Why Map does not work for GString in Groovy? and Why groovy does not see some values in dictionary? but my question specifically applies to map values so not sure those apply or not?

Snippet :

def tdollar='dollar'

System.setProperty('key1', 'value1')
System.setProperty('key2', "value2$tdollar")
// Replace the below with any property setting method other than
// the above with the same results
System.properties['key4']='value4'
System.properties['key5']="value5$tdollar"

println System.hasProperty('key1')
println System.hasProperty('key2')
println System.hasProperty('key4')
println System.hasProperty('key5')
println

println System.getProperty('key1')
println System.getProperty('key2')
println System.getProperty('key4')
println System.getProperty('key5')
println

println System.properties.keySet()
println

println System.properties['key1']
println System.properties['key2']
println System.properties['key4']
println System.properties['key5']

Output:

null
null
null
null

value1
value2dollar
value4
null

[java.runtime.name, sun.boot.library.path, java.vm.version, gopherProxySet, java.vm.vendor, java.vendor.url, path.separator, java.vm.name, file.encoding.pkg, user.country, sun.java.launcher, sun.os.patch.level, program.name, key5, key4, java.vm.specification.name, user.dir, key2, java.runtime.version, key1, java.awt.graphicsenv, java.endorsed.dirs, os.arch, java.io.tmpdir, line.separator, java.vm.specification.vendor, os.name, tools.jar, sun.jnu.encoding, script.name, java.library.path, java.specification.name, java.class.version, sun.management.compiler, os.version, user.home, user.timezone, java.awt.printerjob, file.encoding, java.specification.version, java.class.path, user.name, java.vm.specification.version, sun.java.command, java.home, sun.arch.data.model, user.language, java.specification.vendor, awt.toolkit, java.vm.info, java.version, java.ext.dirs, sun.boot.class.path, java.vendor, file.separator, java.vendor.url.bug, sun.io.unicode.encoding, sun.cpu.endian, groovy.starter.conf, groovy.home, sun.cpu.isalist]

value1
value2dollar
value4
value5dollar

Why, if I don't use System.setProperty(key, value) syntax, is the property not readable via System.getProperty(key), but is still readable via any other method?

Given this behavior, is there a best practice documented regarding System properties in Groovy.

Writing this, I wonder if this is just a general map question. Will test.

解决方案

System.setProperty('key2', "value2$tdollar")

When you operate using this above method, the second argument is implicitly cast to a String from a GString

System.properties['key5']="value5$tdollar"

this use the underlying setProperties method in the System class (not setProperty), and hence gets resolved differently, causing issues. The GString might not be getting cast correctly or not getting converted to a string before being sent to the underlying java class. If you change the value from a GString to a string like this:

System.properties['key5']="value5" + tdollar

the issue disappears

这篇关于使用Groovy中的GStrings设置/获取系统属性时的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆