是否有可能在Groovy中获取变量的名称?

问题描述 投票:3回答:2

我想知道是否可以检索变量的名称。

例如,如果我有一个方法:

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()解释为我要找的东西,这只是我假设的方法。

groovy katalon-studio
2个回答
0
投票

是的,这可以通过ASTTransformations或Macros(Groovy 2.5+)实现。

我目前没有适当的开发环境,但这里有一些指示:

并不是说这两个选项都不是微不足道的,不是我推荐的Groovy新手,你不得不做一些研究。如果我没记错,任何一个选项都需要从您的调用代码中单独构建/项目才能可靠地运行。此外,它们中的任何一个都可能会让您模糊并难以调试编译时错误,例如,当您的代码期望变量作为参数但传递文字或方法调用时。所以:有龙。话虽如此:我已经用这些东西做了很多工作,他们真的很有趣;)

Groovy Documentation for Macros

如果您使用的是Groovy 2.5+,则可以使用宏。对于您的用例,请查看@Macro methods部分。你的方法将有两个参数:MacroContext macroContext, MethodCallExpression callExpression,后者是有趣的。 MethodCallExpression具有getArguments()-Methods,它允许您访问作为参数传递给方法的抽象语法树节点。在你的情况下,应该是一个VariableExpression,其中有getName()方法,为您提供您正在寻找的名称。

Developing AST transformations

这是更复杂的版本。你仍然可以使用与宏方法相同的VariableExpression,但是到达那里会很乏味,因为你必须自己识别正确的MethodCallExpression。你从ClassNode开始,然后自己前往VariableExpression。我建议使用局部转换并创建一个注释。但确定正确的MethodCallExpression并非易事。


-1
投票

没有。这是不可能的。

考虑使用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]
© www.soinside.com 2019 - 2024. All rights reserved.