title: Is it possible to get the name of variable in Groovy?
body: <p>I would like to know if it is possible to retrieve the name of a variable. </p>
  
  <p>For example if I have a method:</p>
  
  <pre><code>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)
  }
  </code></pre>
  
  <p>If I call this method as follows:</p>
  
  <pre><code> def ordinary = 58
   printSomething(ordinary)
  </code></pre>
  
  <p>I want to get:</p>
  
  <pre><code>ordinary is 58
  </code></pre>
  
  <p>On the other hand if I call this method like this:</p>
  
  <pre><code>def extraOrdinary = 67
  printSomething(extraOrdinary)
  </code></pre>
  
  <p>I want to get:</p>
  
  <pre><code>extraOrdinary is 67
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>I need the variable name because I have this snippet of code which runs before each <strong>TestSuite</strong> in <em>Katalon Studio</em>, basically it gives you the flexibility of passing GlobalVariables using a katalon.features file. The idea is from: <a href="https://github.com/kazurayam/KatalonPropertiesDemo" rel="nofollow noreferrer">kazurayam/KatalonPropertiesDemo</a></p>
  
  <pre><code>  @BeforeTestSuite
      def sampleBeforeTestSuite(TestSuiteContext testSuiteContext) {
          KatalonProperties props = new KatalonProperties()
          // get appropriate value for GlobalVariable.hostname loaded from katalon.properties files
          WebUI.comment("&gt;&gt;&gt; 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("&gt;&gt;&gt; GlobalVariable.G_Url new value: \'${preferedHostname}\'");
              } else {
                  WebUI.comment("&gt;&gt;&gt; GlobalVariable.G_Url stays unchanged");
              }
   //doing the same for other variables is a lot of duplicate code
          }
  </code></pre>
  
  <p>Now this only handles 1 variable value, if I do this for say 20 variables, that is a lot of duplicate code, so I wanted to create a helper function:</p>
  
  <pre><code>def setProperty(KatalonProperties props, GlobalVariable var){
     WebUI.comment("&gt;&gt;&gt; " + 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("&gt;&gt;&gt; " + var.getName() + " new value: \'${preferedVar}\'");
              } else {
                  WebUI.comment("&gt;&gt;&gt; " + var.getName() + " stays unchanged");
              }
  }
  </code></pre>
  
  <p>Here I just put var.getName() to explain what I am looking for, that is just a method I assume.</p>