可以在Groovy中获得变量的名称吗?

时间:2019-04-02 16:38:44

标签: groovy katalon-studio

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

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

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传递GlobalVariables的灵活性.features文件。这个想法来自: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()来解释我要寻找的内容,这只是我假设的一种方法。

2 个答案:

答案 0 :(得分: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 :(得分:-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]