传递变量在groovy gstring中进行评估 [英] Passing variable to be evaluated in groovy gstring

查看:155
本文介绍了传递变量在groovy gstring中进行评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在gstring评估中将变量作为字符串进行评估。
最简单的例子就是一些类似于
$ b $ pre $ code def def ='person.lName'
def value = $ {var}
println(value)

person实例中lastName的值。作为最后的手段,我可​​以使用反射,但是想知道groovy应该有一些简单的东西,我不知道。

解决方案

你可以试一下:

  def var = Eval.me('new Date()')

code>

代替您的示例中的第一行。

<一个href =http://groovy.codehaus.org/api/groovy/util/Eval.html =nofollow> Eval类记录在这里



(p)编辑



我猜(你的更新后的问题)你有个人变量,然后人们通过像 person.lName 这样的字符串,并且您想要返回该类的 lName 属性?



您可以使用GroovyShell来尝试这样的事情吗?

  //假设我们有一个Person类
class Person {
String fName
String lName
}

//一个变量'person'存储在bi nd脚本
person = new Person(fName:'tim',lName:'yates')

//给出一个执行
的命令字符串def commandString =' person.lName'

GroovyShell shell = new GroovyShell(binding)
def result = shell.evaluate(commandString)

或者,使用直接字符串解析和属性访问

  //假设我们有一个Person类
类Person {
String fName
String lName
}

//一个变量'person'存储在脚本
person = new Person(fName:'tim',lName:'yates')

//给出一个执行
的命令字符串def commandString ='person.lName '

//将命令字符串拆分为基于'。'的列表,并注入以空值开始
def result = commandString.split(/\./).inject(null ){curr,prop - >
//如果curr为null,则从绑定
返回属性//否则尝试从curr对象中获取给定属性
curr?。$ prop?:binding [道具]
}


I am wondering if I can pass variable to be evaluated as String inside gstring evaluation. simplest example will be some thing like

 def var ='person.lName'
 def value = "${var}"
 println(value)

I am looking to get output the value of lastName in the person instance. As a last resort I can use reflection, but wondering there should be some thing simpler in groovy, that I am not aware of.

解决方案

Can you try:

 def var = Eval.me( 'new Date()' )

In place of the first line in your example.

The Eval class is documented here

edit

I am guessing (from your updated question) that you have a person variable, and then people are passing in a String like person.lName , and you want to return the lName property of that class?

Can you try something like this using GroovyShell?

// Assuming we have a Person class
class Person {
  String fName
  String lName
}

// And a variable 'person' stored in the binding of the script
person = new Person( fName:'tim', lName:'yates' )

// And given a command string to execute
def commandString = 'person.lName'

GroovyShell shell = new GroovyShell( binding )
def result = shell.evaluate( commandString )

Or this, using direct string parsing and property access

// Assuming we have a Person class
class Person {
  String fName
  String lName
}

// And a variable 'person' stored in the binding of the script
person = new Person( fName:'tim', lName:'yates' )

// And given a command string to execute
def commandString = 'person.lName'

// Split the command string into a list based on '.', and inject starting with null
def result = commandString.split( /\./ ).inject( null ) { curr, prop ->
  // if curr is null, then return the property from the binding
  // Otherwise try to get the given property from the curr object
  curr?."$prop" ?: binding[ prop ]
}

这篇关于传递变量在groovy gstring中进行评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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