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
} 
1
  • "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? Commented Jun 22, 2014 at 23:42

2 Answers 2

3

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

To access groovy script's method follow syntax :

metaClass.methods

Sign up to request clarification or add additional context in comments.

Comments

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]"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.