3

I have groovy script that has a function with 2 arguments, one of which is a hashmap. I want to check the name of a hashmap variable contains a substring or not, inside a function. How do i do it?

def myfunc(String var, HashMap var2)
{
    // need a routine to retrive the name of the variable var2
} 
Aditya T
  • 1,566
  • 2
  • 13
  • 26
user3721344
  • 221
  • 1
  • 4
  • 13
  • "need a routine to retrive the name of the variable var2" doesn't make sense. var2 is the name of the variable. Can you clarify whatever it is you think you want to be able to do? – Jeff Scott Brown Jun 22 '14 at 23:42

2 Answers2

3

It's well explained here : Getting the name of a method parameter

To access groovy script's method follow syntax :

metaClass.methods

Community
  • 1
  • 1
Kasper Ziemianek
  • 1,329
  • 8
  • 15
0

Sounds like you want the --parameters (alias -pa) compiler argument.

Generates metadata for reflection on method parameter names on JDK 8 and above. Defaults to false.

Usage: groovy --parameters Person.groovy

Also available for groovyc, groovyConsole, groovysh and groovy ant tasks. Check out the PR some awesome person contributed...

e.g.

class HelloWorld {
   static void speak(String firstName) {
      println "Hello $firstName"   
   } 
}

Student.methods.each { 
    if (it.name === 'speak') println it.parameters
}

// groovy HelloWorld.groovy => "[java.lang.String arg0]"
// groovy -pa HelloWorld.groovy => "[java.lang.String firstName]"
Nick Grealy
  • 24,216
  • 9
  • 104
  • 119