是否可以获取Groovy中的变量名?
Is it possible to get the name of variable in Groovy?
我想知道是否可以检索变量的名称。
例如,如果我有一个方法:
def printSomething(def something){
//instead of having the literal String something, I want to be able to use the name of the variable that was passed
println('something is: ' + something)
}
如果我调用这个方法如下:
def ordinary = 58
printSomething(ordinary)
我想得到:
ordinary is 58
另一方面,如果我这样调用这个方法:
def extraOrdinary = 67
printSomething(extraOrdinary)
我想得到:
extraOrdinary is 67
编辑
我需要变量名,因为我有这段代码在 Katalon Studio 中的每个 TestSuite 之前运行,基本上它给了你使用 katalon.features 文件传递 GlobalVariables 的灵活性。思路来自:kazurayam/KatalonPropertiesDemo
@BeforeTestSuite
def sampleBeforeTestSuite(TestSuiteContext testSuiteContext) {
KatalonProperties props = new KatalonProperties()
// get appropriate value for GlobalVariable.hostname loaded from katalon.properties files
WebUI.comment(">>> GlobalVariable.G_Url default value: \'${GlobalVariable.G_Url}\'");
//gets the internal value of GlobalVariable.G_Url, if it's empty then use the one from katalon.features file
String preferedHostname = props.getProperty('GlobalVariable.G_Url')
if (preferedHostname != null) {
GlobalVariable.G_Url = preferedHostname;
WebUI.comment(">>> GlobalVariable.G_Url new value: \'${preferedHostname}\'");
} else {
WebUI.comment(">>> GlobalVariable.G_Url stays unchanged");
}
//doing the same for other variables is a lot of duplicate code
}
现在这只处理 1 个变量值,如果我为 20 个变量执行此操作,那就是很多重复代码,所以我想创建一个辅助函数:
def setProperty(KatalonProperties props, GlobalVariable var){
WebUI.comment(">>> " + var.getName()" + default value: \'${var}\'");
//gets the internal value of var, if it's null then use the one from katalon.features file
GlobalVariable preferedVar = props.getProperty(var.getName())
if (preferedVar != null) {
var = preferedVar;
WebUI.comment(">>> " + var.getName() + " new value: \'${preferedVar}\'");
} else {
WebUI.comment(">>> " + var.getName() + " stays unchanged");
}
}
这里我只是用var.getName()来解释我要找的东西,这只是我假设的一种方法。
没有。这是不可能的。
但是考虑使用 map 作为参数并传递 属性:
的名称和值
def printSomething(Map m){
println m
}
printSomething(ordinary:58)
printSomething(extraOrdinary:67)
printSomething(ordinary:11,extraOrdinary:22)
这将输出
[ordinary:58]
[extraOrdinary:67]
[ordinary:11, extraOrdinary:22]
是的,这可以通过 ASTTransformations 或宏(Groovy 2.5+)实现。
我目前没有合适的开发环境,但这里有一些建议:
并不是说这两个选项都不是微不足道的,也不是我向 Groovy 新手推荐的选项,您需要做一些研究。如果我没记错的话,这两个选项都需要与您的调用代码分开 build/project 才能可靠地工作。此外,它们中的任何一个都可能会给您带来晦涩难懂且难以调试的编译时错误,例如,当您的代码需要一个变量作为参数但传递了文字或方法调用时。所以:有龙。话虽这么说:我在这些事情上做了很多工作,它们真的很有趣 ;)
Groovy Documentation for Macros
如果您使用的是 Groovy 2.5+,则可以使用宏。对于您的用例,请查看 @Macro methods
部分。您的方法将有两个参数:MacroContext macroContext, MethodCallExpression callExpression
后者是有趣的参数。 MethodCallExpression has the getArguments()
-Methods, which allows you to access the Abstract Syntax Tree Nodes that where passed to the method as parameter. In your case that should be a VariableExpression 具有 getName()
方法,可以为您提供所需的名称。
Developing AST transformations
这是更复杂的版本。您仍然会得到与宏方法相同的 VariableExpression
,但是到达那里会很乏味,因为您必须自己确定正确的 MethodCallExpression
。您从 ClassNode
开始,然后自己努力达到 VariableExpression
。我建议使用本地转换并创建注释。但确定正确的 MethodCallExpression
并非易事。
我想知道是否可以检索变量的名称。
例如,如果我有一个方法:
def printSomething(def something){
//instead of having the literal String something, I want to be able to use the name of the variable that was passed
println('something is: ' + something)
}
如果我调用这个方法如下:
def ordinary = 58
printSomething(ordinary)
我想得到:
ordinary is 58
另一方面,如果我这样调用这个方法:
def extraOrdinary = 67
printSomething(extraOrdinary)
我想得到:
extraOrdinary is 67
编辑
我需要变量名,因为我有这段代码在 Katalon Studio 中的每个 TestSuite 之前运行,基本上它给了你使用 katalon.features 文件传递 GlobalVariables 的灵活性。思路来自:kazurayam/KatalonPropertiesDemo
@BeforeTestSuite
def sampleBeforeTestSuite(TestSuiteContext testSuiteContext) {
KatalonProperties props = new KatalonProperties()
// get appropriate value for GlobalVariable.hostname loaded from katalon.properties files
WebUI.comment(">>> GlobalVariable.G_Url default value: \'${GlobalVariable.G_Url}\'");
//gets the internal value of GlobalVariable.G_Url, if it's empty then use the one from katalon.features file
String preferedHostname = props.getProperty('GlobalVariable.G_Url')
if (preferedHostname != null) {
GlobalVariable.G_Url = preferedHostname;
WebUI.comment(">>> GlobalVariable.G_Url new value: \'${preferedHostname}\'");
} else {
WebUI.comment(">>> GlobalVariable.G_Url stays unchanged");
}
//doing the same for other variables is a lot of duplicate code
}
现在这只处理 1 个变量值,如果我为 20 个变量执行此操作,那就是很多重复代码,所以我想创建一个辅助函数:
def setProperty(KatalonProperties props, GlobalVariable var){
WebUI.comment(">>> " + var.getName()" + default value: \'${var}\'");
//gets the internal value of var, if it's null then use the one from katalon.features file
GlobalVariable preferedVar = props.getProperty(var.getName())
if (preferedVar != null) {
var = preferedVar;
WebUI.comment(">>> " + var.getName() + " new value: \'${preferedVar}\'");
} else {
WebUI.comment(">>> " + var.getName() + " stays unchanged");
}
}
这里我只是用var.getName()来解释我要找的东西,这只是我假设的一种方法。
没有。这是不可能的。
但是考虑使用 map 作为参数并传递 属性:
的名称和值def printSomething(Map m){
println m
}
printSomething(ordinary:58)
printSomething(extraOrdinary:67)
printSomething(ordinary:11,extraOrdinary:22)
这将输出
[ordinary:58]
[extraOrdinary:67]
[ordinary:11, extraOrdinary:22]
是的,这可以通过 ASTTransformations 或宏(Groovy 2.5+)实现。
我目前没有合适的开发环境,但这里有一些建议:
并不是说这两个选项都不是微不足道的,也不是我向 Groovy 新手推荐的选项,您需要做一些研究。如果我没记错的话,这两个选项都需要与您的调用代码分开 build/project 才能可靠地工作。此外,它们中的任何一个都可能会给您带来晦涩难懂且难以调试的编译时错误,例如,当您的代码需要一个变量作为参数但传递了文字或方法调用时。所以:有龙。话虽这么说:我在这些事情上做了很多工作,它们真的很有趣 ;)
Groovy Documentation for Macros
如果您使用的是 Groovy 2.5+,则可以使用宏。对于您的用例,请查看 @Macro methods
部分。您的方法将有两个参数:MacroContext macroContext, MethodCallExpression callExpression
后者是有趣的参数。 MethodCallExpression has the getArguments()
-Methods, which allows you to access the Abstract Syntax Tree Nodes that where passed to the method as parameter. In your case that should be a VariableExpression 具有 getName()
方法,可以为您提供所需的名称。
Developing AST transformations
这是更复杂的版本。您仍然会得到与宏方法相同的 VariableExpression
,但是到达那里会很乏味,因为您必须自己确定正确的 MethodCallExpression
。您从 ClassNode
开始,然后自己努力达到 VariableExpression
。我建议使用本地转换并创建注释。但确定正确的 MethodCallExpression
并非易事。